- appid 公众号id
- mch_id 商户号 公众号申请微信支付成功后 会有个商户号
- key 密钥 申请成功后会收到邮件 会带着密钥 用于签名
- 配置支付目录(开发,生产环境)
- 配置支付成功回调地址
- 获取prepay_id 首先根据统一下单接口获取 https://api.mch.weixin.qq.com/pay/unifiedorder 注意: 1、区分大小写 2、排序
let item = {} Object.keys(json).sort().map(function(key) { item[key] = json[key]; });
3、拼接 (记得加上 key) 放在最后就行
querystring.stringify(json)
4、签名 md5 记得转换大写
let t = crypto.createHash("md5"); t.update(url, "utf8", "hex"); t.digest("hex").toUpperCase();
5、增加sign字段
6、请求的时候 记得转换xml
- h5发起支付
签名注意 1、时间戳记得转换成字符串 ios会提示有问题 2、记得加上key
const json = { appid: '****', mch_id: '****', nonce_str:Math.random().toString(36).substr(2), out_trade_no:process.argv[2], //支付的订单id out_refund_no:"HT*******", //退款id需要自己生成 total_fee:"1", refund_fee:"1" } let _post = util.sign(json); _post = js2xmlparser.parse("xml", _post);
注意 退款是需要证书的: https://api.mch.weixin.qq.com/secapi/pay/refund
const agent = new http.Agent({ pfx: fs.readFileSync(__dirname + "/../../cert/apiclient_cert.p12"), passphrase: json.mch_id }); let _options = { protocol: protocol, hostname: host, port: port, path: url, method: method, agent:agent, headers: { } }; let _req = http.request(_options, (res)=>{});