iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔

摘要:
写的不错,给留个言哈...一.支付准备工作1.微信相关准备工作(1)向微信官方开通支付功能.这个不是前端的工作.(2)导入官方下载的微信支付SDK包.我用的是微信开放平台下载的SDK1.6.2(3)导入必要的库文件SystemConfiguration.framework,libz.dylib,libsqlite3.0.dylib,libc++.dylib,CoreTelephoy.framewo

写的不错,给留个言哈...

一. 支付准备工作

1. 微信相关准备工作

(1) 向微信官方开通支付功能. 这个不是前端的工作.

(2) 导入官方下载的微信支付SDK包. 我用的是微信开放平台下载的SDK 1.6.2

iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔第1张

(3) 导入必要的库文件

SystemConfiguration.framework,

libz.dylib,

libsqlite3.0.dylib,

libc++.dylib,

CoreTelephoy.framework (坑一: 这个库是必要的,但是微信官方文档中没有说到要导入)

iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔第2张

iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔第3张

(4) 该项目中的Bundle Identifier 应该填向微信注册的Bundle Identifier

(5) 注册微信 (回调的时候用到,告诉微信,从微信返回到哪个APP)

Target --> info --> URL Types --> +按钮 --> 填写identifier 和 URLSchemes. 前一个是标识符,一般填@"weixin".后一个是注册的微信appId. 比如"wx19a984b788a8a0b1".(注释: 假的appid)

iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔第4张

(6) 添加微信白名单

info.plist --> 右击 --> open as --> source Code --> 添加白名单

我是在<key>CFBundleVersion</key>这一行上面添加的. 注意保持正确的键值对.别插错了.

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>wechat</string>
        <string>weixin</string>
    </array>

iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔第5张

(7) 如果支付成功,回调方法不执行,或者回调不成功.请再次确认(4)(5)(6),是否填写正确.

(8) 运行一下,不报错.报错,再次确认(1)--(7)步骤.

二.代码相关

1. 在AppDelegate.h中

(1) 导入微信类 #import @"WXApi.h".

(2) 遵守微信代理方法 <WXApiDelegate>

2. 在APPDelegate.m中

(1) 注册微信

#pragma mark 注册微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    //注册 微信
    /**
     参数1 : 微信Appid
     参数2 : 对项目的描述信息(用项目名称)
     */[WXApi registerApp:@"微信Appid" withDescription:@"云宴"];
    
    returnYES;
}

(2) 跳转方法,并设置代理

#pragma mark 跳转处理
//被废弃的方法. 但是在低版本中会用到.建议写上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WXApi handleOpenURL:url delegate:self];
}
//被废弃的方法. 但是在低版本中会用到.建议写上

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}

//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    return [WXApi handleOpenURL:url delegate:self];
}

(3) 微信回调方法 (注意: 不要写成Req方法了)

#pragma mark 微信回调方法

- (void)onResp:(BaseResp *)resp
{
    NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
    NSLog(@"strMsg: %@",strMsg);

    NSString * errStr       = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
    NSLog(@"errStr: %@",errStr);

    
    NSString *strTitle;
    //判断是微信消息的回调 --> 是支付回调回来的还是消息回调回来的.
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"发送媒体消息的结果"];
    }

    NSString *wxPayResult;
    //判断是否是微信支付回调 (注意是PayResp 而不是PayReq)

    if ([resp isKindOfClass:[PayResp class]])
    {
        //支付返回的结果, 实际支付结果需要去微信服务器端查询strTitle = [NSString stringWithFormat:@"支付结果"];
        switch(resp.errCode)
        {
            caseWXSuccess:
            {
                strMsg = @"支付结果:";
                NSLog(@"支付成功: %d",resp.errCode);
                wxPayResult = @"success";
                break;
            }
caseWXErrCodeUserCancel: { strMsg = @"用户取消了支付"; NSLog(@"用户取消支付: %d",resp.errCode); wxPayResult = @"cancel"; break; } default: { strMsg = [NSString stringWithFormat:@"支付失败! code: %d errorStr: %@",resp.errCode,resp.errStr]; NSLog(@":支付失败: code: %d str: %@",resp.errCode,resp.errStr); wxPayResult = @"faile"; break; } } //发出通知 从微信回调回来之后,发一个通知,让请求支付的页面接收消息,并且展示出来,或者进行一些自定义的展示或者跳转NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult]; [[NSNotificationCenter defaultCenter] postNotification:notification]; } }

3. 在ViewController.h (进行支付请求的类)

暂时没有任何操作

4. 在ViewController.m中(进行支付的请求的类)

(1) 导入微信库 #import @"WXApi.h"

(2) 监听APPDelegate.m中发送的通知

#pragma mark 监听通知
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //检测是否装了微信软件
    if([WXApi isWXAppInstalled])
    {
        
        //监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
    }
}
#pragma mark - 事件
- (void)getOrderPayResult:(NSNotification *)notification
{
    NSLog(@"userInfo: %@",notification.userInfo);
    
    if ([notification.object isEqualToString:@"success"])
    {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
        [alertView show];
    }
    else{
        [self alert:@"提示" msg:@"支付失败"];
    }
}

//客户端提示信息
- (void)alert:(NSString *)title msg:(NSString *)msg
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
    [alter show];
}

(3) 移除通知

#pragma mark 移除通知
- (void)dealloc 
{
//移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self]; }

(4) 调起微信去支付

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self pay_button];
}

#pragma mark - 实现方法

#pragma mark 创建支付按钮
- (void)pay_button
{
    self.payButton =[UIButton buttonWithType:UIButtonTypeCustom];
    self.payButton.frame = CGRectMake(10, 100, 300, 40);
    self.payButton.backgroundColor =[UIColor orangeColor];
    [self.payButton setTitle:@"去支付"forState:UIControlStateNormal];
    [self.payButton addTarget:self action:@selector(payButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.payButton];
}

#pragma mark - 点击事件
- (void)payButtonClicked
{
    [self sendNetWorking_WXPay];
}

- (void)sendNetWorking_WXPay
{
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",YYIP,@"wx/pay"];
    
    NSDictionary * parameter =@{
                                 @"pay_type"       : @"1",
                                 @"total_price"    : @"10",
                                 @"appointment_id" : @"208"};
    
    [self sendNetWorkingWith:urlStr andParameter:parameter];
}

#pragma mark 网络请求 -->> post
- (void)sendNetWorkingWith:(NSString *)url andParameter:(NSDictionary *)parameter
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    
    [manager POST:url parameters:parameter progress:^(NSProgress *_Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
        //网络请求回来的8个参数.详见微信开发平台
NSDictionary * result = responseObject[@"result"];
        
        NSString * appid         = result[@"appid"];
        NSString * noncestr      = result[@"noncestr"];
        NSString * package1      = result[@"package1"];
        NSString * partnerid     = result[@"partnerid"];
        NSString * paySign       = result[@"paySign"];
        NSString * prepayid      = result[@"prepayid"];
        NSString * timestamp     = result[@"timestamp"];
        
        
//NSString * err_code      = result[@"err_code"];
//NSString * timeStamp     = result[@"timeStamp"];
//NSString * tradeid       = result[@"tradeid"];

    [self WXPayRequest:appid nonceStr:noncestr package:package1 partnerId:partnerid prepayId:prepayid timeStamp:timestamp sign:paySign];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *_Nonnull error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 调起微信支付
- (void)WXPayRequest:(NSString *)appId nonceStr:(NSString *)nonceStr package:(NSString *)package partnerId:(NSString *)partnerId prepayId:(NSString *)prepayId timeStamp:(NSString *)timeStamp sign:(NSString *)sign{
    //调起微信支付
    PayReq* wxreq             =[[PayReq alloc] init];
    
    wxreq.openID              = appId; //微信的appid
    wxreq.partnerId           =partnerId;
    wxreq.prepayId            =prepayId;
    wxreq.nonceStr            =nonceStr;
    wxreq.timeStamp           =[timeStamp intValue];
    wxreq.package             =package;
    wxreq.sign                =sign;
    
    [WXApi sendReq:wxreq];
}

三.上代码

1. AppDelegate.h

#import <UIKit/UIKit.h>

#import "WXApi.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
@property (strong, nonatomic) UIWindow *window;

@end

2. AppDelegate.m

#import "AppDelegate.h"

@interfaceAppDelegate ()

@end

@implementationAppDelegate


#pragma mark 注册微信
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    //注册 微信
    /**
     参数1 : 微信Appid
     参数2 : 对项目的描述信息(用项目名称)
     */[WXApi registerApp:@"wx09a984b788a8a0b0" withDescription:@"云宴"];
    
    returnYES;
}

#pragma mark 跳转处理
//被废弃的方法. 但是在低版本中会用到.建议写上
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    return [WXApi handleOpenURL:url delegate:self];
}
//被废弃的方法. 但是在低版本中会用到.建议写上

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return [WXApi handleOpenURL:url delegate:self];
}

//新的方法
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
    return [WXApi handleOpenURL:url delegate:self];
}

#warning 这一步中  不要错误的把req 当成了 resp
#pragma mark 微信回调方法
- (void)onResp:(BaseResp *)resp
{
    NSString * strMsg = [NSString stringWithFormat:@"errorCode: %d",resp.errCode];
    NSLog(@"strMsg: %@",strMsg);
    
    NSString * errStr       = [NSString stringWithFormat:@"errStr: %@",resp.errStr];
    NSLog(@"errStr: %@",errStr);
    
    
    NSString *strTitle;
    //判断是微信消息的回调
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    {
        strTitle = [NSString stringWithFormat:@"发送媒体消息的结果"];
    }
    
    
    NSString *wxPayResult;
    //判断是否是微信支付回调 (注意是PayResp 而不是PayReq)
    if ([resp isKindOfClass:[PayResp class]])
    {
        //支付返回的结果, 实际支付结果需要去微信服务器端查询
strTitle = [NSString stringWithFormat:@"支付结果"];
        
        switch(resp.errCode)
        {
            caseWXSuccess:
            {
                strMsg = @"支付结果:";
                NSLog(@"支付成功: %d",resp.errCode);
                wxPayResult = @"success";
                break;
            }
                caseWXErrCodeUserCancel:
            {
                strMsg = @"用户取消了支付";
                NSLog(@"用户取消支付: %d",resp.errCode);
                wxPayResult = @"cancel";
                break;
            }
            default:
            {
                strMsg = [NSString stringWithFormat:@"支付失败! code: %d  errorStr: %@",resp.errCode,resp.errStr];
                NSLog(@":支付失败: code: %d str: %@",resp.errCode,resp.errStr);
                wxPayResult = @"faile";
                break;
            }
        }
        
        //发出通知
        NSNotification * notification = [NSNotification notificationWithName:@"WXPay" object:wxPayResult];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

@end

3. ViewController.h

#import <UIKit/UIKit.h>

@interfaceViewController : UIViewController


@end

4. ViewController.m

#import "ViewController.h"

#import "AFHTTPSessionManager.h"
#import "WXApi.h"

#define YYIP       @"http:公司IP地址"


@interfaceViewController ()

@property (nonatomic, strong) UIButton *payButton;


@end

@implementationViewController

#pragma mark 监听通知
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //检测是否装了微信软件
    if([WXApi isWXAppInstalled])
    {
        
        //监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getOrderPayResult:) name:@"WXPay" object:nil];
    }
}

#pragma mark 移除通知
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    //移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self pay_button];

}

#pragma mark - 实现方法

#pragma mark 创建支付按钮
- (void)pay_button
{
    self.payButton =[UIButton buttonWithType:UIButtonTypeCustom];
    self.payButton.frame = CGRectMake(10, 100, 300, 40);
    self.payButton.backgroundColor =[UIColor orangeColor];
    [self.payButton setTitle:@"去支付"forState:UIControlStateNormal];
    [self.payButton addTarget:self action:@selector(payButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.payButton];
}

#pragma mark - 点击事件
- (void)payButtonClicked
{

if ([WXApi isWXAppInstalled])

{

[self sendNetWorking_WXPay];

}

else

{

[self alert:@"提示" msg:@"未安装微信"];

}

}

- (void)sendNetWorking_WXPay
{
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",YYIP,@"wx/pay"];
       
    NSDictionary * parameter =@{
                                 @"pay_type"       : @"1",
                                 @"total_price"    : @"10",
                                 @"appointment_id" : @"208"};
    
    [self sendNetWorkingWith:urlStr andParameter:parameter];
}

#pragma mark 网络请求 -->> post
- (void)sendNetWorkingWith:(NSString *)url andParameter:(NSDictionary *)parameter
{
    AFHTTPSessionManager * manager =[AFHTTPSessionManager manager];
    
    [manager POST:url parameters:parameter progress:^(NSProgress *_Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
        
        NSLog(@"%@",responseObject);
        
        //网络请求回来的8个参数.详见微信开发平台
NSDictionary * result = responseObject[@"result"];
        
        NSString * appid         = result[@"appid"];
        NSString * noncestr      = result[@"noncestr"];
        NSString * package1      = result[@"package1"];
        NSString * partnerid     = result[@"partnerid"];
        NSString * paySign       = result[@"paySign"];
        NSString * prepayid      = result[@"prepayid"];
        NSString * timestamp     = result[@"timestamp"];
        
        
//NSString * err_code      = result[@"err_code"];
//NSString * timeStamp     = result[@"timeStamp"];
//NSString * tradeid       = result[@"tradeid"];

    [self WXPayRequest:appid nonceStr:noncestr package:package1 partnerId:partnerid prepayId:prepayid timeStamp:timestamp sign:paySign];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError *_Nonnull error) {
        
        NSLog(@"%@",error);
    }];
}

#pragma mark - 调起微信支付
- (void)WXPayRequest:(NSString *)appId nonceStr:(NSString *)nonceStr package:(NSString *)package partnerId:(NSString *)partnerId prepayId:(NSString *)prepayId timeStamp:(NSString *)timeStamp sign:(NSString *)sign{
    //调起微信支付
    PayReq* wxreq             =[[PayReq alloc] init];
    
    wxreq.openID              = appId; //微信的appid
    wxreq.partnerId           =partnerId;
    wxreq.prepayId            =prepayId;
    wxreq.nonceStr            =nonceStr;
    wxreq.timeStamp           =[timeStamp intValue];
    wxreq.package             =package;
    wxreq.sign                =sign;
    
    [WXApi sendReq:wxreq];
}


#pragma mark - 事件
- (void)getOrderPayResult:(NSNotification *)notification
{
    NSLog(@"userInfo: %@",notification.userInfo);
    
    if ([notification.object isEqualToString:@"success"])
    {
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];
        [alertView show];
    }
else if([notification.object isEqualToString:@"cancel"])
{
[self alert:@"提示" msg:@"用户取消了支付"];
}
else{ [self alert:@"提示" msg:@"支付失败"]; } } //客户端提示信息 - (void)alert:(NSString *)title msg:(NSString *)msg { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil]; [alter show]; } @end

免责声明:文章转载自《iOS- 微信支付 (服务器调起支付 )以及回调不成功的原因 不看后悔》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux配置 依赖安装iOS中循环引用的解除下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

iphone 数据存储之属性和归档archive

     在IPHONE中有四种方式可以永久存储数据分别是属性列表、归档、SQLITE3、coredata。前两者、后二者操作的时候有些地方是相同的,以属 性列表和归档来说都会用writeToFile/URL:path atomically:flag 和initWithContentofFile/URL:path;两都都不能直接操作基本数据类型,不过前者不...

oc之NSSortDescriptor(描述器排序)

1.将以上四个字典存入数组中,输出数据以班级:XXX,姓名:XXX,年龄:XX格式。 2.删除小明此条数据,列出剩余数据,输出格式同上。 3.按照班级进行排序,如果班级相同则按照年龄排序输出数据,格式同上。(描述器) 1 int main(int argc, const char *argv[]) { 2 @autoreleasepool { 3...

.NET Core 委托delegate (一)

1、什么是委托 当需要把方法传递给其他方法时,就需要使用委托。 我们习惯于把数据作为参数传递给方法,而有时某个方法执行的操作并不是针对数据进行的,而是要对另外一个方法进行调用。更麻烦的是,在编译时我们是不知道第二个方法是什么的,这个信息只能在运行时得到,所以需要把第二个方法作为参数传递给第一个方法。例如: 1、启动线程和任务——基类System.Threa...

iOS 关于时间天数星期月份的总结

#import <Foundation/Foundation.h> @interface NSDate (SLExtend) // 判断是否是本周更早 - (BOOL)isThisWeekEarlier; // 判断是否是本周晚些 - (BOOL)isThisWeekLater; // 判断是否是下一周或者更远 - (BOOL)isNextWe...

iOS AVAudioSession 配置(录音完声音变小问题)

有这么一个场景,首先我们录音,录音完再播放发现音量变小了; 百思不得其解,查看API发现AVAudioSession里面有这么一个选项, 如果你的app涉及到了音视频通话以及播放其他语音,那么当遇到声音变小的时候,可以看看下面的配置。 AVAudioSessionCategoryOptionDuckOthers 苹果文档上说,如果把AVAduioSessi...

c# 工厂模式 ,委托 ,事件。

有些时间 不用 c#了 ,想 写 委托 和 事件 又会 卡下 ,之前也没认真总结过。干脆 做个小结 。 什么是委托:狭义,不安全函数的指针。不安全在哪里: 任何地方都可以调用委托对象。(实际上委托对象才是函数的指针,而delegate只是一个语法) 什么是事件:狭义,安全的函数指针,安全在哪里:只允许包含事件的类,的内部调用。 联系和区别: delegat...