java--springboot wechat payment refund server implementation and certificate configuration (including code)

Last time I wrote an article about wechat payment. I was asked by a kid's shoe how to write a refund. The refund is much easier than the payment. I don't need to talk much nonsense. I'll post the code.

First, in the controller:

 public String refund(
    ) throws Exception {

                Map<String, String> data = new HashMap<String, String>();
                System.err.println("Enter wechat refund application");
                Date now = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//Date format can be easily modified
                String hehe = dateFormat.format(now);

                String out_refund_no = hehe + "wxrefund";
                String transaction_id = "Serial number generated during wechat payment";
                String total_fee = "Wechat payment (unit: minute)";

                data.put("out_refund_no", out_refund_no);
                data.put("transaction_id", transaction_id);
                data.put("total_fee", total_fee);
                data.put("refund_fee", total_fee);

                String result = wxPayService.refund(data);
                //Judge whether the refund is successful
                if (result.equals("\"Refund application successful\"")) {
                    return  "Refunded ";
                return "Wechat refund failed";

For the specific business logic of your own, I only post the one related to wechat refund.

The wxPayService method is called:

     * Apply for refund
     * @param data Including merchant order number, merchant refund number, order amount and refund amount
     * @return
    public String refund(Map<String, String> data) throws Exception {
        WXMyConfigUtil config = new WXMyConfigUtil();
        WXPay wxpay = new WXPay(config);
        data.put("appid", config.getAppID());
        data.put("mch_id", config.getMchID());
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        data.put("sign", md5Util.getSign(data));

        Map<String, String> resp = null;
        try {
            resp = wxpay.refund(data);
        } catch (Exception e) {
        String return_code = resp.get("return_code");   //Return status code
        String return_msg = resp.get("return_msg");     //Return information

        String resultReturn = null;
        if ("SUCCESS".equals(return_code)) {
            String result_code = resp.get("result_code");       //Business results
            String err_code_des = resp.get("err_code_des");     //Error code description
            if ("SUCCESS".equals(result_code)) {
                //Indicates that the refund application has been accepted successfully. The result is queried through the refund query interface
                //Modifying user's order status to refund application (not written for now)
                resultReturn = "Refund application successful";
            } else {
      "order number:{}error message:{}", err_code_des);
                resultReturn = err_code_des;
        } else {
  "order number:{}error message:{}", return_msg);
            resultReturn = return_msg;
        return JSON.toJSONString(resultReturn);

How to import packages and configure pom files, I wrote in the last blog. This involves the configuration of the config file.

This is a very pitiful point of wechat. Its demo doesn't tell you how to configure it. I still put the code directly:

package com.kyd.callcenter.util.weixin;

import com.github.wxpay.sdk.WXPayConfig;


public class WXMyConfigUtil implements WXPayConfig {
    private byte[] certData;

    public WXMyConfigUtil() throws Exception {

        String certPath = "apiclient_cert.p12";//Directory of security certificates downloaded from wechat merchant platform

        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];;

    public String getAppID() {
        return "Your wechat account appid";

    public String getMchID() {
        return "Merchants of your wechat account id";

    public String getKey() {
        return "Your wechat account api secret key";

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;

    public int getHttpConnectTimeoutMs() {
        return 8000;

    public int getHttpReadTimeoutMs() {
        return 10000;

If there are any unclear partners, please let me check them from time to time.

Tags: Java JSON github SDK

Posted on Mon, 04 May 2020 12:13:54 -0400 by tinkertron