-
在支付寶開發中心與支付寶簽約
店家我的商家服務—在頁面的下方找到——>簽約管理—>找到移動支付—–>點擊下載集成文檔—>跳到新的頁面,在頁面下方—>找到下載開發包
獲取相應的ID和密鑰(公鑰加密,私鑰解密)
密鑰一般由后臺提供,也可通過官方的密鑰生成器來生成,生成的公鑰需要關聯到賬號(首頁—>查看PID|KEY—>輸入支付密碼,查詢key、支付寶公鑰—>上傳RSA公鑰<注意不能有空格,換行之類的>) -
添加相應的文件
sdk文件.png
sdk文件夾路徑.png
-
導入系統依賴庫
libc++.tbd
Iibz.tbd
CoreGraphics.framework
CoreTeIephonyframework
CoreText.framework
QuartzCore.framework
SystemConfiguration .framework
CFNetworkframework
CoreMotion .framework
libcrypto.a
AlipaySDK.framework
libssl.a
依賴庫.png
-
配置App跳轉的白名單
-
添加URL Schemes
和調用支付接口時傳入的Schemes一致,例如
-
配置代碼
NSDictionary *dict = (NSDictionary *)result; NSString *partner = [[dict[@"data"] objectAtIndex:0] objectForKey:@"partner"]; NSString *seller = [[dict[@"data"] objectAtIndex:0] objectForKey:@"seller_id"]; NSString *privateKey = [[dict[@"data"] objectAtIndex:0] objectForKey:@"sign"]; //partner和seller獲取失敗,提示 if ([partner length] == 0 || [seller length] == 0 || [privateKey length] == 0) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"缺少partner或者seller或者私鑰。" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; return; } /* *生成訂單信息及簽名 */ //將商品信息賦予AlixPayOrder的成員變量 Order *order = [[Order alloc] init]; order.partner = partner; order.seller = seller; order.tradeNO = self.orderID;//訂單ID(由商家自行制定) order.productName = self.orderID; //商品標題 order.productDescription = self.orderID; //商品描述 order.amount = self.totalPrice; //商品價格 order.notifyURL = self.notifyUrl;//回調URL //下述參數按照官方給定輸入 order.service = @"mobile.securitypay.pay";//支付接口名稱 order.paymentType = @"1";//支付類型。 order.inputCharset = @"utf-8";//編碼 order.itBPay = @"30m";//超時時間 order.showUrl = @"m.alipay.com";//商品展示網站 //應用注冊scheme NSString *appScheme = @"XXXXX"; //將商品信息拼接成字符串 NSString *orderSpec = [order description]; //將簽名成功字符串格式化為訂單字符串,請嚴格按照該格式 NSString *orderString = nil; if (privateKey != nil) { orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"", orderSpec, privateKey, @"RSA"]; [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) { if ([[resultDic objectForKey:@"resultStatus"] isEqualToString:@"9000"]) { //支付成功 }else{ //支付失敗 } }]; }
-
注意事項:
- 導入文件后還需添加文件路徑(文件路徑為相對路徑$(PROJECT_DIR)),否側在使用時會出現頭文件無法找到的錯誤
- 進行了上述操作,當編譯時會出現base64.h openssl_wrapper.h文件存在大量的錯誤信息,這因為.h文件缺少#import<Foundation/Foundation.h>
- 由于在跳轉支付寶客戶端支付的過程中,商戶app在后臺很可能被系統kill了,所以pay接口的callback就會失效,請商戶對standbyCallback返回的回調結果進行處理,就是在這個方法里面處理跟callback一樣的邏輯。
上述邏輯在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) {
NSLog(@"支付結果 = %@",resultDic);
//在resultStatus=9000,并且success=“true”以及sign=“xxx”校驗通過的情況下,證明支付成功。其它情況歸為失敗。較低安全級別的場合,也可以只通過檢查resultStatus以及success=“true”來判定支付結果
}];
}
return YES;
}