From f3ca0f066b64d43c3b35c9b204e135a18c9529c9 Mon Sep 17 00:00:00 2001 From: jason <2667446@qq.com> Date: Tue, 5 Sep 2023 12:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E5=91=98=E9=92=B1=E5=8C=85=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AE=A2=E5=8D=95=E5=92=8C=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E9=80=80=E6=AC=BE=E5=8D=95=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wallet/PayWalletTransactionMapper.java | 5 +- .../framework/pay/wallet/WalletPayClient.java | 75 ++++++++++++++++++- .../pay/service/wallet/PayWalletService.java | 4 +- .../service/wallet/PayWalletServiceImpl.java | 6 +- .../wallet/PayWalletTransactionService.java | 4 +- .../PayWalletTransactionServiceImpl.java | 6 +- 6 files changed, 81 insertions(+), 19 deletions(-) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/wallet/PayWalletTransactionMapper.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/wallet/PayWalletTransactionMapper.java index 3d329d118..7831e77bd 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/wallet/PayWalletTransactionMapper.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/wallet/PayWalletTransactionMapper.java @@ -30,9 +30,8 @@ public interface PayWalletTransactionMapper extends BaseMapperX { private PayWalletService wallService; + private PayWalletTransactionService walletTransactionService; + + private PayOrderService payOrderService; + + private PayRefundService payRefundService; + public WalletPayClient(Long channelId, NonePayClientConfig config) { super(channelId, PayChannelEnum.WALLET.getCode(), config); } @@ -38,6 +54,9 @@ public class WalletPayClient extends AbstractPayClient { if (wallService == null) { wallService = SpringUtil.getBean(PayWalletService.class); } + if (walletTransactionService == null) { + walletTransactionService = SpringUtil.getBean(PayWalletTransactionService.class); + } } @Override @@ -47,7 +66,7 @@ public class WalletPayClient extends AbstractPayClient { String userType = MapUtil.getStr(reqDTO.getChannelExtras(), "user_type"); Assert.notEmpty(userId, "用户 id 不能为空"); Assert.notEmpty(userType, "用户类型不能为空"); - PayWalletTransactionDO transaction = wallService.pay(Long.valueOf(userId), Integer.valueOf(userType), + PayWalletTransactionDO transaction = wallService.orderPay(Long.valueOf(userId), Integer.valueOf(userType), reqDTO.getOutTradeNo(), reqDTO.getPrice()); return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), transaction.getCreateTime(), @@ -73,13 +92,37 @@ public class WalletPayClient extends AbstractPayClient { @Override protected PayOrderRespDTO doGetOrder(String outTradeNo) { - throw new UnsupportedOperationException("待实现"); + if (payOrderService == null) { + payOrderService = SpringUtil.getBean(PayOrderService.class); + } + PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo); + // 支付交易拓展单不存在, 返回关闭状态 + if (orderExtension == null) { + return PayOrderRespDTO.closedOf(String.valueOf(ORDER_EXTENSION_NOT_FOUND.getCode()), + ORDER_EXTENSION_NOT_FOUND.getMsg(), outTradeNo, ""); + } + // 关闭状态 + if (PayOrderStatusEnum.isClosed(orderExtension.getStatus())) { + return PayOrderRespDTO.closedOf(orderExtension.getChannelErrorCode(), + orderExtension.getChannelErrorMsg(), outTradeNo, ""); + } + // 成功状态 + if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) { + PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction( + String.valueOf(orderExtension.getOrderId()), PayWalletBizTypeEnum.PAYMENT); + Assert.notNull(walletTransaction, "支付单 {} 钱包流水不能为空", outTradeNo); + return PayOrderRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreator(), + walletTransaction.getCreateTime(), outTradeNo, walletTransaction); + } + // 其它状态为无效状态 + log.error("[doGetOrder] 支付单 {} 的状态不正确", outTradeNo); + throw new IllegalStateException(String.format("支付单[%s] 状态不正确", outTradeNo)); } @Override protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { try { - PayWalletTransactionDO payWalletTransaction = wallService.refund(reqDTO.getOutRefundNo(), + PayWalletTransactionDO payWalletTransaction = wallService.orderRefund(reqDTO.getOutRefundNo(), reqDTO.getRefundPrice(), reqDTO.getReason()); return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreateTime(), reqDTO.getOutRefundNo(), payWalletTransaction); @@ -104,7 +147,31 @@ public class WalletPayClient extends AbstractPayClient { @Override protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) { - throw new UnsupportedOperationException("待实现"); + if (payRefundService == null) { + payRefundService = SpringUtil.getBean(PayRefundService.class); + } + PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo); + // 支付退款单不存在, 返回退款失败状态 + if (payRefund == null) { + return PayRefundRespDTO.failureOf(String.valueOf(REFUND_NOT_FOUND), REFUND_NOT_FOUND.getMsg(), + outRefundNo, ""); + } + // 退款失败 + if (PayRefundStatusRespEnum.isFailure(payRefund.getStatus())) { + return PayRefundRespDTO.failureOf(payRefund.getChannelErrorCode(), payRefund.getChannelErrorMsg(), + outRefundNo, ""); + } + // 退款成功 + if (PayRefundStatusRespEnum.isSuccess(payRefund.getStatus())) { + PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction( + String.valueOf(payRefund.getId()), PayWalletBizTypeEnum.PAYMENT_REFUND); + Assert.notNull(walletTransaction, "支付退款单 {} 钱包流水不能为空", outRefundNo); + return PayRefundRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreateTime(), + outRefundNo, walletTransaction); + } + // 其它状态为无效状态 + log.error("[doGetRefund] 支付退款单 {} 的状态不正确", outRefundNo); + throw new IllegalStateException(String.format("支付退款单[%s] 状态不正确", outRefundNo)); } } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletService.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletService.java index 370b776a7..3ea75ad09 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletService.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletService.java @@ -27,7 +27,7 @@ public interface PayWalletService { * @param outTradeNo 外部订单号 * @param price 金额 */ - PayWalletTransactionDO pay(Long userId, Integer userType, String outTradeNo, Integer price); + PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price); /** @@ -64,6 +64,6 @@ public interface PayWalletService { * @param refundPrice 退款金额 * @param reason 退款原因 */ - PayWalletTransactionDO refund(String outRefundNo, Integer refundPrice, String reason); + PayWalletTransactionDO orderRefund(String outRefundNo, Integer refundPrice, String reason); } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java index a3606323b..ea607c0e8 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java @@ -75,7 +75,7 @@ public class PayWalletServiceImpl implements PayWalletService { @Override @Transactional(rollbackFor = Exception.class) - public PayWalletTransactionDO pay(Long userId, Integer userType, String outTradeNo, Integer price) { + public PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price) { // 判断支付交易拓展单是否存 PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo); if (orderExtension == null) { @@ -150,7 +150,7 @@ public class PayWalletServiceImpl implements PayWalletService { @Override @Transactional(rollbackFor = Exception.class) - public PayWalletTransactionDO refund(String outRefundNo, Integer refundPrice, String reason) { + public PayWalletTransactionDO orderRefund(String outRefundNo, Integer refundPrice, String reason) { // 1.1 判断退款单是否存在 PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo); if (payRefund == null) { @@ -182,7 +182,7 @@ public class PayWalletServiceImpl implements PayWalletService { throw exception(WALLET_REFUND_AMOUNT_ERROR); } PayWalletTransactionDO refundTransaction = payWalletTransactionService.getWalletTransaction( - payWalletTransaction.getWalletId(), refundId, PAYMENT_REFUND); + String.valueOf(refundId), PAYMENT_REFUND); if (refundTransaction != null) { throw exception(WALLET_REFUND_EXIST); } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionService.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionService.java index e0714081b..432e2f2e0 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionService.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionService.java @@ -40,11 +40,9 @@ public interface PayWalletTransactionService { /** * 获取钱包流水 * - * @param walletId 钱包编号 * @param bizId 业务编号 * @param type 业务类型 * @return 钱包流水 */ - PayWalletTransactionDO getWalletTransaction(Long walletId, Long bizId, PayWalletBizTypeEnum type); - + PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type); } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionServiceImpl.java index 1b156576a..971f59416 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletTransactionServiceImpl.java @@ -22,7 +22,6 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ @Resource private PayWalletService payWalletService; - @Resource private PayWalletTransactionMapper payWalletTransactionMapper; @@ -45,8 +44,7 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ } @Override - public PayWalletTransactionDO getWalletTransaction(Long walletId, Long bizId, PayWalletBizTypeEnum type) { - return payWalletTransactionMapper.selectByWalletIdAndBiz(walletId, bizId, type.getType()); + public PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type) { + return payWalletTransactionMapper.selectByBiz(bizId, type.getType()); } - }