mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
钱包支付 Client 的实现
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
});
|
||||
}
|
||||
|
@ -106,6 +106,14 @@ public interface PayOrderService {
|
||||
*/
|
||||
PayOrderExtensionDO getOrderExtension(Long id);
|
||||
|
||||
/**
|
||||
* 获得支付订单
|
||||
*
|
||||
* @param no 支付订单 no
|
||||
* @return 支付订单
|
||||
*/
|
||||
PayOrderExtensionDO getOrderExtensionByNo(String no);
|
||||
|
||||
/**
|
||||
* 同步订单的支付状态
|
||||
*
|
||||
|
@ -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. 查询指定创建时间内的待支付订单
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -20,4 +20,6 @@ public interface PayWalletTransactionService {
|
||||
*/
|
||||
PageResult<PayWalletTransactionDO> getWalletTransactionPage(Long userId, Integer userType,
|
||||
AppPayWalletTransactionPageReqVO pageVO);
|
||||
|
||||
Long addPayWalletTransaction(PayWalletTransactionDO payWalletTransaction);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user