|
|
|
|
@ -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<Long, AbstractPayClient<?>> clients = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 支付客户端 Class Map
|
|
|
|
|
* key: 支付渠道的编码的枚举
|
|
|
|
|
*/
|
|
|
|
|
private final Map<PayChannelEnum, Class<?>>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 <Config extends PayClientConfig> void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient) {
|
|
|
|
|
clients.put(channelId, delegatePayClient);
|
|
|
|
|
public void registerPayClientClass(PayChannelEnum payChannelEnum, Class<?> payClientClass) {
|
|
|
|
|
clientClass.put(payChannelEnum, payClientClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(
|
|
|
|
|
Long channelId, String channelCode, Config config) {
|
|
|
|
|
private <Config extends PayClientConfig> AbstractPayClient<Config> 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<Config>) new WxPubPayClient(channelId, (WxPayClientConfig) config);
|
|
|
|
|
case WX_LITE: return (AbstractPayClient<Config>) new WxLitePayClient(channelId, (WxPayClientConfig) config);
|
|
|
|
|
case WX_APP: return (AbstractPayClient<Config>) new WxAppPayClient(channelId, (WxPayClientConfig) config);
|
|
|
|
|
case WX_BAR: return (AbstractPayClient<Config>) new WxBarPayClient(channelId, (WxPayClientConfig) config);
|
|
|
|
|
case WX_NATIVE: return (AbstractPayClient<Config>) new WxNativePayClient(channelId, (WxPayClientConfig) config);
|
|
|
|
|
// 支付宝支付
|
|
|
|
|
case ALIPAY_WAP: return (AbstractPayClient<Config>) new AlipayWapPayClient(channelId, (AlipayPayClientConfig) config);
|
|
|
|
|
case ALIPAY_QR: return (AbstractPayClient<Config>) new AlipayQrPayClient(channelId, (AlipayPayClientConfig) config);
|
|
|
|
|
case ALIPAY_APP: return (AbstractPayClient<Config>) new AlipayAppPayClient(channelId, (AlipayPayClientConfig) config);
|
|
|
|
|
case ALIPAY_PC: return (AbstractPayClient<Config>) new AlipayPcPayClient(channelId, (AlipayPayClientConfig) config);
|
|
|
|
|
case ALIPAY_BAR: return (AbstractPayClient<Config>) new AlipayBarPayClient(channelId, (AlipayPayClientConfig) config);
|
|
|
|
|
// 其它支付
|
|
|
|
|
case MOCK: return (AbstractPayClient<Config>) 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<Config>) ReflectUtil.newInstance(payClientClass, channelId, config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|