调用微信支付

发布时间:2023年12月26日

获取微信支付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',
      });
    }
  })
}
文章来源:https://blog.csdn.net/qq_43206280/article/details/135219603
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。