java-mall-app/helpers/pPay.js
2024-11-01 16:35:40 +08:00

64 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
微信支付方法(uni-app h5)适用
获取微信加签信息
@param{data}:获取的微信加签
@param{res}:成功回调
@param{fail}:失败回调
@warn:因为package为严格模式下的保留字不能用作变量.
@use
wPay({
appId,
timeStamp,
nonceStr,
signature,
package,
paySign
},res=>{
console.log('调用成功!');
},fail=>{
console.log('调用失败!');
})
*/
import wx from './jweixin'
const wexinPay = (data, cb, errorCb) => {
let [appId, timestamp, nonceStr, signature, packages, paySign] = [data.appId, data.timeStamp, data.nonceStr, data.signature,
data.package, data.paySign
];
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来若要查看传入的参数可以在pc端打开参数信息会通过log打出仅在pc端时才会打印。
appId, // 必填,公众号的唯一标识
timestamp, // 必填,生成签名的时间戳
nonceStr, // 必填,生成签名的随机串
signature, // 必填签名见附录1
jsApiList: ['chooseWXPay'] // 必填需要使用的JS接口列表所有JS接口列表见附录2
});
wx.ready(function() {
wx.chooseWXPay({
timestamp, // 支付签名时间戳注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr, // 支付签名随机串,不长于 32 位
'package': packages, // 统一支付接口返回的prepay_id参数值提交格式如prepay_id=***
'signType': signature, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign, // 支付签名
success(res) {
// 支付成功后的回调函数
cb(res);
},
fail(res) {
errorCb(res);
}
});
});
wx.error(function(res) {
// config信息验证失败会执行error函数如签名过期导致验证失败具体错误信息可以打开config的debug模式查看也可以在返回的res参数中查看对于SPA可以在这里更新签名。
/*alert("config信息验证失败");*/
});
}
export default wexinPay;