微信退款

1.证书

在这里插入图片描述windows点击第一个进行安装

代码开始

签名相关PayUtil

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;

import static org.apache.commons.codec.digest.DigestUtils.md5;

public class PayUtil {

    /**
     * 用于建立十六进制字符的输出的小写字符数组
     */
    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    /**
     * 用于建立十六进制字符的输出的大写字符数组
     */
    private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    /**
     * 签名字符串
     *
     * @param text 需要签名的字符串
     * @param key               密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + "&key=" + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }

    public static String md5Hex(final byte[] data) {
        return Hex.encodeHexString(md5(data));
    }

    public static String encodeHexString(final byte[] data) {
        return new String(encodeHex(data));
    }

    public static char[] encodeHex(final byte[] data) {
        return encodeHex(data, true);
    }

    public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
        return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);
    }

    protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
        final int l = data.length;
        final char[] out = new char[l << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
            out[j++] = toDigits[0x0F & data[i]];
        }
        return out;
    }

    private static byte[] getContentBytes(String content, String charset) {
        if (charset != null && !"".equals(charset)) {
            try {
                return content.getBytes(charset);
            }catch (UnsupportedEncodingException var3) {
                throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
            }
        }else{
            return content.getBytes();
        }
    }
}

WXMyConfigUtil配置文件

appId 商户号 商家密钥

package com.ruoyi.admin.util;

import com.github.wxpay.sdk.WXPayConfig;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

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

    public WXMyConfigUtil() throws Exception {

        String certPath = (String)PropertiesUtils.getCommonYml("pay.wxpay.certPath");//从微信商户平台下载的安全证书存放的目录

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

    @Override
    public String getAppID() {
        return (String)PropertiesUtils.getCommonYml("pay.wxpay.appId");
    }

    //parnerid
    @Override
    public String getMchID() {
        return (Integer)PropertiesUtils.getCommonYml("pay.wxpay.mchID")+"";
    }

    @Override
    public String getKey() {
        return (String)PropertiesUtils.getCommonYml("pay.wxpay.paternerKEY");
    }

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

    @Override
    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 10000;
    }
}


工具类,用于util里获取yml里的配置
package com.ruoyi.admin.util;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.util.Properties;

/**
 * Created by admin on 2020/1/4.
 */
public class PropertiesUtils {
    private static String PROPERTY_NAME = "application.yml";

    /**
     * 用于util中获取yml文件的配置
     * @param key
     * @return
     */
    public static Object getCommonYml(Object key){
        Resource resource = new ClassPathResource(PROPERTY_NAME);
        Properties properties = null;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties =  yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }

    public static void main(String[] args) {
        System.out.println(getCommonYml("pay.wxpay.mchID"));
    }
}
主要实现代码片段
/**
     * 根据 订单号进行退款操作
     * @param outTradeNo
     * @param refundFee
     * @param totalFee
     * @return
     * @throws Exception
     */
    @Override
    public Map refund(String outTradeNo,Long refundFee,Long totalFee) throws Exception {
        Map<String, String> data = new HashMap<String, String>();
        System.err.println("进入微信退款申请");
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
        String hehe = dateFormat.format(now);

        String out_refund_no = hehe + "wxrefund";
        String transaction_id = ""; //微信支付时生成的流水号
        /*String total_fee = totalFee+""; //微信支付时支付的钱(单位为分)
        String out_trade_no = "12325903098970112";*/

        data.put("out_refund_no", out_refund_no);
        data.put("out_trade_no", outTradeNo);
        data.put("transaction_id", transaction_id);
        data.put("total_fee", totalFee+"");
        data.put("refund_fee", (refundFee==null ? totalFee:refundFee)+"");

        WXMyConfigUtil config = new WXMyConfigUtil();
        WXPay wxpay = new WXPay(config);
        data.put("appid", APPID);
        data.put("mch_id", MCHID);
        data.put("nonce_str", WXPayUtil.generateNonceStr());
        data.put("sign", PayUtil.sign(WXPayUtil.generateNonceStr(), PATERNERKEY, "utf-8").toUpperCase());

        Map<String, String> resp = null;
        try {
            resp = wxpay.refund(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        resp.put("out_refund_no",out_refund_no);
        System.err.println(resp);
        return resp;
    }
返回结果判断
Map<String, Object> resp = this.refund(entity.getOut_refund_no(), entity.getRefundFee(), entity.getTotalFee());
        String return_code = (String) resp.get("return_code");   //返回状态码
        String return_msg = (String) resp.get("return_msg");     //返回信息
        String out_refund_no = (String) resp.get("return_msg");     //退款號

        String resultReturn = null;
        if ("SUCCESS".equals(return_code)) {
            String result_code = (String) resp.get("result_code");       //业务结果
            String err_code_des = (String) resp.get("err_code_des");     //错误代码描述
            if ("SUCCESS".equals(result_code)) {
                //表示退款成功,结果通过退款查询接口查询
                //修改用户订单状态为退款成功(暂时未写)
                    resultReturn = "退款申请成功";
                } else {
                    logger.info("订单号:{}错误信息:{}" + err_code_des.toString());
                    resultReturn = err_code_des;
                }
            } else {
                logger.info("订单号:{}错误信息:{}" + return_msg.toString());
                resultReturn = return_msg;
            }
        }
application.yml

在这里插入图片描述

pay:
  wxpay:
    #商户号
    mchID: 1525911111
    appId: wxa33d794411111
    appSecret: a6d5653f7d7811111a7111111111
    #商户Key
    paternerKEY: 1hdW20of4Laxhmxdz4EXYox11111111
    # 从微信商户平台下载的安全证书存放的目录
#    certPath: D:/2020Project/mallplus/1525911111_20200526_cert/apiclient_cert.p12
    certPath: /var/local/1525911111_20200526_cert/apiclient_cert.p12
  port: 8899
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值