Commit 716727bfa7ebcfb0f298e0f805f06efd4d54f695
1 parent
ddcb3e69
创建 PayClient 迁移到 PayChannelService
Showing
6 changed files
with
65 additions
and
86 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.delegate.DelegatePayClient; | 3 | +import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; |
| 4 | 4 | ||
| 5 | /** | 5 | /** |
| 6 | * 支付客户端的工厂接口 | 6 | * 支付客户端的工厂接口 |
| @@ -21,18 +21,8 @@ public interface PayClientFactory { | @@ -21,18 +21,8 @@ public interface PayClientFactory { | ||
| 21 | * 创建支付客户端 | 21 | * 创建支付客户端 |
| 22 | * | 22 | * |
| 23 | * @param channelId 渠道编号 | 23 | * @param channelId 渠道编号 |
| 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 代理支付客户端 | 24 | + * @param client 支付客户端 |
| 34 | * @param <Config> 支付配置 | 25 | * @param <Config> 支付配置 |
| 35 | */ | 26 | */ |
| 36 | - <Config extends PayClientConfig> void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient); | ||
| 37 | - | 27 | + <Config extends PayClientConfig> void addOrUpdatePayClient(Long channelId, AbstractPayClient<Config> client); |
| 38 | } | 28 | } |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java
| @@ -9,6 +9,7 @@ import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDT | @@ -9,6 +9,7 @@ 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; | ||
| 12 | import lombok.extern.slf4j.Slf4j; | 13 | import lombok.extern.slf4j.Slf4j; |
| 13 | 14 | ||
| 14 | import java.util.Map; | 15 | import java.util.Map; |
| @@ -35,6 +36,7 @@ public abstract class AbstractPayClient<Config extends PayClientConfig> implemen | @@ -35,6 +36,7 @@ public abstract class AbstractPayClient<Config extends PayClientConfig> implemen | ||
| 35 | /** | 36 | /** |
| 36 | * 支付配置 | 37 | * 支付配置 |
| 37 | */ | 38 | */ |
| 39 | + @Getter | ||
| 38 | protected Config config; | 40 | protected Config config; |
| 39 | 41 | ||
| 40 | public AbstractPayClient(Long channelId, String channelCode, Config config) { | 42 | 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; | ||
| 4 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; | 3 | import cn.iocoder.yudao.framework.pay.core.client.PayClient; |
| 5 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; | 4 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; |
| 6 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; | 5 | 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; | ||
| 13 | import lombok.extern.slf4j.Slf4j; | 6 | import lombok.extern.slf4j.Slf4j; |
| 14 | 7 | ||
| 15 | import java.util.concurrent.ConcurrentHashMap; | 8 | import java.util.concurrent.ConcurrentHashMap; |
| @@ -40,48 +33,12 @@ public class PayClientFactoryImpl implements PayClientFactory { | @@ -40,48 +33,12 @@ public class PayClientFactoryImpl implements PayClientFactory { | ||
| 40 | 33 | ||
| 41 | @Override | 34 | @Override |
| 42 | @SuppressWarnings("unchecked") | 35 | @SuppressWarnings("unchecked") |
| 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); | 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) { | ||
| 48 | client.init(); | 39 | client.init(); |
| 49 | - clients.put(client.getId(), client); | ||
| 50 | } else { | 40 | } else { |
| 51 | - client.refresh(config); | 41 | + prePayClient.refresh(client.getConfig()); |
| 52 | } | 42 | } |
| 53 | } | 43 | } |
| 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 | - | ||
| 87 | } | 44 | } |
yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImplIntegrationTest.java
| @@ -9,7 +9,6 @@ import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayQrPayClient; | @@ -9,7 +9,6 @@ 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; | ||
| 13 | import org.junit.jupiter.api.Disabled; | 12 | import org.junit.jupiter.api.Disabled; |
| 14 | import org.junit.jupiter.api.Test; | 13 | import org.junit.jupiter.api.Test; |
| 15 | 14 | ||
| @@ -41,7 +40,8 @@ public class PayClientFactoryImplIntegrationTest { | @@ -41,7 +40,8 @@ public class PayClientFactoryImplIntegrationTest { | ||
| 41 | config.setMchKey("0alL64UDQdlCwiKZ73ib7ypaIjMns06p"); | 40 | config.setMchKey("0alL64UDQdlCwiKZ73ib7ypaIjMns06p"); |
| 42 | // 创建客户端 | 41 | // 创建客户端 |
| 43 | Long channelId = RandomUtil.randomLong(); | 42 | Long channelId = RandomUtil.randomLong(); |
| 44 | - payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config); | 43 | + |
| 44 | + payClientFactory.addOrUpdatePayClient(channelId, new WxPubPayClient(channelId, 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.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config); | 67 | + payClientFactory.addOrUpdatePayClient(channelId, new WxPubPayClient(channelId, 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.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_QR.getCode(), config); | 90 | + payClientFactory.addOrUpdatePayClient(channelId, new AlipayQrPayClient(channelId, 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.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_WAP.getCode(), config); | 114 | + payClientFactory.addOrUpdatePayClient(channelId, new AlipayWapPayClient(channelId, 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; | ||
| 8 | import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; | 9 | 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 DelegatePayClient<NonePayClientConfig> { | 24 | +public class WalletPayClient extends AbstractPayClient<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,9 +6,14 @@ import cn.hutool.core.util.ObjectUtil; | @@ -6,9 +6,14 @@ 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; | ||
| 10 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; | 9 | import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; |
| 11 | import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; | 10 | 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.*; | ||
| 12 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; | 17 | import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; |
| 13 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | 18 | import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; |
| 14 | import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelCreateReqVO; | 19 | import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelCreateReqVO; |
| @@ -29,7 +34,6 @@ import javax.annotation.PostConstruct; | @@ -29,7 +34,6 @@ import javax.annotation.PostConstruct; | ||
| 29 | import javax.annotation.Resource; | 34 | import javax.annotation.Resource; |
| 30 | import javax.validation.Validator; | 35 | import javax.validation.Validator; |
| 31 | import java.time.LocalDateTime; | 36 | import java.time.LocalDateTime; |
| 32 | -import java.util.ArrayList; | ||
| 33 | import java.util.Collection; | 37 | import java.util.Collection; |
| 34 | import java.util.Collections; | 38 | import java.util.Collections; |
| 35 | import java.util.List; | 39 | import java.util.List; |
| @@ -81,29 +85,55 @@ public class PayChannelServiceImpl implements PayChannelService { | @@ -81,29 +85,55 @@ public class PayChannelServiceImpl implements PayChannelService { | ||
| 81 | log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); | 85 | log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); |
| 82 | } | 86 | } |
| 83 | log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); | 87 | 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 | - }); | ||
| 95 | // 第二步:构建缓存:创建或更新支付 Client | 88 | // 第二步:构建缓存:创建或更新支付 Client |
| 96 | - otherChannels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), | ||
| 97 | - payChannel.getCode(), payChannel.getConfig())); | 89 | + channels.forEach(payChannel ->{ |
| 90 | + AbstractPayClient<PayClientConfig> payClient = createPayClient(payChannel.getId(), payChannel.getCode(), payChannel.getConfig()); | ||
| 91 | + payClientFactory.addOrUpdatePayClient(payChannel.getId(), payClient); | ||
| 98 | 92 | ||
| 99 | - walletChannels.forEach(payChannel -> { | ||
| 100 | - WalletPayClient walletPayClient = new WalletPayClient(payChannel.getId(), payChannel.getCode(), | ||
| 101 | - (NonePayClientConfig) payChannel.getConfig(), payWalletService); | ||
| 102 | - payClientFactory.addOrUpdateDelegatePayClient(payChannel.getId(), walletPayClient); | ||
| 103 | }); | 93 | }); |
| 104 | this.channelCache = channels; | 94 | this.channelCache = channels; |
| 105 | }); | 95 | }); |
| 106 | } | 96 | } |
| 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 | + } | ||
| 107 | 137 | ||
| 108 | /** | 138 | /** |
| 109 | * 通过定时任务轮询,刷新缓存 | 139 | * 通过定时任务轮询,刷新缓存 |