获取微信支付code,重新加载
const handleBuyPackage = (packageItem: { packageId: any; }) => {
var requestId = getRandomDigits()
getRedirectUrl({
requestId: requestId,
}).then((res: any) => {
if (res.code === 200) {
localStorage.setItem('requestId', requestId)
localStorage.setItem('packageItem', JSON.stringify(packageItem))
window.location.href = res.data
}
})
}
监听微信code,调用微信自定义支付
var codeStr = new URLSearchParams(window.location.search).get('code')
if(codeStr){
toPayOrderReq(packageItemInfo,requestIdInfo)
}
const wxPayOrder = (orderData: { timeStamp: any; nonceStr: any; packageStr: any; signType: any; paySign: any; })=> {
return new Promise((resolve, reject) => {
// 调用微信支付接口
WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{
appId: '',
timeStamp: orderData.timeStamp,
nonceStr: orderData.nonceStr,
package: orderData.packageStr,
signType: orderData.signType,
paySign: orderData.paySign
},
function (res: { err_msg: string; }) {
if (res.err_msg === 'get_brand_wcpay_request:ok') {
resolve();
} else {
reject();
}
}
);
});
}
const toPayOrderReq = (packageItem: { packageId: any; },requestId: any) => {
toPayOrder({
wxCode:new URLSearchParams(window.location.search).get('code')
}).then((res: any) => {
if(res.code==200){
const prepayId = res.data
//验签
getJsSign(prepayId).then((res: { data: any; }) => {
const signData = res.data;
wxPayOrder(signData).then(() => {
// 支付成功回调
console.log('支付成功');
}).catch(() => {
// 支付失败回调
console.log('支付失败');
});
}).catch((error: any) => {
console.error('获取验签数据失败', error);
});
}else{
showToast({
message: res.msg,
position: 'top',
});
}
})
}