code review:支付钱包

This commit is contained in:
YunaiV 2023-08-31 20:13:12 +08:00
parent bca6fb4698
commit ba444c9538
5 changed files with 19 additions and 15 deletions

View File

@ -28,10 +28,11 @@ public interface PayClientFactory {
Config config);
/**
* 注册支付客户端 Class, 用于模块中实现的 PayClient
* 注册支付客户端 Class用于模块中实现的 PayClient
*
* @param payChannelEnum 支付渠道的编码的枚举
* @param channel 支付渠道的编码的枚举
* @param payClientClass 支付客户端 class
*/
void registerPayClientClass(PayChannelEnum payChannelEnum, Class<?> payClientClass);
void registerPayClientClass(PayChannelEnum channel, Class<?> payClientClass);
}

View File

@ -27,15 +27,15 @@ public class PayClientFactoryImpl implements PayClientFactory {
/**
* 支付客户端 Map
*
* key渠道编号
*/
private final ConcurrentMap<Long, AbstractPayClient<?>> clients = new ConcurrentHashMap<>();
/**
* 支付客户端 Class Map
* key: 支付渠道的编码的枚举
*/
private final Map<PayChannelEnum, Class<?>>clientClass = new ConcurrentHashMap<>(16);
private final Map<PayChannelEnum, Class<?>> clientClass = new ConcurrentHashMap<>();
public PayClientFactoryImpl() {
// 微信支付客户端
@ -54,6 +54,11 @@ public class PayClientFactoryImpl implements PayClientFactory {
clientClass.put(MOCK, MockPayClient.class);
}
@Override
public void registerPayClientClass(PayChannelEnum channel, Class<?> payClientClass) {
clientClass.put(channel, payClientClass);
}
@Override
public PayClient getPayClient(Long channelId) {
AbstractPayClient<?> client = clients.get(channelId);
@ -77,11 +82,6 @@ public class PayClientFactoryImpl implements PayClientFactory {
}
}
@Override
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) {

View File

@ -25,7 +25,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC
@Slf4j
public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
private PayWalletService client;
private PayWalletService wallService;
public WalletPayClient(Long channelId, NonePayClientConfig config) {
super(channelId, PayChannelEnum.WALLET.getCode(), config);
@ -33,15 +33,15 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
@Override
protected void doInit() {
if (client == null) {
client = SpringUtil.getBean(PayWalletService.class);
if (wallService == null) {
wallService = SpringUtil.getBean(PayWalletService.class);
}
}
@Override
protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
try {
PayWalletTransactionDO transaction = client.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
PayWalletTransactionDO transaction = wallService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(),
transaction.getTransactionTime(),
reqDTO.getOutTradeNo(), transaction);
@ -72,7 +72,7 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
@Override
protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
try {
PayWalletTransactionDO payWalletTransaction = client.refund(reqDTO.getOutRefundNo(),
PayWalletTransactionDO payWalletTransaction = wallService.refund(reqDTO.getOutRefundNo(),
reqDTO.getRefundPrice(), reqDTO.getReason());
return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(),
reqDTO.getOutRefundNo(), payWalletTransaction);

View File

@ -65,6 +65,7 @@ public class PayChannelServiceImpl implements PayChannelService {
public void initLocalCache() {
// 注册钱包支付 Class
payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class);
// 注意忽略自动多租户因为要全局初始化缓存
TenantUtils.executeIgnore(() -> {
// 第一步查询数据
@ -78,6 +79,7 @@ public class PayChannelServiceImpl implements PayChannelService {
log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]");
}
log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size());
// 第二步构建缓存创建或更新支付 Client
channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(),
payChannel.getCode(), payChannel.getConfig()));

View File

@ -45,6 +45,7 @@ public class PayWalletServiceImpl implements PayWalletService {
private PayWalletMapper payWalletMapper;
@Resource
private PayNoRedisDAO noRedisDAO;
@Resource
private PayWalletTransactionService payWalletTransactionService;
@Resource