partner(合作夥伴 ID)、seller(賬號)、privateKey(商家私鑰)AlipaySDK.framework 拖入項目SystemConfiguration.framework、CoreTelephony.framework、CoreMotion.framework、libc++.tbd、libz.tbdOther Linker Flags 添加 -ObjC在 Info.plist 中添加 URL Types,URL Scheme 設置為你的 App Scheme(如 alisdkdemo),用於支付完成回調喚醒 App。
// 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
- (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];
}];
}
在 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 必須全局唯一notifyURL 必須為可公開訪問的 HTTPS 地址| 現象 | 可能原因 | 解決方案 |
|---|---|---|
| 彈窗提示「缺少 partner/seller/私鑰」 | 參數為空或未正確配置 | 檢查商家配置是否正確賦值 |
| 支付寶 App 未安裝 | 自動跳轉 H5 支付頁面 | 屬正常行為,確保 H5 頁面體驗良好 |
| 回調不觸發 | URL Scheme 未配置或不匹配 | 檢查 Info.plist 及調用參數 scheme 一致性 |
| 簽名驗證失敗 | 簽名算法/參數順序/編碼不一致 | 嚴格按照支付寶文檔規範生成簽名字符串 |