官方建议使用服务器签名来接入微信支付,从安全上考虑,客户端不应该知道RAS密钥,也不需要每个客户端都写一遍签名的算法。
前言
申请开通微信支付功能,需要认证开发者资质300/次,微信发邮件给API参数表。后面的代码当中的APPID 为测试代码的APPID.
支付流程
- 导入支付SDK,注册微信支付
- Info.plist中增加如下代码
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 导入第三方框架和链接库
Security.framework
CoreTelephony.framework
SystemConfiguration.framework
libc++.tbd
lib.tbd
libsqlite3.tbd
libWeChatSDK.a
-
APPID设置为URL Schemes
- 在AppDelegate中注册
//注册微信
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//向微信注册wxd930ea5d5a258f4f
[WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"demo 2.0"];
return YES;
}
//设置代理
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
- 发起支付
-(IBAction)weixinPayAction:(UIButton *)sender {
//先判断是否安装微信或是否支持微信支付
if (![WXApi isWXAppInstalled])
return ;
if (![WXApi isWXAppSupportApi])
return ;
//向后台请求数据
[self PayAction];
}
-(NSString*)PayAction{
NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
//解析服务端返回json数据
NSError *error;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if ( response != nil) {
NSMutableDictionary *dict = NULL;
dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
if(dict != nil){
NSMutableString *retcode = [dict objectForKey:@"retcode"];
if (retcode.intValue == 0){
//调起微信支付
PayReq* req = [[PayReq alloc] init];
//微信号和AppID组成的唯一标识,发送请求时第三方程序必须填写,
//用于校验微信用户是否换号登录
req.openID = [dict objectForKey:@"openID"]; //商户
req.partnerId = [dict objectForKey:@"partnerid"]; //商户号ID
req.prepayId = [dict objectForKey:@"prepayid"];//预支付交易ID
req.nonceStr = [dict objectForKey:@"noncestr"]; //随机字符串
req.timeStamp = [dict objectForKey:@"timestamp"].intValue;//时间戳
req.sign = [dict objectForKey:@"sign"]; //签名
req.package = [dict objectForKey:@"package"];//扩展字段,固定值@“Sign=WXPay”
[WXApi sendReq:req];
return @"一切OK";
}else{
return [dict objectForKey:@"retmsg"];
}
}else{
return @"服务器返回错误,未获取到json对象";
}
}else{
return @"服务器返回错误";
}
}
- 处理微信支付返回信息
- (void)onResp:(BaseResp *)resp {
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
switch (resp.errCode) {
case WXSuccess:
NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
break;
default:
NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr);
break;
}
}
}
商户系统和微信支付系统主要交互说明:
步骤1:用户在商户APP中选择商品,提交订单,选择微信支付。
步骤2:商户后台收到用户支付单,调用微信支付****统一下单接口****。
步骤3:统一下单接口返回正常的prepay_id,再按签名规范重新生成签名后,将数据传输给APP。参与签名的字段名为appId,partnerId,prepayId,nonceStr,timeStamp,package。注意:package的值格式为Sign=WXPay
步骤4:商户APP调起微信支付。
步骤5:商户后台接收支付通知。