Commit ba444c953811a681535734d5c24cc379ba9e4e77

Authored by YunaiV
1 parent bca6fb46

code review:支付钱包

yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java
@@ -28,10 +28,11 @@ public interface PayClientFactory { @@ -28,10 +28,11 @@ public interface PayClientFactory {
28 Config config); 28 Config config);
29 29
30 /** 30 /**
31 - * 注册支付客户端 Class, 用于模块中实现的 PayClient 31 + * 注册支付客户端 Class,用于模块中实现的 PayClient
32 * 32 *
33 - * @param payChannelEnum 支付渠道的编码的枚举 33 + * @param channel 支付渠道的编码的枚举
34 * @param payClientClass 支付客户端 class 34 * @param payClientClass 支付客户端 class
35 */ 35 */
36 - void registerPayClientClass(PayChannelEnum payChannelEnum, Class<?> payClientClass); 36 + void registerPayClientClass(PayChannelEnum channel, Class<?> payClientClass);
  37 +
37 } 38 }
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java
@@ -27,15 +27,15 @@ public class PayClientFactoryImpl implements PayClientFactory { @@ -27,15 +27,15 @@ public class PayClientFactoryImpl implements PayClientFactory {
27 27
28 /** 28 /**
29 * 支付客户端 Map 29 * 支付客户端 Map
  30 + *
30 * key:渠道编号 31 * key:渠道编号
31 */ 32 */
32 private final ConcurrentMap<Long, AbstractPayClient<?>> clients = new ConcurrentHashMap<>(); 33 private final ConcurrentMap<Long, AbstractPayClient<?>> clients = new ConcurrentHashMap<>();
33 34
34 /** 35 /**
35 * 支付客户端 Class Map 36 * 支付客户端 Class Map
36 - * key: 支付渠道的编码的枚举  
37 */ 37 */
38 - private final Map<PayChannelEnum, Class<?>>clientClass = new ConcurrentHashMap<>(16); 38 + private final Map<PayChannelEnum, Class<?>> clientClass = new ConcurrentHashMap<>();
39 39
40 public PayClientFactoryImpl() { 40 public PayClientFactoryImpl() {
41 // 微信支付客户端 41 // 微信支付客户端
@@ -55,6 +55,11 @@ public class PayClientFactoryImpl implements PayClientFactory { @@ -55,6 +55,11 @@ public class PayClientFactoryImpl implements PayClientFactory {
55 } 55 }
56 56
57 @Override 57 @Override
  58 + public void registerPayClientClass(PayChannelEnum channel, Class<?> payClientClass) {
  59 + clientClass.put(channel, payClientClass);
  60 + }
  61 +
  62 + @Override
58 public PayClient getPayClient(Long channelId) { 63 public PayClient getPayClient(Long channelId) {
59 AbstractPayClient<?> client = clients.get(channelId); 64 AbstractPayClient<?> client = clients.get(channelId);
60 if (client == null) { 65 if (client == null) {
@@ -77,11 +82,6 @@ public class PayClientFactoryImpl implements PayClientFactory { @@ -77,11 +82,6 @@ public class PayClientFactoryImpl implements PayClientFactory {
77 } 82 }
78 } 83 }
79 84
80 - @Override  
81 - public void registerPayClientClass(PayChannelEnum payChannelEnum, Class<?> payClientClass) {  
82 - clientClass.put(payChannelEnum, payClientClass);  
83 - }  
84 -  
85 @SuppressWarnings("unchecked") 85 @SuppressWarnings("unchecked")
86 private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(Long channelId, String channelCode, 86 private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(Long channelId, String channelCode,
87 Config config) { 87 Config config) {
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java
@@ -25,7 +25,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC @@ -25,7 +25,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC
25 @Slf4j 25 @Slf4j
26 public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { 26 public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
27 27
28 - private PayWalletService client; 28 + private PayWalletService wallService;
29 29
30 public WalletPayClient(Long channelId, NonePayClientConfig config) { 30 public WalletPayClient(Long channelId, NonePayClientConfig config) {
31 super(channelId, PayChannelEnum.WALLET.getCode(), config); 31 super(channelId, PayChannelEnum.WALLET.getCode(), config);
@@ -33,15 +33,15 @@ public class WalletPayClient extends AbstractPayClient&lt;NonePayClientConfig&gt; { @@ -33,15 +33,15 @@ public class WalletPayClient extends AbstractPayClient&lt;NonePayClientConfig&gt; {
33 33
34 @Override 34 @Override
35 protected void doInit() { 35 protected void doInit() {
36 - if (client == null) {  
37 - client = SpringUtil.getBean(PayWalletService.class); 36 + if (wallService == null) {
  37 + wallService = SpringUtil.getBean(PayWalletService.class);
38 } 38 }
39 } 39 }
40 40
41 @Override 41 @Override
42 protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { 42 protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
43 try { 43 try {
44 - PayWalletTransactionDO transaction = client.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice()); 44 + PayWalletTransactionDO transaction = wallService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
45 return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), 45 return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(),
46 transaction.getTransactionTime(), 46 transaction.getTransactionTime(),
47 reqDTO.getOutTradeNo(), transaction); 47 reqDTO.getOutTradeNo(), transaction);
@@ -72,7 +72,7 @@ public class WalletPayClient extends AbstractPayClient&lt;NonePayClientConfig&gt; { @@ -72,7 +72,7 @@ public class WalletPayClient extends AbstractPayClient&lt;NonePayClientConfig&gt; {
72 @Override 72 @Override
73 protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { 73 protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
74 try { 74 try {
75 - PayWalletTransactionDO payWalletTransaction = client.refund(reqDTO.getOutRefundNo(), 75 + PayWalletTransactionDO payWalletTransaction = wallService.refund(reqDTO.getOutRefundNo(),
76 reqDTO.getRefundPrice(), reqDTO.getReason()); 76 reqDTO.getRefundPrice(), reqDTO.getReason());
77 return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(), 77 return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(),
78 reqDTO.getOutRefundNo(), payWalletTransaction); 78 reqDTO.getOutRefundNo(), payWalletTransaction);
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java
@@ -65,6 +65,7 @@ public class PayChannelServiceImpl implements PayChannelService { @@ -65,6 +65,7 @@ public class PayChannelServiceImpl implements PayChannelService {
65 public void initLocalCache() { 65 public void initLocalCache() {
66 // 注册钱包支付 Class 66 // 注册钱包支付 Class
67 payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class); 67 payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class);
  68 +
68 // 注意:忽略自动多租户,因为要全局初始化缓存 69 // 注意:忽略自动多租户,因为要全局初始化缓存
69 TenantUtils.executeIgnore(() -> { 70 TenantUtils.executeIgnore(() -> {
70 // 第一步:查询数据 71 // 第一步:查询数据
@@ -78,6 +79,7 @@ public class PayChannelServiceImpl implements PayChannelService { @@ -78,6 +79,7 @@ public class PayChannelServiceImpl implements PayChannelService {
78 log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); 79 log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]");
79 } 80 }
80 log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); 81 log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size());
  82 +
81 // 第二步:构建缓存:创建或更新支付 Client 83 // 第二步:构建缓存:创建或更新支付 Client
82 channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), 84 channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(),
83 payChannel.getCode(), payChannel.getConfig())); 85 payChannel.getCode(), payChannel.getConfig()));
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java
@@ -45,6 +45,7 @@ public class PayWalletServiceImpl implements PayWalletService { @@ -45,6 +45,7 @@ public class PayWalletServiceImpl implements PayWalletService {
45 private PayWalletMapper payWalletMapper; 45 private PayWalletMapper payWalletMapper;
46 @Resource 46 @Resource
47 private PayNoRedisDAO noRedisDAO; 47 private PayNoRedisDAO noRedisDAO;
  48 +
48 @Resource 49 @Resource
49 private PayWalletTransactionService payWalletTransactionService; 50 private PayWalletTransactionService payWalletTransactionService;
50 @Resource 51 @Resource