前言
微信支付的iOS的Demo不言而喻,所有的参数都是后台生成传过来的,完全没参考价值,并且有的注意点文档上也没说,现在我就说一下微信支付开发中需要注意的地方
项目配置
把实例项目中的一下文件拖到项目中
Control文件夹下的WXApiManager.h和WXApiManager.mlibWeChatSDK.aWXApi.hWXApiObject.h
桥接文件中添加引用
//微信支付#import "WXApi.h"#import "WXApiObject.h"#import "WXApiManager.h"
项目配置选项卡Info
中URL Types
中的添加一项
identifier 设置为 weixinURL Schemes 设置为你app微信开放平台上的appid
在AppDelegate中注册
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {//微信支付WXApi.registerApp("你的APPID ");}func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {return WXApi.handleOpenURL(url, delegate: WXApiManager.sharedManager());//return UMSocialSnsService.handleOpenURL(url);}//这里演示多个共存的处理方法,其中中间是和微信有关的func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {//支付宝支付if(sourceApplication!.containsString("com.alipay.iphoneclient")){AlipaySDK.defaultService().processOrderWithPaymentResult(url, standbyCallback: nil)return true;}//微信支付else if(sourceApplication!.containsString("com.tencent.xin")){return WXApi.handleOpenURL(url, delegate: WXApiManager.sharedManager());}//友盟分享else{return UMSocialSnsService.handleOpenURL(url);}}
需要的三个参数
appid (微信开放平台中获取)partnerId (商户ID,在商户平台获取)API_KEY (API密钥,在商户平台设置)
iOS开发交流技术群:[563513413](正在跳转),不管你是大牛还是小白都欢迎入驻 ,分享BAT,阿里面试题、面试经验,讨论技术, 大家一起交流学习成长!
支付步骤
支付总体上分为两步
获取prepayid(预付款ID)根据prepayid发起支付
第一步获取prepayid
这一步比较变态,它不像平常我们请求接口那样传參,而是把参数和值又拼成XML,再写入request中,实现起来较麻烦,官方也是在后台去实现的,所以建议第一步在后台实现,app端直接掉后台提供的接口
第二步发起请求 这一步除下签名相对麻烦点其他都很简单
代码
let req = PayReq();req.partnerId = LoadData.WX_PARTNERID;//商户IDreq.prepayId = prepayId;//预支付IDreq.nonceStr = nonceStr;//和第一步一样的随机数req.timeStamp = UInt32(NSDate(timeIntervalSinceNow: 0).timeIntervalSince1970);//时间戳req.package = "Sign=WXPay";//固定值var dic:[String:String] = [:];dic["appid"] = LoadData.WX_APPID;//APPIDdic["partnerid"] = LoadData.WX_PARTNERID;//商户IDdic["prepayid"] = prepayId;dic["package"] = "Sign=WXPay";dic["noncestr"] = req.nonceStr;dic["timestamp"] = "(req.timeStamp)";//下面参数中的key是 API密钥let sign = ZJ_WXPayUtils.getSign(dic, key: LoadData.WX_APIKEY);//获取签名req.sign = sign;WXApi.sendReq(req);
下面提供一下我用的工具类
import Foundationimport CryptoSwiftclass ZJ_WXPayUtils{static var xiadanUrl = "https://api.mch./pay/unifiedorder";static var appid = "";//公众账号IDstatic var mch_id = "";//商户号static var nonce_str = "";//随机字符串static var sign = "";//签名static var body = "";//商品描述static var out_trade_no = "";//商户订单号static var total_fee:Int = 0;//总金额 单位为分static var spbill_create_ip = "";//终端IPstatic var notify_url = "";//通知地址static var trade_type = "JSAPI";//交易类型///获取随机数 包括from 包括tostatic func getRandomNum(from:Int,to:Int) -> Int{let result = Int(from + (Int(arc4random()) % (to - from + 1)));return result;}static func getSign(dic:Dictionary<String,String>,key:String) -> String{var sign = "";let dicNew = dic.sort { (a, b) -> Bool inreturn a.0 < b.0;}sign = getQueryStrByDic(dicNew);sign += "&key=(key)";sign = sign.md5().uppercaseString;return sign;}static func getQueryStrByDic(dic:[(String,String)])->String{var pars = "";for (index, element) in dic.enumerate() {if(index == 0){pars += "(element.0)=(element.1)";}else{pars += "&(element.0)=(element.1)";}}return pars;}}
工具类中用到的第三方库
pod 'CryptoSwift'