一、什么是支付寶
第三方支付平台
和內購非常相似
內購是用戶將錢付款給蘋果,之后蘋果分成給商戶
支付寶是用戶將錢付款給支付寶,之后支付寶將錢轉入我們的賬戶
使用支付寶前提
購買的物品必須是和應用程序無關的.比如:團購卷/衣服/電子產品
如果和應用程序有關,必須采用內購(否則不允許上架).比如:會員/游戲道具
二、集成支付寶
現在不少app內都集成了支付寶功能
使用支付寶進行一個完整的支付功能,大致有以下步驟:
向支付寶申請, 與支付寶簽約,獲得商戶ID(partner)和賬號ID(seller)和私鑰(privateKey)
下載支付寶SDK
生成訂單信息,簽名加密
調用支付寶客戶端,由支付寶客戶端跟支付寶安全服務器打交道
支付完畢后,支付寶客戶端會自動跳回到原來的應用程序
在原來的應用程序中顯示支付結果給用戶看
三、集成SDK注意事項
* 支付寶的SDK下載和百度、訊飛語言等SDK不一樣,直接搜支付寶進入官網下載
* 支付寶下載(選擇我是商戶用戶—>如何集成—>移動開發)
* 集成前查看demo
* 集成時需要用到的庫
* 集成之后可能遇到的問題
1)集成SDK編譯時找不到 openssl/asn1.h 文件
解決方案:Build Settings --> Search Paths --> Header Search paths : $(SRCROOT)/支付寶集成/Classes/Alipay
2)鏈接時:找不到 SystemConfiguration.framework 這個庫
解決方案:
四、具體代碼
比如現在將產品展示在tableView上,選中時即購買此商品,集成支付寶的核心代碼如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 1.取出模型對象 Product *product = self.products[indexPath.row]; // 2.購買商品 [self buyProduct:product]; } - (void)buyProduct:(Product *)product { // 1.添加寫partner/seller/privateKey,簽約后獲得 NSString *partner = @""; NSString *seller = @""; NSString *privateKey = @""; // 2.生成訂單 // 2.1.創建訂單 Order *order = [[Order alloc] init]; // 2.2.設置商戶ID/賬號ID order.partner = partner; order.seller = seller; // 2.3.設置訂單號 order.tradeNO = nil; // 2.4.設置產品相關的信息 order.productName = product.title; order.productDescription = product.detail; order.amount = [NSString stringWithFormat:@"%.2f", product.price]; // 2.5.設置訂單常量 order.service = @"mobile.securitypay.pay"; order.paymentType = @"1"; order.inputCharset = @"utf-8"; order.itBPay = @"30m"; order.showUrl = @"m.alipay.com"; // 2.6.回調URL(異步通知服務器的地址) order.notifyURL = @"http://www.xxx.com"; //回調URL // 2.7.將訂單信息拼接成字符串 NSString *orderSpec = [order description]; // 3.對訂單進行簽名加密 // 3.1.對訂單進行加密 id<DataSigner> signer = CreateRSADataSigner(privateKey); NSString *signedString = [signer signString:orderSpec]; // 3.2.將簽名成功字符串格式化為訂單字符串,請嚴格按照該格式 NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"", orderSpec, signedString, @"RSA"]; // 4.打開支付寶客戶端進行支付(用戶沒有安裝支付寶客戶端,直接在應用程序中添加一個WebView,通過網頁讓用戶進行支付) // 注意:如果是通過網頁支付完成,那么會回調該block:callback [[AlipaySDK defaultService] payOrder:orderString fromScheme:@"jingdong" callback:^(NSDictionary *resultDic) { }]; }
AppDelegate.m
// 當通過別的應用程序,將該應用程序打開時,會調用該方法 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options { // 當用戶通過支付寶客戶端進行支付時,會回調該block:standbyCallback [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { NSLog(@"result = %@",resultDic); }]; return YES; }