一 注册微信支付appkey
二 sdk集成
//如果项目中有 友盟分享到微信 不需要在集成微信支付sdk否则报错
$ cd/你的项目地址
$ open -e Podfile
target 'zanqian' do
pod 'WeChatSDK-JDBR', '~> 0.0.1'
end
$ pod install
三 相关配置
如果项目没有集成友盟分享到微信 则需要配置URL 和 info.plist的白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<string>sinaweibo</string>
<string>sinaweibohd</string>
<string>weibosdk2.5</string>
<string>weibosdk</string>
<string>sinaweibosso</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqzone</string>
<string>sinaweibo</string>
<string>alipayauth</string>
<string>alipay</string>
<string>safepay</string>
<string>mqq</string>
<string>mqqapi</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV2</string>
<string>mqqapiwallet</string>
<string>mqqwpa</string>
<string>mqqbrowser</string>
<string>wtloginmqq2</string>
<string>weixin</string>
<string>wechat</string>
</array>
四 具体流程
1.下单
App调用后台统一下单 --->商户后台 --->微信后台返回订单 --->商户后台返回订单 --->商户App
2.支付
商户App发送sendReq方法--->调起微信发起支付请求--->微信后台授权支付--->微信进行支付
3.支付完成之后
微信后台异步通知--->商户后台
微信App发送支付状态信息--->商户App去后台查询是否真正支付成功--->商户后台(如果此时后台未收到微信后台异步通知商户后台则会调用查询API--->微信后台返回支付结果--->商户后台)否则,直接根据微信后台异步通知商户支付成功与否给--->商户App确认
五 代码示例
1、基本设置
//在appdelegate注册注册APPID 并遵守WXApiDelegate
WXApi.registerApp("wxdc1f628e3a90a9c8")
//MARK: 代理回调方法
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
let str = "\(url)"
//此种方法判断 不会产生 分享无法实现回调 的问题
if str.contains("pay"){
return WXApi.handleOpen(url, delegate: self)
} else {
//其他
return UMSocialManager.default().handleOpen(url)
}
}
// MARK: 微信支付回调
func onResp(_ resp: BaseResp!) {
if let payResp = resp as? PayResp {
switch payResp.errCode {
case WXSuccess.rawValue:
NotificationCenter.default.post(Notification(name: NotificationName.didWXPaySucceeded))
print("支付成功")
default:
NotificationCenter.default.post(Notification(name: NotificationName.didWXPayFailed))
print("支付失败")
}
}
}
2、应用
//在支付界面
class func startRewardPayRequest(wish: Wish, money: Float, note: String?, completion: @escaping (Bool, String?, String?) -> Void) {
guard let _ = User.current() else {
completion(false, nil, nil)
return
}
if !WXApi.isWXAppInstalled(){
MBProgressHUD.showForInfo(to: self.view, text: "没有安装微信")
return
}
Utils.getWxPrepayId(money: money, desc: note ?? "", orderType: 3, productId: String(wish.id)) { (succeeded, wxPrepayInfo) in
guard succeeded && wxPrepayInfo != nil else {
completion(false, nil, nil)
return
}
//调用后台获取相应参数并生成预订单
let request = PayReq()
request.partnerId = wxPrepayInfo!.partnerId
request.prepayId = wxPrepayInfo!.prepayId
request.package = "Sign=WXPay"
request.nonceStr = wxPrepayInfo!.nonceStr
request.timeStamp = UInt32(NSDate().timeIntervalSince1970)
request.sign = Utils.generateSignForWXPay(paramters: ["appid": "wxdc1f628e3a90a9c8",
"partnerid": request.partnerId,
"prepayid": request.prepayId,
"package": request.package,
"noncestr": request.nonceStr,
"timestamp": request.timeStamp])
let result = WXApi.send(request)
completion(result, wxPrepayInfo!.appId, wxPrepayInfo!.outtradeno)
}
}
*//接收支付结果的通知 调用后台接口判断是否支付成功 在继续处理
3、签名生成方法
class func generateSignForWXPay(paramters: [String: Any]) -> String {
var _paramters = [String: Any]()
for (key, value) in paramters {
if let str = value as? String, str == "" {
continue
}
_paramters[key] = value
}
let sortedKeys = _paramters.keys.sorted()
var temp = sortedKeys.map({ "\($0)=\(_paramters[$0]!)" }).joined(separator: "&")
temp += "&key=64255ed288e44b7298f49a1239103d15"
return temp.md5().uppercased()
}
如有不妥,请多多指教:)