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