Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the mistakes in the Development of payment function in WeChat Mini Programs

2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

The editor of this article introduces in detail "how to solve the mistakes in the development of payment function in WeChat Mini Programs". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to solve the mistakes in the development of payment function in WeChat Mini Programs" can help you solve your doubts. Let's follow the editor's ideas slowly and deeply, and let's learn new knowledge together.

The business process can be seen here when you look at the document. The first pit is to get the user's openid. The parameter must be spelled on the url connection, otherwise {"errcode": 40013, "errmsg": "invalid appid, hints: [req_id: iil1ba0504ns86]"} error will be reported.

OnLoad: function () {var that = this wx.login ({success: function (res) {if (res.code) {/ / initiate a network request 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 ('failed to get user login status!' + res.errMsg)}});}

The second pit is the API for unified payment and issuing orders. Signing this pit is a problem encountered by many people. This is that MD5 encryption is often different from the encrypted signature in the signature tool.

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

When signing and encrypting, you need to convert it to utf-8, and encrypt the digest.update (data.getBytes ("utf-8")) that I encrypt with my own interface.

/ / Unified API to obtain sign (signature) 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 / / encryption signature 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} / / Unified order success: function (res) {var sign = res.data.strMd5 var formData = "" formData + = "" + appid + "" / / appid formData + = "" + attach + "" / / additional data formData + = "" + body + "" / / title formData + = "" + mch_id + "/ / merchant formData + =" + nonce_str + "" / / Random string No longer than 32 bits. FormData + = "" + notify_url + "" / / callback address formData + = "" + openid + "" / / user Id formData + = "" + out_trade_no + "" / / merchant order number formData + = "" + spbill_create_ip + "" formData + = "" + total_fee + "" / / amount formData + = "+ trade_type +" / / Public Co-signed payment formData + = "" + sign + "" / / signed formData + = ""

Returned data parsing xml

/ / request unified order issuing API 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 Competition = 'prepay_id=' + res.data.prepayId Var timeStamp = self.createTimeStamp (); / / get the payment signature and pay self.getsignType (appid, timeStamp, nonce_str, Competition, "MD5", key);})}});});}

The third is to call payment. There are also several small pits. The first is that a lot of appId is written as appid, and the second is that the parameter format of preoatid should be written to prepay_id=wx2017011711060194dccf725232155886323. The third is to report a payment signature error when calling payment. You also need to go to the signature API to check whether the signature is consistent and check whether the parameters are correct. You must add appId when calling WeChat Pay.

GetsignType: function (appid, timeStamp, nonce_str, Competition, 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, Competition: competition, signType: signType, key: key} Success: function (res) {console.log (res.data.paySign) var paySign = res.data.paySign / / call WeChat Pay wx.requestPayment ({'appId': appid,' timeStamp': timeStamp, 'nonceStr': nonce_str,' package': Competition, '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');}}) }})} read here, this article "how to solve the mistakes in the development of payment functions in WeChat Mini Programs" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report