mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
修改 PayClientFactory 创建 PayClient 方法
This commit is contained in:
parent
7d4d3bea55
commit
457312d5aa
@ -1,6 +1,6 @@
|
|||||||
package cn.iocoder.yudao.framework.pay.core.client;
|
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);
|
Config config);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增或更新代理支付客户端
|
* 注册支付客户端 Class, 用于模块中实现的 PayClient
|
||||||
* @param channelId 渠道编号
|
*
|
||||||
* @param delegatePayClient 代理支付客户端
|
* @param payChannelEnum 支付渠道的编码的枚举
|
||||||
* @param <Config> 支付配置
|
* @param payClientClass 支付客户端 class
|
||||||
*/
|
*/
|
||||||
<Config extends PayClientConfig> void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient);
|
void registerPayClientClass(PayChannelEnum payChannelEnum, Class<?> payClientClass);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,22 @@
|
|||||||
package cn.iocoder.yudao.framework.pay.core.client.impl;
|
package cn.iocoder.yudao.framework.pay.core.client.impl;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
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.PayClient;
|
||||||
import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig;
|
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.PayClientFactory;
|
||||||
import cn.iocoder.yudao.framework.pay.core.client.impl.alipay.*;
|
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.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.client.impl.weixin.*;
|
||||||
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
|
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
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<>();
|
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
|
@Override
|
||||||
public PayClient getPayClient(Long channelId) {
|
public PayClient getPayClient(Long channelId) {
|
||||||
AbstractPayClient<?> client = clients.get(channelId);
|
AbstractPayClient<?> client = clients.get(channelId);
|
||||||
@ -53,35 +78,18 @@ public class PayClientFactoryImpl implements PayClientFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <Config extends PayClientConfig> void addOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient) {
|
public void registerPayClientClass(PayChannelEnum payChannelEnum, Class<?> payClientClass) {
|
||||||
clients.put(channelId, delegatePayClient);
|
clientClass.put(payChannelEnum, payClientClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(
|
private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(Long channelId, String channelCode,
|
||||||
Long channelId, String channelCode, Config config) {
|
Config config) {
|
||||||
PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode);
|
PayChannelEnum channelEnum = PayChannelEnum.getByCode(channelCode);
|
||||||
Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelEnum));
|
Assert.notNull(channelEnum, String.format("支付渠道(%s) 为空", channelCode));
|
||||||
// 创建客户端
|
Class<?> payClientClass = clientClass.get(channelEnum);
|
||||||
switch (channelEnum) {
|
Assert.notNull(payClientClass, String.format("支付渠道(%s) Class 为空", channelCode));
|
||||||
// 微信支付
|
return (AbstractPayClient<Config>) ReflectUtil.newInstance(payClientClass, channelId, config);
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
package cn.iocoder.yudao.module.pay.framework.pay.wallet;
|
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.common.exception.ServiceException;
|
||||||
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
|
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.order.PayOrderUnifiedReqDTO;
|
||||||
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
|
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.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.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.dal.dataobject.wallet.PayWalletTransactionDO;
|
||||||
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
|
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -21,32 +23,28 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC
|
|||||||
* @author jason
|
* @author jason
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WalletPayClient extends DelegatePayClient<NonePayClientConfig> {
|
public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
|
||||||
|
|
||||||
private PayWalletService payWalletService;
|
private PayWalletService client;
|
||||||
|
|
||||||
public WalletPayClient(Long channelId, String channelCode, NonePayClientConfig config) {
|
public WalletPayClient(Long channelId, NonePayClientConfig config) {
|
||||||
super(channelId, channelCode, config);
|
super(channelId, PayChannelEnum.WALLET.getCode(), config);
|
||||||
}
|
|
||||||
|
|
||||||
public WalletPayClient(Long channelId, String channelCode, NonePayClientConfig config,
|
|
||||||
PayWalletService payWalletService) {
|
|
||||||
this(channelId, channelCode, config);
|
|
||||||
this.payWalletService = payWalletService;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doInit() {
|
protected void doInit() {
|
||||||
// 钱包支付,无需初始化
|
if (client == null) {
|
||||||
|
client = SpringUtil.getBean(PayWalletService.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
|
protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
|
||||||
try {
|
try {
|
||||||
PayWalletTransactionDO transaction = payWalletService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
|
PayWalletTransactionDO transaction = client.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
|
||||||
return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(),
|
return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(),
|
||||||
transaction.getTransactionTime(),
|
transaction.getTransactionTime(),
|
||||||
reqDTO.getOutTradeNo(), "WALLET_PAY_SUCCESS"); // TODO @jason:transaction 作为 traData 好了;
|
reqDTO.getOutTradeNo(), transaction);
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
log.error("[doUnifiedOrder] 失败", ex);
|
log.error("[doUnifiedOrder] 失败", ex);
|
||||||
Integer errorCode = INTERNAL_SERVER_ERROR.getCode();
|
Integer errorCode = INTERNAL_SERVER_ERROR.getCode();
|
||||||
@ -74,10 +72,10 @@ public class WalletPayClient extends DelegatePayClient<NonePayClientConfig> {
|
|||||||
@Override
|
@Override
|
||||||
protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
|
protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
|
||||||
try {
|
try {
|
||||||
PayWalletTransactionDO payWalletTransaction = payWalletService.refund(reqDTO.getOutRefundNo(),
|
PayWalletTransactionDO payWalletTransaction = client.refund(reqDTO.getOutRefundNo(),
|
||||||
reqDTO.getRefundPrice(), reqDTO.getReason());
|
reqDTO.getRefundPrice(), reqDTO.getReason());
|
||||||
return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(),
|
return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(),
|
||||||
reqDTO.getOutRefundNo(), "WALLET_REFUND_SUCCESS");
|
reqDTO.getOutRefundNo(), payWalletTransaction);
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
log.error("[doUnifiedRefund] 失败", ex);
|
log.error("[doUnifiedRefund] 失败", ex);
|
||||||
Integer errorCode = INTERNAL_SERVER_ERROR.getCode();
|
Integer errorCode = INTERNAL_SERVER_ERROR.getCode();
|
||||||
|
@ -6,7 +6,6 @@ import cn.hutool.core.util.ObjectUtil;
|
|||||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
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.PayClientConfig;
|
||||||
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
|
import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
|
||||||
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
|
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.dataobject.channel.PayChannelDO;
|
||||||
import cn.iocoder.yudao.module.pay.dal.mysql.channel.PayChannelMapper;
|
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.framework.pay.wallet.WalletPayClient;
|
||||||
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@ -29,7 +27,6 @@ import javax.annotation.PostConstruct;
|
|||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.validation.Validator;
|
import javax.validation.Validator;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -60,14 +57,14 @@ public class PayChannelServiceImpl implements PayChannelService {
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private Validator validator;
|
private Validator validator;
|
||||||
@Resource
|
|
||||||
private PayWalletService payWalletService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化 {@link #payClientFactory} 缓存
|
* 初始化 {@link #payClientFactory} 缓存
|
||||||
*/
|
*/
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void initLocalCache() {
|
public void initLocalCache() {
|
||||||
|
// 注册钱包支付 Class
|
||||||
|
payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class);
|
||||||
// 注意:忽略自动多租户,因为要全局初始化缓存
|
// 注意:忽略自动多租户,因为要全局初始化缓存
|
||||||
TenantUtils.executeIgnore(() -> {
|
TenantUtils.executeIgnore(() -> {
|
||||||
// 第一步:查询数据
|
// 第一步:查询数据
|
||||||
@ -81,26 +78,9 @@ public class PayChannelServiceImpl implements PayChannelService {
|
|||||||
log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]");
|
log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]");
|
||||||
}
|
}
|
||||||
log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size());
|
log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size());
|
||||||
// 钱包 client 需要和其它 client 分开了创建
|
|
||||||
List<PayChannelDO> walletChannels = new ArrayList<>();
|
|
||||||
// TODO @jason:有点复杂,看看用 PayClientInitializer 能不能简化
|
|
||||||
List<PayChannelDO> otherChannels = new ArrayList<>();
|
|
||||||
channels.forEach(t -> {
|
|
||||||
if (PayChannelEnum.WALLET.getCode().equals(t.getCode())) {
|
|
||||||
walletChannels.add(t);
|
|
||||||
} else {
|
|
||||||
otherChannels.add(t);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 第二步:构建缓存:创建或更新支付 Client
|
// 第二步:构建缓存:创建或更新支付 Client
|
||||||
otherChannels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(),
|
channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(),
|
||||||
payChannel.getCode(), payChannel.getConfig()));
|
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;
|
this.channelCache = channels;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,6 @@ public class PayWalletServiceImpl implements PayWalletService {
|
|||||||
private PayWalletMapper payWalletMapper;
|
private PayWalletMapper payWalletMapper;
|
||||||
@Resource
|
@Resource
|
||||||
private PayNoRedisDAO noRedisDAO;
|
private PayNoRedisDAO noRedisDAO;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private PayWalletTransactionService payWalletTransactionService;
|
private PayWalletTransactionService payWalletTransactionService;
|
||||||
@Resource
|
@Resource
|
||||||
|
Loading…
Reference in New Issue
Block a user