Cocoapods 引入
pod 'MZPay', '~> 0.0.2'
使用前提
前往微信开放平台和支付宝开放平台注册应用或开通支付功能
一、项目配置
- InfoPlist添加微信白名单
LSApplicationQueriesSchemes
weixin
weixinULAPI
-
添加URL Schemes
第一个为微信相关URL Schemes(identifier填写weixin、URL Schemes填写微信开放平台注册应用获取的AppID)
第二个为支付宝相关URL Schemes,用于应用跳转(URL Schemes填入唯一的标识字符串,与其他应用不一样即可)
二、注册微信SDK
MZPay.registerWechat(appid: "", universalLink: "")
三、在AppDelegate中调用如下方法
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
let result = MZPay.handleOpenURL(url)
if result {
return result
}
return false
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let result = MZPay.handleOpenUniversalLink(userActivity)
if result {
return result
}
return false
}
四、调用支付接口
- 微信支付
MZPay.payWechat(mchid: "", prepayId: "", appid: "", appKey: "") {
NSLog("支付成功")
} failure: { reason in
NSLog("支付失败")
}
- 支付宝支付
MZPay.payAli(payURL: "", appScheme: "") {
NSLog("支付成功")
} failure: { reason in
NSLog("支付失败")
}