1蟹倾、申請開放平臺賬號广凸,并添加應(yīng)用
https://docs.open.alipay.com/200/105310#s2
2拌屏、開通支付功能
3罚攀、配置支付寶的公鑰
注:配置支付寶公鑰后党觅,服務(wù)端需提供接口給APP端簽名
官網(wǎng)簽名demo:https://docs.open.alipay.com/54/106370/
4、開始APP集成
1)導(dǎo)入需要的依賴包
a. 從地址:https://docs.open.alipay.com/54/104509 下載官網(wǎng)Demo斋泄,將項(xiàng)目下
AlipaySDK.bundle
AlipaySDK.framework
兩個(gè)包導(dǎo)入工程
b. 在Build Phases的Link Binary With Libraries中杯瞻,添加iOS依賴包
2)創(chuàng)建橋接文件導(dǎo)入支付寶的SDK
#import <AlipaySDK/AlipaySDK.h>
3)創(chuàng)建支付結(jié)果處理類AliSdkManager
PaymentType.ALIPAY 支付方式
PaymentResult 自定義支付結(jié)果
import UIKit
public class AliSdkManager: NSObject {
public static var aliSdkManager:AliSdkManager!
internal var orderPayController:OrderPayController!
public static func sharedManager () -> AliSdkManager{
AliSdkManager.aliSdkManager = AliSdkManager.init()
return AliSdkManager.aliSdkManager
}
internal func showResult(result:NSDictionary){
// 9000 訂單支付成功
// 8000 正在處理中
// 4000 訂單支付失敗
// 6001 用戶中途取消
// 6002 網(wǎng)絡(luò)連接出錯(cuò)
let returnCode:String = result["resultStatus"] as! String
var returnMsg:String = result["memo"] as! String
var subResultMsg:String = ""
switch returnCode{
case "6001":
break
case "8000":
orderPayController.paySuccess(PaymentType.ALIPAY, payResult: PaymentResult.PROCESS)
break
case "4000":
orderPayController.paySuccess(PaymentType.ALIPAY, payResult: PaymentResult.FAIL)
break
case "9000":
returnMsg = "支付成功"
//支付返回信息:外系統(tǒng)訂單號、內(nèi)部系統(tǒng)訂單號等信息
JSON.init(parseJSON: (result["result"] as! String))["alipay_trade_app_pay_response"]["sub_msg"].stringValue
orderPayController.paySuccess(PaymentType.ALIPAY, payResult: PaymentResult.SUCCESS)
break
default:
break
}
}
}
4)創(chuàng)建支付工具類AliPayUtils炫掐,用于調(diào)起支付
import UIKit
public class AliPayUtils: NSObject {
var context:UIViewController;
public init(context:UIViewController) {
self.context = context;
}
public func pay(sign:String){
let decodedData = sign.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
let decodedString:String = (NSString(data: decodedData, encoding: String.Encoding.utf8.rawValue))! as String
AlipaySDK.defaultService().payOrder(decodedString, fromScheme: "com.xmars.porsche.m2m", callback: { (resp) in
print(resp)
} )
}
}
5)在AppDelegate.swift的方法:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
添加代碼:
//初始化支付管理類
AliSdkManager.sharedManager()
6)在AppDelegate.swift文件中重寫方法
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if url.host == "safepay" {
AlipaySDK.defaultService().processOrder(withPaymentResult: url as URL!, standbyCallback: {
(resultDic) -> Void in
//調(diào)起支付結(jié)果處理
AliSdkManager.aliSdkManager.showResult(result: resultDic! as NSDictionary);
})
}
return true;
}
7)點(diǎn)擊工程文件中Info
此處的Url Schemes 需要和AliPayUtils中支付方法的fromScheme一樣的魁莉,否則可能回調(diào)失敗
AlipaySDK.defaultService().payOrder(decodedString, fromScheme: "com.xmars.porsche.m2m", callback: { (resp) in
print(resp)
} )
8)頁面調(diào)起支付
注:此處所有數(shù)據(jù)為測試數(shù)據(jù)
alipayUtils = AliPayUtils.init(context: self);
AliSdkManager.aliSdkManager.orderPayController = self
alipayUtils.pay(sign: "app_id=20177383904816397&method=alipay.trade.app.pay&charset=utf-8&sign_type=RSA2×tamp=2017-06-30 14:59:58&version=1.0&format=json¬ify_url=http://ceshidizhi.com:8080/store-manage/payment/callback/notifyAlipayApp.ihtml&biz_content={\"total_amount\":\"0.01\",\"timeout_express\":\"30m\",\"product_code\":\"QUICK_MSECURITY_PAY\",\"subject\":\"測試商品1\",\"out_trade_no\":\"10120170630123671234537281\"}&sign=df15Q+AQ3lZLlF95wNdXKRpc35nmpDeEL8XrlIlTbfTQE39Z+RTRrOlSxuBtAqZpCzZy0ulxlkI64Y85PFiU9E6x+vnaimFTMuVNUYqasfdsaNaH3+85dhTk5u0SxV5n/fOrklx+B1SzADl4218/lhjz4bNnMqE6lXiJAFdfasdsafdsafasfdsqZDFjmJxmnjI9nfH74lEP+yF4X68uRsdUFrXaEt2CciyL2RVNj5ZJcJ7njjwtAcgdcHblA6j6qHqFRnrvdxV5i5VGQn1SHwpvMEAdsadsa1mBl7kDlCpWh/M1chCNdB79e3B1O8KsX7Iaz8g2snBr/y/wHU7YHfsnuLw==}")
支付寶集成差不多就這樣啦!D嘉浮旗唁!
備注: 文中OrderPayController 為自定義的支付頁面,
支付寶版本為2017-06-10