欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 支付功能開(kāi)發(fā)錯(cuò)誤總結(jié)

 更新時(shí)間:2017年02月21日 15:01:06   投稿:lqh  
這篇文章主要介紹了微信小程序 支付功能開(kāi)發(fā)錯(cuò)誤總結(jié)的相關(guān)資料,需要的朋友可以參考下

微信小程序 支付功能開(kāi)發(fā)錯(cuò)誤總結(jié)

微信小程序支付終于踩完坑了,發(fā)現(xiàn)里面坑挺大的,現(xiàn)在發(fā)個(gè)貼,希望以后入坑的同學(xué)可以看一下 :

https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=2

業(yè)務(wù)流程在這里大家看文檔的時(shí)候可以看到。第一個(gè)坑,獲取用戶(hù)的openid,參數(shù)一定要拼在url連接上,否則會(huì)報(bào){"errcode":40013,"errmsg":"invalid appid, hints: [ req_id: iil1ba0504ns86 ]"}錯(cuò)誤

 onLoad: function () {
  var that = this
  wx.login({
   success: function (res) {
    if (res.code) {
     //發(fā)起網(wǎng)絡(luò)請(qǐng)求
     wx.request({
      url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxaacf22345345cfc7162fe3&secret=83ebd41c3e6f34a49b3a34578063434548ff3f71&js_code=' + res.code + '&grant_type=authorization_code',
      method: "POST",
      success: function (res) {
       that.setData({
        openid: res.data.openid
       })
      }
     })
    } else {
     console.log('獲取用戶(hù)登錄態(tài)失?。? + res.errMsg)
    }
   }
  });
 }

第二個(gè)坑,支付統(tǒng)一下單接口,簽名這個(gè)坑是比較多人遇到問(wèn)題的這個(gè)是MD5加密經(jīng)常和簽名工具里面的加密簽名不一樣

簽名加密工具地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1

簽名加密的時(shí)候要轉(zhuǎn)成utf-8,加密我用自己的接口進(jìn)行加密的 digest.update(data.getBytes("utf-8"));

 // 統(tǒng)一下單接口獲取sign(簽名)
 paysignjsapi: function (appid, attach, body, mch_id, nonce_str, notify_url, openid, out_trade_no, spbill_create_ip, total_fee, trade_type, key) {
  var self = this;
  //加密簽名
  wx.request({
   url: 'http://localhost:8080/XinXingWXApi/wxXcxApi/Md5Encrypt.do',
   method: 'GET',
   data: {
    appid: appid,
    attach: attach,
    body: body,
    mch_id: mch_id,
    nonce_str: nonce_str,
    notify_url: notify_url,
    openid: openid,
    out_trade_no: out_trade_no,
    spbill_create_ip: spbill_create_ip,
    total_fee: total_fee,
    trade_type: trade_type,
    key: key
   },
   //統(tǒng)一下單
   success: function (res) {
    var sign = res.data.strMd5
    var formData = "<xml>"
    formData += "<appid>" + appid + "</appid>" //appid 
    formData += "<attach>" + attach + "</attach>" //附加數(shù)據(jù) 
    formData += "<body>" + body + "</body>"    //標(biāo)題
    formData += "<mch_id>" + mch_id + "</mch_id>" //商戶(hù)號(hào) 
    formData += "<nonce_str>" + nonce_str + "</nonce_str>" //隨機(jī)字符串,不長(zhǎng)于32位。 
    formData += "<notify_url>" + notify_url + "</notify_url>" //異步接收微信支付結(jié)果通知的回調(diào)地址
    formData += "<openid>" + openid + "</openid>"  //用戶(hù)Id
    formData += "<out_trade_no>" + out_trade_no + "</out_trade_no>" //商戶(hù)訂單號(hào)
    formData += "<spbill_create_ip>" + spbill_create_ip + "</spbill_create_ip>"
    formData += "<total_fee>" + total_fee + "</total_fee>" //金額
    formData += "<trade_type>" + trade_type + "</trade_type>"  //公共號(hào)支付
    formData += "<sign>" + sign + "</sign>"http://簽名
    formData += "</xml>"

返回?cái)?shù)據(jù)解析xml

 //請(qǐng)求統(tǒng)一下單接口
    wx.request({
     url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
     method: 'POST',
     data: formData,
     success: function (data) {
      wx.request({
       url: "http://localhost:8080/XinXingWXApi/wxXcxApi/xmlAnalyze.do?strXml=" + data.data,
       method: 'POST',
       success: function (res) {
        var pk = 'prepay_id=' + res.data.prepayId;
        var timeStamp = self.createTimeStamp();
        //獲取支付簽名,并支付
        self.getsignType(appid, timeStamp, nonce_str, pk, "MD5", key);
       }
      })
     }
    })
   }
  });
 }

第三就是調(diào)用支付了,這里也有幾個(gè)小坑,第一就是appId很多寫(xiě)成appid就不行了,第二個(gè)就是preoatid 的參數(shù)格式要寫(xiě)對(duì)prepay_id=wx2017011711060194dccf725232155886323 第三個(gè)就是調(diào)用支付的時(shí)候報(bào)支付簽名錯(cuò)誤,也需要到簽名接口查看簽名是否一致,查看參數(shù)是否是對(duì)的,調(diào)用微信支付的時(shí)候必須加上appId

getsignType: function (appid, timeStamp, nonce_str, pk, signType, key) {
  var that = this;
  wx.request({
   url: "http://localhost:8080/XinXingWXApi/wxXcxApi/getSignType.hn",
   method: 'GET',
   data: {
    appId: appid,
    timeStamp: timeStamp,
    nonceStr: nonce_str,
    pk: pk,
    signType: signType,
    key: key
   },
   success: function (res) {
    console.log(res.data.paySign)
    var paySign = res.data.paySign
    //調(diào)用微信支付
    wx.requestPayment({
     'appId': appid,
     'timeStamp': timeStamp,
     'nonceStr': nonce_str,
     'package': pk,
     'signType': 'MD5',
     'paySign': paySign,
     'success': function (res) {
      console.log(res);
      console.log('success');
     },
     'fail': function (res) {
      console.log(res);
      console.log('fail');
     },
     'complete': function (res) {
      // console.log(res);
      console.log('complete');
     }
    });
   }
  })
 }

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論