集成SDK(开放平台地址https://docs.open.alipay.com/204/105295/)
怎么集成SDK自行百度
1.第一步在程序入口注册微信 (支付宝不需要)
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//向微信终端程序注册第三方应用WXApi.registerApp(WX_APPID)
}
2.微信支付管理类客户端只负责调用(支付信息什么的都是服务器完成的)
import UIKit
//微信appid
let WX_APPID=""
//AppSecret
let SECRET=""
//微信
class WXApiManager:NSObject,WXApiDelegate {
static let shared = WXApiManager()
// 用于弹出警报视图,显示成功或失败的信息()
private weak var sender:BaseViewController! //(UIViewController)
// 支付成功的闭包
private var paySuccessClosure: (() -> Void)?
// 支付失败的闭包
private var payFailClosure: (() -> Void)?
//登录成功
private var loginSuccessClosure:((_ code:String) -> Void)?
//登录失败
private var loginFailClosure:(() -> Void)?
// 外部用这个方法调起微信支付
func payAlertController(_ sender:BaseViewController,
request:PayReq,
paySuccess: @escaping () -> Void,
payFail:@escaping () -> Void) {
// sender 是调用这个方法的控制器,
// 用于提示用户微信支付结果,可以根据自己需求是否要此参数。
self.sender = sender
self.paySuccessClosure = paySuccess
self.payFailClosure = payFail
if checkWXInstallAndSupport(){//检查用户是否安装微信
WXApi.send(request)
}
}
//外部用这个方法调起微信登录
func login(_ sender:BaseViewController,loginSuccess: @escaping ( _ code:String) -> Void,
loginFail:@escaping () -> Void){
// sender 是调用这个方法的控制器,
// 用于提示用户微信支付结果,可以根据自己需求是否要此参数。
self.sender = sender
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
if checkWXInstallAndSupport(){
let req=SendAuthReq()
req.scope="snsapi_userinfo"
req.state="app"
WXApi.send(req)
}
}
}
extension WXApiManager {
func onResp(_ resp: BaseResp!) {
if resp is PayResp {//支付
if resp.errCode == 0 {
self.paySuccessClosure?()
}else{
self.payFailClosure?()
}
}else if resp is SendAuthResp{//登录结果
let authResp = resp as! SendAuthResp
var strMsg: String
if authResp.errCode == 0{
strMsg="微信授权成功"
}else{
switch authResp.errCode{
case -4:
strMsg="您拒绝使用微信登录"
break
case -2:
strMsg="您取消了微信登录"
break
default:
strMsg="微信登录失败"
break
}
}
UIAlertController.showAlertYes(sender, title: "授权结果", message: strMsg, okButtonTitle:"确定", okHandler: { (alert) in
if authResp.errCode == 0 {
self.loginSuccessClosure?(authResp.code)
}else{
self.loginFailClosure?()
}
})
}
}
}
extension WXApiManager {
// 检查用户是否已经安装微信并且有支付功能
private func checkWXInstallAndSupport() -> Bool {
if !WXApi.isWXAppInstalled() {
///这里的弹窗是我写的扩展方法
UIAlertController.showAlertYes(sender,title:"", message: "微信未安装", okButtonTitle:"确定")
return false
}
if !WXApi.isWXAppSupport() {
///这里的弹窗是我写的扩展方法
UIAlertController.showAlertYes(sender,title:"", message: "当前微信版本不支持支付", okButtonTitle:"确定")
return false
}
return true
}
}
3. 支付宝管理类客户端只负责调用(支付信息什么的都是服务器完成的)
//支付宝
class AliPayManager:NSObject{
static let shared = AliPayManager()
// 用于弹出警报视图,显示成功或失败的信息
fileprivate weak var sender: UIViewController!
// 支付成功的闭包
fileprivate var paySuccessClosure: (() -> Void)?
// 支付失败的闭包
fileprivate var payFailClosure: (() -> Void)?
///登录成功的闭包
fileprivate var loginSuccessClosure:((_ auth_code:String) -> Void)?
///登录失败的闭包
fileprivate var loginFailClosure:(() -> Void)?
// 外部用这个方法调起支付支付
func payAlertController(_ sender: UIViewController,
request:String,
paySuccess: @escaping () -> Void,
payFail:@escaping () -> Void) {
// sender 是调用这个方法的控制器,
self.sender = sender
//用于提示用户支付宝支付结果,可以根据自己需求是否要此参数。
self.paySuccessClosure = paySuccess
self.payFailClosure = payFail
AlipaySDK.defaultService().payOrder(request, fromScheme:"这里是URL Types配置的URLShcemes",callback:nil)
}
//外部用这个方法调起支付宝登录
func login(_ sender:BaseViewController,withInfo:String,loginSuccess: @escaping (_ str:String) -> Void,loginFail:@escaping () -> Void){
// sender 是调用这个方法的控制器,
// 用于提示用户微信支付结果,可以根据自己需求是否要此参数。
self.sender = sender
self.loginSuccessClosure = loginSuccess
self.loginFailClosure = loginFail
AlipaySDK.defaultService().auth_V2(withInfo:withInfo, fromScheme:"这里是URL Types配置的URLSchemes", callback:nil)
}
///授权回调
func showAuth_V2Result(result:NSDictionary){
// 9000 请求处理成功
// 4000 系统异常
// 6001 用户中途取消
// 6002 网络连接出错
let returnCode:String = result["resultStatus"] as! String
var returnMsg:String = ""
switch returnCode{
case "6001":
returnMsg = "用户中途取消"
break
case "6002":
returnMsg = "网络连接出错"
break
case "4000":
returnMsg = "系统异常"
break
case "9000":
returnMsg = "授权成功"
break
default:
returnMsg = "系统异常"
break
}
UIAlertController.showAlertYes(sender, title: "授权结果", message: returnMsg, okButtonTitle:"确定", okHandler: { (alert) in
if returnCode == "9000" {
let r=result["result"] as! String
self.loginSuccessClosure?(r)
}else{
self.loginFailClosure?()
}
})
}
//传入回调参数
func showResult(result:NSDictionary){
// 9000 订单支付成功
// 8000 正在处理中
// 4000 订单支付失败
// 6001 用户中途取消
// 6002 网络连接出错
let returnCode:String = result["resultStatus"] as! String
// var returnMsg:String = ""
// switch returnCode{
// case "6001":
// returnMsg = "用户中途取消"
// break
// case "6002":
// returnMsg = "网络连接出错"
// break
// case "8000":
// returnMsg = "正在处理中"
// break
// case "4000":
// returnMsg = "订单支付失败"
// break
// case "9000":
// returnMsg = "支付成功"
// break
// default:
// returnMsg = "订单支付失败"
// break
// }
// UIAlertController.showAlertYes(sender, title: "支付结果", message: returnMsg, okButtonTitle:"确定", okHandler: { (alert) in
// if returnCode == "9000" {
// self.paySuccessClosure?()
//
// }else{
// self.payFailClosure?()
// }
// })
if returnCode == "9000" {
self.paySuccessClosure?()
}else{
self.payFailClosure?()
}
}
}
4.在AppDelegate中回调
// MARK: - 支付回调
extension AppDelegate{
// iOS 8 及以下请用这个
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
if url.host == "safepay"{//支付宝
//支付回调
AlipaySDK.defaultService().processOrder(withPaymentResult: url, standbyCallback:{ (resultDic) in
if resultDic != nil{
AliPayManager.shared.showResult(result:resultDic! as NSDictionary)
}
})
//授权回调
AlipaySDK.defaultService().processAuth_V2Result(url, standbyCallback: { (resultDic) in
if resultDic != nil{
AliPayManager.shared.showAuth_V2Result(result:resultDic! as NSDictionary)
}
})
return true
}else{//微信
return WXApi.handleOpen(url, delegate:WXApiManager.shared)
}
}
// iOS 9 以上请用这个
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
if url.host == "safepay"{//支付宝
//支付回调
AlipaySDK.defaultService().processOrder(withPaymentResult: url, standbyCallback:{ (resultDic) in
if resultDic != nil{
AliPayManager.shared.showResult(result:resultDic! as NSDictionary)
}
})
//授权回调
AlipaySDK.defaultService().processAuth_V2Result(url, standbyCallback: { (resultDic) in
if resultDic != nil{
AliPayManager.shared.showAuth_V2Result(result:resultDic! as NSDictionary)
}
})
return true
}else{//微信
return WXApi.handleOpen(url,delegate:WXApiManager.shared)
}
}
}
5.支付调用 这里拿数据是用SwiftyJSON
1.微信支付
///从服务器拿到生成好的请求参数
let charge=json["charge"]
let req=PayReq()
req.timeStamp=charge["timestamp"].uInt32Value
req.partnerId=charge["partnerid"].stringValue
req.package=charge["package"].stringValue
req.nonceStr=charge["noncestr"].stringValue
req.sign=charge["sign"].stringValue
req.prepayId=charge["prepayid"].stringValue
WXApiManager.shared.payAlertController(self, request: req, paySuccess: {
print("支付成功")
}, payFail: {
print("支付失败")
})
2.支付宝支付
///从服务器拿到生成好的请求参数
let orderString=json["charge"]["orderString"].stringValue
AliPayManager.shared.payAlertController(self, request:orderString, paySuccess: {
print("支付成功")
}, payFail: {
print("支付失败")
})
网友评论