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) } } }); }
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>"
Return data analysis 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); } }) } }) } }); }
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: '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'); } }); } }) }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
