其他分享
首页 > 其他分享> > 微信支付

微信支付

作者:互联网

支付步骤:

  1. 先传订单信息到后台,拿取后台返回的数据
  2. 调用微信支付接口wx.requestPayment,用拿到的数据进行对应的填写

微信支付参考地址:
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html

import { appId } from '../config/config';
import getOrderRes from '../resource/wxpay';

const defaultSuccessCallback = (res, backUrl = '/pages/tabbar/sevices/index') => {
  wx.redirectTo({
    url: `/pages/wxpay/success/index?backUrl=${backUrl}`,
  });
};

const defaultFailCallback = (res) => {
  const { orderNo, orderType } = res;
  wx.redirectTo({
    url: `/pages/wxpay/fail/index?orderNo=${orderNo}&orderType=${orderType}`,
  });
};

const wxpay = ({
  orderNo: orderNo,
  orderType: orderType,
  success: successCallback = defaultSuccessCallback,
  fail: failCallback = defaultFailCallback,
}) => {
  wx.login({
    async success({ code }) {
      if (code) {
        const { data } = await getOrderRes.getOrderRes({ code, orderNo, orderType, appId });
        if (!data) {
          successCallback({ result: '支付成功' });
        } else {
          toPay({
            orderInfo: data,
            orderNo,
            orderType,
            success: successCallback,
            fail: failCallback,
          });
        }
      } else {
        console.log('登录code获取失败!');
      }
    },
  });
};

const toPay = ({
  orderInfo,
  orderNo,
  orderType,
  success: successCallback = defaultSuccessCallback,
  fail: failCallback = defaultFailCallback,
}) => {
  const { nonceStr, packageValue, paySign, timeStamp, appId } = orderInfo;
  wx.requestPayment({
    appId,
    timeStamp,
    nonceStr,
    package: packageValue,
    signType: 'MD5',
    paySign,
    success(res) {
      successCallback((res = { result: res }));
    },
    fail(res) {
      failCallback((res = { orderNo, orderType, result: res }));
    },
  });
};


export { wxpay, toPay };

标签:const,success,微信,支付,res,orderType,orderNo,wx
来源: https://blog.csdn.net/w_D_lufei/article/details/109991338