From 9812881094d4ff90e459bd5540df15cf6356c584 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 23 Jul 2023 21:16:03 +0800 Subject: [PATCH] =?UTF-8?q?mall=20+=20pay=EF=BC=9A=201.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=80=80=E6=AC=BE=E5=90=8C=E6=AD=A5=E7=9A=84=20Job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/dal/mysql/refund/PayRefundMapper.java | 4 ++ .../module/pay/job/order/PayOrderSyncJob.java | 2 +- .../yudao/module/pay/job/package-info.java | 4 -- .../pay/job/refund/PayRefundSyncJob.java | 31 ++++++++++++ .../service/order/PayOrderServiceImpl.java | 2 +- .../pay/service/refund/PayRefundService.java | 7 +++ .../service/refund/PayRefundServiceImpl.java | 47 ++++++++++++++++++- 7 files changed, 89 insertions(+), 8 deletions(-) delete mode 100644 yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/package-info.java create mode 100644 yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/refund/PayRefundSyncJob.java diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/refund/PayRefundMapper.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/refund/PayRefundMapper.java index 788576059..af5fad77b 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/refund/PayRefundMapper.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/refund/PayRefundMapper.java @@ -68,4 +68,8 @@ public interface PayRefundMapper extends BaseMapperX { .orderByDesc(PayRefundDO::getId)); } + default List selectListByStatus(Integer status) { + return selectList(PayRefundDO::getStatus, status); + } + } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/order/PayOrderSyncJob.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/order/PayOrderSyncJob.java index 8a60a1ee5..c11ade216 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/order/PayOrderSyncJob.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/order/PayOrderSyncJob.java @@ -37,7 +37,7 @@ public class PayOrderSyncJob implements JobHandler { public String execute(String param) { LocalDateTime minCreateTime = LocalDateTime.now().minus(CREATE_TIME_DURATION_BEFORE); int count = orderService.syncOrder(minCreateTime); - return StrUtil.format("同步订单 {} 个", count); + return StrUtil.format("同步支付订单 {} 个", count); } } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/package-info.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/package-info.java deleted file mode 100644 index 845a73547..000000000 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 占位,无特殊含义 - */ -package cn.iocoder.yudao.module.pay.job; diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/refund/PayRefundSyncJob.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/refund/PayRefundSyncJob.java new file mode 100644 index 000000000..23bb0870d --- /dev/null +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/job/refund/PayRefundSyncJob.java @@ -0,0 +1,31 @@ +package cn.iocoder.yudao.module.pay.job.refund; + +import cn.hutool.core.util.StrUtil; +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; +import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; +import cn.iocoder.yudao.module.pay.service.refund.PayRefundService; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; + +/** + * 退款订单的同步 Job + * + * 由于退款订单的状态,是由支付渠道异步通知进行同步,考虑到异步通知可能会失败(小概率),所以需要定时进行同步。 + * + * @author 芋道源码 + */ +@Component +@TenantJob +public class PayRefundSyncJob implements JobHandler { + + @Resource + private PayRefundService refundService; + + @Override + public String execute(String param) { + int count = refundService.syncRefund(); + return StrUtil.format("同步退款订单 {} 个", count); + } + +} diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java index 928423171..fef1625b6 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java @@ -450,7 +450,7 @@ public class PayOrderServiceImpl implements PayOrderService { // 1.2 回调支付结果 notifyOrder(orderExtension.getChannelId(), respDTO); - // 2. 如果是已支付,则返回 1 + // 2. 如果是已支付,则返回 true return PayOrderStatusRespEnum.isSuccess(respDTO.getStatus()); } catch (Throwable e) { log.error("[syncOrder][orderExtension({}) 同步支付状态异常]", orderExtension.getId(), e); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundService.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundService.java index ef5c15803..8a93ec4da 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundService.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundService.java @@ -64,4 +64,11 @@ public interface PayRefundService { */ void notifyRefund(Long channelId, PayRefundRespDTO notify); + /** + * 同步渠道退款的退款状态 + * + * @return 同步到状态的退款数量,包括退款成功、退款失败 + */ + int syncRefund(); + } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java index a0e0abe8e..a6e88ccf1 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.pay.service.refund; +import cn.hutool.core.collection.CollUtil; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.pay.core.client.PayClient; @@ -242,7 +243,7 @@ public class PayRefundServiceImpl implements PayRefundService { // 2. 更新订单 orderService.updateOrderRefundPrice(refund.getOrderId(), refund.getRefundPrice()); - // 3. 插入退款通知记录 TODO 芋艿:退款成功 + // 3. 插入退款通知记录 notifyService.createPayNotifyTask(PayNotifyTaskCreateReqDTO.builder() .type(PayNotifyTypeEnum.REFUND.getType()).dataId(refund.getId()).build()); } @@ -274,11 +275,53 @@ public class PayRefundServiceImpl implements PayRefundService { } log.info("[notifyRefundFailure][退款订单({}) 更新为退款失败]", refund.getId()); - // 2. 插入退款通知记录 TODO 芋艿:退款失败 + // 2. 插入退款通知记录 notifyService.createPayNotifyTask(PayNotifyTaskCreateReqDTO.builder() .type(PayNotifyTypeEnum.REFUND.getType()).dataId(refund.getId()).build()); } + @Override + public int syncRefund() { + // 1. 查询指定创建时间内的待退款订单 + List refunds = refundMapper.selectListByStatus(PayRefundStatusEnum.WAITING.getStatus()); + if (CollUtil.isEmpty(refunds)) { + return 0; + } + // 2. 遍历执行 + int count = 0; + for (PayRefundDO refund : refunds) { + count += syncRefund(refund) ? 1 : 0; + } + return count; + } + + /** + * 同步单个退款订单 + * + * @param refund 退款订单 + * @return 是否同步到 + */ + private boolean syncRefund(PayRefundDO refund) { + try { + // 1.1 查询退款订单信息 + PayClient payClient = payClientFactory.getPayClient(refund.getChannelId()); + if (payClient == null) { + log.error("[syncRefund][渠道编号({}) 找不到对应的支付客户端]", refund.getChannelId()); + return false; + } + PayRefundRespDTO respDTO = payClient.getRefund(refund.getOrderNo(), refund.getNo()); + // 1.2 回调退款结果 + notifyRefund(refund.getChannelId(), respDTO); + + // 2. 如果同步到,则返回 true + return PayRefundStatusEnum.isSuccess(respDTO.getStatus()) + || PayRefundStatusEnum.isFailure(respDTO.getStatus()); + } catch (Throwable e) { + log.error("[syncRefund][refund({}) 同步退款状态异常]", refund.getId(), e); + return false; + } + } + /** * 获得自身的代理对象,解决 AOP 生效问题 *