1. 程式人生 > >微信小程式實現微信支付

微信小程式實現微信支付

【微信支付】
 /* 微信支付 */
  wxpay: function () {
    var that = this
    //登陸獲取code
    wx.login({
      success: function (res) {
        console.log(res.code)
        //獲取openid
        that.getOpenId(res.code)
      }
    });
  },
 
  /* 獲取openId */
  getOpenId: function (code) {
    var that = this
    wx.request({
      url: "https://api.weixin.qq.com/sns/jscode2session?appid=wxbd5a8270399d41d9&secret=d8aac26a5a9c16266d1a23851ebb7d9b&js_code=" + code + "&grant_type=authorization_code",
      method: 'GET',
      success: function (res) {
        //統一支付簽名
        var appid = '';//appid  
        var body = '';//商戶名
        var mch_id = '';//商戶號  
        var nonce_str = that.randomString;//隨機字串,不長於32位。  
        var notify_url = '';//通知地址
        var spbill_create_ip = '';//ip
        // var total_fee = parseInt(that.data.wxPayMoney) * 100;
        var total_fee = 100;
        var trade_type = "JSAPI";
        var key = '';
        var unifiedPayment = 'appid=' + appid + '&body=' + body + '&mch_id=' + mch_id + '&nonce_str=' + nonce_str + '¬ify_url=' + notify_url + '&openid=' + res.data.openid + '&out_trade_no=' + that.data.paySn + '&spbill_create_ip=' + spbill_create_ip + '&total_fee=' + total_fee + '&trade_type=' + trade_type + '&key=' + key
        var sign = MD5.MD5(unifiedPayment).toUpperCase()
        console.log(sign)
 
        //封裝統一支付xml引數
        var formData = "<xml>"
        formData += "<appid>" + appid + "</appid>"
        formData += "<body>" + body + "</body>"
        formData += "<mch_id>" + mch_id + "</mch_id>"
        formData += "<nonce_str>" + nonce_str + "</nonce_str>"
        formData += "<notify_url>" + notify_url + "</notify_url>"
        formData += "<openid>" + res.data.openid + "</openid>"
        formData += "<out_trade_no>" + that.data.paySn + "</out_trade_no>"
        formData += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"
        formData += "<total_fee>" + total_fee + "</total_fee>"
        formData += "<trade_type>" + trade_type + "</trade_type>"
        formData += "<sign>" + sign + "</sign>"
        formData += "</xml>"
 
        //統一支付
        wx.request({
          url: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
          method: 'POST',
          head: 'application/x-www-form-urlencoded',
          data: formData, // 設定請求的 header
          success: function (res) {
            console.log(res.data)
          
            var result_code = that.getXMLNodeValue('result_code', res.data.toString("utf-8"))
            var resultCode = result_code.split('[')[2].split(']')[0]
            if (resultCode == 'FAIL') {
              var err_code_des = that.getXMLNodeValue('err_code_des', res.data.toString("utf-8"))
              var errDes = err_code_des.split('[')[2].split(']')[0]
              wx.navigateBack({
                delta: 1, // 回退前 delta(預設為1) 頁面
                success: function (res) {
                  wx.showToast({
                    title: errDes,
                    icon: 'success',
                    duration: 2000
                  })
                },
 
              })
            } else {
              //發起支付
              var prepay_id = that.getXMLNodeValue('prepay_id', res.data.toString("utf-8"))
              var tmp = prepay_id.split('[')
              var tmp1 = tmp[2].split(']')
              //簽名  
              var key = '';
              var appId = '';
              var timeStamp = that.createTimeStamp();
              var nonceStr = that.randomString();
              var stringSignTemp = "appId=&nonceStr=" + nonceStr + "&package=prepay_id=" + tmp1[0] + "&signType=MD5&timeStamp=" + timeStamp + "&key="
              var sign = MD5.MD5(stringSignTemp).toUpperCase()
              console.log(sign)
              var param = { "timeStamp": timeStamp, "package": 'prepay_id=' + tmp1[0], "paySign": sign, "signType": "MD5", "nonceStr": nonceStr }
              that.pay(param)
            }
 
 
 
          },
 
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /* 隨機數 */
  randomString: function () {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****預設去掉了容易混淆的字元oOLl,9gq,Vv,Uu,I1****/
    var maxPos = chars.length;
    var pwd = '';
    for (var i = 0; i < 32; i++) {
      pwd += chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
  },
  /* 獲取prepay_id */
  getXMLNodeValue: function (node_name, xml) {
    var tmp = xml.split("<" + node_name + ">")
    var _tmp = tmp[1].split("</" + node_name + ">")
    return _tmp[0]
  },
 
  /* 時間戳產生函式   */
  createTimeStamp: function () {
    return parseInt(new Date().getTime() / 1000) + ''
  },
  /* 支付   */
  pay: function (param) {
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success: function (res) {
        // success
        console.log(res)
        wx.navigateBack({
          delta: 1, // 回退前 delta(預設為1) 頁面
          success: function (res) {
            wx.showToast({
              title: '支付成功',
              icon: 'success',
              duration: 2000
            })
          },
          fail: function () {
            // fail
          },
          complete: function () {
            // complete
          }
        })
      },
      fail: function () {
        // fail
        console.log("支付失敗")
      },
      complete: function () {
        // complete
        console.log("pay complete")
      }
    })
  }
【參考文章】
http://www.cnblogs.com/jcscript/p/6126722.html