應(yīng)用場(chǎng)景:
去年六月應(yīng)公司業(yè)務(wù)發(fā)展需要,我們開發(fā)要求接入微信支付和支付寶支付女嘲。一直沒有總結(jié)出來留下只言片語告誡后來人畜份,現(xiàn)如今有時(shí)間來整理下。
總的來說國內(nèi)的微信支付還是比較坑的欣尼,demo一般開發(fā)過程中會(huì)遇到很多問題問題爆雹。打開微信支付開放平臺(tái)給了一張圖,說明其流程媒至,但是我想說的是這TM有個(gè)卵用顶别。老子只要的是一個(gè)支付而已,就是生成一訂單提交給服務(wù)器拒啰,并調(diào)起支付好么驯绎?(這都是后話)好了不在吐槽說正事
Paste_Image.png
第一步
打開該連接https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F 注冊(cè)商戶,忘記了是每年三百塊還是總共三百塊谋旦,注冊(cè)之后會(huì)有幾個(gè)工作日的審核剩失,請(qǐng)耐心等待屈尼,待審核通過后注冊(cè)應(yīng)用后我們會(huì)拿到應(yīng)用的ID用于參數(shù)填寫。
第二步
pods入微信支付sdk拴孤,并填寫相關(guān)參數(shù)
[WXApi registerApp:@"你的微信APPID" withDescription:@"Wechat"];
applelegate中添加用這個(gè)方法脾歧,這個(gè)另外一個(gè)是坑
-(void)onResp:(BaseResp*)resp{//微信ios9.0以下的方法
if ([resp isKindOfClass:[PayResp class]]){
PayResp *response = (PayResp *)resp;
NSString *codeString=[NSString stringWithFormat:@"%d",response.errCode];
switch (response.errCode) {
case WXSuccess: {
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
[MBProgressHUD showMessage:@"正在處理支付結(jié)果..." toView:window];
[Noti_Center postNotificationName:WX_PAY object:codeString];
break;
}
case WXErrCodeCommon:
[Noti_Center postNotificationName:WX_PAY object:codeString];
break;
case WXErrCodeUserCancel:
[Noti_Center postNotificationName:WX_PAY object:codeString];
break;
default:
break;
}
} // 向微信請(qǐng)求授權(quán)后,得到響應(yīng)結(jié)果
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *temp = (SendAuthResp *)resp;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *accessUrlStr = [NSString stringWithFormat:@"%@/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code", WX_BASE_URL, kWeiXinAppID, kWeiXinSecret, temp.code];
manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"text/html",@"text/plain",@"application/json", nil];
[manager GET:accessUrlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"請(qǐng)求access的response = %@", responseObject);
NSDictionary *accessDict = [NSDictionary dictionaryWithDictionary:responseObject];
NSString *accessToken = [accessDict objectForKey:kWeiXinAccessToken];
NSString *openID = [accessDict objectForKey:kWeiXinOpenId];
NSString *refreshToken = [accessDict objectForKey:kWeiXinRefreshToken];
//本地持久化,以便access_token的使用演熟、刷新或者持續(xù)
//NSLog(@"bbb%@ %@",accessToken,openID);
if (accessToken && ![accessToken isEqualToString:@""] && openID && ![openID isEqualToString:@""]) {
[[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:kWeiXinAccessToken];
[[NSUserDefaults standardUserDefaults] setObject:openID forKey:kWeiXinOpenId];
[[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:kWeiXinRefreshToken];
[[NSUserDefaults standardUserDefaults] synchronize]; // 命令直接同步到文件里鞭执,來避免數(shù)據(jù)的丟失
}
[self wechatLoginByRequestForUserInfo];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"獲取access_token時(shí)出錯(cuò) = %@", error);
}];
}
}
第三步
調(diào)起app支付操作
if ([balanceData.paymentMold isEqualToString:WX_PAY]) {//微信支付
//判斷微信是否存在
if ([WXApi isWXAppInstalled]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(WX_PAYStatus:) name:WX_PAY object:nil];
HAOrderSubmitViewController *submitVc = (HAOrderSubmitViewController *)[self viewController];
NSString *productName = @"違章代繳";
HAProduct *product = [[HAProduct alloc] init];
product.amount = [NSString stringWithFormat:@"%@f",balanceData.lastMoney];//金額
product.tradeNO = orderMsg.orderNo;//訂單號(hào)
product.productName = productName;//商品名
[submitVc sendWX_PayWithOrderProduct:product];
}else{
WX_APPAlert=[[HAAlertView alloc]initWithTitle:nil message:@"微信尚未安裝,是否安裝微信" delegate:self cancelButtonTitle:@"取消" sureButtonTitle:@"確認(rèn)"];
[WX_APPAlert show];
}
}
支付回調(diào),有坑見文末
- (void)WX_PAYStatus:(NSNotification *)notication{
[self removeNotificationWithName:WX_PAY];
if ([notication.object isEqualToString:@"0"]) {
//修改訂單狀態(tài)
// NSLog(@"支付成功");
//修改服務(wù)端訂單狀態(tài)
[self changePaySatusWitherrCode:notication.object];
[[NSNotificationCenter defaultCenter] postNotificationName:tableViewData object:nil];
}else if ([notication.object isEqualToString:@"-2"]){
//取消支付芒粹,關(guān)閉微信訂單兄纺,生成新訂單,避免影響下一次操作
//NSLog(@"操作取消");
[MBProgressHUD showMessage:@"取消支付"];
HAOrderSubmitViewController *submitVc = (HAOrderSubmitViewController *)[self viewController];
HAPayOnLineContriller *test =(HAPayOnLineContriller *)[self viewController];
[submitVc.navigationController popToViewController:test animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:tableViewData object:nil];
[HAMKManger createOrderWithOrderDetail:orderMsg.orderDetail
CouponsId:orderMsg.couponsId
OrderAmount:orderMsg.orderAmount
GoodsAmount:orderMsg.goodsAmount
OrderType:@"1" Completion:^(NSDictionary *dict,NSInteger state) {
//DLog(@"%@",dict);
[self closeWX_OrderNetworkOrderCode:dict[@"result"]];
}];
}
}
遇見的坑:
- 低版本適配:我們有個(gè)測(cè)試機(jī)一直保持是iOS8.0的系統(tǒng)在支付的時(shí)候不能調(diào)起支付頁面化漆,其沒有向下兼容
- URLShem填寫正確
- 微信訂單提交bug:微信支付訂單處理個(gè)人覺得是有點(diǎn)問題的估脆,當(dāng)你在支付模塊集成兩種或兩種以上支付方式作如下操作:在APP客戶端提交訂單調(diào)取微信支付進(jìn)入微信APP后取消支付操作,而后重新提交同一訂單將會(huì)出現(xiàn)錯(cuò)誤座云,于是我們?cè)谌∠Ц妒菚r(shí)候關(guān)閉該訂單并生成新訂單疙赠,不知道微信現(xiàn)在有沒有處理這個(gè)問題