Development document learning
Figure borrow, personally think that this picture contains WeChat pay architecture concept (https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=3)
Main interactions between merchant system and wechat Payment system:
Implementation process:
=> Obtain the openID of wechat users (small program side to obtain the background)
=> Pre-order to obtain prepay_id,sign signature (background to sign according to relevant parameters)
=> The applet sends the payment request (according to the prepay_id and sign returned by the background)
2. Implementation code
- Applets get openID
Wx. login({success(res) {if(res.code) {// Initiate a network request wx.request({url:'https://test.com/onLogin',
data: {
code: res.code
},
success:function(res){
letOpenId = res.data. openId}})}else {
console.log('Login failed! '+ res.errmsg)}}}) //nodejs var request = require('request')
const JSCODE = ' '(Value passed from the front end)let wxUrl = "https://api.weixin.qq.com/sns/jscode2session"Var url = wxUrl + var url = wxUrl +'appid=' + Config.wxLogin.appid + '&secret=' + Config.wxLogin.secret
var js_code = req.query.js_code
url = url + '&js_code=' + js_code + '&grant_type=authorization_code'
request(url, function (error, response, body) {
if (error) {
var results = Util.formatErrorRes(error);
res.json(results);
return }
var results = Util.formatRes(body);
res.json(results);
})Copy the code
2. The background obtains prepay_id and sign signatures based on related parameters
=> Backend formatting parameters (according to the needs of the applets unified single document)
=> Background with parameters to request wechat unified order address (notify_URL interface address for receiving payment results at the back end)
=> The request successfully parses the XML data returned by the applet (import NPM package XMLReader, mainly obtain prepay_id)
=> Perform a secondary signature based on the returned value to generate a signature string (returned to the front end along with prepay_id)
=> The front-end makes a payment request based on the returned signature and prepay_id
=> wechat payment succeeds, and the wechat server sends the payment result to the notify_URL set before
=> Update the user information according to the result returned by the wechat background
Note: There are many codes in this part, which will be arranged and put down on GitHub in the near future. If you are in a hurry, please note in the comments below
3. Problem summary
- Payment signature error (see Article 2)
Wechat official answer1) use wechat's online signature tool to check whether the signature is consistent with that generated by the program.//pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=20_1The signature tool opens with Google. Select MD5, XML, and put in the request parameter XML to verify the signature.2) If it is consistent with the online signature tool of wechat, it means there is no error in the program. It is confirmed that the API key is wrong (changed or misremembered by others). Change the API key (Account setting - security setting -API security) in the account information of merchant platform.15Take effect in minutes2.1A merchant number is used only under unification, and it must be A merchant number that logs in to the merchant platform to set the key.2.2Note that the total_fee parameter is of type int.3) If it is inconsistent with wechat's online signature tool, there is an error in the program. Common errors may be:3.1) coding issues, making sure everything is UTF -8If there is Chinese, you can first change the Chinese to English and sign again to see if the signature is wrong, if the English is correct, the Chinese will be wrong, basically it must be a coding problem3.2The case of the fields in the message is exactly the same as in the documentCopy the code
The problem of incorrect payment signature can be solved by configuration according to the above requirements.
Express wechat pay callback value req.body is empty {}
The payback format is XML. The reason is that Express4. x separates body-Parser from other middleware.
So we need to add middleware to app.js
var bodyParser = require("body-parser");
require("body-parser-xml")(bodyParser); Use (bodyParser.xml({bodyParser.xml(bodyparser.xml))limit: "1MB",
// Reject payload bigger than 1 MB
xmlParseOptions: {
normalize: true,
normalizeTags: true,
explicitArray: false
},
verify: function(req, res, buf, encoding) {
if(buf && buf.length) {
// Store the raw XML
req.rawBody = buf.toString(encoding || "utf8"); }}}));Copy the code
The format of the parameter must be strictly documented, and the parameter must be humped.
var ret = {
appId: appid,
package:'prepay_id='+prepayid,
nonceStr: noncestr,
signType:'MD5',
timeStamp: timestamp,
};Copy the code
5. No payment permission
Check whether the mini program account has been qualified for payment.