集成微信支付流程整理
1、首先到微信开放平台官网下载微信支付SDK,包含libWeChatSDK.a、WechatAuthSDK.h、WXApi.h、WXApiObject.h,如图:
2、添加依赖库:在object--General--Linked Framewarks and Libraries位置添加如下三方库:
3、在info--URL Type添加一个URL type,如图:红色圈里面填写在开放平台申请的唯一的AppId
4、此时工程已经配置好了微信支付的环境,接下来就是代码了
话不多说,直接上代码:
AppDelegate.h文件
#import "WXApi.h" @interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
AppDelegate.m文件
#import "WechatAuthSDK.h" #import "WXApiObject.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"WeiXinDemo"]; return YES; } - (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]; } -(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) { case WXSuccess: { strMsg = @"支付结果:"; NSLog(@"支付成功: %d",resp.errCode); wxPayResult = @"success"; break; } case WXErrCodeUserCancel: { 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]; } }
在需要调用微信支付的ViewController.m文件
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //检测是否装了微信软件 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]; } - (IBAction)pay:(UIButton *)sender { if ([WXApi isWXAppInstalled]) { [self sendNetWorking_WXPay]; } else { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"未安装微信" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertView show]; } } - (void)sendNetWorking_WXPay{ PayReq *request = [[PayReq alloc] init]; request.partnerId = @"10000100"; request.prepayId= @"1101000000140415649af9fc314aa427"; request.package = @"Sign=WXPay"; request.nonceStr= @"a462b76e7436e98e0ed6e13c64b4fd1c"; request.timeStamp= @"1397527777"; request.sign= @"582282D72DD2B03AD892830965F428CB16E7A256"; [WXApi sendReq:request]; } #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]; }