我是使用的cocoapods來做的,這樣就省了手動添加這一步。沒有安裝 cocoapods的同學請點擊這里
一般看官方文檔接入的話確實會走不少彎路,其實過程很簡單要自己摸索著一步步來,好了我們開始。
http://blog.csdn.net/wtt692732757/article/details/50966893
http://www.itdecent.cn/p/32cea46e74cd
客戶端:負責使用服務端傳來的訂單信息調用支付寶支付接口,及根據SDK同步返回的支付結果展示結果頁。
- 支付寶移動支付接入我們的app的時候,首先是要先與支付寶簽約,然后得到我們的
partner//簽約的支付寶賬號唯一ID
seller//賣家支付寶賬號
privateKey//私鑰
一般這三個都是放在服務端
生成私鑰
- 私鑰公鑰一定是要對應的
生成私鑰的方法
RSA密鑰生成命令
1. 進入終端,輸入
“openssl”
2. 生成RSA私鑰
openssl>genrsa -out rsa_private_key.pem 1024
3. 生成RSA公鑰
openssl>rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
4. 將RSA私鑰轉換成PKCS8格式
openssl>pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt
注意:“>”符號后面的才是需要輸入的命令。
- 得到公鑰私鑰之后在你的賬戶合作伙伴密鑰里填寫公鑰(私鑰放在你的項目中或者服務器端)

支付寶設置公鑰私鑰截圖
- 這里是支付寶SDK的下載頁面。我就是官方地址
或者使用cocoa pods直接安裝。(我是使用的cocoa pods安裝的)
我們下載的官方demo中

文件
在配置訂單信息之前,因為要遵循RSA簽名規(guī)范,所以我們還要把文檔里的libcrypto.a , libssl.a , openssl , Util , Order.h , Order.m APAuthV2Info.h APAuthV2Info.m 拖入工程。
之后項目會報錯說找不到<openssl/opensslconf.h> 這時不要慌我們找到 Targets -> Build Settings 下的 Header Search Paths 然后添加
$(SRCROOT)/項目名稱/文件的絕對地址
這樣就不會報錯了
- 現在我們可以開始寫代碼了
頭文件

添加頭文件
//點擊支付
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
/*============================================================================*/
/*=======================需要填寫商戶app申請的===================================*/
/*============================================================================*/
NSString *partner = @"";//簽約的支付寶賬號唯一ID
NSString *seller = @"";//賣家支付寶賬號
NSString *privateKey =@"";//私鑰
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];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
// 生成訂單和簽名
Order * order = [[Order alloc]init];
order.partner = partner;
order.seller = seller;
order.tradeNO = @"1234567810";
order.productName = @"測試數據1";
order.productDescription = @"四月五號下午";
order.amount = @"0.01";
order.notifyURL = @"http:www.xxx.com";//回調給服務器的url
order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showUrl = @"m.alipay.com";
//應用注冊scheme,在AlixPayDemo-Info.plist定義URL types
NSString *appScheme = @"alisdkdemo";
NSString * orderSpec = [order description];
NSLog(@"orderspec = %@",orderSpec);
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString * signedstring = [signer signString:orderSpec];
//將簽名成功字符串格式化為訂單字符串,請嚴格按照該格式
NSString *orderString = nil;
if (signedstring != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedstring, @"RSA"];
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",[resultDic objectForKey:@"memo"]);
}];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
最后說一句 在AppDelegate.m里面寫的回調方法,我一直沒搞懂有什么作用文檔說要寫就寫了好像并沒有什么用 但是為了保險還是寫上比較好
-(BOOL)application:(UIApplication *)app openURL:(nonnull NSURL *)url options:(nonnull NSDictionary<NSString *,id> *)options{
if([url.host isEqualToString:@"safepay"]){
[[AlipaySDK defaultService]processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"resultshi = %@",[resultDic objectForKey:@"memo"]);
}];
}
return YES;
}