首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

iOS 支付寶 SDK 整合完整教學

     編輯:Java 相關知識

iOS App 接入支付寶支付 SDK 完整步驟指南

前置準備

  1. 申請支付寶商家賬號,簽約「手機網站支付」或「App 支付」產品
  2. 從支付寶開放平台下載最新 iOS SDK(含 Demo)
  3. 獲取關鍵參數:partner(合作夥伴 ID)、seller(賬號)、privateKey(商家私鑰)

SDK 整合步驟

1. 導入 SDK 及依賴庫

  • 將 AlipaySDK.framework 拖入項目
  • 添加系統依賴:SystemConfiguration.framework、CoreTelephony.framework、CoreMotion.framework、libc++.tbd、libz.tbd
  • 設置 Other Linker Flags 添加 -ObjC

2. 配置 URL Schemes

在 Info.plist 中添加 URL Types,URL Scheme 設置為你的 App Scheme(如 alisdkdemo),用於支付完成回調喚醒 App。

3. 創建訂單模型

// AlipayOrder.h
@interface AlipayOrder : NSObject
@property (nonatomic, copy) NSString *partner;       // 商家 ID
@property (nonatomic, copy) NSString *seller;        // 賬號
@property (nonatomic, copy) NSString *tradeNO;       // 訂單號(商家自定義)
@property (nonatomic, copy) NSString *productName;   // 商品標題
@property (nonatomic, copy) NSString *productDescription; // 商品描述
@property (nonatomic, copy) NSString *amount;        // 金額(元)
@property (nonatomic, copy) NSString *notifyURL;     // 異步通知地址
@property (nonatomic, copy) NSString *service;       // 固定:mobile.securitypay.pay
@property (nonatomic, copy) NSString *paymentType;   // 固定:1
@property (nonatomic, copy) NSString *inputCharset;  // 固定:utf-8
@property (nonatomic, copy) NSString *itBPay;        // 固定:30m
@property (nonatomic, copy) NSString *showUrl;       // 固定:m.alipay.com
- (NSString *)description; // 重寫生成訂單字符串
@end

4. 實現支付調用邏輯

- (void)alipay {
    // 1. 獲取商家配置(實際應從服務端下發)
    NSString *partner = @"你的partner";
    NSString *seller = @"你的seller";
    NSString *privateKey = @"你的私鑰";

    if ([partner length] == 0 || [seller length] == 0 || [privateKey length] == 0) {
        [self showAlert:@"缺少商家配置參數"];
        return;
    }

    // 2. 構建訂單
    AlipayOrder *order = [[AlipayOrder alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = @"ORDER_20240101_001";
    order.productName = @"測試商品";
    order.productDescription = @"商品描述";
    order.amount = @"0.01";
    order.notifyURL = @"https://yourdomain.com/alipay/notify";
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";

    // 3. 生成訂單字符串
    NSString *orderSpec = [order description];

    // 4. 簽名(建議由服務端簽名後下發,客戶端不持有私鑰)
    // id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = @"服務端返回的簽名字符串";

    // 5. 拼接最終支付字符串
    NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"", 
                             orderSpec, signedString, @"RSA"];

    // 6. 呼起支付寶
    [[AlipaySDK defaultService] payOrder:orderString fromScheme:@"alisdkdemo" callback:^(NSDictionary *resultDic) {
        NSLog(@"支付結果: %@", resultDic);
        // 解析 resultDic[@"resultStatus"]:9000 成功,8000 正在處理,4000 失敗,6001 用戶取消
        [self handlePayResult:resultDic];
    }];
}

5. 處理支付回調

在 AppDelegate.m 中實現:

#import <AlipaySDK/AlipaySDK.h>

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    if ([url.scheme hasPrefix:@"alisdkdemo"]) {
        return [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"回調結果: %@", resultDic);
            // 發送通知通知業務邏輯層
        }];
    }
    return YES;
}

// iOS 8 兼容
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [self application:application openURL:url options:nil];
}

關鍵注意事項

  • 私鑰安全:生產環境嚴禁將私鑰放在客戶端,必須由服務端簽名,客戶端只負責調起支付
  • 訂單號唯一:商家訂單號 tradeNO 必須全局唯一
  • 金額單位:以「元」為單位,精確到小數點後兩位
  • 沙箱測試:上線前務必在支付寶沙箱環境全流程測試
  • HTTPS 回調:notifyURL 必須為可公開訪問的 HTTPS 地址

常見問題排查

總結

支付寶 iOS SDK 整合核心流程:配置參數 → 構建訂單 → 服務端簽名 → 客戶端調起支付 → 處理回調。重點在於私鑰不落地、訂單號唯一、回調配置正確三大原則。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
現象可能原因解決方案
彈窗提示「缺少 partner/seller/私鑰」參數為空或未正確配置檢查商家配置是否正確賦值
支付寶 App 未安裝自動跳轉 H5 支付頁面屬正常行為,確保 H5 頁面體驗良好
回調不觸發URL Scheme 未配置或不匹配檢查 Info.plist 及調用參數 scheme 一致性
簽名驗證失敗簽名算法/參數順序/編碼不一致嚴格按照支付寶文檔規範生成簽名字符串