支付流程:
(1)注册微信开放平台,创建应用获取appid,appSecret,申请支付功能,申请成功之后会返回一些参数详情见图;
(2)申请开通微信支付功能;
(3)用XCode打开项目,【项目属性】-【Info】-【URL Schemes】设置微信开放平台申请的应用APPID,如图文件夹下"设置appid.jpg"所示。如果这的APPID设置不正确将无法调起微信支付;
(4)需要调用代码注册APPID:[WXApi registerApp:APP_ID withDescription:@"demo 2.0”];项目该APPID需与步骤2中APPID保持一致;
代码端接入:
+ (NSString *)jumpToBizPay { //============================================================ // V3&V4支付流程实现 // 注意:参数配置请查看服务器端Demo // 更新时间:2015年11月20日 //============================================================ NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios"; //解析服务端返回json数据 NSError *error; //加载一个NSURL对象 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; //将请求的url数据放到NSData对象中 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; if ( response != nil) { NSMutableDictionary *dict = NULL; //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; NSLog(@"url:%@",urlString); if(dict != nil){ NSMutableString *retcode = [dict objectForKey:@"retcode"]; if (retcode.intValue == 0){ NSMutableString *stamp = [dict objectForKey:@"timestamp"]; //调起微信支付 PayReq* req = [[[PayReq alloc] init]autorelease]; req.partnerId = [dict objectForKey:@"partnerid"]; req.prepayId = [dict objectForKey:@"prepayid"]; req.nonceStr = [dict objectForKey:@"noncestr"]; req.timeStamp = stamp.intValue; req.package = [dict objectForKey:@"package"]; req.sign = [dict objectForKey:@"sign"]; [WXApi sendReq:req]; //日志输出 NSLog(@"appid=%@ partid=%@ prepayid=%@ noncestr=%@ timestamp=%ld package=%@ sign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign ); return @""; }else{ return [dict objectForKey:@"retmsg"]; } }else{ return @"服务器返回错误,未获取到json对象"; } }else{ return @"服务器返回错误"; } }
支付完成回调:WXApiManager.m中的onResp方法中接收返回支付状态:
- (void)onResp:(BaseResp *)resp { if ([resp isKindOfClass:[SendMessageToWXResp class]]) { if (_delegate && [_delegate respondsToSelector:@selector(managerDidRecvMessageResponse:)]) { SendMessageToWXResp *messageResp = (SendMessageToWXResp *)resp; [_delegate managerDidRecvMessageResponse:messageResp]; } } else if ([resp isKindOfClass:[SendAuthResp class]]) { if (_delegate && [_delegate respondsToSelector:@selector(managerDidRecvAuthResponse:)]) { SendAuthResp *authResp = (SendAuthResp *)resp; [_delegate managerDidRecvAuthResponse:authResp]; } } else if ([resp isKindOfClass:[AddCardToWXCardPackageResp class]]) { if (_delegate && [_delegate respondsToSelector:@selector(managerDidRecvAddCardResponse:)]) { AddCardToWXCardPackageResp *addCardResp = (AddCardToWXCardPackageResp *)resp; [_delegate managerDidRecvAddCardResponse:addCardResp]; } }else if([resp isKindOfClass:[PayResp class]]){ //支付返回结果,实际支付结果需要去微信服务器端查询 NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"]; switch (resp.errCode) { case WXSuccess: strMsg = @"支付结果:成功!"; NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode); break; default: strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr]; NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr); break; } UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; [alert release]; } }