node 微信支付 V3版本签名生成

const at = require('auto-bind')
const { KJUR, hextob64 } = require('jsrsasign')
const axios = require('axios')

class Post {
  constructor() {
    at(this)
  }

  // 上传发票文件到微信
  async uploadInvoice(req, res, next) {
    try {
      const method = 'POST'
      const pathname = '/v3/pay/transactions/native'
      const timestamp = Math.round(new Date().getTime() / 1000);  // 十位的时间戳
      const onece_str = await this.randomString()  // 随机字符串
      const bodyParamsStr = {
        'mchid': '商户号',
        'out_trade_no': '证书序列号',
        'appid': '应用id',
        'description': 'Image形象店-深圳腾大-QQ公仔',
        'notify_url': '支付成功的回调地址',
        'support_fapiao': true,	// 是否开发票
        'amount': {
          'total': 1,	// 总金额,单位分
          'currency': 'CNY'	// 币种
        }
      }
      const mchid = 'xxxxxx'  // 商户号
      const serial_no = 'xxxxxxxxxx'  // 商户API证书序列号
      const private_key = `-----BEGIN PRIVATE KEY-----
      
      -----END PRIVATE KEY-----
      `	// appiclient_key.pem里的内容全部复制过来
      const signature = await this.rsaSign(`${method}\n${pathname}\n${timestamp}\n${onece_str}\n${JSON.stringify(bodyParamsStr)}\n`, private_key, 'SHA256withRSA')
      //获取到signature后就可以获取到Authorization了
      let Authorization = `WECHATPAY2-SHA256-RSA2048 mchid="${mchid}",nonce_str="${onece_str}",timestamp="${timestamp}",signature="${signature}",serial_no="${serial_no}"`
      const data = await axios.post('https://api.mch.weixin.qq.com/v3/pay/transactions/native', bodyParamsStr, {
        headers: {
          Authorization
        }
      })
      console.log(data);
      res.json(data.data)
    } catch (error) {
      console.log(error.response.data);
      return next(error)
    }
  }

  // 加密
  async rsaSign(content, privateKey, hash = 'SHA256withRSA') {
    // 创建 Signature 对象
    const signature = new KJUR.crypto.Signature({
      alg: hash,
      //!这里指定 私钥 pem!
      prvkeypem: privateKey
    })
    signature.updateString(content)
    const signData = signature.sign()
    // 将内容转成base64
    return hextob64(signData)
  }

  // 生成随机数
  async randomString(e = 32) {
    const t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    const a = t.length
    let n = ''
    for (let index = 0; index < e; index++) {
      n += t.charAt(Math.floor(Math.random() * a))
    }
    return n
  }
}

module.exports = new Post()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成微信支付二维码可以通过以下步骤: 1. 调用微信支付接口生成预支付订单,获取到预支付订单号。可以通过微信支付官方提供的API实现这一步骤。 2. 将预支付订单号作为参数,调用微信支付接口生成支付二维码,获取到支付二维码的url。可以使用微信支付官方提供的“统一下单接口”来实现这一步骤。 3. 在后端将支付二维码的url返回给前端前端展示二维码供用户扫描。 以下是生成微信支付二维码的代码示例(使用Node.js): ```javascript const request = require('request'); const crypto = require('crypto'); const querystring = require('querystring'); // 微信支付API密钥,需要在微信商户平台设置 const key = 'your_wechat_pay_api_key'; // 微信支付统一下单接口URL const unifiedOrderUrl = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; function generateUnifiedOrder(params) { // 按照微信支付要求对参数进行签名 const signParams = Object.assign({}, params, {key}); const signString = querystring.stringify(signParams); const hash = crypto.createHash('md5'); const sign = hash.update(signString, 'utf8').digest('hex').toUpperCase(); // 组装请求参数 const requestData = Object.assign({}, params, {sign}); // 发送请求 request({ url: unifiedOrderUrl, method: 'POST', body: requestData, json: true }, function(err, response, body) { if (err) { console.error(err); return; } // 解析响应数据 const { return_code, result_code, code_url } = body; if (return_code === 'SUCCESS' && result_code === 'SUCCESS') { // 支付二维码URL const qrCodeUrl = code_url; console.log(qrCodeUrl); } else { console.error('生成支付二维码失败'); } }); } // 调用生成支付二维码函数,传入必要的参数 generateUnifiedOrder({ appid: 'your_wechat_appid', mch_id: 'your_wechat_mch_id', nonce_str: Math.random().toString(36).substr(2, 15), body: '商品描述', out_trade_no: '订单号', total_fee: 1, // 单位为分 spbill_create_ip: '用户IP地址', notify_url: '支付结果通知地址', trade_type: 'NATIVE' // 生成扫码支付二维码 }); ``` 以上代码演示了如何调用微信支付API生成支付二维码。需要注意的是,这里只是演示了生成支付二维码的过程,具体实现还需要根据实际情况进行调整和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值