From 3bde43b148faf4e95bdc655cf65c0ceed178d8bc Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 24 Sep 2024 09:34:52 +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=E9=92=B1=E5=8C=85=E5=85=85?= =?UTF-8?q?=E5=80=BC=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 | 13 +-- .../wallet/PayWalletRechargeServiceImpl.java | 86 ++++++++++--------- 2 files changed, 54 insertions(+), 45 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 75ede8fa9..cf3d054f3 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 @@ -115,18 +115,19 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService { // 1.1 校验订单是否存在 PayDemoOrderDO order = payDemoOrderMapper.selectById(id); if (order == null) { + log.error("[updateDemoOrderPaid][order({}) payOrder({}) 不存在订单,请进行处理!]", id, payOrderId); throw exception(DEMO_ORDER_NOT_FOUND); } // 1.2 校验订单已支付 if (order.getPayStatus()) { // 特殊:如果订单已支付,且支付单号相同,直接返回,说明重复回调 if (ObjectUtil.equals(order.getPayOrderId(), payOrderId)) { - log.warn("[updateDemoOrderPaid][order({}) 已支付,且支付单号相同({}),直接返回]", id, payOrderId); + log.warn("[updateDemoOrderPaid][order({}) 已支付,且支付单号相同({}),直接返回]", order, payOrderId); return; } // 异常:支付单号不同,说明支付单号错误 log.error("[updateDemoOrderPaid][order({}) 支付单不匹配({}),请进行处理!order 数据是:{}]", - id, payOrderId, toJsonString(order)); + order, payOrderId, toJsonString(order)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); } @@ -153,24 +154,24 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService { // 1. 校验支付单是否存在 PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId); if (payOrder == null) { - log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 不存在,请进行处理!]", order, payOrderId); + log.error("[validatePayOrderPaid][order({}) payOrder({}) 不存在,请进行处理!]", order, payOrderId); throw exception(PAY_ORDER_NOT_FOUND); } // 2.1 校验支付单已支付 if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { - log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 未支付,请进行处理!payOrder 数据是:{}]", + log.error("[validatePayOrderPaid][order({}) payOrder({}) 未支付,请进行处理!payOrder 数据是:{}]", order, payOrderId, toJsonString(payOrder)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_STATUS_NOT_SUCCESS); } // 2.1 校验支付金额一致 if (notEqual(payOrder.getPrice(), order.getPrice())) { - log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 支付金额不匹配,请进行处理!order 数据是:{},payOrder 数据是:{}]", + log.error("[validatePayOrderPaid][order({}) payOrder({}) 支付金额不匹配,请进行处理!order 数据是:{},payOrder 数据是:{}]", order, payOrderId, toJsonString(order), toJsonString(payOrder)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_PRICE_NOT_MATCH); } // 2.2 校验支付订单匹配(二次) if (notEqual(payOrder.getMerchantOrderId(), order.getId().toString())) { - log.error("[validateDemoOrderCanPaid][order({}) 支付单不匹配({}),请进行处理!payOrder 数据是:{}]", + log.error("[validatePayOrderPaid][order({}) 支付单不匹配({}),请进行处理!payOrder 数据是:{}]", order, payOrderId, toJsonString(payOrder)); throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR); } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletRechargeServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletRechargeServiceImpl.java index 98e32ec79..bfa656112 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletRechargeServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletRechargeServiceImpl.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.pay.service.wallet; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; @@ -113,16 +114,28 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService { @Override @Transactional(rollbackFor = Exception.class) public void updateWalletRechargerPaid(Long id, Long payOrderId) { - // 1.1 获取钱包充值记录 - PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(id); - if (walletRecharge == null) { - log.error("[updateWalletRechargerPaid][钱包充值记录不存在,钱包充值记录 id({})]", id); + // 1.1 校验钱包充值是否存在 + PayWalletRechargeDO recharge = walletRechargeMapper.selectById(id); + if (recharge == null) { + log.error("[updateWalletRechargerPaid][recharge({}) payOrder({}) 不存在充值订单,请进行处理!]", id, payOrderId); throw exception(WALLET_RECHARGE_NOT_FOUND); } // 1.2 校验钱包充值是否可以支付 - PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId); + if (recharge.getPayStatus()) { + // 特殊:如果订单已支付,且支付单号相同,直接返回,说明重复回调 + if (ObjectUtil.equals(recharge.getPayOrderId(), payOrderId)) { + log.warn("[updateWalletRechargerPaid][recharge({}) 已支付,且支付单号相同({}),直接返回]", recharge, payOrderId); + return; + } + // 异常:支付单号不同,说明支付单号错误 + log.error("[updateWalletRechargerPaid][recharge({}) 已支付,但是支付单号不同({}),请进行处理!]", recharge, payOrderId); + throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); + } - // 2. 更新钱包充值的支付状态 + // 2. 校验支付订单的合法性 + PayOrderDO payOrderDO = validatePayOrderPaid(recharge, payOrderId); + + // 3. 更新钱包充值的支付状态 int updateCount = walletRechargeMapper.updateByIdAndPaid(id, false, new PayWalletRechargeDO().setId(id).setPayStatus(true).setPayTime(LocalDateTime.now()) .setPayChannelCode(payOrderDO.getChannelCode())); @@ -130,14 +143,14 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService { throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); } - // 3. 更新钱包余额 + // 4. 更新钱包余额 // TODO @jason:这样的话,未来提现会不会把充值的,也提现走哈。类似先充 100,送 110;然后提现 110; // TODO 需要钱包中加个可提现余额 - payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(id), - PayWalletBizTypeEnum.RECHARGE, walletRecharge.getTotalPrice()); + payWalletService.addWalletBalance(recharge.getWalletId(), String.valueOf(id), + PayWalletBizTypeEnum.RECHARGE, recharge.getTotalPrice()); - // 4. 发送订阅消息 - getSelf().sendWalletRechargerPaidMessage(payOrderId, walletRecharge); + // 5. 发送订阅消息 + getSelf().sendWalletRechargerPaidMessage(payOrderId, recharge); } @Async @@ -267,43 +280,38 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService { return wallet; } - private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) { - // 1.1 校验充值记录的支付状态 - if (walletRecharge.getPayStatus()) { - log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]", - walletRecharge.getId(), toJsonString(walletRecharge)); - throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID); - } - // 1.2 校验支付订单匹配 - if (notEqual(walletRecharge.getPayOrderId(), payOrderId)) { // 支付单号 - log.error("[validateWalletRechargerCanPaid][钱包({}) 支付单不匹配({}),请进行处理! 钱包数据是:{}]", - walletRecharge.getId(), payOrderId, toJsonString(walletRecharge)); - throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); - } - - // 2.1 校验支付单是否存在 + /** + * 校验支付订单的合法性 + * + * @param recharge 充值订单 + * @param payOrderId 支付订单编号 + * @return 支付订单 + */ + private PayOrderDO validatePayOrderPaid(PayWalletRechargeDO recharge, Long payOrderId) { + // 1. 校验支付单是否存在 PayOrderDO payOrder = payOrderService.getOrder(payOrderId); if (payOrder == null) { - log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 不存在,请进行处理!]", - walletRecharge.getId(), payOrderId); + log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 不存在,请进行处理!]", + recharge, payOrderId); throw exception(PAY_ORDER_NOT_FOUND); } - // 2.2 校验支付单已支付 + + // 2.1 校验支付单已支付 if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) { - log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 未支付,请进行处理!payOrder 数据是:{}]", - walletRecharge.getId(), payOrderId, toJsonString(payOrder)); + log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 未支付,请进行处理!payOrder 数据是:{}]", + recharge, payOrderId, toJsonString(payOrder)); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS); } - // 2.3 校验支付金额一致 - if (notEqual(payOrder.getPrice(), walletRecharge.getPayPrice())) { - log.error("[validateDemoOrderCanPaid][钱包({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{},payOrder 数据是:{}]", - walletRecharge.getId(), payOrderId, toJsonString(walletRecharge), toJsonString(payOrder)); + // 2.2 校验支付金额一致 + if (notEqual(payOrder.getPrice(), recharge.getPayPrice())) { + log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{},payOrder 数据是:{}]", + recharge, payOrderId, toJsonString(recharge), toJsonString(payOrder)); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH); } - // 2.4 校验支付订单的商户订单匹配 - if (notEqual(payOrder.getMerchantOrderId(), walletRecharge.getId().toString())) { - log.error("[validateDemoOrderCanPaid][钱包({}) 支付单不匹配({}),请进行处理!payOrder 数据是:{}]", - walletRecharge.getId(), payOrderId, toJsonString(payOrder)); + // 2.3 校验支付订单的商户订单匹配 + if (notEqual(payOrder.getMerchantOrderId(), recharge.getId().toString())) { + log.error("[validatePayOrderPaid][充值订单({}) 支付单不匹配({}),请进行处理!payOrder 数据是:{}]", + recharge, payOrderId, toJsonString(payOrder)); throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR); } return payOrder;