Explorar o código

会员钱包 查询订单和查询退款单的实现

jason hai 1 ano
pai
achega
f3ca0f066b

+ 2 - 3
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<PayWalletTransac
         return selectOne(PayWalletTransactionDO::getNo, no);
     }
 
-    default PayWalletTransactionDO selectByWalletIdAndBiz(Long walletId, Long bizId, Integer bizType) {
-        return selectOne(PayWalletTransactionDO::getWalletId, walletId,
-                PayWalletTransactionDO::getBizId, bizId,
+    default PayWalletTransactionDO selectByBiz(String bizId, Integer bizType) {
+        return selectOne(PayWalletTransactionDO::getBizId, bizId,
                 PayWalletTransactionDO::getBizType, bizType);
     }
 

+ 71 - 4
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java

@@ -11,13 +11,23 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReq
 import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient;
 import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig;
 import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
+import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum;
+import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
+import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
 import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
+import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
+import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
+import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
+import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
 import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
+import cn.iocoder.yudao.module.pay.service.wallet.PayWalletTransactionService;
 import lombok.extern.slf4j.Slf4j;
 
 import java.util.Map;
 
 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
+import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.ORDER_EXTENSION_NOT_FOUND;
+import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.REFUND_NOT_FOUND;
 
 /**
  * 钱包支付的 PayClient 实现类
@@ -29,6 +39,12 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
 
     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<NonePayClientConfig> {
         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<NonePayClientConfig> {
             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<NonePayClientConfig> {
 
     @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<NonePayClientConfig> {
 
     @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));
     }
 
 }

+ 2 - 2
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);
 
 }

+ 3 - 3
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);
         }

+ 1 - 3
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);
 }

+ 2 - 4
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());
     }
-
 }