Java实现微信V3版本退款

Java实现微信V3版本退款

之前的文章写过java实现V3版本的微信支付,现续上篇支付文章之后,使用相同的配置文件实现微信退款

核心步骤代码

public void mpOrderRefund(MpOrderDTO mpOrder) throws Exception {
        //这个上游过滤可退款订单,这直接处理
         String orderNo = mpOrder.getOrderNo();
            BigDecimal totalPrice = mpOrder.getTotalPrice();
            totalPrice = totalPrice.multiply(new BigDecimal(100));
            //生成退款订单编号
            String orderRefundNo = generateNonceStr(PayTypeConstant.MP_ORDER.getType());
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("out_trade_no", orderNo);
            data.put("out_refund_no", orderRefundNo);
            data.put("notify_url", "这个是退款回调接收地址,自己定义填写");
            Map<String, Object> amount = new HashMap<String, Object>();
             //退款金额
            amount.put("refund", totalPrice.intValue());
            amount.put("total", totalPrice.intValue());
            amount.put("currency", "CNY");
            data.put("amount", amount);
            String refundUrl = WxApiType.DOMESTIC_REFUNDS.toString();
            String privateKeyPath = "apiclient_key.pem这个文件的位置";
            //请求退款
            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethod.POST,
                    WxDomain.CHINA.toString(),
                    refundUrl,
                    // 商户号
                    "1111111",
                    // 获取证书序列号
                    getSerialNumber(),
                    null,
                    // 私钥
                    privateKeyPath,
                    // 请求参数
                    JSONUtil.toJsonStr(data)
            );
            log.info("统一退款响应 {}", response);
            String body = response.getBody();
            //余额不足特殊处理
            try {
                if (StringUtil.isNotBlank(body) && body.contains("NOT_ENOUGH") && body.contains("基本账户余额不足")) {
                    mpOrder.setRefundStatus(6);
                    MpOrderMapper.updateById(mpOrder);
                }
            } catch (Exception e) {
                log.info("退款调整失败 {}", e.getMessage());
            }
    }
/**
     * 获取证书序列号
     *
     * @return
     */
    private String getSerialNumber() throws IOException {
        String certPath = "apiclient_cert.pem这个文件的位置";
        log.info("path:{}", certPath);
        // 获取证书序列号
        X509Certificate certificate = PayKit.getCertificate(FileUtil.getInputStream(certPath));
        String serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
        log.info("获取证书序列号:{},", serialNo);
        return serialNo;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java微信支付v3退款的示例代码: ```java import com.github.wxpay.sdk.WXPay; import com.github.wxpay.sdk.WXPayConstants; import com.github.wxpay.sdk.WXPayUtil; import java.util.HashMap; import java.util.Map; public class WeChatPayRefund { public static void main(String[] args) throws Exception { // 商户号 String mchId = "YOUR_MCH_ID"; // 商户密钥 String mchKey = "YOUR_MCH_KEY"; // 应用ID String appId = "YOUR_APP_ID"; // 退款证书路径 String certPath = "YOUR_CERT_PATH"; // 订单号 String orderId = "YOUR_ORDER_ID"; // 退款金额,单位为分 int refundAmount = 100; // 退款描述 String refundDesc = "退款测试"; // 初始化WXPay配置 WXPayConfigImpl config = new WXPayConfigImpl(); config.setAppID(appId); config.setMchID(mchId); config.setKey(mchKey); config.setCertPath(certPath); WXPay wxPay = new WXPay(config, WXPayConstants.SignType.HMACSHA256); // 构造退款请求参数 Map<String, String> data = new HashMap<>(); data.put("out_trade_no", orderId); data.put("out_refund_no", WXPayUtil.generateUUID()); data.put("total_fee", String.valueOf(refundAmount)); data.put("refund_fee", String.valueOf(refundAmount)); data.put("refund_desc", refundDesc); // 发起退款请求 Map<String, String> result = wxPay.refund(data); // 处理退款结果 if ("SUCCESS".equals(result.get("return_code")) && "SUCCESS".equals(result.get("result_code"))) { System.out.println("退款成功!"); } else { System.out.println("退款失败:" + result.get("return_msg")); } } } ``` 需要注意的是,退款证书需要从微信商户平台下载,并且退款金额不能大于原订单金额。另外,如果使用的是证书,需要在初始化WXPay时传入WXPayConstants.SignType.MD5或WXPayConstants.SignType.HMACSHA256。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值