修改退款申请封装

This commit is contained in:
gong
2026-01-15 13:32:55 +08:00
parent ee25493570
commit 98dfda0bca
20 changed files with 361 additions and 136 deletions

View File

@@ -1,10 +1,11 @@
package com.czg;
import com.czg.constants.SystemConstants;
import com.czg.dto.req.RefundParamsDto;
import com.czg.exception.CzgException;
import com.czg.pay.CzgPayBaseReq;
import com.czg.pay.CzgRefundReq;
import com.czg.pay.NativeMerchantDTO;
import com.czg.pay.RefundRespDTO;
import com.czg.third.alipay.AlipayIsvPayManager;
import com.czg.third.wechat.WechatPayManager;
@@ -64,11 +65,11 @@ public class PayManager {
/**
* 退款
*/
public static Map<String, Object> refund(String platform, RefundParamsDto paramsDto) {
if (SystemConstants.PayType.WECHAT.equals(platform)) {
return WechatPayManager.refundOrder(null, paramsDto);
} else if (SystemConstants.PayType.ALIPAY.equals(platform)) {
return AlipayIsvPayManager.refundOrder(null, paramsDto);
public static RefundRespDTO refund(CzgRefundReq paramsDto, String notifyUrl, NativeMerchantDTO merchantDTO) {
if (PayCst.Platform.WECHAT.equals(paramsDto.getPlatform())) {
return WechatPayManager.refundOrder(null, paramsDto, notifyUrl, merchantDTO);
} else if (PayCst.Platform.ALIPAY.equals(paramsDto.getPlatform())) {
return AlipayIsvPayManager.refundOrder(null, paramsDto, notifyUrl, merchantDTO);
} else {
throw new CzgException("不支持的支付平台");
}

View File

@@ -1,6 +1,8 @@
package com.czg.dto.req;
import com.alibaba.fastjson2.annotation.JSONField;
import com.czg.PayCst;
import com.czg.pay.PayNotifyRespDTO;
import lombok.Data;
/**
@@ -115,4 +117,35 @@ public class WechatPayNotifyDataDto {
public Long getPayAmount() {
return Long.valueOf(amount.getTotal());
}
public PayNotifyRespDTO convertToPayNotifyRespDTO() {
PayNotifyRespDTO respDTO = new PayNotifyRespDTO()
.setMchOrderNo(outTradeNo)
.setThirdOrderNo(transactionId)
.setAmount(getPayAmount())
.setPlatform(PayCst.Platform.WECHAT)
.setExtData(attach)
.setPaySuccessTime(successTime)
.setErrorMsg(tradeStateDesc);
switch (tradeState) {
case "SUCCESS":
respDTO.setStatus("TRADE_SUCCESS");
break;
case "CLOSED":
respDTO.setStatus("TRADE_CLOSE");
break;
case "USERPAYING":
respDTO.setStatus("TRADE_AWAIT");
break;
case "NOTPAY":
respDTO.setStatus("TRADE_CANCEL");
break;
default:
respDTO.setStatus("TRADE_FAIL");
break;
}
return respDTO;
}
}

View File

@@ -9,10 +9,11 @@ import com.alipay.v3.model.AlipayTradePayModel;
import com.alipay.v3.model.ExtendParams;
import com.alipay.v3.util.model.CustomizedParams;
import com.czg.PayCst;
import com.czg.dto.req.RefundParamsDto;
import com.czg.exception.CzgException;
import com.czg.pay.CzgPayBaseReq;
import com.czg.pay.CzgRefundReq;
import com.czg.pay.NativeMerchantDTO;
import com.czg.pay.RefundRespDTO;
import com.czg.third.alipay.dto.config.AlipayConfigDto;
import lombok.extern.slf4j.Slf4j;
@@ -107,8 +108,8 @@ public class AlipayIsvPayManager {
return new HashMap<>();
}
public static Map<String, Object> refundOrder(AlipayConfigDto configDto, RefundParamsDto paramsDto) {
return new HashMap<>();
public static RefundRespDTO refundOrder(AlipayConfigDto configDto, CzgRefundReq paramsDto, String notifyUrl, NativeMerchantDTO merchantDTO) {
return new RefundRespDTO();
}
/**

View File

@@ -5,10 +5,11 @@ import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.PayCst;
import com.czg.dto.req.RefundParamsDto;
import com.czg.exception.CzgException;
import com.czg.pay.CzgPayBaseReq;
import com.czg.pay.CzgRefundReq;
import com.czg.pay.NativeMerchantDTO;
import com.czg.pay.RefundRespDTO;
import com.czg.third.wechat.dto.config.WechatPayConfigDto;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.cipher.Signer;
@@ -257,64 +258,63 @@ public class WechatPayManager {
* PROCESSING: 退款处理中
* ABNORMAL: 退款异常,退款到银行发现用户的卡作废或者冻结了,导致原路退款银行卡失败,可前往服务商平台-交易中心,手动处理此笔退款
*/
public static Map<String, Object> refundOrder(WechatPayConfigDto configDto, RefundParamsDto paramsDto) {
public static RefundRespDTO refundOrder(WechatPayConfigDto configDto, CzgRefundReq paramsDto, String notifyUrl, NativeMerchantDTO merchantDTO) {
try {
if (configDto == null) {
configDto = WechatPayConfigDto.getDefaultConfig();
}
JSONObject refundParam = new JSONObject();
refundParam.put("sub_mchid", paramsDto.getMerchantId());
refundParam.put("out_trade_no", paramsDto.getOrderNo());
refundParam.put("out_refund_no", paramsDto.getRefundOrderNo());
refundParam.put("sub_mchid", merchantDTO.getWechatMerchantId());
refundParam.put("out_trade_no", paramsDto.getMchOrderNo());
refundParam.put("out_refund_no", paramsDto.getMchRefundNo());
refundParam.put("reason", paramsDto.getRefundReason());
refundParam.put("notify_url", paramsDto.getRefundNotifyUrl() + "/" + PayCst.Platform.WECHAT);
refundParam.put("notify_url", notifyUrl + "/" + PayCst.Platform.WECHAT);
JSONObject amount = new JSONObject();
amount.put("total", paramsDto.getOrderAmount());
amount.put("total", paramsDto.getOrderTotalAmount());
amount.put("refund", paramsDto.getRefundAmount());
amount.put("currency", "CNY");
refundParam.put("amount", amount);
String resp = WechatReqUtils.postReq(configDto, "/v3/refund/domestic/refunds", refundParam.toJSONString());
log.info("微信退款,订单号:{} 响应:{}", paramsDto.getOrderNo(), resp);
log.info("微信退款,订单号:{} 响应:{}", paramsDto.getMchOrderNo(), resp);
JSONObject object = JSONObject.parseObject(resp);
JSONObject res = new JSONObject();
res.put("mchRefundNo", object.getString("out_refund_no"));
res.put("refundOrderId", object.getString("refund_id"));
res.put("oriPayOrderId", object.getString("out_trade_no"));
res.put("mercNo", paramsDto.getMerchantId());
res.put("refundReason", paramsDto.getRefundReason());
res.put("payType", PayCst.Platform.WECHAT);
res.put("refundTime", object.getString("success_time"));
switch (object.getString("status")) {
case "SUCCESS":
res.put("status", "SUCCESS");
break;
case "CLOSED":
res.put("status", "CLOSED");
break;
case "PROCESSING":
res.put("status", "ING");
break;
case "ABNORMAL":
res.put("status", "INIT");
break;
default:
res.put("status", "FAIL");
break;
}
RefundRespDTO respDTO = new RefundRespDTO()
.setMerchantRefundNo(object.getString("out_refund_no"))
.setThirdRefundNo(object.getString("refund_id"))
.setRefundTime(object.getString("success_time"))
.setOriginalData(resp)
.setPlatform(PayCst.Platform.WECHAT);
JSONObject resAmount = object.getJSONObject("amount");
if (resAmount != null) {
res.put("refundAmt", resAmount.getLong("refund"));
res.put("oriAmount", resAmount.getLong("total"));
respDTO.setRefundAmount(resAmount.getLong("refund"));
}
return res;
switch (object.getString("status")) {
case "SUCCESS":
respDTO.setStatus("SUCCESS");
break;
case "CLOSED":
respDTO.setStatus("CLOSED");
break;
case "PROCESSING":
respDTO.setStatus("ING");
break;
case "ABNORMAL":
respDTO.setStatus("INIT");
break;
default:
respDTO.setStatus("FAIL");
break;
}
return respDTO;
} catch (Exception e) {
log.error("微信退款异常: orderNo = {}, e = {}", paramsDto.getOrderNo(), e.getMessage());
log.error("微信退款异常: orderNo = {}, e = {}", paramsDto.getMchOrderNo(), e.getMessage());
throw new CzgException("微信退款异常");
}
}
@@ -369,13 +369,13 @@ public class WechatPayManager {
// Map<String, Object> sss1 = queryOrder(null, "sa101293120sss1", "1738216504");
// System.out.println(sss1);
refundOrder(null, new RefundParamsDto()
.setMerchantId("1738216504")
.setOrderNo("sa101293120sss1")
.setRefundOrderNo("sa101293120sss1")
.setOrderAmount(1L)
.setRefundAmount(1L)
.setRefundReason("测试")
.setRefundNotifyUrl("https://www.baidu.com"));
// refundOrder(null, new RefundParamsDto()
// .setMerchantId("1738216504")
// .setOrderNo("sa101293120sss1")
// .setRefundOrderNo("sa101293120sss1")
// .setOrderAmount(1L)
// .setRefundAmount(1L)
// .setRefundReason("测试")
// .setRefundNotifyUrl("https://www.baidu.com"));
}
}