#支付条件
1.微信公众号-申请支付 (https://mp.weixin.qq.com)
2.微信商户平台-申请支付(https://pay.weixin.qq.com)
#支付参数
var appid = ‘wxxxxxxxxxxxxxxxxxx‘; //微信公众号appid
var mchid = ‘159xxxxxxx‘ //微信商户平台账号mchid
var mchkey = ‘haixxxxxxxxxxxxxxxxxx‘; //微信商户平台秘钥
var wxurl = ‘http://xxxxxx.com‘;//支付回调地址
#封装微信转换参数方法
var xmlreader = require("xmlreader"); var wxpay = { //把金额转为分 getmoney: function (money) { return parseFloat(money) * 100; }, // 随机字符串产生函数 createNonceStr: function () { return Math.random().toString(36).substr(2, 15); }, // 时间戳产生函数 createTimeStamp: function () { return parseInt(new Date().getTime() / 1000) + ‘‘; }, //签名加密算法 paysignjsapi: function (appid, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, mchkey) { var ret = { appid: appid, mch_id: mch_id, nonce_str: nonce_str, body: body, notify_url: notify_url, openid: openid, out_trade_no: out_trade_no, spbill_create_ip: spbill_create_ip, total_fee: total_fee, trade_type: trade_type }; var string = raw(ret); var key = mchkey; string = string + ‘&key=‘ + key; var crypto = require(‘crypto‘); return crypto.createHash(‘md5‘).update(string, ‘utf8‘).digest(‘hex‘).toUpperCase(); }, // 小程序签名 paysignjsapimini: function (appId, nonceStr, package, signType, timestamp, mchkey) { var ret = { appId: appId, nonceStr: nonceStr, package: package, signType: signType, timeStamp: timestamp, }; var string = raw(ret); var key = mchkey; string = string + ‘&key=‘ + key; var crypto = require(‘crypto‘); return crypto.createHash(‘md5‘).update(string, ‘utf8‘).digest(‘hex‘).toUpperCase(); }, getXMLNodeValue: function (xml) { xmlreader.read(xml, function (errors, response) { if (null !== errors) { console.log(errors) return; } var prepay_id = response.xml.prepay_id.text(); return prepay_id; }); } } function raw(args) { var keys = Object.keys(args); keys = keys.sort() var newArgs = {}; keys.forEach(function (key) { newArgs[key] = args[key]; }); var string = ‘‘; for (var k in newArgs) { string += ‘&‘ + k + ‘=‘ + newArgs[k]; } string = string.substr(1); return string; } module.exports = wxpay;
#api接口方法,传入后台订单号和订单金额
static async _wxPay(ctx) { //首先拿到前端传过来的参数 let orderCode = ‘2020090202‘; let money = ‘0.01‘; let openid = ‘oc2a-4jkdE7zVV-RjamMWf-wErsQ‘; //首先生成签名sign // appid let mch_id = mchid; let nonce_str = wxpay.createNonceStr(); let timestamp = wxpay.createTimeStamp(); let body = ‘测试微信支付‘; let out_trade_no = orderCode; let total_fee = wxpay.getmoney(money); let spbill_create_ip = ‘116.199.41.58‘; // 支持IPV4和IPV6两种格式的IP地址。调用微信支付API的机器IP let notify_url = wxurl; let trade_type = ‘JSAPI‘; // ‘APP‘;公众号:‘JSAPI‘或‘NATIVE‘ let sign = wxpay.paysignjsapi(appid, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, mchkey); //组装xml数据 var formData = "<xml>"; formData += "<appid>" + appid + "</appid>"; //appid formData += "<body><![CDATA[" + "测试微信支付" + "]]></body>"; formData += "<mch_id>" + mch_id + "</mch_id>"; //商户号 formData += "<nonce_str>" + nonce_str + "</nonce_str>"; //随机字符串,不长于32位。 formData += "<notify_url>" + notify_url + "</notify_url>"; formData += "<openid>" + openid + "</openid>"; formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>"; formData += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"; formData += "<total_fee>" + total_fee + "</total_fee>"; formData += "<trade_type>" + trade_type + "</trade_type>"; formData += "<sign>" + sign + "</sign>"; formData += "</xml>"; var url = ‘https://api.mch.weixin.qq.com/pay/unifiedorder‘; let promise = new Promise(function(resolve, reject) { request({ url: url, method: ‘POST‘, body: formData }, function (err, response, body) { if (!err && response.statusCode == 200) { xmlreader.read(body.toString("utf-8"), function (errors, response) { if (null !== errors) { console.log(errors) return; } var prepay_id = response.xml.prepay_id.text(); //将预支付订单和其他信息一起签名后返回给前端 let package1 = "prepay_id=" + prepay_id; let signType = "MD5"; let minisign = wxpay.paysignjsapimini(appid, nonce_str, package1, signType, timestamp, mchkey); let json = JSON.stringify({ status: ‘200‘, data: { ‘appId‘: appid, ‘partnerId‘: mchid, ‘prepayId‘: prepay_id, ‘nonceStr‘: nonce_str, ‘timeStamp‘: timestamp, ‘package‘: ‘Sign=WXPay‘,‘paySign‘: minisign } }) resolve(json) }); } }); }); await promise.then((res)=>{ ctx.body = CommonSend.list(res) }) }