If there are any improper articles, please correct them. If you like reading on wechat, you can also follow my wechat official account: Learn Java well and get quality learning resources.

I. wechat official documents wechat Payment development process (payment by official account)

First of all, let’s go to the development steps section of wechat Pay official document to check the Settings required. [image upload failed…(image-5eb825-1531014079742)]

Because wechat Pay requires a high level of authority, only the authenticated service number can be granted the permission to use wechat pay interface. It is difficult for us to apply for it personally, so we need to borrow the account from other friends.

Go to the business process section of the document and check the process of wechat Payment (I think this still needs to be very carefully understood and viewed, which helps you understand the process of wechat development).

Then, access to wechat pay interface is to transfer a lot of parameters, see unified order [image-df7051-1531014079742]

After checking the above official documents of wechat Payment, I believe you have a certain understanding of these, but still feel that the development of wechat payment is very troublesome, so we will use the SDK of a third party to develop.

Ii. Third-party SDK development of wechat Pay (payment by public account)

This is the public account payment, we use ** best-pay-SDK **, this SDK uses PayRequest and PayResponse to make a lot of encapsulation of the request interface and corresponding results, the main parameters that need to be dynamically passed in are OpenID (user unique identifier) and orderId. Now let’s look at how to develop.

1, configuration,

WxPayH5Config WxPayH5Config = new WxPayH5Config(); wxPayH5Config.setAppId("xxxxx");
    wxPayH5Config.setAppSecret("xxxxxxxx");
    wxPayH5Config.setMchId("xxxxxx");
    wxPayH5Config.setMchKey("xxxxxxx");
    wxPayH5Config.setNotifyUrl("http://xxxxx"); BestPayServiceImpl bestPayService = new BestPayServiceImpl(); bestPayService.setWxPayH5Config(wxPayH5Config);Copy the code

2. Initiate payment

  PayRequest payRequest = new PayRequest();
     payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
     payRequest.setOrderId("123456");
     payRequest.setOrderName("Order payment by wechat public Account"); PayRequest. SetOrderAmount (0.01); payRequest.setOpenid("openid_xxxxxx");
     bestPayService.pay(payRequest);
Copy the code

3. Asynchronous callback

  bestPayService.asyncNotify();
Copy the code

That’s what this SDK says about solving wechat Pay in 10 lines of code.

After the payment is completed, wechat will return the payment result to us as a piece of payment XML data. We need to send this data to notify_URL to complete the verification of the payment result (verify signature and verify payment status). The SDK has done both steps for us. Just so call bestPayService. AsyncNotify (notifyData); After verification, we need to return wechat with the following data:

<xml> <return_code><! [CDATA[SUCCESS]]></return_code> <return_msg><! [CDATA[OK]]></return_msg> </xml>Copy the code

Tell wechat to complete the verification and stop sending us asynchronous notification requests.

Are you still not quite sure how to integrate into the project? It doesn’t matter, there is an example of this, which can be made clearer.

3. Demo operation

The demo website is https://github.com/Pay-Group/best-pay-demo

Our main controller is here:

@Controller @Slf4j public 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(); // Request. SetPayTypeEnum (BestPayTypeEnum.WXPAY_H5); request.setOrderId(String.valueOf(random.nextInt(1000000000))); Request. SetOrderAmount (0.01); request.setOrderName("The best payment SDK");
        request.setOpenid(openid);
        log.info(Request ={}", JsonUtil.toJson(request));

        PayResponse payResponse = bestPayService.pay(request);
        log.info("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(Request ={}", notifyData);
        PayResponse response = bestPayService.asyncNotify(notifyData);
        log.info("Response ={}", JsonUtil.toJson(response));

        return new ModelAndView("pay/success"); }}Copy the code

This can be downloaded by yourself. Let’s see how it works

Project description

This project is developed using SpringBoot1.5.1 and needs to run on Jdk version >1.8

The project structure

SRC/main/Java/com/lot/lly835 ├ ─ ─ PayDemoApplication. Java ├ ─ ─ ServletInitializer. Java ├ ─ ─ the config │ └ ─ ─ PayConfig. Java / / key configuration class └ ─ ─ controller └ ─ ─ PayController. Java / / pay for callsCopy the code

Run the example

Before running the command, configure the key. For details, see payconfig. Java

git clone https://github.com/Pay-Group/best-pay-demo
cd best-pay-demo
mvn clean package
java -jar target/*.war
Copy the code

Browser to access http://127.0.0.1:8080/pay