SoFunction
Updated on 2025-03-10

The method of mini program calling WeChat payment

This article has shared the specific code of mini-program calling WeChat payment for your reference. The specific content is as follows

First call the applet login interface to get the user code

Pass the code as a parameter to the backend call the backend interface wechat/pay/prepay

Get backend these five return values

nonceStr 
package2
paySign 
signType
timeStamp

({
   success: function (res) {
    var userId = ('userId');
    var passw = userId + orderId;
    (passw)
    var password = util.sha1(passw);
    //(res) //Get the value returned by the user's code WeChat    ({
     url: url + 'wechat/pay/prepay',
     method: "POST",
     header: {
      'content-type': 'application/json',
     },
     data: {
      code: , //WeChat return code      userId: userId, //The backend returned to me when logging in and registering      orderId: orderId, //The work order number returned to me by the backend when the user submits      sign: password, //SerId + orderId encryption required by the backend     },
     success: function (res) {
      // if ( == "000000"){
       nonceStr = ;
       package2 = ;
       paySign = ;
       signType = ;
       timeStamp = ;
       that.pay2()
      // }
       ()
       ()
       ()
       ()
       ()
     },
     fail(err) {
      (err)
     }
    })
   },
   fail(errs) {
    (errs)
   }
  })

After successful return, the user clicks payment and calls the WeChat payment interface:

Set the amount on the backend and the user can pay by calling it

pay2() {
  (
   {
    timeStamp: timeStamp, //Time stamp    nonceStr: nonceStr, //Random string    package: package2, //Unify the prepay_id parameter value returned by a single interface    signType: signType, //Signature type    paySign: paySign, //sign    success(res) {
     (res)
     ({
       url: '../successful/successful'
      })
    },
    fail(res) {
     (res)
    }
   })
 },

Finally, when our project called the WeChat payment interface, we requested our backend interface in the success function to obtain the status code and determine whether the payment was successful. Because the user pays to WeChat, after success, only the user can see that our backend cannot obtain whether the payment is successful. This is the purpose of requesting this interface. After the WeChat request to pay successfully, the backend requests to get the return value.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.