微信查询支付情况和退款的工具类

1.demo

package com.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
import com.github.wxpay.sdk.WXPayUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.KeyManagerFactory;
import java.io.*;
import java.security.KeyStore;
import java.security.Security;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

// 工具的依赖需用到微信支付官方给的工具类和hutool
public class WxPayUtil {
     
    private static final Logger logger = LoggerFactory.getLogger(WxPayUtil.class);

    public static final String REFUND_URL = "https://api.mch.weixin.qq.com/secapi/pay/refund";
    public static final String SEARCH_ORDER_URL = "https://api.mch.weixin.qq.com/pay/orderquery";

    // ----- 商户信息 开始----
    //小程序appid  开发者自己拥有的
    public static final String APPID = "appid";
    //微信支付的商户id   开发者去问自己id的前端同事或者领导。
    public static final String MCH_ID = "mch_id";
    //微信支付的商户密钥  开发者问领导,去微信商户平台去申请(要下插件等等)
    public static final String KEY = "key";
    // ----- 商户信息 开始----

    /**
     * 微信退款接口
     */
    public static Map<String, Object> refund(RefundPojo refundPojo) throws Exception {
     
        Map<String, Object> refundParams = new TreeMap<>();
        refundParams.put("appid", refundPojo.getAppid());
        refundParams.put("mch_id", refundPojo.getMchId());
        refundParams.put("notify_url", refundPojo.getNotifyUrl());
        refundParams.put("nonce_str", RandomUtil.randomString(32).toUpperCase());
        refundParams.put("out_trade_no", refundPojo.getOrderSn());
        refundParams.put("out_refund_no", RandomUtil.randomString(32));
        // 订单总金额,单位为分,只能为整数
        refundParams.put("total_fee", "1");
        refundParams.put("refund_fee", "1");
        Map<String, Object> sign = sortParams(refundParams, "sign", refundPojo.getKey());
        String xml = XmlUtil.mapToXmlStr(sign, "xml");

        logger.info("refund orderSn:[{}],xml:[{}]", refundPojo.getOrderSn(), xml);

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        File file = new File(refundPojo.getApiclientCertUrl());
        InputStream inputStream = new FileInputStream(file);
        keyStore.load(inputStream, refundPojo.getMchId().toCharArray());
        // 初始化密钥库
        KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        factory.init(keyStore, refundPojo.getMchId().toCharArray());
        HttpRequest request = HttpRequest.post(REFUND_URL)
                // 自定义返回编码
                .charset(CharsetUtil.CHARSET_UTF_8)
                // 禁用缓存
                .disableCache()
                .setSSLSocketFactory(SSLSocketFactoryBuilder.create()
                        .setKeyManagers(factory.getKeyManagers())
                        .setProtocol(SSLSocketFactoryBuilder.TLSv1).build());
        HttpResponse response = request.body(xml).execute();
        Map<String, Object> resultMap = XmlUtil.xmlToMap(response.body());
        logger.info("refund orderSn:[{}],resultMap:[{}]", refundPojo.getOrderSn(), resultMap);
        return resultMap;
    }

    /**
     * 微信退款接口
     * 无需填写商户信息
     */
    public static Map<String, Object> refund(RefundSimplePojo refundSimplePojo) throws Exception {
     
        RefundPojo refundPojo = new RefundPojo();
        refundPojo.setAppid(APPID);
        refundPojo.setMchId(MCH_ID);
        refundPojo.setKey(KEY);
        refundPojo.setOrderSn(refundSimplePojo.getOrderSn());
        refundPojo.setApiclientCertUrl(refundSimplePojo.getApiclientCertUrl());
        refundPojo.setNotifyUrl(refundSimplePojo.getNotifyUrl());
        Map<String, Object> resultMap = refund(refundPojo);
        return resultMap;
    }

    /**
     * 查询支付情况
     */
    public static Map<String, Object> searchOrder(String orderId) throws Exception {
     
        //1.封装参数
        Map param = new HashMap();
        param.put("appid", APPID);
        param.put("mch_id", MCH_ID);
        param.put("out_trade_no", orderId);
        param.put("nonce_str", RandomUtil.randomString(32));
        String xmlParam = WXPayUtil.generateSignedXml(param, KEY);
        //2.发送请求
        String xmlResult = HttpUtil.post(SEARCH_ORDER_URL, xmlParam);
        Map<String, Object> resultMap = XmlUtil.xmlToMap(xmlResult);
        System.out.println("调动查询API返回结果:" + resultMap);
        return resultMap;
    }


    public static Map<String, Object> sortParams(Map<String, Object> params, String signName, String key) {
     
        // 签名
        params = MapUtil.sort(params);
        String sign = MD5.create().digestHex(URLUtil.decode(HttpUtil.toParams(params) + "&key=" + key)).toUpperCase();
        params.put(signName, sign);
        return params;
    }

    public static String signWxpay(Map params) {
     
        Object[] keys = params.keySet().toArray();
        Arrays.sort(keys);
        String k, v;
        String str = "";

        for (int i = 0; i < keys.length; i++) {
     
            k = (String) keys[i];
            if (k.equals("sign")) {
     
                continue;
            }
            if (params.get(k) == null) {
     
                continue;
            }
            v = (String) params.get(k);
            if (v.equals("0") || v.equals("")) {
     
                continue;
            }
            str += k + "=" + v + "&";
        }
        String appSecret = KEY;
        str += "key=" + appSecret;
        String md5Str = MD5Util.MD5(str).toUpperCase();
        return md5Str;
    }

    public static String responseFailedWxpay(String msg) {
     
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("xml");
        root.addElement("return_code").addText("FAIL");
        root.addElement("return_msg").addText(msg + "");
        String result = doc.asXML();
        System.out.println(result);
        return result;
    }

    public static String responseSuccessWxpay() {
     
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("xml");
        root.addElement("return_code").addText("SUCCESS");
        root.addElement("return_msg").addText("OK");
        String result = doc.asXML();
        return result;
    }

    /**
     * 微信退款支付回调解密req_info
     */
    public static Map<String, Object> AESDecode(String reqInfo) throws Exception {
     
        byte[] digest = MD5.create().digestHex(WxPayUtil.KEY).toLowerCase().getBytes();
        SecretKeySpec key = new SecretKeySpec(digest, "AES");
        byte[] decode = Base64.decode(reqInfo);
        Security.addProvider(new BouncyCastleProvider());
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(decode);
        String s = new String(bytes);
        Map<String, Object> stringObjectMap = XmlUtil.xmlToMap(s);
        return stringObjectMap;
    }

    @Getter
    @Setter
    @ToString
    public static class RefundSimplePojo {
     
        private String orderSn;
        private String apiclientCertUrl;
        private String notifyUrl;
    }

    @Getter
    @Setter
    @ToString
    public static class RefundPojo extends RefundSimplePojo {
     
        private String appid;
        private String mchId;
        private String key;
    }
}

2.退款需注意
微信退款操作需替换jdk的加密包,教程如下:
https://blog.csdn.net/qq_34793634/article/details/86533035

3.文章参考链接
a. https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4
b. https://blog.csdn.net/qq_34793634/article/details/86533035
c. https://www.jianshu.com/p/6d3259e88b80
d. https://hutool.cn/docs/#/

你可能感兴趣的:(工具类,java)