1. 準備工作
簽約與審核
- 1 商戶在b.alipay.com里進行產品簽約
- 2 商戶登錄qy.alipay.com,可在“簽約訂單”中查看審核進度
PID 和 密鑰管理
- 1 登錄支付寶官方網站b.alipay.com 點擊導航欄中“商家服務,即可查詢 PID
- 2 生成公鑰和私鑰
- 3 RSA加密
2. SDK 下載
首先從支付寶開放平臺下載SDK. 解壓后的目錄下包含「服務端demo」和「客戶端demo」。「客戶端demo」包含了集成支付寶所需要的所有文件。
3. 交易流程
- 1 首先是用戶在App上面下訂單,同時App請求服務端API,服務端接收到POST請求后生成相應的訂單,記錄到DB,然后將訂單信息返回到。
- 2 App接收到返回訂單信息后,在客戶端生成符合支付寶API要求 的數據請求。其中就包括notify URL,這個URL必須是外網可以訪問的,目的是用來接收支付寶支付成功后的反饋。
- 3 App調用支付寶支付API。
- 4 支付成功或失敗后返回App,并根據返回結果給用戶相應反饋信息。
- 5 如果支付成功后,支付寶服務器會調用第2步提到的 notify URL. 這個notify URL需要在你自己的服務器端實現相應功能,用來接受支付寶服務器發送的支付成功的通知,并將相應訂單標記為已經支付。
-
流程圖如下:
4. 關聯庫
-
1 啟動IDE(如Xcode),把iOS包中的壓縮文件中以下文件拷貝到項目文件夾下,并導入到項目工程中。
(1)AlipaySDK.bundle
(2)AlipaySDK.framework
(3)在Build Phases選項卡的Link Binary With Libraries中,增加以下依賴:
2 新建一個Bridging-Header.h文件,并把該文件的路徑添加到 Build Settings => Objective-C Bridging Header。然后在該文件中添加下面兩行.
<pre><code>
import < AlipaySDK/AlipaySDK.h>
import "RSADataSigner.h"
</code></pre>
5. code
- 1 配置請求信息
<pre><code>
let aliOrder = AlipayOrder(partner: AlipayPartner, seller: AlipaySeller, tradeNO: order.id, productName: order.title, productDescription: order.content, amount: order.price, notifyURL: AlipayNotifyURL, service: "mobile.securitypay.pay", paymentType: "1", inputCharset: "utf-8", itBPay: "30m", showUrl: "m.alipay.com", rsaDate: nil, appID: nil)
let orderSpec = aliOrder.description //orderA.description
let signer = RSADataSigner(privateKey: AlipayPrivateKey)
let signedString = signer.signString(orderSpec)
let orderString = "\(orderSpec)&sign=\"\(signedString)\"&sign_type=\"RSA\""
print(orderString)
AlipaySDK.defaultService().payOrder(orderString, fromScheme: AppScheme, callback: {[weak self] resultDic in
if let strongSelf = self {
print("Alipay result = \(resultDic as Dictionary)")
let resultDic = resultDic as Dictionary
if let resultStatus = resultDic["resultStatus"] as? String {
if resultStatus == "9000" {
strongSelf.delegate?.paymentSuccess(paymentType: .Alipay)
let msg = "支付成功!"
let alert = UIAlertView(title: nil, message: msg, delegate: nil, cancelButtonTitle: "好的")
alert.show()
//strongSelf.navigationController?.popViewControllerAnimated(true)
} else {
strongSelf.delegate?.paymentFail(paymentType: .Alipay)
let alert = UIAlertView(title: nil, message: "支付失敗,請您重新支付!", delegate: nil, cancelButtonTitle: "好的")
alert.show()
}
}
}
})
</code></pre>
- 2 配置支付寶客戶端返回url處理方法
<pre><code>
//跳轉支付寶錢包進行支付,處理支付結果
AlipaySDK.defaultService().processOrderWithPaymentResult(url, standbyCallback: { (resultDict:[NSObject : AnyObject]!) -> Void in
print("openURL result: (resultDict)")
})
</code></pre>
通知服務器支付成功
如果支付成功后,支付寶服務器會向你的服務器 notify URL 發起支付成功的請求。這樣就可以在服務端實現一些相關的業務邏輯,比如標記訂單為已支付,發短信給用戶等.
參考 :