In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the nodejs Wechat official account payment development example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
OdeJs Wechat official account function development, mobile H5 page calls Wechat's payment function. These days, according to the needs of the company, use node and H6 pages to call Wechat's payment function to complete the payment requirements. Now go through the development process again, in order to help more developers successfully complete the development of WeChat Pay functions. (Wechat does not provide the payment function of node yet)
one。 Request CODE
The purpose of requesting code is to obtain the user's openid (the user's unique identity relative to the current official account) and access_token, the requested API: https://open.weixin.qq.com/connect/oauth3/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect.
There are several parameters to pay attention to in this api:
1. The appid of appid official account, which can be seen in the official account.
2. Redirect_uri custom Wechat callback address. Wechat will jump to your defined redirect_uri address with code after you have requested the above address. The redirect_url here requires * * url_encode** * php*,. If your program is node, you need to use * * encodeURLComponent (url) * * code.
3. Response_type=code, there is nothing to say but a fixed response_type=code. For more information, please see the description on Wechat's official website.
4. Scope=snsapi_userinfo, always write this way. For more information, please see the description on Wechat's official website.
5. State=STATE is always written in this way. For more information, please see the description on Wechat's official website.
6. Wechat_redirect is always written in this way. For more information, please see the description on Wechat's official website.
Ps: official website link:
two。 Obtain access_token,openid through code
The first step has already obtained the value of code, then you need to get the value of access_token,openid through code, the requested api
API https://api.weixin.qq.com/sns/oauth3/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
The parameters of api here are described:
1. Appid Wechat official account id, Wechat official account obtained at backend
2. The key of the official account of secret Wechat, which is obtained by the official account of Wechat.
3. Code. The first step is to get the code used.
4. Just fix the grant_type=authorization_code
three。 Call the interface through access_token
Access_token can do follow-up functions. You can refer to the official example:
Https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316518&lang=zh_CN
four。 The web end calls up the payment API.
Do you feel like you're almost done when you see this? as long as the web page calls the WeChat Pay function, it's done? No, almost.
Open the H5 web page in Wechat browser and execute JS to call up the payment. The input and output data format of the interface is JSON.
Note: WeixinJSBridge built-in objects are not valid in other browsers.
The sample code is as follows:
Function onBridgeReady () {WeixinJSBridge.invoke ('getBrandWCPayRequest', {"appId": "wx2421b1c4370ec43b", / / official account name, passed by the merchant to "timeStamp": "1395712654", / / timestamp Seconds since 1970: "nonceStr": "e61463f8efa94090b1f366cccfbbb444", / / Random string "package": "prepay_id=u802345jgfjsdfgsdg888", "signType": "MD5", / / Wechat signature method: "paySign": "70EA570631E4BB79628FBCA90534C63FF7FADD89" / / Wechat signature}, function (res) {if (res.err_msg = = "get_brand_wcpay_request:ok") {} / / use the above method to determine the return of the frontend Wechat team solemnly reminds: res.err_msg will return ok after the user pays successfully. But there is no guarantee that it is absolutely reliable. };} if (typeof WeixinJSBridge = = "undefined") {if (document.addEventListener) {document.addEventListener ('WeixinJSBridgeReady', onBridgeReady, false);} else if (document.attachEvent) {document.attachEvent (' WeixinJSBridgeReady', onBridgeReady); document.attachEvent ('onWeixinJSBridgeReady', onBridgeReady);}} else {onBridgeReady ();}
If you see the code above, you need to pass parameters if you want to call Wechat's payment function.
{"appId": "wx2421b1c4370ec43b", / / official account name, passed by merchant "timeStamp": "1395712654", / / timestamp, number of seconds since 1970 "nonceStr": "e61463f8efa94090b1f366cccfbbb444", / / random string "package": "prepay_id=u802345jgfjsdfgsdg888", "signType": "MD5", / / Wechat signature method: "paySign": "70EA570631E4BB79628FBCA90534C63FF7FADD89" / / Wechat signature}
Parameter description:
1. AppId / / official account name, introduced by merchants
2. TimeStamp / / timestamp, the number of seconds since 1970 requires special attention here. It needs to be in the timestamp format of the string, which means it must be in quotation marks.
3. NonceStr / / random 32-bit string, and then provide a method
4. SignType / / Wechat signature method: MD5
5. PaySign / / Wechat signed, and then said
6. * * package** / / this is the most important. Where did you get it? Let's go on.
Ps: official website API description
Https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
five。 Obtain package and prepay_id from Wechat's unified API for issuing orders.
Official api:
Https://api.mch.weixin.qq.com/pay/unifiedorder
There are a lot of request parameters, but some are not required. Here are the required parameters.
{appid: APPID, attach: ATTACH, body: BODY, mch_id: MCH_ID, nonce_str: NONCE_STR, notify_url: NOTIFY_URL,// callback address of Wechat after payment openid: OPENID, out_trade_no: OUT_TRADE_NO, / / new Date () .getTime (), / / order number spbill_create_ip: SPBILL_CREATE_IP, / / ip total_fee: TOTAL_FEE of the client, / / Price of the product It should be noted here that this price is divided into several, so it is usually yuan. You need to convert it to RMB's meta trade_type: 'JSAPI',}.
Wechat's unified API for issuing orders requires the transmission of xml data, and the data also needs to be signed, so first of all, sign the data.
Signature rules can be found in the signature rules given by Wechat (the signature method will be given later)
Official signature rules of Wechat:
Https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3
After generating the signature, you need to assemble the data into xml format:
Var body ='+''+ config.wxappid+''+''+ obj.attach+''+'+ obj.body+''+'+ config.mch_id+''+'+ obj.nonce_str+''+'+ obj.notify_url+'' +'+ obj.openid+''+''+ obj.out_trade_no+''+'+ obj.spbill_create_ip+''+'+ obj.total_fee+''+''+ obj.trade_type+' '+' + obj.sign+''+ / / signature is required here Otherwise, Wechat will fail when verifying the data.
The next step is to request api to get the value of prepay_id. Send the api under the above xml data request to Wechat. Wechat will put back the desired value after verifying that the data is all right.
Api: https://api.mch.weixin.qq.com/pay/unifiedorder
six。 After getting the prepay_id, can you directly call Wechat's payment in paragraph H6? The answer is no.
After getting the prepay_id, the parameters of the payment function of Wechat on H6 are as follows:
{"appId": "wx2421b1c4370ec43b", / / official account name, passed by merchant "timeStamp": "1395712654", / / timestamp, number of seconds since 1970 "nonceStr": "e61463f8efa94090b1f366cccfbbb444", / / random string "package": "prepay_id=u802345jgfjsdfgsdg888", "signType": "MD5", / / Wechat signature method:}
With such parameters, you also need to sign all the participating parameters. The signature rule is the same as above. After the signature is generated, you need to assign the signature parameter paySign to the H6 parameter of Wechat's payment function (that is, Wechat's signature does not participate in the signature generation).
The final parameter looks like this:
{"appId": "wx2421b1c4370ec43b", / / official account name, passed by merchant "timeStamp": "1395712654", / / timestamp, number of seconds since 1970 "nonceStr": "e61463f8efa94090b1f366cccfbbb444", / / random string "package": "prepay_id=u802345jgfjsdfgsdg888", "signType": "MD5", / / Wechat signature method: "paySign": "70EA570631E4BB79628FBCA90534C63FF7FADD89" / / Wechat signature}
If there is no problem with all your links, then after you get the parameters, you can call Wechat's payment function normally, which is no different from the original function. (it is estimated that your heart is also very happy now that there is no app can use the function of app, it is so magical).
seven。 Callback for payment completion
WeChat Pay will put back the value in WeChat Pay's callback function on the h6 page when he is finished.
Res.err_msg = = "get_brand_wcpay_request:ok", that's a success, but isn't it done? Not really. Why? Did Wechat really get the money? Is the money received the value you passed to Wechat? You also need to write the results of the payment to the database and so on, which are unknown. Also remember that there is a necessary parameter in the unified order issuing interface: notify_url: the callback address of NOTIFY_URL,// Wechat after payment is passed to Wechat by the user. Wechat will request this interface in the form of post after receiving the user's payment, and Wechat will transmit the user's payment information, but it is in xml format.
Class is in this xml format:
one
It is fine to parse the data in xml format according to your own business logic.
Note: here you need your response from Wechat after you get the data. If you do not respond to Wechat, Wechat will ask you for several times, so it is estimated that there will be something wrong with your logic, so you need to return a response in the format of xml to Wechat.
Xiaokeng: node, express framework development, if you do not get any xml value in the callback after Wechat's successful payment, then you need to install a component: body-parser-xml, you can use npm install body-parser-xml-- save installation, require ('body-parser-xml') (bodyParser) in app.js; use middleware
/ / resolve the callback data app.use of WeChat Pay notification (bodyParser.xml ({limit: '2MBmarker, / / Reject payload bigger than 1 MB xmlParseOptions: {normalize: true, / / Trim whitespace inside text nodes normalizeTags: true, / / Transform tags to lowercase explicitArray: false / / Only put nodes in array if > 1}}))
In this way, you can get the xml data of Wechat normally.
How to use it:
Pay.getAccessToken ({notify_url: 'http://demo.com/', / / callback out_trade_no: new Date (). GetTime ()), / / order number attach:' name', body: 'purchase Information', total_fee: '1quota, / / the quota here is spbill_create_ip: req.connection.remoteAddress,}, function (error, responseData) {res.render (' payment') {title: 'WeChat Pay', wxPayParams: JSON.stringify (responseData), / / userInfo: userInfo}) }); Thank you for reading this article carefully. I hope the article "sample Analysis of the Development of nodejs Wechat official account" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.