Swift3.0接入微信支付:
一、去官网下载SDK;
二、配置URL TYPE
三、AppDelegate的一些配置
1、遵循WXApiDelegate的协议;
2、配置Key:WXApi.registerApp("***");
3、微信回掉:
a、// iOS9.0以前调用此方法
fun capplication(_application:UIApplication, handleOpen url:URL) ->Bool{
if url.host=="pay"{
returnWXApi.handleOpen(url, delegate:self)
}
return true
}
// iOS9.0及9.0以后调用此方法
func application(_application:UIApplication, open url:URL, sourceApplication:String?, annotation:Any) ->Bool{
// 在此方法中做如下判断,因为还有可能有其他的支付,如支付宝就是@"safepay"
ifurl.host=="pay"{
returnWXApi.handleOpen(url, delegate:self)
}
return true
}
b、微信代理回掉
//微信支付回调用
funconResp(_resp:BaseResp){
let response = respas!PayResp
switch response.errCode {
case0:
//支付完成
case-1:
//支付失败
case-2:
//您已放弃支付
default:
//支付失败
}
}
四、WeChatPayManger的类
我为微信支付自己封装了一个类,统一管理支付这一块逻辑
a、单利(安全):static let shared = WeChatPayManger.init()
b、写下如下方法、收到后台返回的参数,直接调用即可:func WXPayWithAppid(appid:String,noncestr:String,package:String,partnerid:String,prepayid:String,timestamp:String,sign:String){
let req = PayReq()
//由用户微信号和AppID组成的唯一标识,用于校验微信用户
req.openID = appid
// 商家id,在注册的时候给的
req.partnerId = partnerid
// 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
req.prepayId = prepayid
// 根据财付通文档填写的数据和签名
req.package = package
// 随机编码,为了防止重复的,在后台生成
req.nonceStr = noncestr
// 这个是时间戳,也是在后台生成的,为了验证支付的
req.timeStamp =UInt32(timestamp)!
print(UInt32(timestamp)!)
// 这个签名也是后台做的
req.sign = sign
//发送请求到微信,等待微信返回onResp
WXApi .send(req)
}