1、申请开放平台账号,并添加应用
支付宝应用添加.png2、开通支付功能
开通支付功能.png3、配置支付宝的公钥
配置支付宝公钥.png4、开始APP集成
1)导入需要的依赖包
iOS依赖包.png2)创建桥接文件导入支付宝的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
8BB46E76-4F65-4E09-B3B9-4C0AFF54752F.png此处的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
支付宝集成差不多就这样啦!!!
备注: 文中OrderPayController 为自定义的支付页面,
支付宝版本为2017-06-10