首先,支付寶支付的流程大致如下:
1、下載和查看支付寶官方Demo
直接進入支付寶的支付平臺下載:doc.open.alipay.com/doc2/detail.htm
集成過程基本就是按照上面的四個流程就可以完成iOS的支付寶SDK。
那我們一起按著步驟來集成:
1、下載SDK和Demo蔗蹋。SDK&DEMO
下載后解壓:
運行Demo:
查找方法:
由上圖可以看出需要三個參數(shù)的值:partner、seller囱淋、privateKey猪杭。這三個參數(shù)就需要商戶app申請的。
2妥衣、實現(xiàn)支付寶支付功能:
2-1皂吮、添加SDK:
2-2、添加SDK依賴庫:
2-3税手、創(chuàng)建一個訂單對象:AlipayOrder
2-4蜂筹、把支付寶Demo中的訂單對象的參數(shù)都拷貝進去:
Order.h的參數(shù) 都拷貝到 AlipayOrder.h中。
2-5芦倒、處理支付的代碼:
2-6艺挪、對拷貝部分的代碼適當?shù)某槿『托薷模旁谝粋€alipay方法中:
- (void)aliPay {? ?
? ? ? ? // 支付寶支付? ??
? ? ? ?/* 在調(diào)用支付寶支付之前兵扬,需要我們將相關(guān)訂單參數(shù)發(fā)送至我們的后臺服務(wù)器麻裳,由后臺服務(wù)器進行簽名處理姥敛,? ? 并返回客戶端所有相關(guān)參數(shù)银择,客戶端直接使用參數(shù)調(diào)起支付寶支付。? ??
? ? ?*/? ??
? ? ?/*? ??
? ? ?*商戶的唯一的parnter和seller蚕冬。? ??
? ? ?*簽約后傲霸,支付寶會為每個商戶分配一個唯一的 parnter 和 seller国瓮。? ?
? ? ?*/ ? ? ? ? ? /*======================================================================*/? ? /*=======================需要填寫商戶app申請的==============================*/? ? /*======================================================================*/? ? NSString *partner = @"";? ?
?NSString *seller = @"";? ??
NSString *privateKey = @"";? ? /*======================================================================*/? ? /*======================================================================*/? ? /*======================================================================*/? ? ? ? //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的成員變量? ??
AlipayOrder *order = [[AlipayOrder alloc] init];? ??
order.partner = partner;? ??
order.seller = seller;? ??
order.tradeNO = @"1234567890"; //訂單ID(由商家自行制定)? ??
order.productName = @"測試商品標題"; //商品標題? ??
order.productDescription = @"測試商品描述"; //商品描述? ??
order.amount = @"0.01"; //商品價格? ??
order.notifyURL =? @"http://www.xxx.com"; //回調(diào)URL? ? ? ??
order.service = @"mobile.securitypay.pay";? ??
order.paymentType = @"1";? ??
order.inputCharset = @"utf-8";? ??
order.itBPay = @"30m";? ??
order.showUrl = @"m.alipay.com";? ? ? ??
//應(yīng)用注冊scheme,在AlixPayDemo-Info.plist定義URL types? ??
NSString *appScheme = @"alisdkdemo";? ? ? ?
?//將商品信息拼接成字符串? ??
NSString *orderSpec = [order description];? ??
NSLog(@"orderSpec = %@",orderSpec);? ? ? ?
?//獲取私鑰并將商戶信息簽名,外部商戶可以根據(jù)情況存放私鑰和簽名,只需要遵循RSA簽名規(guī)范,并將簽名字符串base64編碼和UrlEncode? ??
//? ? id<DataSinger> signer = CreateRSADataSigner(privateKey);
// 簽名值由服務(wù)器處理并返回客戶端
NSString *signedString = @"xxxxxxx_sign";
//將簽名成功字符串格式化為訂單字符串,請嚴格按照該格式
NSString *orderString = nil;
if (signedString != nil) {
orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
orderSpec, signedString, @"RSA"];
// 發(fā)起支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"支付結(jié)果 reslut = %@",resultDic);
}];
}
}
2-7狞谱、另外需要應(yīng)用中注冊appScheme乃摹,可以在上面代碼中獲取:
//應(yīng)用注冊scheme,在AlixPayDemo-Info.plist定義URL types
NSString *appScheme = @"alisdkdemo";
2-8跟衅、添加appScheme
2-9孵睬、支付代碼處理之后,我們開始處理回調(diào)結(jié)果伶跷。
需要在Appdelegate中添加支付寶頭文件#import<AlipaySDK/AlipaySDK.h>并添加處理回到結(jié)果的代理方法:
2-10掰读、添加按鈕監(jiān)聽支付事件和設(shè)置商戶ID的參數(shù):
3秘狞、獲取商戶支付ID。
【說明:使用支付寶支付的時蹈集,如果手機安裝了支付寶App烁试,就會直接調(diào)用支付寶進行支付;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 如果手機沒有安裝支付寶App拢肆,就會調(diào)用支付寶H5界面進行支付减响。】