Common mistakes are strongly recommended to read again to avoid trample pits to spend money in vain

1. This ID has not completed wechat authentication, please confirm and try again to complete appID authentication, wechat service number is enterprise authentication, open platform is developer authentication. 2. The APPID authentication body name you entered is inconsistent with the actual authentication body name. Please check and modify it and try again to check whether the authentication body of open platform or public platform is consistent with the merchant number body. 3. Wechat Native Payment (PC website) cannot use appID of open platform. The appID of wechat Native payment must be the wechat service account, that is, the public account of the enterprise whose authentication subject is the enterprise. The appID of wechat Native payment must be the wechat service account, that is, the public account of the enterprise whose authentication subject is the enterprise. The appID of wechat Native payment must be the wechat service account, that is, the public account of the enterprise whose authentication subject is the enterprise. 4. Modify the name of the payee as suggested in the payment. The name of the payee shall modify the abbreviation of the merchant in the merchant number

Wechat Native payment application process

1. Apply for wechat merchant number; 2. Apply for wechat enterprise service number and obtain appID through authentication; 3

Wechat Native payment access

IJPay is recommended for Java development. 1

<dependency>
    <groupId>com.github.javen205</groupId>
    <artifactId>IJPay-WxPay</artifactId>
    <version>2.7.3</version>
</dependency>
Copy the code

2. Create the required parameter classes for the request. The configuration information is in application

public class WxPayConfig {
    /** * Set the wechat public number or small program appID. */
    @Value("${wx.pay.appId}")
    private String appId;

    /** * wechat Pay merchant id. */
    @Value("${wx.pay.mchId}")
    private String mchId;

    /** * wechat Pay merchant key. */
    @Value("${wx.pay.mchKey}")
    private String mchKey;

    /** * asynchronous callback address */
    @Value("${wx.pay.notifyUrl}")
    private String notifyUrl;
}
Copy the code

3. Configure information application.properties

Wxpay. appId= appId of the applied service ID Wxpay. appSecret= appSecret of the applied service ID Wxpay. McHId = Merchant ID wxPay. partnerKey= partnerKey of the merchant id Wxpay. certPath= Merchant id P12 certificate wxpay.domain= URL of asynchronous verification notification after scanning paymentCopy the code

4. Place unified orders

  Map<String, String> params = UnifiedOrderModel
                .builder()
                .appid(wxPayConfig.appId)
                .mch_id(wxPayConfig.mchId)
                .nonce_str(WxPayKit.generateStr())
                .body("Zhao Tangyuan payment")
                .attach("Zhao Tangyuan pay stomp pit")
                .out_trade_no(createOrderRequestDto.getId())
                .total_fee(total_fee)
                .spbill_create_ip(ip)
                .notify_url(wxPayConfig.domain)
                .trade_type(TradeType.NATIVE.getTradeType())
                .build()
                .createSign(wxPayConfig.partnerKey, SignType.HMACSHA256);
        String xmlResult = WxPayApi.pushOrder(false, params);
        Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
        String returnCode = result.get("return_code");
        String returnMsg = result.get("return_msg");
        System.out.println(returnMsg);
        if(! WxPayKit.codeIsOk(returnCode)) {// Replace the exception status judgment with your own
            throw new ServiceException(ErrorTypeEnum.RECHARGE_ERROR);
        }
        String resultCode = result.get("result_code");
        if(! WxPayKit.codeIsOk(resultCode)) {// Replace the exception status judgment with your own
            throw new ServiceException(ErrorTypeEnum.RECHARGE_ERROR);
        }
        // Generate the prepaid order SUCCESS
        String qrCodeUrl = result.get("code_url");
        try {
            // Generate the qr code of the order and return it to the client for display
						ByteArrayOutputStream out = new ByteArrayOutputStream();
            QrCodeKit.encodeOutPutSteam(out, qrCodeUrl, BarcodeFormat.QR_CODE,
                    3, ErrorCorrectionLevel.H, "png".200.200);
            BASE64Encoder encoder = new BASE64Encoder();
            imgBase64Str = encoder.encode(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }

        returnimgBase64Str; ` ` `5Java String xmlMsg = httpKit. readData(request); logger.info("Notice of payment =" + xmlMsg);
        Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);

        String returnCode = params.get("return_code");

        // Pay attention to the situation of repeated notifications, the same order number may receive multiple notifications, please be careful to judge the order status first
        // Notice The signature type must be the same as that of the unified order
        if (WxPayKit.verifyNotify(params, wxPayConfig.partnerKey, SignType.HMACSHA256)) {
            if (WxPayKit.codeIsOk(returnCode)) {
                // Update order information
                // Send notifications, etc
                String out_trade_no = params.get("out_trade_no");
                String total_fee = params.get("total_fee");
                Map<String, String> xml = new HashMap<String, String>(2);
                xml.put("return_code"."SUCCESS");
                xml.put("return_msg"."OK");
                returnWxPayKit.toXml(xml); }}Copy the code

Original is not easy to reprint please indicate the source

If this article has helped you, please buy the author a cup of coffee