mall + pay:

1. 优化 PayClient 退款逻辑,返回业务失败 errorCode + errorMsg 错误码
This commit is contained in:
YunaiV 2023-07-18 22:36:03 +08:00
parent 3f410c2735
commit 888c488206
4 changed files with 54 additions and 9 deletions

View File

@ -25,4 +25,8 @@ public enum PayRefundStatusRespEnum {
return Objects.equals(status, SUCCESS.getStatus()); return Objects.equals(status, SUCCESS.getStatus());
} }
public static boolean isFailure(Integer status) {
return Objects.equals(status, FAILURE.getStatus());
}
} }

View File

@ -25,4 +25,8 @@ public enum PayRefundStatusEnum {
return Objects.equals(status, SUCCESS.getStatus()); return Objects.equals(status, SUCCESS.getStatus());
} }
public static boolean isFailure(Integer status) {
return Objects.equals(status, FAILURE.getStatus());
}
} }

View File

@ -237,6 +237,7 @@ public class PayOrderServiceImpl implements PayOrderService {
TenantUtils.execute(channel.getTenantId(), () -> notifyPayOrder(channel, notify)); TenantUtils.execute(channel.getTenantId(), () -> notifyPayOrder(channel, notify));
} }
// TODO 芋艿事务问题
private void notifyPayOrder(PayChannelDO channel, PayOrderRespDTO notify) { private void notifyPayOrder(PayChannelDO channel, PayOrderRespDTO notify) {
// 情况一支付成功的回调 // 情况一支付成功的回调
if (PayOrderStatusRespEnum.isSuccess(notify.getStatus())) { if (PayOrderStatusRespEnum.isSuccess(notify.getStatus())) {
@ -372,7 +373,7 @@ public class PayOrderServiceImpl implements PayOrderService {
throw exception(ORDER_NOT_FOUND); throw exception(ORDER_NOT_FOUND);
} }
if (!PayOrderStatusEnum.isSuccess(order.getStatus())) { if (!PayOrderStatusEnum.isSuccess(order.getStatus())) {
throw exception(REFUND_PRICE_EXCEED); throw exception(ORDER_STATUS_IS_NOT_SUCCESS);
} }
if (order.getRefundPrice() + incrRefundPrice > order.getPrice()) { if (order.getRefundPrice() + incrRefundPrice > order.getPrice()) {
throw exception(REFUND_PRICE_EXCEED); throw exception(REFUND_PRICE_EXCEED);
@ -389,7 +390,10 @@ public class PayOrderServiceImpl implements PayOrderService {
updateObj.setStatus(PayOrderStatusEnum.CLOSED.getStatus()) updateObj.setStatus(PayOrderStatusEnum.CLOSED.getStatus())
.setRefundStatus(PayOrderRefundStatusEnum.PART.getStatus()); .setRefundStatus(PayOrderRefundStatusEnum.PART.getStatus());
} }
orderMapper.updateByIdAndStatus(id, PayOrderStatusEnum.SUCCESS.getStatus(), updateObj); int updateCount = orderMapper.updateByIdAndStatus(id, PayOrderStatusEnum.SUCCESS.getStatus(), updateObj);
if (updateCount == 0) {
throw exception(ORDER_STATUS_IS_NOT_SUCCESS);
}
} }
} }

View File

@ -206,7 +206,6 @@ public class PayRefundServiceImpl implements PayRefundService {
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public void notifyRefund(Long channelId, PayRefundRespDTO notify) { public void notifyRefund(Long channelId, PayRefundRespDTO notify) {
// 校验支付渠道是否有效 // 校验支付渠道是否有效
channelService.validPayChannel(channelId); channelService.validPayChannel(channelId);
@ -218,15 +217,20 @@ public class PayRefundServiceImpl implements PayRefundService {
TenantUtils.execute(channel.getTenantId(), () -> notifyRefund(channel, notify)); TenantUtils.execute(channel.getTenantId(), () -> notifyRefund(channel, notify));
} }
// TODO 芋艿事务问题
private void notifyRefund(PayChannelDO channel, PayRefundRespDTO notify) { private void notifyRefund(PayChannelDO channel, PayRefundRespDTO notify) {
// 情况一退款成功
if (PayRefundStatusRespEnum.isSuccess(notify.getStatus())) { if (PayRefundStatusRespEnum.isSuccess(notify.getStatus())) {
notifyRefundSuccess(channel, notify); notifyRefundSuccess(channel, notify);
} else { return;
}
// 情况二退款失败
if (PayRefundStatusRespEnum.isFailure(notify.getStatus())) {
notifyRefundFailure(channel, notify); notifyRefundFailure(channel, notify);
} }
} }
private void notifyRefundSuccess(PayChannelDO channel, PayRefundRespDTO notify) { public void notifyRefundSuccess(PayChannelDO channel, PayRefundRespDTO notify) {
// 1.1 查询 PayRefundDO // 1.1 查询 PayRefundDO
PayRefundDO refund = refundMapper.selectByAppIdAndNo( PayRefundDO refund = refundMapper.selectByAppIdAndNo(
channel.getAppId(), notify.getOutRefundNo()); channel.getAppId(), notify.getOutRefundNo());
@ -234,12 +238,12 @@ public class PayRefundServiceImpl implements PayRefundService {
throw exception(ErrorCodeConstants.REFUND_NOT_FOUND); throw exception(ErrorCodeConstants.REFUND_NOT_FOUND);
} }
if (PayRefundStatusEnum.isSuccess(refund.getStatus())) { // 如果已经是成功直接返回不用重复更新 if (PayRefundStatusEnum.isSuccess(refund.getStatus())) { // 如果已经是成功直接返回不用重复更新
log.info("[notifyRefundSuccess][退款订单({}) 已经是退款成功,无需更新]", refund.getId());
return; return;
} }
if (!PayRefundStatusEnum.WAITING.getStatus().equals(refund.getStatus())) { if (!PayRefundStatusEnum.WAITING.getStatus().equals(refund.getStatus())) {
throw exception(ErrorCodeConstants.REFUND_STATUS_IS_NOT_WAITING); throw exception(ErrorCodeConstants.REFUND_STATUS_IS_NOT_WAITING);
} }
// 1.2 更新 PayRefundDO // 1.2 更新 PayRefundDO
PayRefundDO updateRefundObj = new PayRefundDO() PayRefundDO updateRefundObj = new PayRefundDO()
.setSuccessTime(notify.getSuccessTime()) .setSuccessTime(notify.getSuccessTime())
@ -250,17 +254,46 @@ public class PayRefundServiceImpl implements PayRefundService {
if (updateCounts == 0) { // 校验状态必须是等待状态 if (updateCounts == 0) { // 校验状态必须是等待状态
throw exception(ErrorCodeConstants.REFUND_STATUS_IS_NOT_WAITING); throw exception(ErrorCodeConstants.REFUND_STATUS_IS_NOT_WAITING);
} }
log.info("[notifyRefundSuccess][退款订单({}) 更新为退款成功]", refund.getId());
// 2. 更新订单 // 2. 更新订单
orderService.updateOrderRefundPrice(refund.getOrderId(), refund.getRefundPrice()); orderService.updateOrderRefundPrice(refund.getOrderId(), refund.getRefundPrice());
// 3. 插入退款通知记录 // 3. 插入退款通知记录 TODO 芋艿退款成功
notifyService.createPayNotifyTask(PayNotifyTaskCreateReqDTO.builder() notifyService.createPayNotifyTask(PayNotifyTaskCreateReqDTO.builder()
.type(PayNotifyTypeEnum.REFUND.getType()).dataId(refund.getId()).build()); .type(PayNotifyTypeEnum.REFUND.getType()).dataId(refund.getId()).build());
} }
private void notifyRefundFailure(PayChannelDO channel, PayRefundRespDTO notify) { @Transactional(rollbackFor = Exception.class)
// TODO 芋艿未实现 public void notifyRefundFailure(PayChannelDO channel, PayRefundRespDTO notify) {
// 1.1 查询 PayRefundDO
PayRefundDO refund = refundMapper.selectByAppIdAndNo(
channel.getAppId(), notify.getOutRefundNo());
if (refund == null) {
throw exception(ErrorCodeConstants.REFUND_NOT_FOUND);
}
if (PayRefundStatusEnum.isFailure(refund.getStatus())) { // 如果已经是成功直接返回不用重复更新
log.info("[notifyRefundSuccess][退款订单({}) 已经是退款关闭,无需更新]", refund.getId());
return;
}
if (!PayRefundStatusEnum.WAITING.getStatus().equals(refund.getStatus())) {
throw exception(ErrorCodeConstants.REFUND_STATUS_IS_NOT_WAITING);
}
// 1.2 更新 PayRefundDO
PayRefundDO updateRefundObj = new PayRefundDO()
.setChannelRefundNo(notify.getChannelRefundNo())
.setStatus(PayRefundStatusEnum.FAILURE.getStatus())
.setChannelNotifyData(toJsonString(notify))
.setChannelErrorCode(notify.getChannelErrorCode()).setChannelErrorMsg(notify.getChannelErrorMsg());
int updateCounts = refundMapper.updateByIdAndStatus(refund.getId(), refund.getStatus(), updateRefundObj);
if (updateCounts == 0) { // 校验状态必须是等待状态
throw exception(ErrorCodeConstants.REFUND_STATUS_IS_NOT_WAITING);
}
log.info("[notifyRefundFailure][退款订单({}) 更新为退款失败]", refund.getId());
// 2. 插入退款通知记录 TODO 芋艿退款失败
notifyService.createPayNotifyTask(PayNotifyTaskCreateReqDTO.builder()
.type(PayNotifyTypeEnum.REFUND.getType()).dataId(refund.getId()).build());
} }
} }