


How to implement the WeChat code scanning payment function in nodejs environment?
This article uses code examples to explain in detail how to use nodejs to write the WeChat scan code payment function. Interested friends can refer to it.
Preface
This article mainly records the problems I encountered during the WeChat scan code payment process, and provides a reference for everyone. I hope it will be helpful to you
Development environment
nodejs v8.1.0
egg v1.1.0
Preparation
WeChat public account-appid
WeChat merchant account-mch_id
key value (required by the signature algorithm, it is actually a 32-bit password, you can use md5 to generate one) (key Setting path: WeChat Merchant Platform (pay.weixin.qq.com)-->Account Settings-->API Security-->Key Settings)
Scan QR code to pay-unified order
The following is WeChat mode 2, because it is relatively simple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The above are some parameters we need
For the signature generation algorithm, see WeChat official: https://pay. weixin.qq.com/wiki/doc/api/native.php?chapter=4_3
spbill_create_ip is the terminal ip address
Now splice all the parameters into xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
The egg request method is used above. The native node can use request
1 2 3 4 5 6 7 8 9 |
|
If the request is successful, an xml will eventually be returned, and then we will parse it into json format, which will have a code_url and out_trade_no. We need to These two are returned to the front end, and then the QR code is generated and displayed to the user to scan the code to complete the payment
Monitor whether the payment is successful
After the above operation is completed, we need To know whether the user has completed the payment, because the user will stay on this page, we need to notify the user that the payment is successful after the user completes the payment.
First of all, when the user initiates payment, we will generate a QR code to allow the user to scan the code to pay. What we also need to do is to set up a timer and send a request every once in a while. At this time , our node background needs to write an interface for querying orders. Previously we got out_trade_no, which is the order number inside our system. We send this data to the background interface for querying orders, and then after receiving it, the background will request WeChat query. The interface address https://api.mch.weixin.qq.com/pay/orderquery, the process is the same as above, except that the interface address is different from the xml returned by WeChat. The returned fields will have a status of SUCCESS and NOTPAY. We You can return it to the front end by judging whether to pay. After success, it will prompt the user that the payment is successful and close the timer.
Callback address
This is a very important part. Most operations can actually be completed on it, but there are special circumstances, such as the user's computer being disconnected from the Internet. The request cannot be sent, but the mobile payment is made, which will cause us to not record the user's payment information. At this time, the callback address is very important
Set the callback address
WeChat Merchant Center->Product Center->Development Configuration->Scan QR code to pay
What we need to do after that is to use post on the backend to receive the asynchronous callback information sent by WeChat, which is also in xml format. Note here that if receiving xml is not supported, you may get empty data
here It should also be noted that while saving the user's payment information, we must first check whether the order is paid to avoid repeated operations and the possibility of inserting multiple records
Summary
There are still pitfalls in WeChat scan code payment. If this is your first time, here is a list of things you need to pay attention to
The signature algorithm must be written correctly, otherwise it will not work. It will succeed, the splicing must be correct
WeChat returns data in xml format, we have to convert it into json through the plug-in, so that it is convenient to obtain the data
The returned code_url is used to generate a QR code for the front end, and then a timer needs to be set up to check whether the order has been paid, and finally the user is notified of the result
The callback address is very important , our backend needs to post to receive the callback information returned by WeChat, and then save the information. However, before saving the user's payment information, we need to know whether the order has been saved to avoid repeated additions. Also, the returned XML data must be ensured by the backend to be able to receive it. It cannot be received in the normal way and requires additional settings.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Data grouping example of v-for in Vue
vue2.0 computed calculates the accumulated value after the list loop Example
vue.js Example of nested loop, if judgment, dynamic deletion
The above is the detailed content of How to implement the WeChat code scanning payment function in nodejs environment?. For more information, please follow other related articles on 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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.
