2020年最新微信支付接入nodejs

2025-04-19 22:10:26

1、申请公众摒蛲照燔号https://mp.weixin.qq.com/cgi-bin/registermidpage?action=index&lang=zh_CN&to娱浣嫁装ken=,一般申请服务号即可。也可以根据自己的实际情况申请。请后等待审核通过。

2020年最新微信支付接入nodejs

2、申请开通微信商户,开通微信支付。并将微信商户与公众号关联。

2020年最新微信支付接入nodejs
2020年最新微信支付接入nodejs

3、详细代码实现如下,直接调用pcPaySign方法传入订单号和ip即可。将返回结果传给前端,生成二维码用户扫二维码完成支付。,支付二维码页面需要开一个定时器几秒查询一次订单的支付状态。用户支付成功跳转成功页面。

4、const crypto = require('crypto')const axios = require('ax足毂忍珩ios')const xml2js = require('xml2js')// 微信支付接口的数据都是xml, 为了方便, 需要将 xml 转换成 jsonconst xmlParser = new xml2js.Parser()// md5 加密算法const md5 = str => { let m = crypto.createHash('md5') return m.update(str).digest('hex')}// 一些需要用的变量// 接收支付结果通知的地址const payNotifyUrl = 'http://www.xxxx.com/payNotify'// 微信公众号的 appidconst appId = 'xxxxx'// 商户号const mchId = 'yyyyy'// 支付密钥const PAY_API_KEY = 'xxxxxxxfxaxdx2xx2xxxxxxxxxxxxxxx' //API密钥// 接下来准备一些方法// 生成一个随机字符串const getNonceStr = () => { let text = "" const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" for (let i = 0; i < 16; i++) { text += possible.charAt(Math.floor(Math.random() * possible.length)) } return text}// 生成预支付签名, 将会发到微信支付接口const getPcPrePaySignForWX = (appId, attach, productIntro, mchId, nonceStr, notifyUrl, openId, tradeId, ip, price, PAY_API_KEY) => { let stringA = 'appid=' + appId + '&attach=' + attach + '&body=' + productIntro + '&mch_id=' + mchId + '&nonce_str=' + nonceStr + '&notify_url=' + payNotifyUrl + '&out_trade_no=' + tradeId + '&spbill_create_ip=' + ip + '&total_fee=' + price + '&trade_type=NATIVE' let stringSignTemp = stringA + '&key=' + PAY_API_KEY return md5(stringSignTemp).toUpperCase()}// 按照要求的格式打包将要发送给微信的数据 下单const wxPcSendData = (appId, attach, productIntro, mchId, nonceStr,notifyUrl, openId, tradeId, ip, price, sign) => { // attach 将会在通知支付结果时返回 const sendData = '<xml>' + '<appid>' + appId + '</appid>' + '<attach>' + attach + '</attach>' + '<body>' + productIntro + '</body>' + '<mch_id>' + mchId + '</mch_id>' + '<nonce_str>' + nonceStr + '</nonce_str>' + '<notify_url>' + notifyUrl + '</notify_url>' + '<out_trade_no>' + tradeId + '</out_trade_no>' + '<spbill_create_ip>' + ip + '</spbill_create_ip>' + '<total_fee>' + price + '</total_fee>' + '<trade_type>NATIVE</trade_type>' + '<sign>' + sign + '</sign>' + '</xml>' return sendData}/** * * @param tradeId =》商家订单号 我们自己生成 * @param ip =》服务器ip * @returns {Promise<unknown>} */const pcPaySign = (tradeId, ip) =>{ return new Promise(async (resolve,reject)=>{ try { const nonceStr = getNonceStr() // 交易的费用, 单位是分 let price = 1 // 交易的描述, 将会出现在用户的微信支付详情中 let productIntro = '腾讯充值中心-QQ会员充值' //腾讯充值中心-QQ会员充值 let openId = '' let attach = '' const prePaySign = getPcPrePaySignForWX(appId, attach, productIntro, mchId, nonceStr, payNotifyUrl, openId, tradeId, ip, price, PAY_API_KEY) const data = wxPcSendData(appId, attach, productIntro, mchId, nonceStr,payNotifyUrl, openId, tradeId, ip, price, prePaySign) let wxResponse //下面内容 wxResponse = await axios.post('https://api.mch.weixin.qq.com/pay/unifiedorder', data) xmlParser.parseString(wxResponse.data, (err, success) => { if (err) { console.log(err) reject({ code:2001, msg:'生成支付二维码失败', data:err }) // } else { if (success.xml.return_code[0] === 'SUCCESS') { const prepayId = success.xml.prepay_id[0] // codeUrl => 这里拿到的这个地址, 将它返回给前端同学即可 const codeUrl = success.xml.code_url[0] resolve({ prepayId, codeUrl, orderNumber:tradeId }) }else{ reject({ code:success.xml.return_code[0], msg:success.xml.return_msg[0] }) } } }) }catch (e) { console.log(e) reject({ msg:e }) } })}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢