处理退款回调

This commit is contained in:
2025-08-25 01:25:18 +08:00
parent 10dd78bbf6
commit af51ba522e
6 changed files with 201 additions and 67 deletions

View File

@ -240,6 +240,7 @@ public class WechatPayService {
public Map<String, String> refund(RefundRequest request) throws Exception {
// 根据companyId获取微信支付配置
CompanyWechatConfig companyConfig = companyWechatConfigMapper.getWechatConfigByCompanyId(request.getCompanyId());
PaymentRecord paymentRecord = paymentRecordMapper.getPaymentRecordByOutTradeNo(request.getOutTradeNo());
if (companyConfig == null) {
Map<String, String> errorResult = new HashMap<>();
errorResult.put("return_code", "FAIL");
@ -260,7 +261,7 @@ public class WechatPayService {
Map<String, String> reqData = new HashMap<>();
reqData.put("out_trade_no", request.getOutTradeNo());
reqData.put("out_refund_no", request.getOutRefundNo());
reqData.put("total_fee", String.valueOf(request.getTotalFee()));
reqData.put("total_fee", String.valueOf(paymentRecord.getTotalFee()));
reqData.put("refund_fee", String.valueOf(request.getRefundFee()));
reqData.put("notify_url", refundNotifyUrl);
@ -417,88 +418,44 @@ public class WechatPayService {
}
}
/**
* 处理退款成功通知并更新退款状态
*/
@Transactional
public boolean processRefundSuccessNotify(Long companyId, Map<String, String> notifyData) {
try {
String outRefundNo = notifyData.get("out_refund_no");
String refundId = notifyData.get("refund_id");
String refundStatus = notifyData.get("refund_status");
// 根据退款状态更新退款记录
String statusDesc = "";
switch (refundStatus) {
case "SUCCESS":
statusDesc = "退款成功";
break;
case "REFUNDCLOSE":
statusDesc = "退款关闭";
break;
case "PROCESSING":
statusDesc = "退款处理中";
break;
case "CHANGE":
statusDesc = "退款异常";
break;
default:
statusDesc = "未知状态";
}
int updated = refundRecordMapper.updateRefundStatus(
outRefundNo,
refundStatus,
statusDesc,
refundId,
"SUCCESS".equals(refundStatus) ? new Date() : null, // 退款成功时间
new Date() // 更新时间
);
if (updated > 0) {
logger.info("退款记录状态已更新,退款单号: {}, 微信退款单号: {}, 状态: {}", outRefundNo, refundId, refundStatus);
// TODO: 在这里调用其他业务服务更新实际订单退款状态
return true;
} else {
logger.warn("未找到对应的退款记录,退款单号: {}", outRefundNo);
return false;
}
} catch (Exception e) {
logger.error("处理退款成功通知异常,退款单号: {}", notifyData.get("out_refund_no"), e);
return false;
}
}
/**
* 处理退款通知并更新退款状态
*/
@Transactional
public boolean processRefundNotify(Long companyId, Map<String, String> notifyData) {
public boolean processRefundNotify(Map<String, String> notifyData) {
try {
String outRefundNo = notifyData.get("out_refund_no");
String refundId = notifyData.get("refund_id");
String refundStatus = notifyData.get("refund_status");
String outTradeNo = notifyData.get("out_trade_no");
BigDecimal refundFee = new BigDecimal(notifyData.get("refund_fee"));
// 根据退款状态更新退款记录
String statusDesc = "";
String payStatus = "";
switch (refundStatus) {
case "SUCCESS":
statusDesc = "退款成功";
payStatus = "REFUND_SUCCESS";
break;
case "REFUNDCLOSE":
statusDesc = "退款关闭";
payStatus = "REFUND_SUCCESS";
break;
case "PROCESSING":
statusDesc = "退款处理中";
payStatus = "REFUNDING";
break;
case "CHANGE":
statusDesc = "退款异常";
payStatus = "REFUND_ERROR";
break;
default:
statusDesc = "未知状态";
payStatus = "REFUND_ERROR";
}
orderPayMapper.updateSubOrderRefundStatus(outTradeNo,payStatus,refundFee,new Date(),refundId);
int updated = refundRecordMapper.updateRefundStatus(
outRefundNo,
refundStatus,
@ -510,7 +467,6 @@ public class WechatPayService {
if (updated > 0) {
logger.info("微信退款记录状态已更新,退款单号: {}, 微信退款单号: {}, 状态: {}", outRefundNo, refundId, refundStatus);
orderPayMapper.updateRefundOrderStatus(outTradeNo,"REFUND_SUCCESS",outRefundNo);
return true;
} else {
logger.warn("未找到对应的微信退款记录,退款单号: {}", outRefundNo);
@ -522,4 +478,35 @@ public class WechatPayService {
}
}
public Map<String, String> decryptRefundNotify(String xmlData, String apiKey) throws Exception {
try {
// 解析XML数据
Map<String, String> notifyData = WXPayUtil.xmlToMap(xmlData);
// 检查是否包含req_info字段
if (!notifyData.containsKey("req_info")) {
logger.warn("微信退款通知中不包含req_info字段");
return notifyData;
}
// 获取加密的req_info
String reqInfo = notifyData.get("req_info");
// 解密req_info
String decryptedReqInfo = WXPayUtil.decryptReqInfo(reqInfo, apiKey);
// 解析解密后的XML数据
Map<String, String> decryptedData = WXPayUtil.xmlToMap(decryptedReqInfo);
// 将解密后的数据合并到原始通知数据中
notifyData.putAll(decryptedData);
return notifyData;
} catch (Exception e) {
logger.error("处理微信退款通知失败", e);
throw new Exception("处理微信退款通知失败: " + e.getMessage(), e);
}
}
}