App接入支付寶
正在尋找直擊內心的話……
背景介紹
正在開發一個類大眾點評的應用,涉及到支付寶支付。雖說不是第一次做支付,但之前遇到的問題現在還是遇到了。所以想記錄一下,不對或不合理的地方大家多多指正,共同進步。
資源
請點選一下連接獲取資源:
1.螞蟻金服開放平臺App開發文件【包括支付寶Logo和相關素材】
2.App支付客戶端SDK和Demo下載
3.【App支付架構】
4.【支付場景描述】
5.【退款流程】
建立應用
假設你已經登入支付寶開放平臺,並與支付寶簽約
1.建立應用的入口:
(1)【登陸後的網站首頁】-【右上角的管理中心】-【開發者中心】
(2)
入口展示:
2.建立步驟:
(1)一般選擇自用型,具體區別請看這裡
(2)記得將APPID儲存在你知道的一個地方,Demo測試要用。另外這裡需填寫的基礎資訊之後都是可以修改的。
這裡的功能,根據需要跟支付寶簽約使用。具體看這裡
請一定要仔細閱讀,點選【檢視如何使用】後的內容。
【設定應用閘道器】
【授權回撥地址由後臺設定】
【設定應用公鑰】
步驟和設定應用閘道器差不多,都是需要校驗身份,然後繼續操作。驗證身份後,根據該文件操作即可
在此需要說明的是:(1)只需要設定一個應用公鑰就可以了,當你將自己的公鑰設定完後,支付寶會自動生成一個支付寶公鑰與其對應。(2)設定公鑰時,根據實際條件選擇是否為JAVA,以及1024或2048(當然選擇2048加密了)。
Demo執行
(1)我是使用真機測試,當你配置好下圖的內容時就可以真機運行了。
(2)執行後,你會發現不管你點選首頁的哪個按鈕都會給你提示:缺少appId或者私鑰或缺少pid或者appID或者私鑰,你需要這樣做:在工程內搜尋缺少兩個字,找到相應的位置,新增AppID,私鑰,pid就好了。
AppID:在開發者中心找到你的應用即可看到AppID;
私鑰:在你【設定應用公鑰】的時候就會得到一個應用私鑰,直接用就好,在Demo中你只用設定rsa2PrivateKey
(支付寶建議)。
pid:【賬戶的管理中心】-【服務市場】
(3)我在配置完Pid,AppID,私鑰後,依然報錯:
* rsa_private read error : private key is NULL*
,後來經網上查詢使用這個方式解決了錯誤:
1)在RSADataSigner.m檔案中 搜尋程式碼
[result appendString:@"-----BEGIN PRIVATE KEY-----\n"];
將其改成[result appendString:@"-----BEGIN RSA PRIVATE KEY-----\n"];
2)在RSADataSigner.m檔案中 搜尋程式碼
[result appendString:@"\n-----END PRIVATE KEY-----"];
將其改成[result appendString:@"\n-----END RSA PRIVATE KEY-----"];
實際開發
【整合SDK】:只可以手動整合。
請按照點選這裡,認真按步驟操作
【業務邏輯】
使用者提交訂單後,待商家端確認即可進行支付。待支付的訂單都展示在一個列表中。
【實現邏輯】
在待支付列表中點選“去支付按鈕”,跳轉選擇支付方式的頁面;
選擇好支付方式後,將支付方式傳遞給後臺,後臺會返回訂單字串,我們根據訂單字串發起支付請求,喚起支付寶支付。
【程式碼實現】
AppDelegate.m
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
}];
// 授權跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
}
NSLog(@"授權結果 authCode = %@", authCode?:@"");
}];
}
return YES;
}
// NOTE: 9.0以後使用新API介面
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"safepay"]) {
// 支付跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
}];
// 授權跳轉支付寶錢包進行支付,處理支付結果
[[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSString *authCode = nil;
if (result.length>0) {
NSArray *resultArr = [result componentsSeparatedByString:@"&"];
for (NSString *subResult in resultArr) {
if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
authCode = [subResult substringFromIndex:10];
break;
}
}
}
NSLog(@"授權結果 authCode = %@", authCode?:@"");
}];
}
return YES;
}
ViewController.m中
網路請求成功後,獲取到訂單字串後,喚起支付寶支付
self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.mode = MBProgressHUDAnimationFade;
NSString *url = [NSString stringWithFormat:@"%@%@",Main_URL,Build_Url];
NSDictionary *par = @{@"apitoken":[UserDefaut objectForKey:BYD_APIToken],
@"ord_type":self.ord_type,
@"ord_id":self.orderID,
@"pay_type":self.payStr};
[PPNetworkHelper POST:url parameters:par success:^(id responseObject) {
[self.hud removeFromSuperview];
if ([responseObject[@"code"] isEqual:@0]) {
[MBProgressHUD showMessage:responseObject[@"message"] ToView:self.view.window];
}else if([responseObject[@"code"] isEqual:@1]){
// NOTE: 呼叫支付結果開始支付
[[AlipaySDK defaultService] payOrder:responseObject[@"data"][@"order_string"] fromScheme:BYD_ScemeStr callback:^(NSDictionary *resultDic) {
if ([resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付結果" message:@"您已支付成功,再去逛逛吧!" preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
[MBProgressHUD showMessage:@"跳轉詳情頁" ToView:self.view];
}];
[alert addAction:action1];
[self presentViewController:alert animated:YES completion:^{
}];
}else if ([resultDic[@"resultStatus"] isEqualToString:@"8000"]){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付結果" message:@"您還未完成支付,請繼續支付!" preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
self.AlipayBtn.selected = NO;
}];
[alert addAction:action1];
[self presentViewController:alert animated:YES completion:^{
}];
}else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付結果" message:@"很抱歉,您支付失敗了!" preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
self.AlipayBtn.selected = NO;
}];
[alert addAction:action1];
[self presentViewController:alert animated:YES completion:^{
}];
}
}];
}else if([responseObject[@"code"] isEqual:@(-2)]){
[MBProgressHUD showMessage:responseObject[@"message"] ToView:self.view.window];
}
} failure:^(NSError *error) {
[self.hud removeFromSuperview];
[MBProgressHUD showMessage:Tip_URL ToView:self.view.window];
}];
【說明】
1.支付回撥結果引數如下,請比對這些引數,來進行個性化提示:
點我帶你去看引數
2.這是iOS呼叫說明,你可能會用到
總結
(1)其實用到第三方的東西,特別是有官方說明文件和Demo的這種,都不是難的,關鍵在於你需要認真閱讀官方文件,按照步驟進行操作。
(2)App接入支付的大量工作都在建立應用時配置環境以及後臺了,移動端做的工作並不多。