2020年最新微信支付接入nodejs

2025-10-22 16:27:52

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

2020年最新微信支付接入nodejs

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

2020年最新微信支付接入nodejs

2020年最新微信支付接入nodejs

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

4、const crypto = require('crypto')


const axios = require('axios')
const xml2js = require('xml2js')
// 微信支付接口的数据都是xml, 为了方便, 需要将 xml 转换成 json
const 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'
// 微信公众号的 appid
const 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。
猜你喜欢