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 encountered by WeChat Mini Programs payment developer

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

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points on how to solve the mistakes encountered in WeChat Mini Programs's payment development. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.

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)

}

}

})

}

Copy the code

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.

/ / A unified API to obtain sign (signature) for issuing an order

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

/ / encrypted 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

}

/ / issue the order uniformly

Success: function (res) {

Var sign = res.data.strMd5

Var formData = ""

FormData + = "" + appid + "" / / appid

FormData + = "" + attach + "" / / additional data

FormData + = "" + body + "" / / title

FormData + = "" + mch_id + "" / / merchant number

FormData + = "" + nonce_str + "" / / random string, no longer than 32 bits.

FormData + = "" + notify_url + "" / / callback address that asynchronously receives WeChat Pay's result notification

FormData + = "" + openid + "" / / user Id

FormData + = "" + out_trade_no + "" / / merchant order number

FormData + = "" + spbill_create_ip + ""

FormData + = "" + total_fee + "" / / amount

FormData + = "" + trade_type + "" / / Public account payment

FormData + = "" + sign + "" / / signature

FormData + = ""

Copy the code

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 ()

/ / obtain 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')

}

})

}

})

}

These are all the contents of the article "how to solve the mistakes encountered by WeChat Mini Programs payment developer". Thank you for your reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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