[WeChat mini-program payment function] uniapp implements WeChat mini-program payment function

Scenario : To implement the WeChat payment function of the e-commerce module of the company's WeChat applet

1. Implementation steps and ideas

  1. In the login state, the code is obtained when logging in, and the openid is obtained by using the code: https://blog.csdn.net/weixin_45308405/article/details/128868377?spm=1001.2014.3001.5501

  1. Under "Payment" in the "App Module Configuration" item of the manifest.json file, check the "WeChat Payment" item

  1. Go to the corresponding interface to get the order number and use the order number to call the background interface to return the payment data

  1. Call the uniapp applet payment API to log in

2. Realize

  1. Set up Payment (payment) under the manifest.json file

  1. Call the background interface to return the payment data

const res = await RequesApi.OrderApi(isCart.value == '2' ? cartOrderData : orderData)
    if (res.data.Code == '0') {
        const res_a = await RequesApi.AppPayApi({
            "DoType": "8", //1支付操作
            "OrderNO": res.data.Data[0], //订单编号
            'OpenId': uni.getStorageSync('openid')//登录时获取openid
        })
}
  1. Call the WeChat payment API provided by uniapp

Document address: uniapp WeChat payment

uni.requestPayment({
            provider: 'wxpay',
            orderInfo: '',
            // appId: appId,//小程序的appid
            timeStamp: res_a.data.Data[0].TimeStamp,//时间戳,要字符串类型的
            nonceStr: res_a.data.Data[0].NonceStr,//随机字符串,长度为32个字符以下。
            package: res_a.data.Data[0].Package,//prepay_id 参数值,提交格式如:prepay_id=xx
            signType: res_a.data.Data[0].SignType, //MD5类型
            paySign: res_a.data.Data[0].PaySign,//签名
            success: function (res) {
                //支付成功的回调    成功之后你想做什么在这里操作  比如弹窗一个提示:支付成功等
                uni.showToast({
                    title: '支付成功!',
                    icon: 'success'
                })
            },
            fail: function (err) {
                //支付失败的回调   失败之后你想做什么在这里操作  比如弹窗一个提示:支付失败等
                console.log(err);

            }
        });

Guess you like

Origin blog.csdn.net/weixin_45308405/article/details/128914077