
作者:@lp_馬建成,經授權轉載本站。
開場白
前不久做了一個專案,牽涉到支付寶與銀聯支付。這兩家是業界的老大哥,文檔與 SDK 齊全得驚人,但也「高深」到讓人翻半天還摸不著頭緒(純粹吐槽)。本文把用到的最新可用 SDK、文檔,以及我在實作過程中踩過的坑一併標記出來,供大家參考。
資料來源
支付寶(文檔中已包含 Android 與 iOS 版本)
銀聯
銀聯官網資料
Demo
Demo 提供了一組測試用訂單號,若遇到支付失敗,可能是訂單已被他人支付或伺服器訂單過期,屬正常現象。
一、支付寶
1.1 先讀支付寶文檔與 Demo
1.2 導入對應的庫
把支付寶 Demo 裡的這些檔案全部複製進來:
APAuthV2Info.h Order.h libssl.a APAuthV2Info.m Order.m openssl AlipaySDK.bundle Util AlipaySDK.framework libcrypto.a
接著導入系統庫:
SystemConfiguration.framework
然後設定一下 search paths:
build setting -> 搜尋 search path,接著你懂的
完成後先編譯一次看看有沒有錯誤,然後再往下一步走。
1.3 對接
支付寶對接前,你需要先從伺服器取得以下參數(依接口文檔,這裡只列出 Demo 用到的部分):
合作者身份ID alipayPartner = @"2088一串數字"; 介面名稱 alipaySeller = @"tianticar@126.com"; 簽名 aliPayPrivateKey = @"很長很長的私鑰"; 服務器異步通知頁面路徑 alipayNotifServerURL = @"一個網址"; //支付結果,支付寶會通知服務器
其餘與商品相關的參數(涉及業務,客戶端或伺服器提供皆可)直接看 order 程式碼:
Order *order = [[Order alloc] init]; order.partner = alipayPartner; order.seller = alipaySeller; order.tradeNO = tn; //訂單ID(由商家自行制定) order.productName = [NSString stringWithFormat:@"汽車服務充值-%@",@"支付"]; //商品標題 order.productDescription = [NSString stringWithFormat:@"%@:支付寶移動支付充值",@"xxxx"]; //商品描述 order.amount = _txtCNY.text; //商品價格 order.notifyURL = alipayNotifServerURL; //回調URL order.service = @"mobile.securitypay.pay"; order.paymentType = @"1"; order.inputCharset = @"utf-8"; order.itBPay = @"30m"; order.showUrl = @"m.alipay.com"; NSString *appScheme = URLScheme;
呼叫支付寶:
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) { NSLog(@"reslut = %@",resultDic); if ([resultDic[@"resultStatus"] intValue]==9000) { NSLog(@"支付成功"); } else{ NSString *resultMes = resultDic[@"memo"]; resultMes = (resultMes.length<=0?@"支付失敗":resultMes); NSLog(@"%@",resultMes); } }];若發現回呼沒反應,記得設定回調 scheme,並在 AppDelegate 中處理來自支付寶的 URL 回調:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ //跳轉支付寶錢包進行支付,處理支付結果 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; return YES; }二、銀聯
2.1 先讀銀聯文檔與 Demo
2.2 導入對應的庫
SDK 分為兩個版本:
① 支援純無卡交易的靜態庫(UPPayPlugin),包含:
UPPayPlugin.h UPPayPluginDelegate.h libUPPayPlugin.a
② 同時支援純無卡交易與 VIPOS 音頻口支付的靜態庫(UPPayPluginPro),包含:
UPPayPluginPro.h UPPayPluginDelegate.h libUPPayPluginPro.a
兩者的差異主要在於版本②需要把 .m 檔改成 .mm(推測是以 C++ 封裝了音頻相關功能)。添加 SDK 時:
a) 依商戶選擇的版本,把 sdk/inc 與 sdk/libs 下對應的三個檔案加入工程;
b) 若選 UPPayPlugin 版本,加入 QuartzCore.framework、Security.framework;
c) 若選 UPPayPluginPro 版本,另需加入 AudioToolbox.framework、CoreAudio.framework、MediaPlayer.framework、AVFoundation.framework;
d) 在 Build Settings 的 Other Linker Flags 中加入 -ObjC。
2.3 對接
讓伺服器準備好交易流水號後即可對接:
+ (BOOL)startPay:(NSString*)tn mode:(NSString*)mode viewController:(UIViewController*)viewController delegate:(id)delegate;
參數說明(皆為必填):tn 為交易流水號(由銀聯後台生成,經商戶後台回傳);mode 為接入模式,@"00" 代表生產環境、@"01" 代表開發測試環境;viewController 為當前呼叫支付的頁面;delegate 為實作 UPPayPluginDelegate 的物件。上線前務必把 mode 改回正式環境。
在 viewcontroller 中引入頭文件並呼叫介面:
#import "UPPayPluginPro.h" [UPPayPluginPro startPay:tn mode:self.tnMode viewController:self delegate:self]; - (void)UPPayPluginResult:(NSString *)result { NSString* msg = [NSString stringWithFormat:@"%@", result]; NSLog(@"msg%@",msg); if ([result isEqualToString:@"msgcancel"]) { NSLog(@"取消銀聯支付..."); } else if([result containsString:@"success"]){ NSLog(@"支付成功"); } }微信支付
微信支付整體來說審核較為繁瑣,作者未親自申請帳號測試,依文檔與 Demo 走一般不會出問題,讀者可對照微信支付文檔與 SDK 實作。
小記
若遇到銀聯的 C++ 編譯問題,可把對應的 viewcontroller 改成 .mm 再編譯看看;若有 CreateRSADataSigner arm64 的錯誤,把支付寶提供的 DataSigner 改成 .mm 即可。其餘可參照作者的 Demo。
— @lp_馬建成