支付寶快捷支付:
官方文檔中,支付寶說建議我們使用支付時要講簽名過程放在服務器端,這樣安全。同時給的demo中簽名是在本地移動端做的。。。不過支付寶的集成還是較簡單的。
為了安全簽名當然放后台做了。我實現支付的過程中主要遇到是參數問題,最后提,先大概梳理一下流程。
1.當然第一步是注冊賬號,創建應用(應用類型根據情況來選擇),申請開通支付功能(多久忘了,印象中是挺快的)。
2.導入SDK添加庫什么的就按照官方文檔來就好了,不贅述了
3.添加完成后command+b試下是否編譯OK,報錯就根據提示去改就好了(注意庫文件重復添加問題,一般不會出現問題)
4.導入頭文件 #import <AlipaySDK/AlipaySDK.h>,開始調用支付API。(配置不要放本地了,交給后台。注意:關於簽名的問題要提前和后台溝通好怎么做,我們是建了個post請求)
case 1:// 支付寶 { //應用注冊scheme,在AlixPayDemo-Info.plist定義URL types NSString *appScheme = @"Alixxxxx";//這個可以任意自定義,不過最好是和自己的應用名字相關的 [[AlipaySDK defaultService] payOrder:result[@"info"] fromScheme:appScheme callback:^(NSDictionary *resultDic) { // 參數result[@"info"]是請求到的驗證后的簽名(字符串)
NSLog(@"resultDic === %@", resultDic); NSInteger heCha; NSString *heChaLable = [NSString stringWithFormat:@"%@",resultDic[@"resultStatus"]]; heCha = [heChaLable integerValue]; if (heCha==9000 ) { //這里做支付成功后你想做的事情 [AlertUtil showAlertInfoSingle:@"支付成功"]; } if (heCha==8000) { UIAlertView *shibai=[[UIAlertView alloc]initWithTitle:@"提示" message:@"訂單正在處理中" delegate:self cancelButtonTitle:nil otherButtonTitles:@"請重新支付", nil]; [shibai show]; } if (heCha==4000) { UIAlertView *shibai=[[UIAlertView alloc]initWithTitle:@"提示" message:@"訂單支付失敗" delegate:self cancelButtonTitle:nil otherButtonTitles:@"請重新支付", nil]; shibai.delegate=self; [shibai show]; } if (heCha==6001) { [AlertUtil showAlertInfoSingle:@"支付失敗"]; } if (heCha==6002) { UIAlertView *shibai=[[UIAlertView alloc]initWithTitle:@"提示" message:@"網絡連接出錯" delegate:self cancelButtonTitle:nil otherButtonTitles:@"請重新支付", nil]; shibai.delegate=self; [shibai show]; } }]; } break;
注意://應用注冊scheme,操作過程見下圖
圖中 URL Scheme 處填寫上面代碼中定義的 Alixxxxx
5.OK,這樣支付寶集成算完成了,但是!后台返回的字符串可不一定能用啊。。。
6.所以提下我出現的問題(后台要解決的):
a.參數問題,一定要符合支付寶的要求,后台給你的簽名字符串中要與下面參數的名字一毛一樣(我們之前有個參數名字不同,調了N久才發現,哭),下面貼個簽名字符串的實例(參數標綠色了):
"partner=\"2088101568353491\"&seller_id=\"2088101568353491\"&out_trade_no=\"YR2VGG3G1I31XDZ\"&subject=\"1\"&body=\"我是測試數據\"&total_fee=\"0.02\"¬ify_url=\"http://www.xxx.com\"&service=\"mobile.securitypay.pay\"&payment_type=\"1\"&_input_charset=\"utf-8\"&it_b_pay=\"30m\"&sign=\"GsSZgPloF1vn52XAItRAldwQAbzIgkDyByCxMfTZG%2FMapRoyrNIJo4U1LUGjHp6gdBZ7U8jA1kljLPqkeGv8MZigd3kH25V0UK3Jc3C94Ngxm5S%2Fz5QsNr6wnqNY9sx%2Bw6DqNdEQnnks7PKvvU0zgsynip50lAhJmflmfHvp%2Bgk%3D\"&sign_type=\"RSA\""
主要參數解釋(具體的不見得一樣,這個看你簽名時葯傳的有什么了):https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.fdQytM&treeId=59&articleId=103678&docType=1
notify_url:支付的回調地址
body:商品的描述
total_fee:商品的價格
sign:簽名串
sign_type:驗簽方式
微信支付:
很多小伙伴都說微信支付真的是太坑了,官方文檔都帶有錯的。。。。我也被坑了N次,鑒於這個坑太多,做下簡單步驟和備份記錄,盡量避免再次進坑里。
1.注冊什么的略過,關於支付的申請,相關資料向公司相關人員要到就OK;
2.配置:參照文檔來 微信官方文檔
3.支付請求主要代碼
case 3:// 微信支付 { NSLog(@"微信支付"); NSDictionary *data = result[@"data"];// result[@"data"]是請求后台獲取到所需數據 需要注意:這里的數據均是二次簽名后的數據,不要用第一次簽名的數據 PayReq *request = [[PayReq alloc] init]; request.partnerId = data[@"partnerid"];//@"10000100";// 商戶號 request.prepayId= data[@"prepayid"];//@"1101000000140415649af9fc314aa427";// 預支付交易會話id request.package = @"Sign=WXPay";// 固定值 request.nonceStr= data[@"noncestr"];//@"a462b76e7436e98e0ed6e13c64b4fd1c";// 隨機字符串 request.timeStamp= [data[@"timestamp"] intValue];//@"1397527777";// 時間戳 request.sign= data[@"sign"];// 簽名字符串 [WXApi sendReq:request];// 發起請求 } break;
4.AppDelegate.m 中設置回調
// 微信 WXApiDelegate協議的方法 - (void)onResp:(BaseResp *)resp { // 如果第三方程序向微信發送了sendReq的請求,那么onResp會被回調。sendReq請求調用后,會切到微信終端程序界面。 NSLog(@"%@",resp); if ([resp isKindOfClass:[PayResp class]]) { PayResp *response = (PayResp *)resp; NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode]; switch (response.errCode) { case WXSuccess: {// 支付成功 NSString *strTitle = @"支付結果"; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } break; default: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付結果" message:@"支付失敗" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; break; } } } }
5.注意問題:
a.如果程序中同時集成了友盟,那么在 register 時,要先友盟后微信
b.支付時只有一個確定按鈕,確認返回后 ret = -2 一般就是請求時參數有問題,簽名問題的幾率較大。
c.請求所用數據一定是二次簽名的數據!