1,申请微信支付的功能,获取到开发需要的参数
2,iOS端集成微信支付
2.1项目设置APPID
在URL Scheme中设置
2.2 导入sdk,配置项目,注册APPID
2.2.1 导入demo中的sdk,
2.2.2 还需要倒入demo中的几个系统库
SystemConfiguration.framework
Security.framework
CoreTelephony.framework
Foundation.framework
UIKit.framework
libsqlite3.0.tbd
libz.tbd
2.2.3 调用API前,需要先向微信注册您的APPID,代码如下:
[WXApi registerApp:@"wxd930ea5d5a258f4f" withDescription:@"demo 2.0"];
2.2.4 设置白名单
wechat,wexin
2.3 调起支付
从服务端获取支付需要的参数
PayReq *request = [[[PayReq alloc] init] autorelease];
request.partnerId = @"10000100";
request.prepayId= @"1101000000140415649af9fc314aa427";
request.package = @"Sign=WXPay";
request.nonceStr= @"a462b76e7436e98e0ed6e13c64b4fd1c";
request.timeStamp= @"1397527777";
request.sign= @"582282D72DD2B03AD892830965F428CB16E7A256";
[WXApi sendReq:request];
2.4 支付结果回调处理
-(void)onResp:(BaseResp*)resp{ if ([respisKindOfClass:[PayRespclass]]){ PayResp*response=(PayResp*)resp; switch(response.errCode){ caseWXSuccess: //服务器端查询支付通知或查询API返回的结果再提示成功 NSlog(@"支付成功"); break; default: NSlog(@"支付失败,retcode=%d",resp.errCode); break; } } }
3 测试和dubug
用微信官方提供的http get接口进行测试
@"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios"
返回的数据格式如下
{
"appid":"wxb4ba3c02aa476ea1", "partnerid":"1305176001", "package":"Sign=WXPay", "noncestr":"60adcb1dcd5d0e8fe903822313810988", "timestamp":1473724660, "prepayid":"wx20160913075740396ab1584b0080984714", "sign":"483F092E7384602E8E7F7ABA3E9F9FC1" }- (IBAction)payWXWithMonicSever:(UIButton *)sender { //============================================================ // 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]; 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=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign ); return ; }else{ NSLog(@"%@",[dict objectForKey:@"retmsg"]); return ; } }else{ NSLog(@"%@",@"服务器返回错误,未获取到json对象"); return ; } }else{ NSLog(@"%@",@"服务器返回错误"); return ; } }
预支付订单接口一般由服务器调用,app本地测试时也可以使用
验证签名是否正确
验证签名是否正确可以用微信提供的这个网页,如果本地的签名结果和网页上一致那就完成了。
http://mch.weixin.qq.com/wiki/tools/signverify/
ios本地调试
本地调试可以使用微信sdk中的payRequsestHandler 类进行,下面的步骤一般不需要进行。协助后台开发调试才会使用。相当于从生成预支付订单到调起微信支付sdk的步骤全部在客户端完成。
//创建支付签名对象 payRequsestHandler *reqHandler = [payRequsestHandler alloc]; //初始化支付签名对象 [reqHandler init:WX_APP_ID mch_id:WX_MCHID]; //设置密钥 [reqHandler setKey:WX_PARTNER_ID]; //价格*100 NSString *priceString=@"1"; NSString *orderName=@"测试标题"; NSString *identifierForVendor = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; //获取到实际调起微信支付的参数后,在app端调起支付 NSMutableDictionary *dict = [reqHandler getPrepayWithOrderName:orderName price:priceString device:identifierForVendor]; NSLog(@"dict%@",dict); //在payRequestHandle.mm文件里调用该方法 if(dict == nil){ //错误提示 NSString *debug = [reqHandler getDebugifo]; NSLog(@"debug %@",debug); NSLog(@"%@\n\n",debug); }else{ NSLog(@"%@\n\n",[reqHandler getDebugifo]); //[self alert:@"确认" msg:@"下单成功,点击OK后调起支付!"]; NSMutableString *stamp = [dict objectForKey:@"timestamp"]; //调起微信支付 PayReq* req = [[PayReq alloc] init]; req.openID = [dict objectForKey:@"appid"]; 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=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",req.openID,req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign ); }
常见问题
一般发现问题就是支付失败,可以逐步检查
1,是否获取到预支付id,
如果获取成功说明微信支付参数配置正确,第一步签名算法正确,获取失败则检查这些内容
2,是否付款成功
失败,检查第二步签名和调起微信sdk的参数
微信支付的金额是以分为单位,支付0.01 元需要在订单中传递price 为1。
参考资料
业务介绍
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1
详细开发步骤
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
微信支付SDK和demo下载地址
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
预支付接口文档
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
http://www.jianshu.com/p/94dcc220b2aa