1. WeChat official document WeChat payment development process (public account payment)
First, let’s go to the development steps section of WeChat Pay’s official documentation to check the required settings.
[Image upload failed...(image-5eb825-1531014079742)]
Because WeChat payment requires high permissions, only certified service accounts can have the permission to use the WeChat payment interface. It is difficult for us to apply for it personally, so we need to borrow an account from other friends.
Come to the business process part of the document and check the WeChat payment process (I think this still requires a very careful understanding and viewing, which will help you understand the WeChat development process).
Then, accessing the WeChat payment interface requires many parameters to be passed. See the unified single
[Image upload failed...(image-df7051-1531014079742)]
After checking the official WeChat payment documents above, I believe you should have a certain understanding of these, but I still think that the development of WeChat payment is very troublesome, so we will use third-party SDK to develop.
2. WeChat Pay third-party SDK development (public account payment)
This is a payment for the official account. We use best-pay-sdk. This SDK uses PayRequest and PayResponse to encapsulate the request interface and corresponding results. The main parameters that need to be dynamically passed are openid (user unique identifier) and orderId. Next, let’s take a look at how to develop.
1. Configuration
//WeChat public account payment configuration WxPayH5Config wxPayH5Config = new WxPayH5Config(); wxPayH5Config.setAppId("xxxxx"); wxPayH5Config.setAppSecret("xxxxxxx"); wxPayH5Config.setMchId("xxxxxx"); wxPayH5Config.setMchKey("xxxxxxx"); wxPayH5Config.setNotifyUrl("http://xxxxx"); //Pay class, all methods are in this class BestPayServiceImpl bestPayService = new BestPayServiceImpl(); bestPayService.setWxPayH5Config(wxPayH5Config);2. Initiate payment
PayRequest payRequest = new PayRequest(); payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5); payRequest.setOrderId("123456"); payRequest.setOrderName("WeChat Public Account Payment Order"); payRequest.setOrderAmount(0.01); payRequest.setOpenid("openid_xxxxxx"); bestPayService.pay(payRequest);3. Asynchronous callback
bestPayService.asyncNotify();
This is what this SDK says 10 lines of code to solve WeChat payment.
After payment is completed, WeChat will return the payment result to us as a piece of payment xml data. We need to pass this data to the asynchronous notification url (notify_url) to complete the verification of the payment result (verify signature, verify payment status). Both steps of the SDK are done for us. Just call bestPayService.asyncNotify(notifyData);, after completing verification, we need to return a piece of data to WeChat:
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg></xml>
Tell WeChat that verification has been completed and do not send us any requests for asynchronous notifications.
Is it still not clear how to integrate into the project? It doesn't matter, there is also an example demo, which can be more clear.
3. Demo run
Demo URL is: https://github.com/Pay-Group/best-pay-demo
Our main controller is here:
@Controller@Slf4jpublic class PayController { @Autowired private BestPayServiceImpl bestPayService; /** * Initiate payment*/ @GetMapping(value = "/pay") public ModelAndView pay(@RequestParam("openid") String openid, Map<String, Object> map) { PayRequest request = new PayRequest(); Random random = new Random(); //Pay request parameter request.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5); request.setOrderId(String.valueOf(random.nextInt(1000000000))); request.setOrderAmount(0.01); request.setOrderName("Best Payment sdk"); request.setOpenid(openid); log.info("【Initiate Payment】request={}", JsonUtil.toJson(request)); PayResponse payResponse = bestPayService.pay(request); log.info("【Initiate payment】response={}", JsonUtil.toJson(payResponse)); map.put("payResponse", payResponse); return new ModelAndView("pay/create", map); } /** * Asynchronous callback*/ @PostMapping(value = "/notify") public ModelAndView notify(@RequestBody String notifyData) throws Exception { log.info("【Async callback】request={}", notifyData); PayResponse response = bestPayService.asyncNotify(notifyData); log.info("【Async callback】response={}", JsonUtil.toJson(response)); return new ModelAndView("pay/success"); }}You can download this by yourself. Let's see how it works
Project description
Need to run on Jdk version > 1.8
This project is developed using SpringBoot 1.5.1
Project structure
src/main/java/com/github/lly835├── PayDemoApplication.java ├── ServletInitializer.java ├── config│ └── PayConfig.java //Payment key configuration class └── controller └── PayController.java //Payment call
Running example
You need to configure the key before running, see PayConfig.java
Run the command
git clone https://github.com/Pay-Group/best-pay-democd best-pay-demomvn clean packagejava -jar target/*.war
Browser access http://127.0.0.1:8080/pay
Summarize
The above is the entire process of payment development of the Java version of WeChat public account introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time!