|
@@ -4,15 +4,15 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.lang.Pair;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils;
|
|
|
import cn.iocoder.yudao.framework.pay.config.PayProperties;
|
|
|
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
|
|
|
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
|
|
|
-import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayNotifyReqDTO;
|
|
|
-import cn.iocoder.yudao.framework.pay.core.client.dto.notify.PayOrderNotifyRespDTO;
|
|
|
+import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
|
|
|
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
|
|
|
-import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedRespDTO;
|
|
|
+import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum;
|
|
|
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
|
|
|
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
|
|
|
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderExportReqVO;
|
|
@@ -26,11 +26,10 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
|
|
|
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
|
|
|
import cn.iocoder.yudao.module.pay.dal.mysql.order.PayOrderExtensionMapper;
|
|
|
import cn.iocoder.yudao.module.pay.dal.mysql.order.PayOrderMapper;
|
|
|
-import cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants;
|
|
|
import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyTypeEnum;
|
|
|
import cn.iocoder.yudao.module.pay.enums.order.PayOrderNotifyStatusEnum;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.order.PayOrderRefundStatusEnum;
|
|
|
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
|
|
|
-import cn.iocoder.yudao.module.pay.enums.refund.PayRefundTypeEnum;
|
|
|
import cn.iocoder.yudao.module.pay.service.app.PayAppService;
|
|
|
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
|
|
|
import cn.iocoder.yudao.module.pay.service.notify.PayNotifyService;
|
|
@@ -48,6 +47,7 @@ import java.util.Objects;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
|
|
+import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
|
* 支付订单 Service 实现类
|
|
@@ -82,6 +82,11 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
return orderMapper.selectById(id);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public PayOrderDO getOrder(Long appId, String merchantOrderId) {
|
|
|
+ return orderMapper.selectByAppIdAndMerchantOrderId(appId, merchantOrderId);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Long getOrderCountByAppId(Long appId) {
|
|
|
return orderMapper.selectCountByAppId(appId);
|
|
@@ -104,7 +109,7 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Long createPayOrder(PayOrderCreateReqDTO reqDTO) {
|
|
|
+ public Long createOrder(PayOrderCreateReqDTO reqDTO) {
|
|
|
// 校验 App
|
|
|
PayAppDO app = appService.validPayApp(reqDTO.getAppId());
|
|
|
|
|
@@ -112,7 +117,7 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
PayOrderDO order = orderMapper.selectByAppIdAndMerchantOrderId(
|
|
|
reqDTO.getAppId(), reqDTO.getMerchantOrderId());
|
|
|
if (order != null) {
|
|
|
- log.warn("[createPayOrder][appId({}) merchantOrderId({}) 已经存在对应的支付单({})]", order.getAppId(),
|
|
|
+ log.warn("[createOrder][appId({}) merchantOrderId({}) 已经存在对应的支付单({})]", order.getAppId(),
|
|
|
order.getMerchantOrderId(), toJsonString(order)); // 理论来说,不会出现这个情况
|
|
|
return order.getId();
|
|
|
}
|
|
@@ -120,20 +125,19 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
// 创建支付交易单
|
|
|
order = PayOrderConvert.INSTANCE.convert(reqDTO).setAppId(app.getId())
|
|
|
// 商户相关字段
|
|
|
- .setNotifyUrl(app.getPayNotifyUrl()).setNotifyStatus(PayOrderNotifyStatusEnum.NO.getStatus())
|
|
|
+ .setNotifyUrl(app.getOrderNotifyUrl()).setNotifyStatus(PayOrderNotifyStatusEnum.NO.getStatus())
|
|
|
// 订单相关字段
|
|
|
.setStatus(PayOrderStatusEnum.WAITING.getStatus())
|
|
|
// 退款相关字段
|
|
|
- .setRefundStatus(PayRefundTypeEnum.NO.getStatus()).setRefundTimes(0).setRefundPrice(0L);
|
|
|
+ .setRefundStatus(PayOrderRefundStatusEnum.NO.getStatus()).setRefundTimes(0).setRefundPrice(0);
|
|
|
orderMapper.insert(order);
|
|
|
return order.getId();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public PayOrderSubmitRespVO submitPayOrder(PayOrderSubmitReqVO reqVO, String userIp) {
|
|
|
+ @Override // 注意,这里不能添加事务注解,避免调用支付渠道失败时,将 PayOrderExtensionDO 回滚了
|
|
|
+ public PayOrderSubmitRespVO submitOrder(PayOrderSubmitReqVO reqVO, String userIp) {
|
|
|
// 1. 获得 PayOrderDO ,并校验其是否存在
|
|
|
- PayOrderDO order = validatePayOrderCanSubmit(reqVO.getId());
|
|
|
+ PayOrderDO order = validateOrderCanSubmit(reqVO.getId());
|
|
|
// 1.2 校验支付渠道是否有效
|
|
|
PayChannelDO channel = validatePayChannelCanSubmit(order.getAppId(), reqVO.getChannelCode());
|
|
|
PayClient client = payClientFactory.getPayClient(channel.getId());
|
|
@@ -148,35 +152,38 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
// 3. 调用三方接口
|
|
|
PayOrderUnifiedReqDTO unifiedOrderReqDTO = PayOrderConvert.INSTANCE.convert2(reqVO, userIp)
|
|
|
// 商户相关的字段
|
|
|
- .setMerchantOrderId(orderExtension.getNo()) // 注意,此处使用的是 PayOrderExtensionDO.no 属性!
|
|
|
+ .setOutTradeNo(orderExtension.getNo()) // 注意,此处使用的是 PayOrderExtensionDO.no 属性!
|
|
|
.setSubject(order.getSubject()).setBody(order.getBody())
|
|
|
- .setNotifyUrl(genChannelPayNotifyUrl(channel))
|
|
|
+ .setNotifyUrl(genChannelOrderNotifyUrl(channel))
|
|
|
.setReturnUrl(reqVO.getReturnUrl())
|
|
|
// 订单相关字段
|
|
|
- .setAmount(order.getPrice()).setExpireTime(order.getExpireTime());
|
|
|
- PayOrderUnifiedRespDTO unifiedOrderRespDTO = client.unifiedOrder(unifiedOrderReqDTO);
|
|
|
+ .setPrice(order.getPrice()).setExpireTime(order.getExpireTime());
|
|
|
+ PayOrderRespDTO unifiedOrderResp = client.unifiedOrder(unifiedOrderReqDTO);
|
|
|
|
|
|
// 4. 如果调用直接支付成功,则直接更新支付单状态为成功。例如说:付款码支付,免密支付时,就直接验证支付成功
|
|
|
- if (unifiedOrderRespDTO.getNotify() != null) {
|
|
|
- notifyPayOrderSuccess(channel, unifiedOrderRespDTO.getNotify(), null);
|
|
|
+ if (unifiedOrderResp != null) {
|
|
|
+ notifyPayOrder(channel, unifiedOrderResp);
|
|
|
+ // 如有渠道错误码,则抛出业务异常,提示用户
|
|
|
+ if (StrUtil.isNotEmpty(unifiedOrderResp.getChannelErrorCode())) {
|
|
|
+ throw exception(PAY_ORDER_SUBMIT_CHANNEL_ERROR, unifiedOrderResp.getChannelErrorCode(),
|
|
|
+ unifiedOrderResp.getChannelErrorMsg());
|
|
|
+ }
|
|
|
// 此处需要读取最新的状态
|
|
|
order = orderMapper.selectById(order.getId());
|
|
|
}
|
|
|
-
|
|
|
- // 返回成功
|
|
|
- return PayOrderConvert.INSTANCE.convert(order, unifiedOrderRespDTO);
|
|
|
+ return PayOrderConvert.INSTANCE.convert(order, unifiedOrderResp);
|
|
|
}
|
|
|
|
|
|
- private PayOrderDO validatePayOrderCanSubmit(Long id) {
|
|
|
+ private PayOrderDO validateOrderCanSubmit(Long id) {
|
|
|
PayOrderDO order = orderMapper.selectById(id);
|
|
|
if (order == null) { // 是否存在
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_NOT_FOUND);
|
|
|
+ throw exception(PAY_ORDER_NOT_FOUND);
|
|
|
}
|
|
|
if (!PayOrderStatusEnum.WAITING.getStatus().equals(order.getStatus())) { // 校验状态,必须是待支付
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_STATUS_IS_NOT_WAITING);
|
|
|
+ throw exception(PAY_ORDER_STATUS_IS_NOT_WAITING);
|
|
|
}
|
|
|
if (LocalDateTimeUtils.beforeNow(order.getExpireTime())) { // 校验是否过期
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_IS_EXPIRED);
|
|
|
+ throw exception(PAY_ORDER_IS_EXPIRED);
|
|
|
}
|
|
|
return order;
|
|
|
}
|
|
@@ -184,14 +191,12 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
private PayChannelDO validatePayChannelCanSubmit(Long appId, String channelCode) {
|
|
|
// 校验 App
|
|
|
appService.validPayApp(appId);
|
|
|
-
|
|
|
// 校验支付渠道是否有效
|
|
|
PayChannelDO channel = channelService.validPayChannel(appId, channelCode);
|
|
|
- // 校验支付客户端是否正确初始化
|
|
|
PayClient client = payClientFactory.getPayClient(channel.getId());
|
|
|
if (client == null) {
|
|
|
log.error("[validatePayChannelCanSubmit][渠道编号({}) 找不到对应的支付客户端]", channel.getId());
|
|
|
- throw exception(ErrorCodeConstants.PAY_CHANNEL_CLIENT_NOT_FOUND);
|
|
|
+ throw exception(PAY_CHANNEL_CLIENT_NOT_FOUND);
|
|
|
}
|
|
|
return channel;
|
|
|
}
|
|
@@ -202,8 +207,8 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
* @param channel 支付渠道
|
|
|
* @return 支付渠道的回调地址 配置地址 + "/" + channel id
|
|
|
*/
|
|
|
- private String genChannelPayNotifyUrl(PayChannelDO channel) {
|
|
|
- return payProperties.getCallbackUrl() + "/" + channel.getId();
|
|
|
+ private String genChannelOrderNotifyUrl(PayChannelDO channel) {
|
|
|
+ return payProperties.getOrderNotifyUrl() + "/" + channel.getId();
|
|
|
}
|
|
|
|
|
|
private String generateOrderExtensionNo() {
|
|
@@ -226,18 +231,57 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void notifyPayOrder(Long channelId, PayOrderNotifyRespDTO notify, PayNotifyReqDTO rawNotify) {
|
|
|
+ public void notifyOrder(Long channelId, PayOrderRespDTO notify) {
|
|
|
// 校验支付渠道是否有效
|
|
|
PayChannelDO channel = channelService.validPayChannel(channelId);
|
|
|
// 更新支付订单为已支付
|
|
|
- TenantUtils.execute(channel.getTenantId(), () -> notifyPayOrderSuccess(channel, notify, rawNotify));
|
|
|
+ TenantUtils.execute(channel.getTenantId(), () -> notifyPayOrder(channel, notify));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateOrderRefundPrice(Long id, Integer incrRefundPrice) {
|
|
|
+ PayOrderDO order = orderMapper.selectById(id);
|
|
|
+ if (order == null) {
|
|
|
+ throw exception(PAY_ORDER_NOT_FOUND);
|
|
|
+ }
|
|
|
+ if (!PayOrderStatusEnum.isSuccess(order.getStatus())) {
|
|
|
+ throw exception(PAY_REFUND_PRICE_EXCEED);
|
|
|
+ }
|
|
|
+ if (order.getRefundPrice() + incrRefundPrice > order.getPrice()) {
|
|
|
+ throw exception(PAY_REFUND_PRICE_EXCEED);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新订单
|
|
|
+ PayOrderDO updateObj = new PayOrderDO()
|
|
|
+ .setRefundPrice(order.getRefundPrice() + incrRefundPrice)
|
|
|
+ .setRefundTimes(order.getRefundTimes() + 1);
|
|
|
+ if (Objects.equals(updateObj.getRefundPrice(), order.getPrice())) {
|
|
|
+ updateObj.setStatus(PayOrderStatusEnum.CLOSED.getStatus())
|
|
|
+ .setRefundStatus(PayOrderRefundStatusEnum.ALL.getStatus());
|
|
|
+ } else {
|
|
|
+ updateObj.setStatus(PayOrderStatusEnum.CLOSED.getStatus())
|
|
|
+ .setRefundStatus(PayOrderRefundStatusEnum.PART.getStatus());
|
|
|
+ }
|
|
|
+ orderMapper.updateByIdAndStatus(id, PayOrderStatusEnum.SUCCESS.getStatus(), updateObj);
|
|
|
}
|
|
|
|
|
|
- private void notifyPayOrderSuccess(PayChannelDO channel, PayOrderNotifyRespDTO notify, PayNotifyReqDTO rawNotify) {
|
|
|
+ private void notifyPayOrder(PayChannelDO channel, PayOrderRespDTO notify) {
|
|
|
+ // 情况一:支付成功的回调
|
|
|
+ if (PayOrderStatusRespEnum.isSuccess(notify.getStatus())) {
|
|
|
+ notifyOrderSuccess(channel, notify);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 情况二:支付失败的回调
|
|
|
+ if (PayOrderStatusRespEnum.isClosed(notify.getStatus())) {
|
|
|
+ notifyOrderClosed(channel, notify);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void notifyOrderSuccess(PayChannelDO channel, PayOrderRespDTO notify) {
|
|
|
// 1. 更新 PayOrderExtensionDO 支付成功
|
|
|
- PayOrderExtensionDO orderExtension = updatePayOrderExtensionSuccess(notify.getOrderExtensionNo(), rawNotify);
|
|
|
+ PayOrderExtensionDO orderExtension = updateOrderExtensionSuccess(notify);
|
|
|
// 2. 更新 PayOrderDO 支付成功
|
|
|
- Pair<Boolean, PayOrderDO> order = updatePayOrderSuccess(channel, orderExtension, notify);
|
|
|
+ Pair<Boolean, PayOrderDO> order = updateOrderExtensionSuccess(channel, orderExtension, notify);
|
|
|
if (order.getKey()) { // 如果之前已经成功回调,则直接返回,不用重复记录支付通知记录;例如说:支付平台重复回调
|
|
|
return;
|
|
|
}
|
|
@@ -250,33 +294,30 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
/**
|
|
|
* 更新 PayOrderExtensionDO 支付成功
|
|
|
*
|
|
|
- * @param no 支付订单号(支付模块)
|
|
|
- * @param rawNotify 通知数据
|
|
|
+ * @param notify 通知
|
|
|
* @return PayOrderExtensionDO 对象
|
|
|
*/
|
|
|
- private PayOrderExtensionDO updatePayOrderExtensionSuccess(String no, PayNotifyReqDTO rawNotify) {
|
|
|
+ private PayOrderExtensionDO updateOrderExtensionSuccess(PayOrderRespDTO notify) {
|
|
|
// 1. 查询 PayOrderExtensionDO
|
|
|
- PayOrderExtensionDO orderExtension = orderExtensionMapper.selectByNo(no);
|
|
|
+ PayOrderExtensionDO orderExtension = orderExtensionMapper.selectByNo(notify.getOutTradeNo());
|
|
|
if (orderExtension == null) {
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_EXTENSION_NOT_FOUND);
|
|
|
+ throw exception(PAY_ORDER_EXTENSION_NOT_FOUND);
|
|
|
}
|
|
|
if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) { // 如果已经是成功,直接返回,不用重复更新
|
|
|
- log.info("[updatePayOrderSuccess][支付拓展单({}) 已经是已支付,无需更新为已支付]", orderExtension.getId());
|
|
|
+ log.info("[updateOrderExtensionSuccess][支付拓展单({}) 已经是已支付,无需更新]", orderExtension.getId());
|
|
|
return orderExtension;
|
|
|
}
|
|
|
if (ObjectUtil.notEqual(orderExtension.getStatus(), PayOrderStatusEnum.WAITING.getStatus())) { // 校验状态,必须是待支付
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING);
|
|
|
+ throw exception(PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING);
|
|
|
}
|
|
|
|
|
|
// 2. 更新 PayOrderExtensionDO
|
|
|
- int updateCounts = orderExtensionMapper.updateByIdAndStatus(orderExtension.getId(),
|
|
|
- PayOrderStatusEnum.WAITING.getStatus(), PayOrderExtensionDO.builder().id(orderExtension.getId())
|
|
|
- .status(PayOrderStatusEnum.SUCCESS.getStatus())
|
|
|
- .channelNotifyData(toJsonString(rawNotify)).build());
|
|
|
+ int updateCounts = orderExtensionMapper.updateByIdAndStatus(orderExtension.getId(), orderExtension.getStatus(),
|
|
|
+ PayOrderExtensionDO.builder().status(PayOrderStatusEnum.SUCCESS.getStatus()).channelNotifyData(toJsonString(notify)).build());
|
|
|
if (updateCounts == 0) { // 校验状态,必须是待支付
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING);
|
|
|
+ throw exception(PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING);
|
|
|
}
|
|
|
- log.info("[updatePayOrderSuccess][支付拓展单({}) 更新为已支付]", orderExtension.getId());
|
|
|
+ log.info("[updateOrderExtensionSuccess][支付拓展单({}) 更新为已支付]", orderExtension.getId());
|
|
|
return orderExtension;
|
|
|
}
|
|
|
|
|
@@ -289,20 +330,20 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
* @return key:是否之前已经成功回调
|
|
|
* value:PayOrderDO 对象
|
|
|
*/
|
|
|
- private Pair<Boolean, PayOrderDO> updatePayOrderSuccess(PayChannelDO channel, PayOrderExtensionDO orderExtension,
|
|
|
- PayOrderNotifyRespDTO notify) {
|
|
|
+ private Pair<Boolean, PayOrderDO> updateOrderExtensionSuccess(PayChannelDO channel, PayOrderExtensionDO orderExtension,
|
|
|
+ PayOrderRespDTO notify) {
|
|
|
// 1. 判断 PayOrderDO 是否处于待支付
|
|
|
PayOrderDO order = orderMapper.selectById(orderExtension.getOrderId());
|
|
|
if (order == null) {
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_NOT_FOUND);
|
|
|
+ throw exception(PAY_ORDER_NOT_FOUND);
|
|
|
}
|
|
|
if (PayOrderStatusEnum.isSuccess(order.getStatus()) // 如果已经是成功,直接返回,不用重复更新
|
|
|
&& Objects.equals(order.getSuccessExtensionId(), orderExtension.getId())) {
|
|
|
- log.info("[updatePayOrderSuccess][支付订单({}) 已经是已支付,无需更新为已支付]", order.getId());
|
|
|
+ log.info("[updateOrderExtensionSuccess][支付订单({}) 已经是已支付,无需更新]", order.getId());
|
|
|
return Pair.of(true, order);
|
|
|
}
|
|
|
if (!PayOrderStatusEnum.WAITING.getStatus().equals(order.getStatus())) { // 校验状态,必须是待支付
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_STATUS_IS_NOT_WAITING);
|
|
|
+ throw exception(PAY_ORDER_STATUS_IS_NOT_WAITING);
|
|
|
}
|
|
|
|
|
|
// 2. 更新 PayOrderDO
|
|
@@ -313,10 +354,43 @@ public class PayOrderServiceImpl implements PayOrderService {
|
|
|
.channelOrderNo(notify.getChannelOrderNo()).channelUserId(notify.getChannelUserId())
|
|
|
.notifyTime(LocalDateTime.now()).build());
|
|
|
if (updateCounts == 0) { // 校验状态,必须是待支付
|
|
|
- throw exception(ErrorCodeConstants.PAY_ORDER_STATUS_IS_NOT_WAITING);
|
|
|
+ throw exception(PAY_ORDER_STATUS_IS_NOT_WAITING);
|
|
|
}
|
|
|
- log.info("[updatePayOrderSuccess][支付订单({}) 更新为已支付]", order.getId());
|
|
|
+ log.info("[updateOrderExtensionSuccess][支付订单({}) 更新为已支付]", order.getId());
|
|
|
return Pair.of(false, order);
|
|
|
}
|
|
|
|
|
|
+ private void notifyOrderClosed(PayChannelDO channel, PayOrderRespDTO notify) {
|
|
|
+ updateOrderExtensionClosed(channel, notify);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateOrderExtensionClosed(PayChannelDO channel, PayOrderRespDTO notify) {
|
|
|
+ // 1. 查询 PayOrderExtensionDO
|
|
|
+ PayOrderExtensionDO orderExtension = orderExtensionMapper.selectByNo(notify.getOutTradeNo());
|
|
|
+ if (orderExtension == null) {
|
|
|
+ throw exception(PAY_ORDER_EXTENSION_NOT_FOUND);
|
|
|
+ }
|
|
|
+ if (PayOrderStatusEnum.isClosed(orderExtension.getStatus())) { // 如果已经是关闭,直接返回,不用重复更新
|
|
|
+ log.info("[updateOrderExtensionClosed][支付拓展单({}) 已经是支付关闭,无需更新]", orderExtension.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 一般出现先是支付成功,然后支付关闭,都是全部退款导致关闭的场景。这个情况,我们不更新支付拓展单,只通过退款流程,更新支付单
|
|
|
+ if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) {
|
|
|
+ log.info("[updateOrderExtensionClosed][支付拓展单({}) 是已支付,无需更新为支付关闭]", orderExtension.getId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (ObjectUtil.notEqual(orderExtension.getStatus(), PayOrderStatusEnum.WAITING.getStatus())) { // 校验状态,必须是待支付
|
|
|
+ throw exception(PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 更新 PayOrderExtensionDO
|
|
|
+ int updateCounts = orderExtensionMapper.updateByIdAndStatus(orderExtension.getId(), orderExtension.getStatus(),
|
|
|
+ PayOrderExtensionDO.builder().status(PayOrderStatusEnum.CLOSED.getStatus()).channelNotifyData(toJsonString(notify))
|
|
|
+ .channelErrorCode(notify.getChannelErrorCode()).channelErrorMsg(notify.getChannelErrorMsg()).build());
|
|
|
+ if (updateCounts == 0) { // 校验状态,必须是待支付
|
|
|
+ throw exception(PAY_ORDER_EXTENSION_STATUS_IS_NOT_WAITING);
|
|
|
+ }
|
|
|
+ log.info("[updateOrderExtensionClosed][支付拓展单({}) 更新为支付关闭]", orderExtension.getId());
|
|
|
+ }
|
|
|
+
|
|
|
}
|