From 7d4d3bea551dd0e11b175983bafe1d9c1d3186bc Mon Sep 17 00:00:00 2001 From: jason <2667446@qq.com> Date: Thu, 31 Aug 2023 08:27:22 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Revert=20"=E5=88=9B=E5=BB=BA=20PayClient=20?= =?UTF-8?q?=E8=BF=81=E7=A7=BB=E5=88=B0=20PayChannelService"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 716727bfa7ebcfb0f298e0f805f06efd4d54f695. --- .../pay/core/client/PayClientFactory.java | 16 ++++- .../core/client/impl/AbstractPayClient.java | 2 - .../client/impl/PayClientFactoryImpl.java | 51 ++++++++++++-- .../PayClientFactoryImplIntegrationTest.java | 10 +-- .../framework/pay/wallet/WalletPayClient.java | 4 +- .../channel/PayChannelServiceImpl.java | 68 ++++++------------- 6 files changed, 86 insertions(+), 65 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java index 4739deb25..03e6bb613 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.pay.core.client; -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; +import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; /** * 支付客户端的工厂接口 @@ -21,8 +21,18 @@ public interface PayClientFactory { * 创建支付客户端 * * @param channelId 渠道编号 - * @param client 支付客户端 + * @param channelCode 渠道编码 + * @param config 支付配置 + */ + void createOrUpdatePayClient(Long channelId, String channelCode, + Config config); + + /** + * 新增或更新代理支付客户端 + * @param channelId 渠道编号 + * @param delegatePayClient 代理支付客户端 * @param 支付配置 */ - void addOrUpdatePayClient(Long channelId, AbstractPayClient client); + void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient delegatePayClient); + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java index d66e64115..e8f898b2e 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/AbstractPayClient.java +++ b/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 import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.exception.PayException; -import lombok.Getter; import lombok.extern.slf4j.Slf4j; import java.util.Map; @@ -36,7 +35,6 @@ public abstract class AbstractPayClient implemen /** * 支付配置 */ - @Getter protected Config config; public AbstractPayClient(Long channelId, String channelCode, Config config) { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java index 64114b264..eb11009d7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java @@ -1,8 +1,15 @@ package cn.iocoder.yudao.framework.pay.core.client.impl; +import cn.hutool.core.lang.Assert; import cn.iocoder.yudao.framework.pay.core.client.PayClient; import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; +import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.*; +import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; +import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClient; +import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClientConfig; +import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.*; +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import lombok.extern.slf4j.Slf4j; import java.util.concurrent.ConcurrentHashMap; @@ -33,12 +40,48 @@ public class PayClientFactoryImpl implements PayClientFactory { @Override @SuppressWarnings("unchecked") - public void addOrUpdatePayClient(Long channelId, AbstractPayClient client) { - AbstractPayClient prePayClient = (AbstractPayClient) clients.putIfAbsent(channelId, client); - if (prePayClient == null) { + public void createOrUpdatePayClient(Long channelId, String channelCode, + Config config) { + AbstractPayClient client = (AbstractPayClient) clients.get(channelId); + if (client == null) { + client = this.createPayClient(channelId, channelCode, config); client.init(); + clients.put(client.getId(), client); } else { - prePayClient.refresh(client.getConfig()); + client.refresh(config); } } + + @Override + public void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient delegatePayClient) { + clients.put(channelId, delegatePayClient); + } + + @SuppressWarnings("unchecked") + private AbstractPayClient createPayClient( + Long channelId, String channelCode, Config config) { + PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode); + Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelEnum)); + // 创建客户端 + switch (channelEnum) { + // 微信支付 + case WX_PUB: return (AbstractPayClient) new WxPubPayClient(channelId, (WxPayClientConfig) config); + case WX_LITE: return (AbstractPayClient) new WxLitePayClient(channelId, (WxPayClientConfig) config); + case WX_APP: return (AbstractPayClient) new WxAppPayClient(channelId, (WxPayClientConfig) config); + case WX_BAR: return (AbstractPayClient) new WxBarPayClient(channelId, (WxPayClientConfig) config); + case WX_NATIVE: return (AbstractPayClient) new WxNativePayClient(channelId, (WxPayClientConfig) config); + // 支付宝支付 + case ALIPAY_WAP: return (AbstractPayClient) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); + case ALIPAY_QR: return (AbstractPayClient) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); + case ALIPAY_APP: return (AbstractPayClient) new AlipayAppPayClient(channelId, (AlipayPayClientConfig) config); + case ALIPAY_PC: return (AbstractPayClient) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); + case ALIPAY_BAR: return (AbstractPayClient) new AlipayBarPayClient(channelId, (AlipayPayClientConfig) config); + // 其它支付 + case MOCK: return (AbstractPayClient) new MockPayClient(channelId, (MockPayClientConfig) config); + } + // 创建失败,错误日志 + 抛出异常 + log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); + throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", config)); + } + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImplIntegrationTest.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImplIntegrationTest.java index c921d09b9..984256063 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/test/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImplIntegrationTest.java +++ b/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; import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayWapPayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.WxPubPayClient; +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -40,8 +41,7 @@ public class PayClientFactoryImplIntegrationTest { config.setMchKey("0alL64UDQdlCwiKZ73ib7ypaIjMns06p"); // 创建客户端 Long channelId = RandomUtil.randomLong(); - - payClientFactory.addOrUpdatePayClient(channelId, new WxPubPayClient(channelId, config)); + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config); PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); @@ -64,7 +64,7 @@ public class PayClientFactoryImplIntegrationTest { config.setApiV3Key("joerVi8y5DJ3o4ttA0o1uH47Xz1u2Ase"); // 创建客户端 Long channelId = RandomUtil.randomLong(); - payClientFactory.addOrUpdatePayClient(channelId, new WxPubPayClient(channelId, config)); + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.WX_PUB.getCode(), config); PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); @@ -87,7 +87,7 @@ public class PayClientFactoryImplIntegrationTest { config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBNlrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); // 创建客户端 Long channelId = RandomUtil.randomLong(); - payClientFactory.addOrUpdatePayClient(channelId, new AlipayQrPayClient(channelId, config)); + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_QR.getCode(), config); PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); @@ -111,7 +111,7 @@ public class PayClientFactoryImplIntegrationTest { config.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnq90KnF4dTnlzzmxpujbI05OYqi5WxAS6cL0gnZFv2gK51HExF8v/BaP7P979PhFMgWTqmOOI+Dtno5s+yD09XTY1WkshbLk6i4g2Xlr8fyW9ODnkU88RI2w9UdPhQU4cPPwBNlrsYhKkVK2OxwM3kFqjoBBY0CZoZCsSQ3LDH5WeZqPArlsS6xa2zqJBuuoKjMrdpELl3eXSjP8K54eDJCbeetCZNKWLL3DPahTPB7LZikfYmslb0QUvCgGapD0xkS7eVq70NaL1G57MWABs4tbfWgxike4Daj3EfUrzIVspQxj7w8HEj9WozJPgL88kSJSits0pqD3n5r8HSuseQIDAQAB"); // 创建客户端 Long channelId = RandomUtil.randomLong(); - payClientFactory.addOrUpdatePayClient(channelId, new AlipayWapPayClient(channelId, config)); + payClientFactory.createOrUpdatePayClient(channelId, PayChannelEnum.ALIPAY_WAP.getCode(), config); PayClient client = payClientFactory.getPayClient(channelId); // 发起支付 PayOrderUnifiedReqDTO reqDTO = buildPayOrderUnifiedReqDTO(); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java index 08cc52654..20f66bb0f 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java +++ b/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; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; +import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; import lombok.extern.slf4j.Slf4j; @@ -21,7 +21,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC * @author jason */ @Slf4j -public class WalletPayClient extends AbstractPayClient { +public class WalletPayClient extends DelegatePayClient { private PayWalletService payWalletService; diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java index 3504b98cd..d29ad973f 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java +++ b/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; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; -import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; -import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; -import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.*; -import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClient; -import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClientConfig; -import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.*; import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelCreateReqVO; @@ -34,6 +29,7 @@ import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.validation.Validator; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -85,55 +81,29 @@ public class PayChannelServiceImpl implements PayChannelService { log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); } log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); + // 钱包 client 需要和其它 client 分开了创建 + List walletChannels = new ArrayList<>(); + // TODO @jason:有点复杂,看看用 PayClientInitializer 能不能简化 + List otherChannels = new ArrayList<>(); + channels.forEach(t -> { + if (PayChannelEnum.WALLET.getCode().equals(t.getCode())) { + walletChannels.add(t); + } else { + otherChannels.add(t); + } + }); // 第二步:构建缓存:创建或更新支付 Client - channels.forEach(payChannel ->{ - AbstractPayClient payClient = createPayClient(payChannel.getId(), payChannel.getCode(), payChannel.getConfig()); - payClientFactory.addOrUpdatePayClient(payChannel.getId(), payClient); + otherChannels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), + payChannel.getCode(), payChannel.getConfig())); + walletChannels.forEach(payChannel -> { + WalletPayClient walletPayClient = new WalletPayClient(payChannel.getId(), payChannel.getCode(), + (NonePayClientConfig) payChannel.getConfig(), payWalletService); + payClientFactory.addOrUpdateDelegatePayClient(payChannel.getId(), walletPayClient); }); this.channelCache = channels; }); } - @SuppressWarnings("unchecked") - private AbstractPayClient createPayClient( - Long channelId, String channelCode, Config config) { - PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode); - Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelEnum)); - // 创建客户端 - switch (channelEnum) { - // 微信支付 - case WX_PUB: - return (AbstractPayClient) new WxPubPayClient(channelId, (WxPayClientConfig) config); - case WX_LITE: - return (AbstractPayClient) new WxLitePayClient(channelId, (WxPayClientConfig) config); - case WX_APP: - return (AbstractPayClient) new WxAppPayClient(channelId, (WxPayClientConfig) config); - case WX_BAR: - return (AbstractPayClient) new WxBarPayClient(channelId, (WxPayClientConfig) config); - case WX_NATIVE: - return (AbstractPayClient) new WxNativePayClient(channelId, (WxPayClientConfig) config); - // 支付宝支付 - case ALIPAY_WAP: - return (AbstractPayClient) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_QR: - return (AbstractPayClient) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_APP: - return (AbstractPayClient) new AlipayAppPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_PC: - return (AbstractPayClient) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_BAR: - return (AbstractPayClient) new AlipayBarPayClient(channelId, (AlipayPayClientConfig) config); - // 其它支付 - case MOCK: - return (AbstractPayClient) new MockPayClient(channelId, (MockPayClientConfig) config); - case WALLET: - return (AbstractPayClient) new WalletPayClient( - channelId, channelCode, (NonePayClientConfig) config, payWalletService); - } - // 创建失败,错误日志 + 抛出异常 - log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); - throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", config)); - } /** * 通过定时任务轮询,刷新缓存 From 457312d5aac4de73540ed34950e65902792be201 Mon Sep 17 00:00:00 2001 From: jason <2667446@qq.com> Date: Thu, 31 Aug 2023 14:30:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20PayClientFactory=20?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=20PayClient=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/core/client/PayClientFactory.java | 13 ++-- .../client/impl/PayClientFactoryImpl.java | 62 +++++++++++-------- .../framework/pay/wallet/WalletPayClient.java | 30 +++++---- .../channel/PayChannelServiceImpl.java | 26 +------- .../service/wallet/PayWalletServiceImpl.java | 1 - 5 files changed, 58 insertions(+), 74 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java index 03e6bb613..12b8cd8d3 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java @@ -1,6 +1,6 @@ package cn.iocoder.yudao.framework.pay.core.client; -import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; /** * 支付客户端的工厂接口 @@ -28,11 +28,10 @@ public interface PayClientFactory { Config config); /** - * 新增或更新代理支付客户端 - * @param channelId 渠道编号 - * @param delegatePayClient 代理支付客户端 - * @param 支付配置 + * 注册支付客户端 Class, 用于模块中实现的 PayClient + * + * @param payChannelEnum 支付渠道的编码的枚举 + * @param payClientClass 支付客户端 class */ - void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient delegatePayClient); - + void registerPayClientClass(PayChannelEnum payChannelEnum, Class payClientClass); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java index eb11009d7..b9ab05830 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java @@ -1,20 +1,22 @@ package cn.iocoder.yudao.framework.pay.core.client.impl; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.ReflectUtil; import cn.iocoder.yudao.framework.pay.core.client.PayClient; import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.*; -import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClient; -import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.impl.weixin.*; import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import lombok.extern.slf4j.Slf4j; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import static cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum.*; + /** * 支付客户端的工厂实现类 * @@ -29,6 +31,29 @@ public class PayClientFactoryImpl implements PayClientFactory { */ private final ConcurrentMap> clients = new ConcurrentHashMap<>(); + /** + * 支付客户端 Class Map + * key: 支付渠道的编码的枚举 + */ + private final Map>clientClass = new ConcurrentHashMap<>(16); + + public PayClientFactoryImpl() { + // 微信支付客户端 + clientClass.put(WX_PUB, WxPubPayClient.class); + clientClass.put(WX_LITE, WxLitePayClient.class); + clientClass.put(WX_APP, WxAppPayClient.class); + clientClass.put(WX_BAR, WxBarPayClient.class); + clientClass.put(WX_NATIVE, WxNativePayClient.class); + // 支付包支付客户端 + clientClass.put(ALIPAY_WAP, AlipayWapPayClient.class); + clientClass.put(ALIPAY_QR, AlipayQrPayClient.class); + clientClass.put(ALIPAY_APP, AlipayAppPayClient.class); + clientClass.put(ALIPAY_PC, AlipayPcPayClient.class); + clientClass.put(ALIPAY_BAR, AlipayBarPayClient.class); + // Mock 支付客户端 + clientClass.put(MOCK, MockPayClient.class); + } + @Override public PayClient getPayClient(Long channelId) { AbstractPayClient client = clients.get(channelId); @@ -53,35 +78,18 @@ public class PayClientFactoryImpl implements PayClientFactory { } @Override - public void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient delegatePayClient) { - clients.put(channelId, delegatePayClient); + public void registerPayClientClass(PayChannelEnum payChannelEnum, Class payClientClass) { + clientClass.put(payChannelEnum, payClientClass); } @SuppressWarnings("unchecked") - private AbstractPayClient createPayClient( - Long channelId, String channelCode, Config config) { + private AbstractPayClient createPayClient(Long channelId, String channelCode, + Config config) { PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode); - Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelEnum)); - // 创建客户端 - switch (channelEnum) { - // 微信支付 - case WX_PUB: return (AbstractPayClient) new WxPubPayClient(channelId, (WxPayClientConfig) config); - case WX_LITE: return (AbstractPayClient) new WxLitePayClient(channelId, (WxPayClientConfig) config); - case WX_APP: return (AbstractPayClient) new WxAppPayClient(channelId, (WxPayClientConfig) config); - case WX_BAR: return (AbstractPayClient) new WxBarPayClient(channelId, (WxPayClientConfig) config); - case WX_NATIVE: return (AbstractPayClient) new WxNativePayClient(channelId, (WxPayClientConfig) config); - // 支付宝支付 - case ALIPAY_WAP: return (AbstractPayClient) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_QR: return (AbstractPayClient) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_APP: return (AbstractPayClient) new AlipayAppPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_PC: return (AbstractPayClient) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config); - case ALIPAY_BAR: return (AbstractPayClient) new AlipayBarPayClient(channelId, (AlipayPayClientConfig) config); - // 其它支付 - case MOCK: return (AbstractPayClient) new MockPayClient(channelId, (MockPayClientConfig) config); - } - // 创建失败,错误日志 + 抛出异常 - log.error("[createPayClient][配置({}) 找不到合适的客户端实现]", config); - throw new IllegalArgumentException(String.format("配置(%s) 找不到合适的客户端实现", config)); + Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelCode)); + Class payClientClass = clientClass.get(channelEnum); + Assert.notNull(payClientClass, String.format("支付渠道(%s) Class 为空", channelCode)); + return (AbstractPayClient) ReflectUtil.newInstance(payClientClass, channelId, config); } } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java index 20f66bb0f..55b8c3cfd 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java @@ -1,12 +1,14 @@ package cn.iocoder.yudao.module.pay.framework.pay.wallet; +import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; +import cn.iocoder.yudao.framework.pay.core.client.impl.AbstractPayClient; import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; -import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient; +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO; import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; import lombok.extern.slf4j.Slf4j; @@ -21,32 +23,28 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC * @author jason */ @Slf4j -public class WalletPayClient extends DelegatePayClient { +public class WalletPayClient extends AbstractPayClient { - private PayWalletService payWalletService; + private PayWalletService client; - public WalletPayClient(Long channelId, String channelCode, NonePayClientConfig config) { - super(channelId, channelCode, config); - } - - public WalletPayClient(Long channelId, String channelCode, NonePayClientConfig config, - PayWalletService payWalletService) { - this(channelId, channelCode, config); - this.payWalletService = payWalletService; + public WalletPayClient(Long channelId, NonePayClientConfig config) { + super(channelId, PayChannelEnum.WALLET.getCode(), config); } @Override protected void doInit() { - // 钱包支付,无需初始化 + if (client == null) { + client = SpringUtil.getBean(PayWalletService.class); + } } @Override protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { try { - PayWalletTransactionDO transaction = payWalletService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice()); + PayWalletTransactionDO transaction = client.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice()); return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), transaction.getTransactionTime(), - reqDTO.getOutTradeNo(), "WALLET_PAY_SUCCESS"); // TODO @jason:transaction 作为 traData 好了; + reqDTO.getOutTradeNo(), transaction); } catch (Throwable ex) { log.error("[doUnifiedOrder] 失败", ex); Integer errorCode = INTERNAL_SERVER_ERROR.getCode(); @@ -74,10 +72,10 @@ public class WalletPayClient extends DelegatePayClient { @Override protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { try { - PayWalletTransactionDO payWalletTransaction = payWalletService.refund(reqDTO.getOutRefundNo(), + PayWalletTransactionDO payWalletTransaction = client.refund(reqDTO.getOutRefundNo(), reqDTO.getRefundPrice(), reqDTO.getReason()); return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(), - reqDTO.getOutRefundNo(), "WALLET_REFUND_SUCCESS"); + reqDTO.getOutRefundNo(), payWalletTransaction); } catch (Throwable ex) { log.error("[doUnifiedRefund] 失败", ex); Integer errorCode = INTERNAL_SERVER_ERROR.getCode(); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java index d29ad973f..e1cfe7d28 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java @@ -6,7 +6,6 @@ import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; -import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig; import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; @@ -17,7 +16,6 @@ import cn.iocoder.yudao.module.pay.convert.channel.PayChannelConvert; import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO; import cn.iocoder.yudao.module.pay.dal.mysql.channel.PayChannelMapper; import cn.iocoder.yudao.module.pay.framework.pay.wallet.WalletPayClient; -import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; @@ -29,7 +27,6 @@ import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.validation.Validator; import java.time.LocalDateTime; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -60,14 +57,14 @@ public class PayChannelServiceImpl implements PayChannelService { @Resource private Validator validator; - @Resource - private PayWalletService payWalletService; /** * 初始化 {@link #payClientFactory} 缓存 */ @PostConstruct public void initLocalCache() { + // 注册钱包支付 Class + payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class); // 注意:忽略自动多租户,因为要全局初始化缓存 TenantUtils.executeIgnore(() -> { // 第一步:查询数据 @@ -81,26 +78,9 @@ public class PayChannelServiceImpl implements PayChannelService { log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); } log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); - // 钱包 client 需要和其它 client 分开了创建 - List walletChannels = new ArrayList<>(); - // TODO @jason:有点复杂,看看用 PayClientInitializer 能不能简化 - List otherChannels = new ArrayList<>(); - channels.forEach(t -> { - if (PayChannelEnum.WALLET.getCode().equals(t.getCode())) { - walletChannels.add(t); - } else { - otherChannels.add(t); - } - }); // 第二步:构建缓存:创建或更新支付 Client - otherChannels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), + channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), payChannel.getCode(), payChannel.getConfig())); - - walletChannels.forEach(payChannel -> { - WalletPayClient walletPayClient = new WalletPayClient(payChannel.getId(), payChannel.getCode(), - (NonePayClientConfig) payChannel.getConfig(), payWalletService); - payClientFactory.addOrUpdateDelegatePayClient(payChannel.getId(), walletPayClient); - }); this.channelCache = channels; }); } diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java index 6992d63f6..a52b12d6d 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java @@ -45,7 +45,6 @@ public class PayWalletServiceImpl implements PayWalletService { private PayWalletMapper payWalletMapper; @Resource private PayNoRedisDAO noRedisDAO; - @Resource private PayWalletTransactionService payWalletTransactionService; @Resource