fix(订单): 修复退款逻辑和支付方式设置

修复订单退款逻辑,增加对支付方式的判断,避免对余额支付的订单进行退款操作。同时,优化退款异常处理,返回更详细的错误信息。在创建订单时,增加支付方式的设置。
This commit is contained in:
dzq 2025-04-10 10:31:40 +08:00
parent 38cb4a1595
commit 7e06c9f73f
4 changed files with 25 additions and 13 deletions

View File

@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.StringUtils;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
/**
@ -63,6 +64,9 @@ public class ApprovalApiController {
try {
if (command.getStatus() == 2) {
if (command.getReturnAmount() == null || command.getReturnAmount().compareTo(BigDecimal.ZERO) <= 0) {
return ResponseDTO.fail(new ApiException(ErrorCode.Internal.INTERNAL_ERROR, "归还金额不能为空或小于等于0"));
}
approvalApplicationService.approveApproval(command);
} else if (command.getStatus() == 3) {
approvalApplicationService.rejectApproval(command);

View File

@ -96,7 +96,10 @@ public class OrderController extends BaseController {
log.info("退款结果:{}", refundVO);
return ResponseDTO.ok(refundVO);
} catch (Exception e) {
throw new RuntimeException(e);
RefundVO refundVO = new RefundVO();
refundVO.setSuccess(false);
refundVO.setMsg(e.getMessage());
return ResponseDTO.fail(refundVO);
}
}
}

View File

@ -25,9 +25,11 @@ import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
/**
@ -119,18 +121,20 @@ public class ReturnApprovalApplicationService {
throw new IllegalArgumentException("退款金额不能超过订单总额");
}
RefundVO refundVO = null;
try {
refundVO = paymentApplicationService.refund(
PayApiConstants.biz_id, PayApiConstants.appkey,
orderModel.getBizOrderId(), orderModel.getUcid(),
"退还", returnAmount.intValue());
} catch (Exception e) {
throw new RuntimeException("退款失败", e);
}
log.info("退款结果:{}", refundVO);
if (null == refundVO || !refundVO.getSuccess()) {
throw new RuntimeException("退款失败");
if (!Objects.equals(orderModel.getPaymentMethod(), "balance")) {
RefundVO refundVO = null;
try {
refundVO = paymentApplicationService.refund(
PayApiConstants.biz_id, PayApiConstants.appkey,
orderModel.getBizOrderId(), orderModel.getUcid(),
"退还", returnAmount.intValue());
} catch (Exception e) {
throw new RuntimeException("退款失败", e);
}
log.info("退款结果:{}", refundVO);
if (null == refundVO || !refundVO.getSuccess()) {
throw new RuntimeException("退款失败");
}
}
model.validateApprovalStatus();

View File

@ -127,6 +127,7 @@ public class OrderApplicationService {
orderModel.setStatus(1);
orderModel.generateOrderNumber();
orderModel.setTotalAmount(BigDecimal.valueOf(0));
orderModel.setPaymentMethod(command.getPaymentType());
orderModel.insert();
processOrderGoods(orderModel, goodsList);