Commit 7d4d3bea551dd0e11b175983bafe1d9c1d3186bc
1 parent
60d4aa97
Revert "创建 PayClient 迁移到 PayChannelService"
This reverts commit 716727bfa7ebcfb0f298e0f805f06efd4d54f695.
Showing
6 changed files
with
86 additions
and
65 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java
| 1 | package cn.iocoder.yudao.framework.pay.core.client; | 1 | package cn.iocoder.yudao.framework.pay.core.client; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; | 3 | +import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; |
| 4 | 4 | ||
| 5 | /** | 5 | /** |
| 6 | * 支付客户端的工厂接口 | 6 | * 支付客户端的工厂接口 |
| @@ -21,8 +21,18 @@ public interface PayClientFactory { | @@ -21,8 +21,18 @@ public interface PayClientFactory { | ||
| 21 | * 创建支付客户端 | 21 | * 创建支付客户端 |
| 22 | * | 22 | * |
| 23 | * @param channelId 渠道编号 | 23 | * @param channelId 渠道编号 |
| 24 | - * @param client 支付客户端 | 24 | + * @param channelCode 渠道编码 |
| 25 | + * @param config 支付配置 | ||
| 26 | + */ | ||
| 27 | + <Config extends PayClientConfig> void createOrUpdatePayClient(Long channelId, String channelCode, | ||
| 28 | + Config config); | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * 新增或更新代理支付客户端 | ||
| 32 | + * @param channelId 渠道编号 | ||
| 33 | + * @param delegatePayClient 代理支付客户端 | ||
| 25 | * @param <Config> 支付配置 | 34 | * @param <Config> 支付配置 |
| 26 | */ | 35 | */ |
| 27 | - <Config extends PayClientConfig> void addOrUpdatePayClient(Long channelId, AbstractPayClient<Config> client); | 36 | + <Config extends PayClientConfig> void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient); |
| 37 | + | ||
| 28 | } | 38 | } |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java
| @@ -9,7 +9,6 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDT | @@ -9,7 +9,6 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDT | ||
| 9 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; | 9 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; |
| 10 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; | 10 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; |
| 11 | import cn.iocoder.yudao.framework.pay.core.client.exception.PayException; | 11 | import cn.iocoder.yudao.framework.pay.core.client.exception.PayException; |
| 12 | -import lombok.Getter; | ||
| 13 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| 14 | 13 | ||
| 15 | import java.util.Map; | 14 | import java.util.Map; |
| @@ -36,7 +35,6 @@ public abstract class AbstractPayClient<Config extends PayClientConfig> implemen | @@ -36,7 +35,6 @@ public abstract class AbstractPayClient<Config extends PayClientConfig> implemen | ||
| 36 | /** | 35 | /** |
| 37 | * 支付配置 | 36 | * 支付配置 |
| 38 | */ | 37 | */ |
| 39 | - @Getter | ||
| 40 | protected Config config; | 38 | protected Config config; |
| 41 | 39 | ||
| 42 | public AbstractPayClient(Long channelId, String channelCode, Config config) { | 40 | public AbstractPayClient(Long channelId, String channelCode, Config config) { |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java
| 1 | package cn.iocoder.yudao.framework.pay.core.client.impl; | 1 | package cn.iocoder.yudao.framework.pay.core.client.impl; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.lang.Assert; | ||
| 3 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; | 4 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; |
| 4 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; | 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; |
| 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; | 6 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; |
| 7 | +import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.*; | ||
| 8 | +import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; | ||
| 9 | +import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClient; | ||
| 10 | +import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClientConfig; | ||
| 11 | +import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.*; | ||
| 12 | +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; | ||
| 6 | import lombok.extern.slf4j.Slf4j; | 13 | import lombok.extern.slf4j.Slf4j; |
| 7 | 14 | ||
| 8 | import java.util.concurrent.ConcurrentHashMap; | 15 | import java.util.concurrent.ConcurrentHashMap; |
| @@ -33,12 +40,48 @@ public class PayClientFactoryImpl implements PayClientFactory { | @@ -33,12 +40,48 @@ public class PayClientFactoryImpl implements PayClientFactory { | ||
| 33 | 40 | ||
| 34 | @Override | 41 | @Override |
| 35 | @SuppressWarnings("unchecked") | 42 | @SuppressWarnings("unchecked") |
| 36 | - public <Config extends PayClientConfig> void addOrUpdatePayClient(Long channelId, AbstractPayClient<Config> client) { | ||
| 37 | - AbstractPayClient<Config> prePayClient = (AbstractPayClient<Config>) clients.putIfAbsent(channelId, client); | ||
| 38 | - if (prePayClient == null) { | 43 | + public <Config extends PayClientConfig> void createOrUpdatePayClient(Long channelId, String channelCode, |
| 44 | + Config config) { | ||
| 45 | + AbstractPayClient<Config> client = (AbstractPayClient<Config>) clients.get(channelId); | ||
| 46 | + if (client == null) { | ||
| 47 | + client = this.createPayClient(channelId, channelCode, config); | ||
| 39 | client.init(); | 48 | client.init(); |
| 49 | + clients.put(client.getId(), client); | ||
| 40 | } else { | 50 | } else { |
| 41 | - prePayClient.refresh(client.getConfig()); | 51 | + client.refresh(config); |
| 42 | } | 52 | } |
| 43 | } | 53 | } |
| 54 | + | ||
| 55 | + @Override | ||
| 56 | + public <Config extends PayClientConfig> void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient) { | ||
| 57 | + clients.put(channelId, delegatePayClient); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @SuppressWarnings("unchecked") | ||
| 61 | + private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient( | ||
| 62 | + Long channelId, String channelCode, Config config) { | ||
| 63 | + PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode); | ||
| 64 | + Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelEnum)); | ||
| 65 | + // 创建客户端 | ||
| 66 | + switch (channelEnum) { | ||
| 67 | + // 微信支付 | ||
| 68 | + case WX_PUB: return (AbstractPayClient<Config>) new WxPubPayClient(channelId, (WxPayClientConfig) config); | ||
| 69 | + case WX_LITE: return (AbstractPayClient<Config>) new WxLitePayClient(channelId, (WxPayClientConfig) config); | ||
| 70 | + case WX_APP: return (AbstractPayClient<Config>) new WxAppPayClient(channelId, (WxPayClientConfig) config); | ||
| 71 | + case WX_BAR: return (AbstractPayClient<Config>) new WxBarPayClient(channelId, (WxPayClientConfig) config); | ||
| 72 | + case WX_NATIVE: return (AbstractPayClient<Config>) new WxNativePayClient(channelId, (WxPayClientConfig) config); | ||
| 73 | + // 支付宝支付 | ||
| 74 | + case ALIPAY_WAP: return (AbstractPayClient<Config>) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 75 | + case ALIPAY_QR: return (AbstractPayClient<Config>) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 76 | + case ALIPAY_APP: return (AbstractPayClient<Config>) new AlipayAppPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 77 | + case ALIPAY_PC: return (AbstractPayClient<Config>) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 78 | + case ALIPAY_BAR: return (AbstractPayClient<Config>) new AlipayBarPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 79 | + // 其它支付 | ||
| 80 | + case MOCK: return (AbstractPayClient<Config>) new MockPayClient(channelId, (MockPayClientConfig) config); | ||
| 81 | + } | ||
| 82 | + // 创建失败,错误日志 + 抛出异常 | ||
| 83 | + log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); | ||
| 84 | + throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", config)); | ||
| 85 | + } | ||
| 86 | + | ||
| 44 | } | 87 | } |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImplIntegrationTest.java
| @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayQrPayClient; | @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayQrPayClient; | ||
| 9 | import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayWapPayClient; | 9 | import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayWapPayClient; |
| 10 | import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPayClientConfig; | 10 | import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPayClientConfig; |
| 11 | import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPubPayClient; | 11 | import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPubPayClient; |
| 12 | +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; | ||
| 12 | import org.junit.jupiter.api.Disabled; | 13 | import org.junit.jupiter.api.Disabled; |
| 13 | import org.junit.jupiter.api.Test; | 14 | import org.junit.jupiter.api.Test; |
| 14 | 15 | ||
| @@ -40,8 +41,7 @@ public class PayClientFactoryImplIntegrationTest { | @@ -40,8 +41,7 @@ public class PayClientFactoryImplIntegrationTest { | ||
| 40 | config.setMchKey("0alL64UDQdlCwiKZ73ib7ypaIjMns06p"); | 41 | config.setMchKey("0alL64UDQdlCwiKZ73ib7ypaIjMns06p"); |
| 41 | // 创建客户端 | 42 | // 创建客户端 |
| 42 | Long channelId = RandomUtil.randomLong(); | 43 | Long channelId = RandomUtil.randomLong(); |
| 43 | - | ||
| 44 | - payClientFactory.addOrUpdatePayClient(channelId, new WxPubPayClient(channelId, config)); | 44 | + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config); |
| 45 | PayClient client = payClientFactory.getPayClient(channelId); | 45 | PayClient client = payClientFactory.getPayClient(channelId); |
| 46 | // 发起支付 | 46 | // 发起支付 |
| 47 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); | 47 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); |
| @@ -64,7 +64,7 @@ public class PayClientFactoryImplIntegrationTest { | @@ -64,7 +64,7 @@ public class PayClientFactoryImplIntegrationTest { | ||
| 64 | config.setApiV3Key("joerVi8y5DJ3o4ttA0o1uH47Xz1u2Ase"); | 64 | config.setApiV3Key("joerVi8y5DJ3o4ttA0o1uH47Xz1u2Ase"); |
| 65 | // 创建客户端 | 65 | // 创建客户端 |
| 66 | Long channelId = RandomUtil.randomLong(); | 66 | Long channelId = RandomUtil.randomLong(); |
| 67 | - payClientFactory.addOrUpdatePayClient(channelId, new WxPubPayClient(channelId, config)); | 67 | + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config); |
| 68 | PayClient client = payClientFactory.getPayClient(channelId); | 68 | PayClient client = payClientFactory.getPayClient(channelId); |
| 69 | // 发起支付 | 69 | // 发起支付 |
| 70 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); | 70 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); |
| @@ -87,7 +87,7 @@ public class PayClientFactoryImplIntegrationTest { | @@ -87,7 +87,7 @@ public class PayClientFactoryImplIntegrationTest { | ||
| 87 | config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBNlrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); | 87 | config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBNlrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); |
| 88 | // 创建客户端 | 88 | // 创建客户端 |
| 89 | Long channelId = RandomUtil.randomLong(); | 89 | Long channelId = RandomUtil.randomLong(); |
| 90 | - payClientFactory.addOrUpdatePayClient(channelId, new AlipayQrPayClient(channelId, config)); | 90 | + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_QR.getCode(), config); |
| 91 | PayClient client = payClientFactory.getPayClient(channelId); | 91 | PayClient client = payClientFactory.getPayClient(channelId); |
| 92 | // 发起支付 | 92 | // 发起支付 |
| 93 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); | 93 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); |
| @@ -111,7 +111,7 @@ public class PayClientFactoryImplIntegrationTest { | @@ -111,7 +111,7 @@ public class PayClientFactoryImplIntegrationTest { | ||
| 111 | config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBNlrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); | 111 | config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBNlrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); |
| 112 | // 创建客户端 | 112 | // 创建客户端 |
| 113 | Long channelId = RandomUtil.randomLong(); | 113 | Long channelId = RandomUtil.randomLong(); |
| 114 | - payClientFactory.addOrUpdatePayClient(channelId, new AlipayWapPayClient(channelId, config)); | 114 | + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_WAP.getCode(), config); |
| 115 | PayClient client = payClientFactory.getPayClient(channelId); | 115 | PayClient client = payClientFactory.getPayClient(channelId); |
| 116 | // 发起支付 | 116 | // 发起支付 |
| 117 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); | 117 | PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java
| @@ -5,8 +5,8 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; | @@ -5,8 +5,8 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; | ||
| 5 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; | 5 | import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; |
| 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; | 6 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; |
| 7 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; | 7 | import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; |
| 8 | -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; | ||
| 9 | import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; | 8 | import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; |
| 9 | +import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; | ||
| 10 | import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; | 10 | import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; |
| 11 | import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; | 11 | import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; |
| 12 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
| @@ -21,7 +21,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC | @@ -21,7 +21,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC | ||
| 21 | * @author jason | 21 | * @author jason |
| 22 | */ | 22 | */ |
| 23 | @Slf4j | 23 | @Slf4j |
| 24 | -public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> { | 24 | +public class WalletPayClient extends DelegatePayClient<NonePayClientConfig> { |
| 25 | 25 | ||
| 26 | private PayWalletService payWalletService; | 26 | private PayWalletService payWalletService; |
| 27 | 27 |
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java
| @@ -6,14 +6,9 @@ import cn.hutool.core.util.ObjectUtil; | @@ -6,14 +6,9 @@ import cn.hutool.core.util.ObjectUtil; | ||
| 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 7 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 8 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 8 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| 9 | +import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; | ||
| 9 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; | 10 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; |
| 10 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; | 11 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; |
| 11 | -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; | ||
| 12 | -import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; | ||
| 13 | -import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.*; | ||
| 14 | -import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClient; | ||
| 15 | -import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClientConfig; | ||
| 16 | -import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.*; | ||
| 17 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; | 12 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; |
| 18 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | 13 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 19 | import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelCreateReqVO; | 14 | import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelCreateReqVO; |
| @@ -34,6 +29,7 @@ import javax.annotation.PostConstruct; | @@ -34,6 +29,7 @@ import javax.annotation.PostConstruct; | ||
| 34 | import javax.annotation.Resource; | 29 | import javax.annotation.Resource; |
| 35 | import javax.validation.Validator; | 30 | import javax.validation.Validator; |
| 36 | import java.time.LocalDateTime; | 31 | import java.time.LocalDateTime; |
| 32 | +import java.util.ArrayList; | ||
| 37 | import java.util.Collection; | 33 | import java.util.Collection; |
| 38 | import java.util.Collections; | 34 | import java.util.Collections; |
| 39 | import java.util.List; | 35 | import java.util.List; |
| @@ -85,55 +81,29 @@ public class PayChannelServiceImpl implements PayChannelService { | @@ -85,55 +81,29 @@ public class PayChannelServiceImpl implements PayChannelService { | ||
| 85 | log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); | 81 | log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); |
| 86 | } | 82 | } |
| 87 | log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); | 83 | log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); |
| 84 | + // 钱包 client 需要和其它 client 分开了创建 | ||
| 85 | + List<PayChannelDO> walletChannels = new ArrayList<>(); | ||
| 86 | + // TODO @jason:有点复杂,看看用 PayClientInitializer 能不能简化 | ||
| 87 | + List<PayChannelDO> otherChannels = new ArrayList<>(); | ||
| 88 | + channels.forEach(t -> { | ||
| 89 | + if (PayChannelEnum.WALLET.getCode().equals(t.getCode())) { | ||
| 90 | + walletChannels.add(t); | ||
| 91 | + } else { | ||
| 92 | + otherChannels.add(t); | ||
| 93 | + } | ||
| 94 | + }); | ||
| 88 | // 第二步:构建缓存:创建或更新支付 Client | 95 | // 第二步:构建缓存:创建或更新支付 Client |
| 89 | - channels.forEach(payChannel ->{ | ||
| 90 | - AbstractPayClient<PayClientConfig> payClient = createPayClient(payChannel.getId(), payChannel.getCode(), payChannel.getConfig()); | ||
| 91 | - payClientFactory.addOrUpdatePayClient(payChannel.getId(), payClient); | 96 | + otherChannels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), |
| 97 | + payChannel.getCode(), payChannel.getConfig())); | ||
| 92 | 98 | ||
| 99 | + walletChannels.forEach(payChannel -> { | ||
| 100 | + WalletPayClient walletPayClient = new WalletPayClient(payChannel.getId(), payChannel.getCode(), | ||
| 101 | + (NonePayClientConfig) payChannel.getConfig(), payWalletService); | ||
| 102 | + payClientFactory.addOrUpdateDelegatePayClient(payChannel.getId(), walletPayClient); | ||
| 93 | }); | 103 | }); |
| 94 | this.channelCache = channels; | 104 | this.channelCache = channels; |
| 95 | }); | 105 | }); |
| 96 | } | 106 | } |
| 97 | - @SuppressWarnings("unchecked") | ||
| 98 | - private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient( | ||
| 99 | - Long channelId, String channelCode, Config config) { | ||
| 100 | - PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode); | ||
| 101 | - Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelEnum)); | ||
| 102 | - // 创建客户端 | ||
| 103 | - switch (channelEnum) { | ||
| 104 | - // 微信支付 | ||
| 105 | - case WX_PUB: | ||
| 106 | - return (AbstractPayClient<Config>) new WxPubPayClient(channelId, (WxPayClientConfig) config); | ||
| 107 | - case WX_LITE: | ||
| 108 | - return (AbstractPayClient<Config>) new WxLitePayClient(channelId, (WxPayClientConfig) config); | ||
| 109 | - case WX_APP: | ||
| 110 | - return (AbstractPayClient<Config>) new WxAppPayClient(channelId, (WxPayClientConfig) config); | ||
| 111 | - case WX_BAR: | ||
| 112 | - return (AbstractPayClient<Config>) new WxBarPayClient(channelId, (WxPayClientConfig) config); | ||
| 113 | - case WX_NATIVE: | ||
| 114 | - return (AbstractPayClient<Config>) new WxNativePayClient(channelId, (WxPayClientConfig) config); | ||
| 115 | - // 支付宝支付 | ||
| 116 | - case ALIPAY_WAP: | ||
| 117 | - return (AbstractPayClient<Config>) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 118 | - case ALIPAY_QR: | ||
| 119 | - return (AbstractPayClient<Config>) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 120 | - case ALIPAY_APP: | ||
| 121 | - return (AbstractPayClient<Config>) new AlipayAppPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 122 | - case ALIPAY_PC: | ||
| 123 | - return (AbstractPayClient<Config>) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 124 | - case ALIPAY_BAR: | ||
| 125 | - return (AbstractPayClient<Config>) new AlipayBarPayClient(channelId, (AlipayPayClientConfig) config); | ||
| 126 | - // 其它支付 | ||
| 127 | - case MOCK: | ||
| 128 | - return (AbstractPayClient<Config>) new MockPayClient(channelId, (MockPayClientConfig) config); | ||
| 129 | - case WALLET: | ||
| 130 | - return (AbstractPayClient<Config>) new WalletPayClient( | ||
| 131 | - channelId, channelCode, (NonePayClientConfig) config, payWalletService); | ||
| 132 | - } | ||
| 133 | - // 创建失败,错误日志 + 抛出异常 | ||
| 134 | - log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); | ||
| 135 | - throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", config)); | ||
| 136 | - } | ||
| 137 | 107 | ||
| 138 | /** | 108 | /** |
| 139 | * 通过定时任务轮询,刷新缓存 | 109 | * 通过定时任务轮询,刷新缓存 |