SoFunction
Updated on 2025-02-28

WeChat public account H5 payment interface call method

The examples in this article have shared with you the specific code of H5 calling the payment interface in WeChat for your reference. The specific content is as follows

Official Documentationaddress

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>WeChat public accountH5Interface call</title>
  <script src='./js/'></script>
</head>
<body>
  <script>
    // Determine whether the WeChat version is above 5.0    // Attributes include browser type, version, operating system type, browser engine and other information    var ua = ()
    // Match whether ua contains MicroMessenger strings through regular expressions    if ((/MicroMessenger/i) != 'micromessenger') {
      return false
    }

    var appId = 'wx123456789'
    // Preparation for WeChat H5 interface call    // 1, timestamp    var timeStamp = new Date()
    timeStamp = ()/1000
    ('Timestamp', timeStamp)

    // 2, generate 32-bit random string    function randomString(len) {
      len = len || 32
      // The confusing characters oOLl, 9gq, Vv, Uu, I1 are removed by default      var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
      var charsLen = ;
      var pwd = '';
      for (var i=0;i<len;i++) {
        pwd += ((() * charsLen))
      }
      return pwd
    }
    ('nonceStr, 32-bit random string', randomString())

    // 3, package, unify the prepay_id parameter value returned by a single interface
    // 4, paySign: WeChat signature    function wxSignFunc() {
      // 4.1, rewrite the parameters into the form of key=value&key2=value2, and need to be sorted in the ASCII dictionary order of the parameter name      var stringA = 'appId=' + appId
            +'&nonceStr=' + randomString()
            +'&package=prepay_id=u802345jgfjsdfgsdg888'
            +'&signType=MD5'
            +'&timeStamp=' + timeStamp

      ///// 4.2, Splicing API Key      var stringSignTemp = stringA + '&key=192006250b4c09247ec02edce69f6a2d' //Note: The key is the key set by the merchant platform      var sign = hex_md5(stringSignTemp).toUpperCase()
      return sign
    }

    (wxSignFunc())

    //////////////////// WeChat interface example    function onBridgeReady(){
     (
        'getBrandWCPayRequest', {
          "appId": appId,   //The name of the official account is transferred to by the merchant          "timeStamp": timeStamp,     // Time stamp, seconds since 1970          "nonceStr": randomString(), //Random string          "package":"prepay_id=u802345jgfjsdfgsdg888",  // Unify the prepay_id parameter value returned by a single interface          "signType":"MD5",     //WeChat signature method:          "paySign": wxSignFunc() //WeChat Signature        },
        function(res){
          if(res.err_msg == "get_brand_wcpay_request:ok" ){
            // Use the above method to judge the front-end return, the WeChat team solemnly reminds:            // res.err_msg will return ok after the user pays successfully, but it does not guarantee that it is absolutely reliable.            (111)
          }
        }
      )
    }

    // Call the WeChat interface    if (typeof WeixinJSBridge == "undefined"){
     if(  ){
      ('WeixinJSBridgeReady', onBridgeReady, false);
     } else if (){
      ('WeixinJSBridgeReady', onBridgeReady); 
      ('onWeixinJSBridgeReady', onBridgeReady);
     }
    }else{
     onBridgeReady();
    }
  </script>

</body>
</html>

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.