微信小程序 支付功能開發錯誤總結
微信小程序支付終于踩完坑了,發現里面坑挺大的,現在發個貼,希望以后入坑的同學可以看一下 :
https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=2 
業務流程在這里大家看文檔的時候可以看到。第一個坑,獲取用戶的openid,參數一定要拼在url連接上,否則會報{"errcode":40013,"errmsg":"invalid appid, hints: [ req_id: iil1ba0504ns86 ]"}錯誤
 onLoad: function () {  var that = this  wx.login({   success: function (res) {    if (res.code) {     //發起網絡請求     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('獲取用戶登錄態失敗!' + res.errMsg)    }   }  }); }第二個坑,支付統一下單接口,簽名這個坑是比較多人遇到問題的這個是MD5加密經常和簽名工具里面的加密簽名不一樣 
簽名加密工具地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1 
簽名加密的時候要轉成utf-8,加密我用自己的接口進行加密的 digest.update(data.getBytes("utf-8"));
 // 統一下單接口獲取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   },   //統一下單   success: function (res) {    var sign = res.data.strMd5    var formData = "<xml>"    formData += "<appid>" + appid + "</appid>" //appid     formData += "<attach>" + attach + "</attach>" //附加數據     formData += "<body>" + body + "</body>"    //標題    formData += "<mch_id>" + mch_id + "</mch_id>" //商戶號     formData += "<nonce_str>" + nonce_str + "</nonce_str>" //隨機字符串,不長于32位。     formData += "<notify_url>" + notify_url + "</notify_url>" //異步接收微信支付結果通知的回調地址    formData += "<openid>" + openid + "</openid>"  //用戶Id    formData += "<out_trade_no>" + out_trade_no + "</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>"http://簽名    formData += "</xml>"返回數據解析xml
 //請求統一下單接口    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);       }      })     }    })   }  }); }第三就是調用支付了,這里也有幾個小坑,第一就是appId很多寫成appid就不行了,第二個就是preoatid 的參數格式要寫對prepay_id=wx2017011711060194dccf725232155886323 第三個就是調用支付的時候報支付簽名錯誤,也需要到簽名接口查看簽名是否一致,查看參數是否是對的,調用微信支付的時候必須加上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    //調用微信支付    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');     }    });   }  }) }感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答