美文网首页
微信支付

微信支付

作者: 前端陈陈陈 | 来源:发表于2021-01-18 10:37 被阅读0次

    1、 排序

     createPaySign(data) {
        //排序
        var keylist = [];
        for (var key in data) {
          keylist.push(key);
        }
        var sortKeyList = this.bundleSort(keylist);
        console.log(sortKeyList);
        //拼接字符串
        var md5strarr = [];
        for (var i = 0; i < sortKeyList.length; i++) {
          md5strarr.push(sortKeyList[i] + "=" + data[sortKeyList[i]]);
        }
    
        var md5str = md5strarr.join("&");
        console.log(md5(md5str));
        //md5加密
        return md5(md5str).toUpperCase();
      },
      bundleSort(list) {
        var len = list.length;
    
        for (var i = 0; i < len; i++) {
          for (var j = 0; j < len; j++) {
            if (list[i] <= list[j]) {
              var temp = list[j];
              list[j] = list[i];
              list[i] = temp;
            }
          }
        }
    
        return list;
      },
    

    2、支付

      pays: function (event) {
        var  h = this 
        wx.request({
          url: 'https://foshantv. 请求地址',
          data: {
            id: h.data.obj.orderid ,
          },
          header: {
            "Content-type": "application/x-www-form-urlencoded",
            "cookie": h.data.obj.cookie
          },
          method: "POST",
          success(res) {
            if (res.data.status == 1) {
                var paysignData = {
                  appId:t.globalData.appid,
                  timeStamp: h.data.obj.timeStamp,
                  nonceStr: h.data.obj.nonceStr,
                  package: h.data.obj.package,
                  signType: h.data.obj.signType,
                }
                var originstr= "appId="+paysignData.appId+"&nonceStr="+paysignData.nonceStr+"&package="+paysignData.package+"&signType=MD5&timeStamp="+paysignData.timeStamp+"&key=xyq4woxtq1lyzz5b3l4nnstvlxwclr3g";
                var paysign = md5(originstr).toUpperCase();
                console.log(originstr, paysign);
                wx.requestPayment({
                  timeStamp: h.data.obj.timeStamp,
                  nonceStr: h.data.obj.nonceStr,
                  package: h.data.obj.package,
                  signType: h.data.obj.signType,
                  paySign: paysign,         
                  success:(res) => {    
                    // h.complete()
                  },
                  fail(res) {
                  },
                  complete(res){
                    console.log("res",res)
                    if(res.errMsg == "requestPayment:ok"){
                      wx.showToast({
                        title:'支付成功',
                        icon:'success',
                        duration:2000
                      }) 
                      setTimeout(function() {
                        wx.redirectTo({
                          url: '/pages/web/orderDetails?orderid=' +h.data.obj.orderid
                        }) 
                     }, 1000);
                    } else{
                      wx.showToast({
                        title:'支付失败',
                        icon:'fail',
                        duration:2000
                      }) 
                    }
                  }
    
                })
            }
          }
        })
      },
    

    相关文章

      网友评论

          本文标题:微信支付

          本文链接:https://www.haomeiwen.com/subject/britzktx.html