前提
在微信商户平台成为商家,按照官网的文档进行配置,弄好下载证书,将证书复制到后端的项目上
实现过程
GitHub - yangfuhe/node-wxpay: 微信支付APIv3 for nodejs
安装 wxpay-v3
npm i wxpay-v3 --save
# 如已安装旧版, 重新安装最新版
npm i wxpay-v3@latest
创建?WxPayUtils.js
const RSA = require('./wx_rsa');
class WxPayUtils {
constructor(privateKey,appId) {
this.privateKey =privateKey;
this.appId = appId;
}
signLong (data) {
// let privateKey =private_key
let sign_rsa = new RSA.RSAKey();
sign_rsa = RSA.KEYUTIL.getKey(this.privateKey);
let hashAlg = 'sha256';//sha256//此处换成腾讯要求的方式
let Sig = sign_rsa.signString(data, hashAlg);
Sig = RSA.hex2b64(Sig); // hex 转 b64
return Sig;
}
paysign(options){//发起支付签的字段准备
let timeStamp=this.createTimeStamp(),//时间戳
nonceStr=this.randomString(),//32位随机数
Ppackage= `prepay_id=${options}`,//prepay_id
signType= 'RSA';//加签方式
//appId、timeStamp、nonceStr、package
let PpaySign=`${this.appId}\n${timeStamp}\n${nonceStr}\n${Ppackage}\n`;//需要加签的字段拼接
let cryptStr=this.signLong(PpaySign);//生成签名
let paySign=cryptStr;
return {timeStamp,nonceStr,Ppackage,signType,paySign};
}
createTimeStamp(){
return parseInt(new Date().getTime() / 1000) + ''
}
randomString(){
const chars='ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; //默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
const len=32;
const maxPos=chars.length;
let pwd='';
for(let i=0;i<len;i++){
pwd+=chars.charAt(Math.floor(Math.random()*maxPos));
}
return pwd;
}
}
module.exports = WxPayUtils;
?创建wx_rsa.js
GitHub - UFO0001/WX_RSA 到github上下载 在utils包
或者去这里复制?
file/wx_rsa.js · huyande/blogimage - Gitee.com
const Payment = require('wxpay-v3');
const path = require("path");
const Ret = require('../common/ret');
const WxPayUtils = require('../common/WxPayUtils');
const private_key = require('fs').readFileSync(path.resolve(__dirname, '../wxpay_20220512_cert/apiclient_key.pem')).toString()
const appid = '微信小程序的appid';
const paymnet = new Payment({
appid: appid,
mchid: '商户号',
private_key: private_key,//或者直接复制证书文件内容
serial_no:'证书编号,在商户后台申请证书后会有编号',
apiv3_private_key:'api v3 接口设置的秘钥',
notify_url: '支付回调地址',
});
const wxPay = new WxPayUtils(private_key,appid);
/**
* amount 单位是分
* @param req
* @param res
* @param next
* @returns {Promise<*>}
*/
exports.createWxPay = async function (req,res,next){
let openid = req.query.openid;
let amount = req.query.amount;
let current_user = req.session.wxuser;
let result = await paymnet.jsapi({
description:'打赏支付',
out_trade_no:Date.now().toString(),
amount:{
total:amount
},
payer:{
openid:openid
},
}).catch(e=>{
return new Ret({ code: -1,data:e}).send(res);
});
let prepay_id = JSON.parse(result.data).prepay_id;
let sign = wxPay.paysign(prepay_id);
return new Ret({ code: 0,data:sign}).send(res);
};
/**
* 支付回调方法
* @param req
* @param res
* @param next
* @returns {*}
*/
exports.callWxPay = function (req,res,next){
return new Ret({ code: 0}).send(res);
};
小程序端
testPay(){
wxpay({openid:'xxx',amount:1}).then(res=>{
let options = res.data
wx.requestPayment
(
{
package:options.Ppackage,...options,
"success":function(res){
console.log(res)
},
"fail":function(res){
console.log(res)
}
}
)
})
}
即可实现
|