From 57a934ae09bbaebaf7ecc51f8f8b58f806e75414 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 24 Sep 2024 09:23:08 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E3=80=91=E6=94=AF=E4=BB=98=EF=BC=9A=E7=A4=BA=E4=BE=8B=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E7=9A=84=E6=94=AF=E4=BB=98=E5=9B=9E=E8=B0=83=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E2=80=9C=E5=B7=B2=E6=94=AF=E4=BB=98=E2=80=9D?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E7=9A=84=E9=98=B2=E9=87=8D=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/demo/PayDemoOrderServiceImpl.java | 74 +++++++++---------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/demo/PayDemoOrderServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/demo/PayDemoOrderServiceImpl.java index 29a9e9aec..75ede8fa9 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/demo/PayDemoOrderServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/demo/PayDemoOrderServiceImpl.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.pay.service.demo; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.pay.api.order.PayOrderApi; @@ -14,11 +15,11 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoOrderDO; import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoOrderMapper; import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum; import cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import jakarta.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; import java.util.HashMap; @@ -111,10 +112,28 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService { @Override public void updateDemoOrderPaid(Long id, Long payOrderId) { - // 校验并获得支付订单(可支付) - PayOrderRespDTO payOrder = validateDemoOrderCanPaid(id, payOrderId); + // 1.1 校验订单是否存在 + PayDemoOrderDO order = payDemoOrderMapper.selectById(id); + if (order == null) { + throw exception(DEMO_ORDER_NOT_FOUND); + } + // 1.2 校验订单已支付 + if (order.getPayStatus()) { + // 特殊:如果订单已支付,且支付单号相同,直接返回,说明重复回调 + if (ObjectUtil.equals(order.getPayOrderId(), payOrderId)) { + log.warn("[updateDemoOrderPaid][order({}) 已支付,且支付单号相同({}),直接返回]", id, payOrderId); + return; + } + // 异常:支付单号不同,说明支付单号错误 + log.error("[updateDemoOrderPaid][order({}) 支付单不匹配({}),请进行处理!order 数据是:{}]", + id, payOrderId, toJsonString(order)); + throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); + } - // 更新 PayDemoOrderDO 状态为已支付 + // 2. 校验支付订单的合法性 + PayOrderRespDTO payOrder = validatePayOrderPaid(order, payOrderId); + + // 3. 更新 PayDemoOrderDO 状态为已支付 int updateCount = payDemoOrderMapper.updateByIdAndPayed(id, false, new PayDemoOrderDO().setPayStatus(true).setPayTime(LocalDateTime.now()) .setPayChannelCode(payOrder.getChannelCode())); @@ -124,56 +143,35 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService { } /** - * 校验交易订单满足被支付的条件 + * 校验支付订单的合法性 * - * 1. 交易订单未支付 - * 2. 支付单已支付 - * - * @param id 交易订单编号 + * @param order 交易订单 * @param payOrderId 支付订单编号 - * @return 交易订单 + * @return 支付订单 */ - private PayOrderRespDTO validateDemoOrderCanPaid(Long id, Long payOrderId) { - // 1.1 校验订单是否存在 - PayDemoOrderDO order = payDemoOrderMapper.selectById(id); - if (order == null) { - throw exception(DEMO_ORDER_NOT_FOUND); - } - // 1.2 校验订单未支付 - if (order.getPayStatus()) { - log.error("[validateDemoOrderCanPaid][order({}) 不处于待支付状态,请进行处理!order 数据是:{}]", - id, toJsonString(order)); - throw exception(DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID); - } - // 1.3 校验支付订单匹配 - if (notEqual(order.getPayOrderId(), payOrderId)) { // 支付单号 - log.error("[validateDemoOrderCanPaid][order({}) 支付单不匹配({}),请进行处理!order 数据是:{}]", - id, payOrderId, toJsonString(order)); - throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); - } - - // 2.1 校验支付单是否存在 + private PayOrderRespDTO validatePayOrderPaid(PayDemoOrderDO order, Long payOrderId) { + // 1. 校验支付单是否存在 PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId); if (payOrder == null) { - log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 不存在,请进行处理!]", id, payOrderId); + log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 不存在,请进行处理!]", order, payOrderId); throw exception(PAY_ORDER_NOT_FOUND); } - // 2.2 校验支付单已支付 + // 2.1 校验支付单已支付 if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 未支付,请进行处理!payOrder 数据是:{}]", - id, payOrderId, toJsonString(payOrder)); + order, payOrderId, toJsonString(payOrder)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_STATUS_NOT_SUCCESS); } - // 2.3 校验支付金额一致 + // 2.1 校验支付金额一致 if (notEqual(payOrder.getPrice(), order.getPrice())) { log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 支付金额不匹配,请进行处理!order 数据是:{},payOrder 数据是:{}]", - id, payOrderId, toJsonString(order), toJsonString(payOrder)); + order, payOrderId, toJsonString(order), toJsonString(payOrder)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_PRICE_NOT_MATCH); } - // 2.4 校验支付订单匹配(二次) - if (notEqual(payOrder.getMerchantOrderId(), id.toString())) { + // 2.2 校验支付订单匹配(二次) + if (notEqual(payOrder.getMerchantOrderId(), order.getId().toString())) { log.error("[validateDemoOrderCanPaid][order({}) 支付单不匹配({}),请进行处理!payOrder 数据是:{}]", - id, payOrderId, toJsonString(payOrder)); + order, payOrderId, toJsonString(payOrder)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); } return payOrder;