Commit f3ca0f066b64d43c3b35c9b204e135a18c9529c9

Authored by jason
1 parent cc4c9247

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

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 @@ -30,9 +30,8 @@ public interface PayWalletTransactionMapper extends BaseMapperX<PayWalletTransac
30 return selectOne(PayWalletTransactionDO::getNo, no); 30 return selectOne(PayWalletTransactionDO::getNo, no);
31 } 31 }
32 32
33 - default PayWalletTransactionDO selectByWalletIdAndBiz(Long walletId, Long bizId, Integer bizType) {  
34 - return selectOne(PayWalletTransactionDO::getWalletId, walletId,  
35 - PayWalletTransactionDO::getBizId, bizId, 33 + default PayWalletTransactionDO selectByBiz(String bizId, Integer bizType) {
  34 + return selectOne(PayWalletTransactionDO::getBizId, bizId,
36 PayWalletTransactionDO::getBizType, bizType); 35 PayWalletTransactionDO::getBizType, bizType);
37 } 36 }
38 37
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 @@ -11,13 +11,23 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReq
11 import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; 11 import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient;
12 import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; 12 import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig;
13 import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; 13 import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
  14 +import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum;
  15 +import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
  16 +import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
14 import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; 17 import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
  18 +import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
  19 +import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
  20 +import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
  21 +import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
15 import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; 22 import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
  23 +import cn.iocoder.yudao.module.pay.service.wallet.PayWalletTransactionService;
16 import lombok.extern.slf4j.Slf4j; 24 import lombok.extern.slf4j.Slf4j;
17 25
18 import java.util.Map; 26 import java.util.Map;
19 27
20 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; 28 import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
  29 +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.ORDER_EXTENSION_NOT_FOUND;
  30 +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.REFUND_NOT_FOUND;
21 31
22 /** 32 /**
23 * 钱包支付的 PayClient 实现类 33 * 钱包支付的 PayClient 实现类
@@ -29,6 +39,12 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { @@ -29,6 +39,12 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
29 39
30 private PayWalletService wallService; 40 private PayWalletService wallService;
31 41
  42 + private PayWalletTransactionService walletTransactionService;
  43 +
  44 + private PayOrderService payOrderService;
  45 +
  46 + private PayRefundService payRefundService;
  47 +
32 public WalletPayClient(Long channelId, NonePayClientConfig config) { 48 public WalletPayClient(Long channelId, NonePayClientConfig config) {
33 super(channelId, PayChannelEnum.WALLET.getCode(), config); 49 super(channelId, PayChannelEnum.WALLET.getCode(), config);
34 } 50 }
@@ -38,6 +54,9 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { @@ -38,6 +54,9 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
38 if (wallService == null) { 54 if (wallService == null) {
39 wallService = SpringUtil.getBean(PayWalletService.class); 55 wallService = SpringUtil.getBean(PayWalletService.class);
40 } 56 }
  57 + if (walletTransactionService == null) {
  58 + walletTransactionService = SpringUtil.getBean(PayWalletTransactionService.class);
  59 + }
41 } 60 }
42 61
43 @Override 62 @Override
@@ -47,7 +66,7 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { @@ -47,7 +66,7 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
47 String userType = MapUtil.getStr(reqDTO.getChannelExtras(), "user_type"); 66 String userType = MapUtil.getStr(reqDTO.getChannelExtras(), "user_type");
48 Assert.notEmpty(userId, "用户 id 不能为空"); 67 Assert.notEmpty(userId, "用户 id 不能为空");
49 Assert.notEmpty(userType, "用户类型不能为空"); 68 Assert.notEmpty(userType, "用户类型不能为空");
50 - PayWalletTransactionDO transaction = wallService.pay(Long.valueOf(userId), Integer.valueOf(userType), 69 + PayWalletTransactionDO transaction = wallService.orderPay(Long.valueOf(userId), Integer.valueOf(userType),
51 reqDTO.getOutTradeNo(), reqDTO.getPrice()); 70 reqDTO.getOutTradeNo(), reqDTO.getPrice());
52 return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), 71 return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(),
53 transaction.getCreateTime(), 72 transaction.getCreateTime(),
@@ -73,13 +92,37 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { @@ -73,13 +92,37 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
73 92
74 @Override 93 @Override
75 protected PayOrderRespDTO doGetOrder(String outTradeNo) { 94 protected PayOrderRespDTO doGetOrder(String outTradeNo) {
76 - throw new UnsupportedOperationException("待实现"); 95 + if (payOrderService == null) {
  96 + payOrderService = SpringUtil.getBean(PayOrderService.class);
  97 + }
  98 + PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo);
  99 + // 支付交易拓展单不存在, 返回关闭状态
  100 + if (orderExtension == null) {
  101 + return PayOrderRespDTO.closedOf(String.valueOf(ORDER_EXTENSION_NOT_FOUND.getCode()),
  102 + ORDER_EXTENSION_NOT_FOUND.getMsg(), outTradeNo, "");
  103 + }
  104 + // 关闭状态
  105 + if (PayOrderStatusEnum.isClosed(orderExtension.getStatus())) {
  106 + return PayOrderRespDTO.closedOf(orderExtension.getChannelErrorCode(),
  107 + orderExtension.getChannelErrorMsg(), outTradeNo, "");
  108 + }
  109 + // 成功状态
  110 + if (PayOrderStatusEnum.isSuccess(orderExtension.getStatus())) {
  111 + PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction(
  112 + String.valueOf(orderExtension.getOrderId()), PayWalletBizTypeEnum.PAYMENT);
  113 + Assert.notNull(walletTransaction, "支付单 {} 钱包流水不能为空", outTradeNo);
  114 + return PayOrderRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreator(),
  115 + walletTransaction.getCreateTime(), outTradeNo, walletTransaction);
  116 + }
  117 + // 其它状态为无效状态
  118 + log.error("[doGetOrder] 支付单 {} 的状态不正确", outTradeNo);
  119 + throw new IllegalStateException(String.format("支付单[%s] 状态不正确", outTradeNo));
77 } 120 }
78 121
79 @Override 122 @Override
80 protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { 123 protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
81 try { 124 try {
82 - PayWalletTransactionDO payWalletTransaction = wallService.refund(reqDTO.getOutRefundNo(), 125 + PayWalletTransactionDO payWalletTransaction = wallService.orderRefund(reqDTO.getOutRefundNo(),
83 reqDTO.getRefundPrice(), reqDTO.getReason()); 126 reqDTO.getRefundPrice(), reqDTO.getReason());
84 return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreateTime(), 127 return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreateTime(),
85 reqDTO.getOutRefundNo(), payWalletTransaction); 128 reqDTO.getOutRefundNo(), payWalletTransaction);
@@ -104,7 +147,31 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { @@ -104,7 +147,31 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
104 147
105 @Override 148 @Override
106 protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) { 149 protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) {
107 - throw new UnsupportedOperationException("待实现"); 150 + if (payRefundService == null) {
  151 + payRefundService = SpringUtil.getBean(PayRefundService.class);
  152 + }
  153 + PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo);
  154 + // 支付退款单不存在, 返回退款失败状态
  155 + if (payRefund == null) {
  156 + return PayRefundRespDTO.failureOf(String.valueOf(REFUND_NOT_FOUND), REFUND_NOT_FOUND.getMsg(),
  157 + outRefundNo, "");
  158 + }
  159 + // 退款失败
  160 + if (PayRefundStatusRespEnum.isFailure(payRefund.getStatus())) {
  161 + return PayRefundRespDTO.failureOf(payRefund.getChannelErrorCode(), payRefund.getChannelErrorMsg(),
  162 + outRefundNo, "");
  163 + }
  164 + // 退款成功
  165 + if (PayRefundStatusRespEnum.isSuccess(payRefund.getStatus())) {
  166 + PayWalletTransactionDO walletTransaction = walletTransactionService.getWalletTransaction(
  167 + String.valueOf(payRefund.getId()), PayWalletBizTypeEnum.PAYMENT_REFUND);
  168 + Assert.notNull(walletTransaction, "支付退款单 {} 钱包流水不能为空", outRefundNo);
  169 + return PayRefundRespDTO.successOf(walletTransaction.getNo(), walletTransaction.getCreateTime(),
  170 + outRefundNo, walletTransaction);
  171 + }
  172 + // 其它状态为无效状态
  173 + log.error("[doGetRefund] 支付退款单 {} 的状态不正确", outRefundNo);
  174 + throw new IllegalStateException(String.format("支付退款单[%s] 状态不正确", outRefundNo));
108 } 175 }
109 176
110 } 177 }
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 { @@ -27,7 +27,7 @@ public interface PayWalletService {
27 * @param outTradeNo 外部订单号 27 * @param outTradeNo 外部订单号
28 * @param price 金额 28 * @param price 金额
29 */ 29 */
30 - PayWalletTransactionDO pay(Long userId, Integer userType, String outTradeNo, Integer price); 30 + PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price);
31 31
32 32
33 /** 33 /**
@@ -64,6 +64,6 @@ public interface PayWalletService { @@ -64,6 +64,6 @@ public interface PayWalletService {
64 * @param refundPrice 退款金额 64 * @param refundPrice 退款金额
65 * @param reason 退款原因 65 * @param reason 退款原因
66 */ 66 */
67 - PayWalletTransactionDO refund(String outRefundNo, Integer refundPrice, String reason); 67 + PayWalletTransactionDO orderRefund(String outRefundNo, Integer refundPrice, String reason);
68 68
69 } 69 }
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 { @@ -75,7 +75,7 @@ public class PayWalletServiceImpl implements PayWalletService {
75 75
76 @Override 76 @Override
77 @Transactional(rollbackFor = Exception.class) 77 @Transactional(rollbackFor = Exception.class)
78 - public PayWalletTransactionDO pay(Long userId, Integer userType, String outTradeNo, Integer price) { 78 + public PayWalletTransactionDO orderPay(Long userId, Integer userType, String outTradeNo, Integer price) {
79 // 判断支付交易拓展单是否存 79 // 判断支付交易拓展单是否存
80 PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo); 80 PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo);
81 if (orderExtension == null) { 81 if (orderExtension == null) {
@@ -150,7 +150,7 @@ public class PayWalletServiceImpl implements PayWalletService { @@ -150,7 +150,7 @@ public class PayWalletServiceImpl implements PayWalletService {
150 150
151 @Override 151 @Override
152 @Transactional(rollbackFor = Exception.class) 152 @Transactional(rollbackFor = Exception.class)
153 - public PayWalletTransactionDO refund(String outRefundNo, Integer refundPrice, String reason) { 153 + public PayWalletTransactionDO orderRefund(String outRefundNo, Integer refundPrice, String reason) {
154 // 1.1 判断退款单是否存在 154 // 1.1 判断退款单是否存在
155 PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo); 155 PayRefundDO payRefund = payRefundService.getRefundByNo(outRefundNo);
156 if (payRefund == null) { 156 if (payRefund == null) {
@@ -182,7 +182,7 @@ public class PayWalletServiceImpl implements PayWalletService { @@ -182,7 +182,7 @@ public class PayWalletServiceImpl implements PayWalletService {
182 throw exception(WALLET_REFUND_AMOUNT_ERROR); 182 throw exception(WALLET_REFUND_AMOUNT_ERROR);
183 } 183 }
184 PayWalletTransactionDO refundTransaction = payWalletTransactionService.getWalletTransaction( 184 PayWalletTransactionDO refundTransaction = payWalletTransactionService.getWalletTransaction(
185 - payWalletTransaction.getWalletId(), refundId, PAYMENT_REFUND); 185 + String.valueOf(refundId), PAYMENT_REFUND);
186 if (refundTransaction != null) { 186 if (refundTransaction != null) {
187 throw exception(WALLET_REFUND_EXIST); 187 throw exception(WALLET_REFUND_EXIST);
188 } 188 }
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 { @@ -40,11 +40,9 @@ public interface PayWalletTransactionService {
40 /** 40 /**
41 * 获取钱包流水 41 * 获取钱包流水
42 * 42 *
43 - * @param walletId 钱包编号  
44 * @param bizId 业务编号 43 * @param bizId 业务编号
45 * @param type 业务类型 44 * @param type 业务类型
46 * @return 钱包流水 45 * @return 钱包流水
47 */ 46 */
48 - PayWalletTransactionDO getWalletTransaction(Long walletId, Long bizId, PayWalletBizTypeEnum type);  
49 - 47 + PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type);
50 } 48 }
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 @@ -22,7 +22,6 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
22 22
23 @Resource 23 @Resource
24 private PayWalletService payWalletService; 24 private PayWalletService payWalletService;
25 -  
26 @Resource 25 @Resource
27 private PayWalletTransactionMapper payWalletTransactionMapper; 26 private PayWalletTransactionMapper payWalletTransactionMapper;
28 27
@@ -45,8 +44,7 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ @@ -45,8 +44,7 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
45 } 44 }
46 45
47 @Override 46 @Override
48 - public PayWalletTransactionDO getWalletTransaction(Long walletId, Long bizId, PayWalletBizTypeEnum type) {  
49 - return payWalletTransactionMapper.selectByWalletIdAndBiz(walletId, bizId, type.getType()); 47 + public PayWalletTransactionDO getWalletTransaction(String bizId, PayWalletBizTypeEnum type) {
  48 + return payWalletTransactionMapper.selectByBiz(bizId, type.getType());
50 } 49 }
51 -  
52 } 50 }