Realization of payment function under the first-line of Java wechat code scanning payment mode

1、 Preparatory work

Countless people have come to ask about the development of mode 1, so they post it here for reference only. I've explained the difference between mode 1 and mode 2 many times. It's just that the QR code of mode 1 is for goods and the QR code of mode 2 is for orders. I don't need to talk about other specific details. You can go to the official document and choose mode 1 or mode 2 according to your business.

1.1. Relevant configuration parameters

Same as before, app_ ID and app_ Secret can be found on the public platform, MCH_ ID and API_ Key is found on the merchant platform, especially API_ The key should be set on the merchant platform. This is related to the correctness of parameter verification, so it must be set correctly. Code scanning payment mode 1 is actually similar to code scanning payment mode 2. In fact, only app is used_ ID、MCH_ ID and API_ Key, nothing else. The official document address of mode 1 is here: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4

1.2 relevant concepts

Here, I want to revise a concept first. In the previous development process of mode 2, I mentioned a concept of "payment callback address". Its function is actually that after the customer completes the scanning and payment, the wechat server needs to access the address provided by us and send us the payment results so that we can verify the order and deliver the goods, This is a common concept and name for other payment instruments. But then I looked through the documents on the wechat official website and found that in the development of mode 1, they called this "asynchronous notification URL" rather than "payment callback address", but in essence, this refers to one meaning. But why should I mention this here? This is because in mode 1, there is actually another so-called "payment callback" called "code scanning payment callback URL", which is different from the above "asynchronous notification URL". It can be considered as an interface on our server to assist in the completion of orders. The development of mode 1 can only be completed through the cooperation of "code scanning payment callback URL" and "asynchronous notification URL", so we should identify it here.

"Asynchronous notification URL" is set when calling the unified ordering interface, which can be set dynamically, as long as the interface receives parameters and responds to parameters according to relevant rules. The "code scanning payment callback URL" is relatively fixed. It is set on the wechat public platform and takes about 10 minutes to take effect. After you log in to the wechat public platform, select wechat payment and you can find it under the development configuration tab:

Here we need to set the address of our own server (again, the public network address is so that the wechat server can find you).

1.3 development environment

I take the most basic servlet 3.0 as an example environment. As for the reference of third-party jar packages, compared with the development of mode 2, in addition to the JDOM using XML operation, there is a two-dimensional code package and log4j package of Google zxing. As shown below:

2、 Development practice

Before you start, I suggest you go to the official document to have a good look at the sequence diagram. If you understand the sequence diagram, it's not difficult to write code. Of course, if you can't understand it by looking at the diagram, you can also try to understand it in combination with my following code.

2.1 QR code generation

The first is the QR code. The content in the QR code is a link in the form of:

weixin://wxpay/bizpayurl? sign=XXXXX&appid=XXXXX&mch_ id=XXXXX&product_ id=XXXXXX&time_ stamp=XXXXXX&nonce_ str=XXXXX

For details, please refer to the official document mode to generate QR code rules. Next, we need to generate the QR code from the link. I use Google zxing to generate the QR code here.

2.2. Scan payment callback URL interface

After the customer scans the above two digit code with wechat, the wechat server will access this interface. Here, we need to place a unified order and obtain the transaction session ID. the main process is as follows:

1) Receive the parameters sent by wechat server and verify the signature of the parameters;

2) . take out the parameter product_ ID, which is the only parameter that can be transmitted through the QR code. Other parameters can be entered by referring to official document mode 1 3.1;

3) . according to product_ ID handles its own business, such as calculating payment amount, generating order number, etc;

4) . call the unified ordering interface to obtain the transaction session ID prepay_ id;

4.1)、 Prepare relevant parameters (such as appid, mch_id, payment amount, order number, product description, etc.) and call wechat unified order interface (similar to the unified order interface called in mode 2). Note that the above-mentioned "asynchronous notification URL", that is, the asynchronous notification URL interface mentioned later, should be added here. For specific parameters, refer to the official document unified order request parameters;

4.2) receive the parameters returned from the unified ordering interface and check and sign the parameters;

4.3) take out the parameter prepay_ ID, which is the transaction session ID and is extremely important. For other parameters, please refer to the official documents to uniformly place orders and return results;

5) Prepare relevant parameters (such as appid, mch_id, return_code, pay_id, etc.) and respond to the initial payment callback (if the above steps are wrong, such as signature verification failure, you can return the wrong parameters to the wechat server). For specific parameters, please refer to the output parameters of official document mode 1 3.2.

At this point, the user's wechat list will display the amount to be paid and product description, and then wait for the customer to complete the payment.

2.3. Asynchronous notification URL interface

As like as two peas, the user will complete the payment operation on WeChat, and the WeChat server will notify the interface asynchronously and send us the final payment result so that we can verify the order and delivery. We notice that the interface and mode two are identical. The general process is as follows:

1) Receive the parameters sent by wechat server and verify the signature of the parameters;

2) . take out the parameter result_ Code, order No. out_ Trade_ No. order amount total_ Fee and other business-related parameters. For specific parameters, please refer to the notification parameters of the general notification of payment results in the official document;

3) . process business, such as verifying order number and order amount, modifying order status, etc;

4) Prepare the relevant parameters (return_code and return_msg) and answer the wechat server.

Note that if wechat does not receive the merchant's response successfully or timeout, wechat considers the notification failed, and wechat will regularly re initiate the notification through certain strategies to improve the success rate of the notification as much as possible, but wechat does not guarantee the final success of the notification. (the notification frequency is 15 / 15 / 30 / 180 / 1800 / 1800 / 1800 / 3600, in seconds)

3、 Test results

3.1. Generated payment QR code link

3.4. Unified order return parameters

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>