Wechat applet recharging and processing after recharging callback

WeChat Applet The recharge process is roughly the same as H5 or official account. Here is a brief introduction to some operation processes of the front end when recharging.

When a user initiates a recharge request in an applet, he usually requests his own server first, sends the recharge parameters to the back-end, and then the back-end requests the wechat recharge, gets the unified order parameters returned by wechat, and then returns them to the front-end. Front end call

The wx.requestPayment API can call up the payment page of the wechat applet. At this time, the user can enter the correct payment password to make the payment. The specific process is as follows:

1. For example, there is a recharge button on the page. When you click the recharge button, you can get the parameters that the user requests to recharge.   

wx.request({
  url: "Back end charging interface",
   method: "POST",
   dataType: "json",
   header: {
     'content-type': 'application/x-www-form-urlencoded'
   },
   data: {
     amount: amount,  // Recharge amount
     token: token,
     openId: openId
   },
   success: function (res) {
     if (res.data.code == '1') {
       that.setData({
         payParams: res.data.data  // Parameters of unified order obtained by the backend from wechat
       })
       that.xcxPay();  // Call up wechat payment page after getting parameters of unified order
     } 
   }
})

2. Call wx.requestPayment to call up wechat payment interface.

wx.requestPayment({
   'timeStamp': that.data.payParams.timestamp.toString(),  // The timestamp must be a string, otherwise an error 'noncestr' will be reported: that.data.payparams.str,
   'package': 'prepay_id=' + that.data.payParams.prepay_id,  // The value here must be in the format of prepay_id = XXXXXXXX, otherwise an error 'signType': 'MD5' will also be reported,
   'paySign': that.data.payParams.sign,
   'success': function (res) { 
    // Official account long time res.errMsg (err_msg = official account) is not the same as the public number. The public number is err_msg, because it has not noticed this, and it has been in a long time for if(res.errMsg == "requestPayment:ok") {/ / call payment successful.
        wx.redirectTo({
              url: '../chargelist/chargelist' // After the recharge is successful, you can jump or do other processing according to your own needs
         })
      }else if(res.errMsg == 'requestPayment:cancel'){
      // User cancel payment
    }
   },
   'fail': function (res) {
     return false;
   },
   'complete': function (res) { }
}) 
Author of this paper: the galloping snail 1031
Original address: Wechat applet recharging and handling after recharging callback - Tutorial - applet community - wechat applet - wechat applet development community - applet development forum - wechat applet Alliance

Tags: JSON

Posted on Fri, 01 May 2020 22:03:16 -0700 by paulbrown83