钱包支付 Client 的实现

This commit is contained in:
jason 2023-08-27 10:10:28 +08:00
parent 1baa7f4aee
commit c7f6f92159
15 changed files with 285 additions and 6 deletions

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.framework.pay.core.client;
import cn.iocoder.yudao.framework.pay.core.client.impl.delegate.DelegatePayClient;
/**
* 支付客户端的工厂接口
*
@ -25,4 +27,6 @@ public interface PayClientFactory {
<Config extends PayClientConfig> void createOrUpdatePayClient(Long channelId, String channelCode,
Config config);
<Config extends PayClientConfig> void createOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient);
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.framework.pay.core.client.impl;
import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig;
import lombok.Data;
import javax.validation.Validator;
/**
* 无需任何配置 PayClientConfig 实现类
*
* @author jason
*/
@Data
public class NonePayClientConfig implements PayClientConfig {
/**
* 配置名称
* <p>
* 如果不加任何属性JsonUtils.parseObject2 解析会报错所以暂时加个名称
*/
private String name;
public NonePayClientConfig(){
this.name = "none-config";
}
@Override
public void validate(Validator validator) {
// 无任何配置不需要校验
}
}

View File

@ -5,6 +5,7 @@ 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.*;
@ -51,6 +52,11 @@ public class PayClientFactoryImpl implements PayClientFactory {
}
}
@Override
public <Config extends PayClientConfig> void createOrUpdateDelegatePayClient(Long channelId, DelegatePayClient<Config> delegatePayClient) {
clients.put(channelId, delegatePayClient);
}
@SuppressWarnings("unchecked")
private <Config extends PayClientConfig> AbstractPayClient<Config> createPayClient(
Long channelId, String channelCode, Config config) {

View File

@ -0,0 +1,58 @@
package cn.iocoder.yudao.framework.pay.core.client.impl.delegate;
import cn.iocoder.yudao.framework.pay.core.client.PayClientConfig;
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 java.util.Map;
/**
* @author jason
*/
public abstract class DelegatePayClient<Config extends PayClientConfig> extends AbstractPayClient<PayClientConfig> {
private final DelegatePayClient<Config> delegate;
public DelegatePayClient(Long channelId, String channelCode, PayClientConfig config) {
super(channelId, channelCode, config);
delegate = this;
}
@Override
protected void doInit() {
delegate.doInit();
}
@Override
protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
return delegate.doUnifiedOrder(reqDTO);
}
@Override
protected PayOrderRespDTO doGetOrder(String outTradeNo) {
return delegate.doGetOrder(outTradeNo);
}
@Override
protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
return delegate.doUnifiedRefund(reqDTO);
}
@Override
protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) {
return delegate.doGetRefund(outTradeNo, outRefundNo);
}
@Override
protected PayRefundRespDTO doParseRefundNotify(Map<String,String> params, String body) {
return delegate.doParseRefundNotify(params, body);
}
@Override
protected PayOrderRespDTO doParseOrderNotify(Map<String,String> params, String body) {
return delegate.doParseOrderNotify(params, body);
}
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.framework.pay.core.enums.channel;
import cn.hutool.core.util.ArrayUtil;
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.impl.alipay.AlipayPayClientConfig;
import cn.iocoder.yudao.framework.pay.core.client.impl.mock.MockPayClientConfig;
@ -29,7 +30,9 @@ public enum PayChannelEnum {
ALIPAY_QR("alipay_qr", "支付宝扫码支付", AlipayPayClientConfig.class),
ALIPAY_BAR("alipay_bar", "支付宝条码支付", AlipayPayClientConfig.class),
MOCK("mock", "模拟支付", MockPayClientConfig.class);
MOCK("mock", "模拟支付", MockPayClientConfig.class),
WALLET("wallet", "钱包支付", NonePayClientConfig.class);
/**
* 编码

View File

@ -43,6 +43,7 @@ public interface ErrorCodeConstants {
// ========== 钱包模块(退款) 1007007000 ==========
ErrorCode WALLET_NOT_FOUND = new ErrorCode(1007007000, "用户钱包不存在");
ErrorCode WALLET_NOT_ENOUGH = new ErrorCode(1007007001, "钱包余额不足");
// ========== 示例订单 1007900000 ==========
ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在");

View File

@ -12,7 +12,8 @@ import lombok.Getter;
@Getter
public enum WalletBizTypeEnum {
RECHARGE(1, "充值"),
RECHARGE_REFUND(2, "充值退款");
RECHARGE_REFUND(2, "充值退款"),
PAYMENT(3, "支付");
// TODO 后续增加
/**

View File

@ -0,0 +1,72 @@
package cn.iocoder.yudao.module.pay.framework.pay.wallet;
import cn.iocoder.yudao.framework.pay.core.client.impl.NonePayClientConfig;
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.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;
import java.util.Map;
/**
* 钱包支付的 PayClient 实现类
*
* @author jason
*/
@Slf4j
public class WalletPayClient extends DelegatePayClient<NonePayClientConfig> {
private PayWalletService payWalletService;
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;
}
@Override
protected void doInit() {
// 钱包支付 无需初始化
}
@Override
protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) {
PayWalletTransactionDO payWalletTransaction = payWalletService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice());
return PayOrderRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getCreator(),
payWalletTransaction.getTransactionTime(),
reqDTO.getOutTradeNo(), "");
}
@Override
protected PayOrderRespDTO doParseOrderNotify(Map<String, String> params, String body) {
throw new UnsupportedOperationException("钱包支付无支付回调");
}
@Override
protected PayOrderRespDTO doGetOrder(String outTradeNo) {
return null;
}
@Override
protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) {
return null;
}
@Override
protected PayRefundRespDTO doParseRefundNotify(Map<String, String> params, String body) {
throw new UnsupportedOperationException("钱包支付无退款回调");
}
@Override
protected PayRefundRespDTO doGetRefund(String outTradeNo, String outRefundNo) {
return null;
}
}

View File

@ -6,6 +6,7 @@ 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;
@ -15,6 +16,8 @@ import cn.iocoder.yudao.module.pay.controller.admin.channel.vo.PayChannelUpdateR
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;
@ -26,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;
@ -56,6 +60,8 @@ public class PayChannelServiceImpl implements PayChannelService {
@Resource
private Validator validator;
@Resource
private PayWalletService payWalletService;
/**
* 初始化 {@link #payClientFactory} 缓存
@ -75,10 +81,24 @@ public class PayChannelServiceImpl implements PayChannelService {
log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]");
}
log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size());
List<PayChannelDO> walletChannels = new ArrayList<>();
List<PayChannelDO> otherChannels = new ArrayList<>();
channels.forEach(t -> {
if (PayChannelEnum.WALLET.getCode().equals(t.getCode())) {
walletChannels.add(t);
} else {
otherChannels.add(t);
}
});
// 第二步构建缓存创建或更新支付 Client
channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(),
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.createOrUpdateDelegatePayClient(payChannel.getId(), walletPayClient);
});
this.channelCache = channels;
});
}

View File

@ -106,6 +106,14 @@ public interface PayOrderService {
*/
PayOrderExtensionDO getOrderExtension(Long id);
/**
* 获得支付订单
*
* @param no 支付订单 no
* @return 支付订单
*/
PayOrderExtensionDO getOrderExtensionByNo(String no);
/**
* 同步订单的支付状态
*

View File

@ -415,6 +415,11 @@ public class PayOrderServiceImpl implements PayOrderService {
return orderExtensionMapper.selectById(id);
}
@Override
public PayOrderExtensionDO getOrderExtensionByNo(String no) {
return orderExtensionMapper.selectByNo(no);
}
@Override
public int syncOrder(LocalDateTime minCreateTime) {
// 1. 查询指定创建时间内的待支付订单

View File

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.pay.service.wallet;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
/**
* 钱包 Service 接口
@ -15,4 +16,6 @@ public interface PayWalletService {
* @param userType 用户类型
*/
PayWalletDO getPayWallet(Long userId, Integer userType);
PayWalletTransactionDO pay(String outTradeNo, Integer price);
}

View File

@ -1,24 +1,81 @@
package cn.iocoder.yudao.module.pay.service.wallet;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserType;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.pay.enums.member.WalletBizTypeEnum.PAYMENT;
/**
* 钱包 Service 实现类
*
* @author jason
*/
@Service
@Slf4j
public class PayWalletServiceImpl implements PayWalletService {
private static final String WALLET_CHANNEL_NO_PREFIX = "W";
@Resource
@Lazy
private PayOrderService payOrderService;
@Resource
private PayWalletMapper payWalletMapper;
@Resource
private PayWalletTransactionService payWalletTransactionService;
@Resource
private PayNoRedisDAO noRedisDAO;
@Override
public PayWalletDO getPayWallet(Long userId, Integer userType) {
return payWalletMapper.selectByUserIdAndType(userId, userType);
}
@Override
@Transactional(rollbackFor = Exception.class)
public PayWalletTransactionDO pay(String outTradeNo, Integer price) {
// 判断支付交易拓展单是否存在
PayOrderExtensionDO orderExtension = payOrderService.getOrderExtensionByNo(outTradeNo);
if (orderExtension == null) {
throw exception(ORDER_EXTENSION_NOT_FOUND);
}
Long userId = getLoginUserId();
Integer userType = getLoginUserType();
PayWalletDO payWallet = getPayWallet(userId, userType);
if (payWallet == null) {
log.error("[pay] 用户 {} 钱包不存在", userId);
throw exception(WALLET_NOT_FOUND);
}
// 判断余额是否足够
int afterBalance = payWallet.getBalance() - price;
if(afterBalance < 0){
throw exception(WALLET_NOT_ENOUGH);
}
payWallet.setBalance(afterBalance);
payWallet.setTotalExpense(payWallet.getTotalExpense() + price);
payWalletMapper.updateById(payWallet);
// 生成钱包渠道流水号
String walletNo = noRedisDAO.generate(WALLET_CHANNEL_NO_PREFIX);
PayWalletTransactionDO payWalletTransaction = new PayWalletTransactionDO().setWalletId(payWallet.getId())
.setNo(walletNo).setAmount(price).setBalance(afterBalance).setTransactionTime(LocalDateTime.now())
.setBizId(orderExtension.getOrderId()).setBizType(PAYMENT.getBizType());
payWalletTransactionService.addPayWalletTransaction(payWalletTransaction);
return payWalletTransaction;
}
}

View File

@ -20,4 +20,6 @@ public interface PayWalletTransactionService {
*/
PageResult<PayWalletTransactionDO> getWalletTransactionPage(Long userId, Integer userType,
AppPayWalletTransactionPageReqVO pageVO);
Long addPayWalletTransaction(PayWalletTransactionDO payWalletTransaction);
}

View File

@ -21,7 +21,7 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.WALLET_NOT_FO
*/
@Service
@Slf4j
public class PayWalletTransactionServiceImpl implements PayWalletTransactionService{
public class PayWalletTransactionServiceImpl implements PayWalletTransactionService {
@Resource
private PayWalletService payWalletService;
@Resource
@ -38,4 +38,12 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionSer
return payWalletTransactionMapper.selectPageByWalletIdAndQueryType(payWallet.getId(),
WalletTransactionQueryTypeEnum.valueOf(pageVO.getType()), pageVO);
}
@Override
public Long addPayWalletTransaction(PayWalletTransactionDO payWalletTransaction) {
payWalletTransactionMapper.insert(payWalletTransaction);
return payWalletTransaction.getId();
}
}