簽名方式使用的是SHA256withRSA文章來源:http://www.zghlxwxcb.cn/news/detail-797244.html
插件 npm install jsrsasign
使用文章來源地址http://www.zghlxwxcb.cn/news/detail-797244.html
import jsrsasign from 'jsrsasign'
let encipher= new jsrsasign.RSAKey()
// 私鑰 在微信端申請好后會有,一般由后端提供
const key = `-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----`
// 把私鑰轉(zhuǎn)成16進制并設(shè)置秘鑰
encipher= jsrsasign.KEYUTIL.getKey(key )
// 設(shè)置SHA256withRSA方式加密
const sig = new jsrsasign.KJUR.crypto.Signature({
alg: 'SHA256withRSA'
})
// 初始化
sig.init(encipher)
// 需要加密的字段,這里注意下一定要按每一個字段一行,切末位加上 \n
const str =
`${this.payObj.appId}\n`+
`${this.payObj.timeStamp}\n`+
`${this.payObj.nonceStr}\n`+
`${this.payObj.package}\n`
const paySign = sig.updateString(str)
// 加密后,轉(zhuǎn)成base64
const sign = jsrsasign.hextob64(sig.sign()
uni.requestPayment({
provider: ‘wxpay’, //支付類型-固定值 這里是微信支付
timeStamp: this.payObj.timeStamp, // 時間戳(單位:秒)要和上面簽名的一致
nonceStr: this.payObj.nonceStr, // 隨機字符串
package: this.payObj.package, // 固定值
signType: this.payObj.signType, //固定值
paySign: sign, //簽名
success: function(res) {
// console.log('success:' + JSON.stringify(res));
console.log("支付成功");
uni.showToast({
icon: 'success',
title: '支付成功'
})
//清空輸入框
that.name = ''
that.idcard = ''
},
fail: function(err) {
// console.log('fail:' + JSON.stringify(err));
console.log("支付失敗", err);
uni.showToast({
icon: 'none',
title: '支付失敗'
})
}
});
到了這里,關(guān)于uniapp微信小程序JSAPI支付前端生成簽名,并調(diào)起微信支付的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!