WeChat applet payment function development error

高洛峰
Release: 2017-02-25 09:20:34
Original
1466 people have browsed it

Summary of errors in the development of WeChat mini program payment function

WeChat mini program payment finally ran out of pitfalls, and found that there are quite big pits. I will post it now and hope that in the future Students who are interested can take a look:

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

The business process can be seen here when you look at the document. The first pitfall is to get the user's openid. The parameters must be spelled in the url connection, otherwise it will report {"errcode":40013,"errmsg":"invalid appid, hints: [ req_id: iil1ba0504ns86 ]"}Error

 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)
    }
   }
  });
 }
Copy after login

The second pitfall is the unified payment order interface and signature. This pitfall is where more people encounter problems. This is because MD5 encryption is often associated with The encrypted signatures in the signature tool are different

Signature encryption tool address: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=20_1

When the signature is encrypted, it needs to be converted to utf-8. For encryption, I use my own interface to encrypt 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>"//签名
    formData += "</xml>"
Copy after login

Return data analysis xml

 //请求统一下单接口
    wx.request({
     url: "https://api.mch.weixin.qq.com/pay/unifiedorder",
     method: &#39;POST&#39;,
     data: formData,
     success: function (data) {
      wx.request({
       url: "http://localhost:8080/XinXingWXApi/wxXcxApi/xmlAnalyze.do?strXml=" + data.data,
       method: &#39;POST&#39;,
       success: function (res) {
        var pk = &#39;prepay_id=&#39; + res.data.prepayId;
        var timeStamp = self.createTimeStamp();
        //获取支付签名,并支付
        self.getsignType(appid, timeStamp, nonce_str, pk, "MD5", key);
       }
      })
     }
    })
   }
  });
 }
Copy after login

The third is to call payment. There are also several pitfalls here. The first is that there are many appIds. Writing it as appid will not work. The second is that the parameter format of preoatid must be written correctly: prepay_id=wx2017011711060194dccf725232155886323. The third is that a payment signature error is reported when calling payment. You also need to go to the signature interface to check whether the signature is consistent and whether the parameters are correct. When calling WeChat payment, you must add the appId

getsignType: function (appid, timeStamp, nonce_str, pk, signType, key) {
  var that = this;
  wx.request({
   url: "http://localhost:8080/XinXingWXApi/wxXcxApi/getSignType.hn",
   method: &#39;GET&#39;,
   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({
     &#39;appId&#39;: appid,
     &#39;timeStamp&#39;: timeStamp,
     &#39;nonceStr&#39;: nonce_str,
     &#39;package&#39;: pk,
     &#39;signType&#39;: &#39;MD5&#39;,
     &#39;paySign&#39;: paySign,
     &#39;success&#39;: function (res) {
      console.log(res);
      console.log(&#39;success&#39;);
     },
     &#39;fail&#39;: function (res) {
      console.log(res);
      console.log(&#39;fail&#39;);
     },
     &#39;complete&#39;: function (res) {
      // console.log(res);
      console.log(&#39;complete&#39;);
     }
    });
   }
  })
 }
Copy after login

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more articles related to WeChat applet payment function development errors, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template