Implementation and callback of second-line payment function in Java wechat code scanning payment mode

1、 Preparatory work

First, WeChat make complaints about the payment system itself, but its official documents are fragmented, and even the decent Java related demo is few. I have never engaged in wechat payment before. I was really confused by it at the beginning. After two days of tossing and turning, I finally got through. I hereby write it down to enjoy future generations!

For the preparatory work, the official document address of "wechat code scanning payment mode 2" is here https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1 You can take a look first. In fact, the following things need to be prepared:

Including 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. For "wechat code scanning payment mode 2" (payment and callback), only app_id, mch_id and api_key will be used, and others will not be used.

I won't be wordy about the development environment. Whether you are spring MVC, struts 2 or direct serverlet, it's almost the same, as long as you can ensure that the corresponding methods can be called. As for the reference of third-party jar packages, I only use a JDOM for XML operation here. Remember that it is 1* The version of is not the latest 2. * on the official website, The two are incompatible. Jdom-1.1 3. Jar, dependent package jaxen-1.1 6. Jar, for these two packages, I didn't use the httpclient used in some examples. I feel it's unnecessary, and relying on the package is particularly complicated. Of course, you're Maven when I didn't say.

2、 Development practice

1. First, access the wechat interface to obtain the wechat payment QR code.

If nothing unexpected happens, a payment URL is obtained from the wechat server, as shown in weixin://wxpay/bizpayurl?pr=pIxXXXX After that, we need to generate a QR code from this URL, and then we can use our mobile wechat terminal to scan the code for payment. There are many methods for QR code generation. Let's take what you need. Here I provide a Google QR code generation interface:

The above code involves several tool classes: payconfigutil, paycommonutil, httputil and xmlutil. Payconfigutil puts some configurations and paths mentioned above. Paycommonutil involves several methods of obtaining current events, generating random strings, obtaining parameter signatures and splicing XML. The code is as follows:

Httputil and xmlutil are as follows:

Of course, there is also an MD5 computing tool class

2. Payment callback

After the payment is completed, wechat will send the relevant payment results and user information to the callback address specified above. We need to receive the processing and return the response. When interacting with the background notification, if wechat receives the merchant's response that is not successful 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)

For the payment callback interface, we first need to verify the signature of the payment result notification, and then carry out the corresponding processing flow according to the payment result.

The signature verification algorithm is similar to the signature generation algorithm, which is provided in the paycommonutil tool class above. 3、 Later words

I think the effect of wechat scanning payment experience is still very good. The only disadvantage is that the relevant documents are scattered. The official demo is not written in Java. I hope wechat officials can gradually improve it in the future!

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
分享
二维码
< <上一篇
下一篇>>