iOS 支付寶支付/最新的支付寶支付詳解

首先,支付寶支付的流程大致如下:

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、對拷貝部分的代碼適當(dāng)?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 = @"測試商品標(biāo)題"; //商品標(biāo)題? ??

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";

//將簽名成功字符串格式化為訂單字符串,請嚴(yán)格按照該格式

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,可以在上面代碼中獲?。?/p>

//應(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界面進行支付?!?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容