|
|
@@ -195,6 +195,21 @@ public class PaymentServiceImpl implements PaymentService {
|
|
|
BigDecimal actualRefundAmount = refundAmount != null ? refundAmount
|
|
|
: (order.getPaidAmount() != null ? order.getPaidAmount() : order.getTotalAmount());
|
|
|
|
|
|
+ // 校验累计退款金额不超过实付金额
|
|
|
+ BigDecimal paidAmount = order.getPaidAmount() != null ? order.getPaidAmount() : order.getTotalAmount();
|
|
|
+ BigDecimal existingRefund = order.getRefundAmount() != null ? order.getRefundAmount() : BigDecimal.ZERO;
|
|
|
+ BigDecimal totalAfterRefund = existingRefund.add(actualRefundAmount);
|
|
|
+ if (totalAfterRefund.compareTo(paidAmount) > 0) {
|
|
|
+ BigDecimal remaining = paidAmount.subtract(existingRefund);
|
|
|
+ log.error("退款失败: 累计退款金额超过实付金额, orderId={}, existingRefund={}, requestAmount={}, remaining={}",
|
|
|
+ orderId, existingRefund, actualRefundAmount, remaining);
|
|
|
+ return RefundResult.builder()
|
|
|
+ .success(false)
|
|
|
+ .errorCode("REFUND_AMOUNT_EXCEEDED")
|
|
|
+ .errorMsg("退款金额超过可退金额,已退" + existingRefund + "元,最多可退" + remaining + "元")
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
// 5. 根据订单的 payChannel 获取支付渠道
|
|
|
PaymentChannel channel = PaymentChannel.fromCode(order.getPayChannel());
|
|
|
|
|
|
@@ -601,14 +616,27 @@ public class PaymentServiceImpl implements PaymentService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 累计退款金额
|
|
|
+ BigDecimal existingRefund = order.getRefundAmount() != null ? order.getRefundAmount() : BigDecimal.ZERO;
|
|
|
+ BigDecimal cumulativeAmount = existingRefund.add(amount);
|
|
|
+
|
|
|
order.setRefundStatus(RefundStatus.REFUNDED.getCode());
|
|
|
- order.setRefundAmount(amount);
|
|
|
+ order.setRefundAmount(cumulativeAmount);
|
|
|
order.setRefundTime(LocalDateTime.now());
|
|
|
order.setRefundReason(reason);
|
|
|
- order.setPayStatus(OrderConstants.PAY_STATUS_REFUND);
|
|
|
+
|
|
|
+ // 仅当累计退款 >= 实付金额时,才标记为全部退款
|
|
|
+ BigDecimal paidAmount = order.getPaidAmount() != null ? order.getPaidAmount() : order.getTotalAmount();
|
|
|
+ if (cumulativeAmount.compareTo(paidAmount) >= 0) {
|
|
|
+ order.setPayStatus(OrderConstants.PAY_STATUS_REFUND);
|
|
|
+ log.info("订单全部退款完成 - orderId={}, cumulativeAmount={}, paidAmount={}",
|
|
|
+ order.getId(), cumulativeAmount, paidAmount);
|
|
|
+ }
|
|
|
+ // 部分退款时保持 payStatus = PAID,允许继续退款
|
|
|
+
|
|
|
orderService.updateById(order);
|
|
|
- log.info("订单退款状态已更新 - orderId={}, refundAmount={}, payStatus={}",
|
|
|
- order.getId(), amount, OrderConstants.PAY_STATUS_REFUND);
|
|
|
+ log.info("订单退款状态已更新 - orderId={}, refundAmount={}(累计), payStatus={}",
|
|
|
+ order.getId(), cumulativeAmount, order.getPayStatus());
|
|
|
}
|
|
|
|
|
|
/**
|