现在很多应用都用了微信支付,具体的步骤微信开放平台上应该都有,我这里只记录一下大体的流程和注意事项。
首先要去微信开放平台注册这个应用,取得应用的appid。
然后在appdelegate里面, [WXApi registerApp:APP_ID withDescription:YOUR_DESCREPTION];
接着去target→ info里面,添加url types,用于调回本应用时的标识。
然后在appdelegate里面,用于返回本应用时,区别到底是从哪个应用返回的,
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"url2 = %@",url);
if ([url.host isEqualToString:@"pay"]) {//微信
return [WXApi handleOpenURL:url delegate:[[WebShopPayPageViewController alloc] init]];
}else if([url.host isEqualToString:@"safepay"]){//支付宝
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result11 = %@",resultDic);
}];
return YES;
}
return YES;
}
这样准备工作就完成了。
根据微信的官方描述,签名在服务器端的生成是最安全的,我们需要的就是把后台需要的参数传过去,然后后台吧我们需要传给微信的参数,包括签名,prepayid等传回来。代码如下:
PayReq *req = [[PayReq alloc] init];
req.partnerId = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"partnerID"]];//申请的partnerID
req.openID = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"appid"]];//申请的appid
req.prepayId = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"prepayid"]];//返回的prepayid
req.package = @"Sign=WXPay";//固定值
req.nonceStr = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"noncestr"]];
req.timeStamp = [infoDict[@"data"][@"timestamp"] intValue];
//这个签名是重新生成的,只有这6个参数参与签名
req.sign = [NSString stringWithFormat:@"%@",infoDict[@"data"][@"sign"]];//签名,后台生成并返回
[WXApi sendReq:req];//发送请求,调用微信客户端
然后支付的结果,通过回调获得:
-(void)onResp:(BaseResp*)resp{
NSLog(@"resp = = %@,%d",resp.errStr,resp.errCode);
if ([resp isKindOfClass:[PayResp class]]){
PayResp*response=(PayResp*)resp;
NSLog(@"res.er = %@",response.errStr);
switch(response.errCode){
case WXSuccess:
//服务器端查询支付通知或查询API返回的结果再提示成功
{
//页面被销毁了,
[self needServiceResult];//下面有说明
NSLog(@"订单ID = %@",SHAREDAPP.orderID);
// NSLog(@"支付成功");
break;
}
default:
{
NSLog(@"支付失败,retcode=%d",resp.errCode);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付失败" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
break;
}
}
}
//微信反复抢到,支付结果要以我们后台的返回结果为主
- (void)needServiceResult
{
NSLog(@"self.ta = %ld",(long)self.tagOfAmount);
HttpRequest *request = [[HttpRequest alloc] init];
[request httpRequestWith:@{@"resultType":@"json",@"token":SHAREDAPP.userToken,@"tradeno":SHAREDAPP.orderID} withURL:[NSString stringWithFormat:@"%@%@",MAIN_URL,BACK_PAY]];
}
request.httpRequestBlock = ^(NSDictionary *infoDict){
NSLog(@"retMsg = %@",infoDict);
if([infoDict[@"retCode"] integerValue] == 1){//如果服务器返回成功
self.hadPayed = YES;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
[self.navigationController popToRootViewControllerAnimated:YES];
return ;
}else{
sleep(1);//每隔1秒掉一次
if (self.tagOfAmount <=30) {//这个地方我用了递归,如果服务器30秒内没返回正确的结果,默认支付失败
[self needServiceResult];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示信息" message:@"支付失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];//次情况为服务器没返回订单支付成功的信息,需要和客服联系。
return ;
}
self.tagOfAmount ++;//计数,记录递归的次数,由于一秒调用一次,所以几次就是几秒
}
};
}
到这里,微信支付就算完成了。