1、申请开放平台账号,并添加应用
https://docs.open.alipay.com/200/105310#s2
2、开通支付功能
3、配置支付宝的公钥
注:配置支付宝公钥后,服务端需提供接口给APP端签名
官网签名demo:https://docs.open.alipay.com/54/106370/
4、开始APP集成
1)导入需要的依赖包
a. 从地址:https://docs.open.alipay.com/54/104509 下载官网Demo,将项目下
AlipaySDK.bundle
AlipaySDK.framework
两个包导入工程
b. 在Build Phases的Link Binary With Libraries中,添加iOS依赖包
2)创建桥接文件导入支付宝的SDK
#import <AlipaySDK/AlipaySDK.h>
3)创建支付结果处理类AliSdkManager
PaymentType.ALIPAY 支付方式
PaymentResult 自定义支付结果
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 网络连接出错
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 = "支付成功"
//支付返回信息:外系统订单号、内部系统订单号等信息
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)创建支付工具类AliPayUtils,用于调起支付
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
//调起支付结果处理
AliSdkManager.aliSdkManager.showResult(result: resultDic! as NSDictionary);
})
}
return true;
}
7)点击工程文件中Info
此处的Url Schemes 需要和AliPayUtils中支付方法的fromScheme一样的,否则可能回调失败
AlipaySDK.defaultService().payOrder(decodedString, fromScheme: "com.xmars.porsche.m2m", callback: { (resp) in
print(resp)
} )
8)页面调起支付
注:此处所有数据为测试数据
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==}")
支付宝集成差不多就这样啦!!!
备注: 文中OrderPayController 为自定义的支付页面,
支付宝版本为2017-06-10