Merge branch 'master-jdk17' of https://gitee.com/zhijiantianya/ruoyi-vue-pro into feature/iot

# Conflicts:
#	yudao-dependencies/pom.xml
#	yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/product/IotProductStatusEnum.java
#	yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/enums/thingmodel/IotThingModelTypeEnum.java
This commit is contained in:
YunaiV
2025-01-28 03:54:16 +08:00
615 changed files with 18343 additions and 7604 deletions

View File

@@ -1,12 +1,14 @@
package cn.iocoder.yudao.module.pay.api.transfer;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferRespDTO;
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
/**
* 转账单 API 实现类
*
@@ -24,4 +26,10 @@ public class PayTransferApiImpl implements PayTransferApi {
return payTransferService.createTransfer(reqDTO);
}
@Override
public PayTransferRespDTO getTransfer(Long id) {
PayTransferDO transfer = payTransferService.getTransfer(id);
return BeanUtils.toBean(transfer, PayTransferRespDTO.class);
}
}

View File

@@ -0,0 +1,33 @@
package cn.iocoder.yudao.module.pay.api.wallet;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.module.pay.api.wallet.dto.PayWalletAddBalanceReqDTO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
/**
* 钱包 API 实现类
*
* @author 芋道源码
*/
@Service
public class PayWalletApiImpl implements PayWalletApi {
@Resource
private PayWalletService payWalletService;
@Override
public void addWalletBalance(PayWalletAddBalanceReqDTO reqDTO) {
// 创建或获取钱包
PayWalletDO wallet = payWalletService.getOrCreateWallet(reqDTO.getUserId(), reqDTO.getUserType());
Assert.notNull(wallet, "钱包({}/{})不存在", reqDTO.getUserId(), reqDTO.getUserType());
// 增加余额
PayWalletBizTypeEnum bizType = PayWalletBizTypeEnum.valueOf(reqDTO.getBizType());
payWalletService.addWalletBalance(wallet.getId(), reqDTO.getBizId(), bizType, reqDTO.getPrice());
}
}

View File

@@ -40,4 +40,8 @@ public class PayAppBaseVO {
@URL(message = "退款结果的回调地址必须为 URL 格式")
private String refundNotifyUrl;
@Schema(description = "转账结果的回调地址", example = "http://127.0.0.1:48080/transfer-callback")
@URL(message = "转账结果的回调地址必须为 URL 格式")
private String transferNotifyUrl;
}

View File

@@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO;
import cn.iocoder.yudao.module.pay.controller.admin.notify.vo.PayNotifyTaskDetailRespVO;
import cn.iocoder.yudao.module.pay.controller.admin.notify.vo.PayNotifyTaskPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.notify.vo.PayNotifyTaskRespVO;
@@ -18,6 +19,7 @@ import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.pay.service.notify.PayNotifyService;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -49,6 +51,8 @@ public class PayNotifyController {
@Resource
private PayRefundService refundService;
@Resource
private PayTransferService payTransferService;
@Resource
private PayNotifyService notifyService;
@Resource
private PayAppService appService;
@@ -65,7 +69,7 @@ public class PayNotifyController {
// 1. 校验支付渠道是否存在
PayClient payClient = channelService.getPayClient(channelId);
if (payClient == null) {
log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
log.error("[notifyOrder][渠道编号({}) 找不到对应的支付客户端]", channelId);
throw exception(CHANNEL_NOT_FOUND);
}
@@ -79,13 +83,13 @@ public class PayNotifyController {
@Operation(summary = "支付渠道的统一【退款】回调")
@PermitAll
public String notifyRefund(@PathVariable("channelId") Long channelId,
@RequestParam(required = false) Map<String, String> params,
@RequestBody(required = false) String body) {
@RequestParam(required = false) Map<String, String> params,
@RequestBody(required = false) String body) {
log.info("[notifyRefund][channelId({}) 回调数据({}/{})]", channelId, params, body);
// 1. 校验支付渠道是否存在
PayClient payClient = channelService.getPayClient(channelId);
if (payClient == null) {
log.error("[notifyCallback][渠道编号({}) 找不到对应的支付客户端]", channelId);
log.error("[notifyRefund][渠道编号({}) 找不到对应的支付客户端]", channelId);
throw exception(CHANNEL_NOT_FOUND);
}
@@ -95,6 +99,26 @@ public class PayNotifyController {
return "success";
}
@PostMapping(value = "/transfer/{channelId}")
@Operation(summary = "支付渠道的统一【转账】回调")
@PermitAll
public String notifyTransfer(@PathVariable("channelId") Long channelId,
@RequestParam(required = false) Map<String, String> params,
@RequestBody(required = false) String body) {
log.info("[notifyTransfer][channelId({}) 回调数据({}/{})]", channelId, params, body);
// 1. 校验支付渠道是否存在
PayClient payClient = channelService.getPayClient(channelId);
if (payClient == null) {
log.error("[notifyTransfer][渠道编号({}) 找不到对应的支付客户端]", channelId);
throw exception(CHANNEL_NOT_FOUND);
}
// 2. 解析通知数据
PayTransferRespDTO notify = payClient.parseTransferNotify(params, body);
payTransferService.notifyTransfer(channelId, notify);
return "success";
}
@GetMapping("/get-detail")
@Operation(summary = "获得回调通知的明细")
@Parameter(name = "id", description = "编号", required = true, example = "1024")

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.*;
@@ -11,12 +12,14 @@ import cn.iocoder.yudao.module.pay.convert.order.PayOrderConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient;
import cn.iocoder.yudao.module.pay.service.app.PayAppService;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
@@ -51,10 +54,21 @@ public class PayOrderController {
@GetMapping("/get")
@Operation(summary = "获得支付订单")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
@Parameters({
@Parameter(name = "id", description = "编号", required = true, example = "1024"),
@Parameter(name = "sync", description = "是否同步", example = "true")
})
@PreAuthorize("@ss.hasPermission('pay:order:query')")
public CommonResult<PayOrderRespVO> getOrder(@RequestParam("id") Long id) {
return success(PayOrderConvert.INSTANCE.convert(orderService.getOrder(id)));
public CommonResult<PayOrderRespVO> getOrder(@RequestParam("id") Long id,
@RequestParam(value = "sync", required = false) Boolean sync) {
PayOrderDO order = orderService.getOrder(id);
// sync 仅在等待支付
if (Boolean.TRUE.equals(sync) && PayOrderStatusEnum.isWaiting(order.getStatus())) {
orderService.syncOrderQuietly(order.getId());
// 重新查询,因为同步后,可能会有变化
order = orderService.getOrder(id);
}
return success(BeanUtils.toBean(order, PayOrderRespVO.class));
}
@GetMapping("/get-detail")

View File

@@ -6,13 +6,13 @@ import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.Set;

View File

@@ -1,24 +1,27 @@
package cn.iocoder.yudao.module.pay.controller.app.order;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderRespVO;
import cn.iocoder.yudao.module.pay.controller.admin.order.vo.PayOrderSubmitRespVO;
import cn.iocoder.yudao.module.pay.controller.app.order.vo.AppPayOrderSubmitReqVO;
import cn.iocoder.yudao.module.pay.controller.app.order.vo.AppPayOrderSubmitRespVO;
import cn.iocoder.yudao.module.pay.convert.order.PayOrderConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.framework.pay.core.WalletPayClient;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import com.google.common.collect.Maps;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import java.util.Map;
import java.util.Objects;
@@ -37,12 +40,22 @@ public class AppPayOrderController {
@Resource
private PayOrderService payOrderService;
// TODO 芋艿:临时 demo技术打样。
@GetMapping("/get")
@Operation(summary = "获得支付订单")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
public CommonResult<PayOrderRespVO> getOrder(@RequestParam("id") Long id) {
return success(PayOrderConvert.INSTANCE.convert(payOrderService.getOrder(id)));
@Parameters({
@Parameter(name = "id", description = "编号", required = true, example = "1024"),
@Parameter(name = "sync", description = "是否同步", example = "true")
})
public CommonResult<PayOrderRespVO> getOrder(@RequestParam("id") Long id,
@RequestParam(value = "sync", required = false) Boolean sync) {
PayOrderDO order = payOrderService.getOrder(id);
// sync 仅在等待支付
if (Boolean.TRUE.equals(sync) && PayOrderStatusEnum.isWaiting(order.getStatus())) {
payOrderService.syncOrderQuietly(order.getId());
// 重新查询,因为同步后,可能会有变化
order = payOrderService.getOrder(id);
}
return success(BeanUtils.toBean(order, PayOrderRespVO.class));
}
@PostMapping("/submit")

View File

@@ -1,4 +0,0 @@
/**
* TODO 芋艿:占个位置,没啥用
*/
package cn.iocoder.yudao.module.pay.controller.app.refund;

View File

@@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.pay.controller.app.wallet;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.pay.controller.app.wallet.vo.wallet.AppPayWalletRespVO;
import cn.iocoder.yudao.module.pay.convert.wallet.PayWalletConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
@@ -35,7 +34,6 @@ public class AppPayWalletController {
@GetMapping("/get")
@Operation(summary = "获取钱包")
@PreAuthenticated
public CommonResult<AppPayWalletRespVO> getPayWallet() {
PayWalletDO wallet = payWalletService.getOrCreateWallet(getLoginUserId(), UserTypeEnum.MEMBER.getValue());
return success(PayWalletConvert.INSTANCE.convert(wallet));

View File

@@ -24,7 +24,8 @@ public interface PayTransferConvert {
PayTransferCreateReqDTO convert(PayDemoTransferCreateReqVO vo);
PayTransferRespVO convert(PayTransferDO bean);
PayTransferRespVO convert(PayTransferDO bean);
PageResult<PayTransferPageItemRespVO> convertPage(PageResult<PayTransferDO> pageResult);
}

View File

@@ -59,7 +59,7 @@ public class PayRefundDO extends BaseDO {
*/
private Long channelId;
/**
* 商户编码
* 渠道编码
*
* 枚举 {@link PayChannelEnum}
*/

View File

@@ -24,6 +24,11 @@ public interface PayOrderExtensionMapper extends BaseMapperX<PayOrderExtensionDO
return selectList(PayOrderExtensionDO::getOrderId, orderId);
}
default List<PayOrderExtensionDO> selectListByOrderIdAndStatus(Long orderId, Integer status) {
return selectList(PayOrderExtensionDO::getOrderId, orderId,
PayOrderExtensionDO::getStatus, status);
}
default List<PayOrderExtensionDO> selectListByStatusAndCreateTimeGe(Integer status, LocalDateTime minCreateTime) {
return selectList(new LambdaQueryWrapper<PayOrderExtensionDO>()
.eq(PayOrderExtensionDO::getStatus, status)

View File

@@ -23,10 +23,6 @@ public interface PayTransferMapper extends BaseMapperX<PayTransferDO> {
PayTransferDO::getMerchantTransferId, merchantTransferId);
}
default PayTransferDO selectByNo(String no){
return selectOne(PayTransferDO::getNo, no);
}
default PageResult<PayTransferDO> selectPage(PayTransferPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<PayTransferDO>()
.eqIfPresent(PayTransferDO::getNo, reqVO.getNo())
@@ -41,9 +37,15 @@ public interface PayTransferMapper extends BaseMapperX<PayTransferDO> {
.orderByDesc(PayTransferDO::getId));
}
default List<PayTransferDO> selectListByStatus(Integer status){
default List<PayTransferDO> selectListByStatus(Integer status) {
return selectList(PayTransferDO::getStatus, status);
}
default PayTransferDO selectByAppIdAndNo(Long appId, String no) {
return selectOne(PayTransferDO::getAppId, appId,
PayTransferDO::getNo, no);
}
}

View File

@@ -68,6 +68,19 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
return update(null, lambdaUpdateWrapper);
}
/**
* 增加余额的时候,更新钱包
*
* @param id 钱包 id
* @param price 钱包金额
*/
default void updateWhenAdd(Long id, Integer price) {
LambdaUpdateWrapper<PayWalletDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<PayWalletDO>()
.setSql(" balance = balance + " + price)
.eq(PayWalletDO::getId, id);
update(null, lambdaUpdateWrapper);
}
/**
* 冻结钱包部分余额
*
@@ -114,7 +127,6 @@ public interface PayWalletMapper extends BaseMapperX<PayWalletDO> {
return update(null, lambdaUpdateWrapper);
}
}

View File

@@ -16,6 +16,15 @@ public interface RedisKeyConstants {
*/
String PAY_NOTIFY_LOCK = "pay_notify:lock:%d";
/**
* 支付钱包的分布式锁
*
* KEY 格式pay_wallet:lock:%d
* VALUE 数据格式HASH // RLock.classRedisson 的 Lock 锁,使用 Hash 数据结构
* 过期时间:不固定
*/
String PAY_WALLET_LOCK = "pay_wallet:lock:%d";
/**
* 支付序号的缓存
*

View File

@@ -0,0 +1,42 @@
package cn.iocoder.yudao.module.pay.dal.redis.wallet;
import jakarta.annotation.Resource;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Repository;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static cn.iocoder.yudao.module.pay.dal.redis.RedisKeyConstants.PAY_WALLET_LOCK;
/**
* 支付钱包的锁 Redis DAO
*
* @author 芋道源码
*/
@Repository
public class PayWalletLockRedisDAO {
@Resource
private RedissonClient redissonClient;
public <V> V lock(Long id, Long timeoutMillis, Callable<V> callable) throws Exception {
String lockKey = formatKey(id);
RLock lock = redissonClient.getLock(lockKey);
try {
lock.lock(timeoutMillis, TimeUnit.MILLISECONDS);
// 执行逻辑
return callable.call();
} catch (Exception e) {
throw e;
} finally {
lock.unlock();
}
}
private static String formatKey(Long id) {
return String.format(PAY_WALLET_LOCK, id);
}
}

View File

@@ -39,6 +39,15 @@ public class PayProperties {
@URL(message = "支付回调地址的格式必须是 URL")
private String refundNotifyUrl;
/**
* 转账回调地址
*
* 实际上,对应的 PayNotifyController 的 notifyTransfer 方法的 URL
*
* 回调顺序:支付渠道(支付宝支付、微信支付) => yudao-module-pay 的 transferNotifyUrl 地址 => 业务的 PayAppDO.transferNotifyUrl 地址
*/
private String transferNotifyUrl;
/**
* 支付订单 no 的前缀
*/

View File

@@ -177,6 +177,11 @@ public class WalletPayClient extends AbstractPayClient<NonePayClientConfig> {
throw new IllegalStateException(String.format("支付退款单[%s] 状态不正确", outRefundNo));
}
@Override
protected PayTransferRespDTO doParseTransferNotify(Map<String, String> params, String body) throws Throwable {
throw new UnsupportedOperationException("未实现");
}
@Override
public PayTransferRespDTO doUnifiedTransfer(PayTransferUnifiedReqDTO reqDTO) {
throw new UnsupportedOperationException("待实现");

View File

@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.pay.service.demo;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
@@ -14,11 +15,11 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoOrderDO;
import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoOrderMapper;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum;
import cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
@@ -111,10 +112,29 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService {
@Override
public void updateDemoOrderPaid(Long id, Long payOrderId) {
// 校验并获得支付订单(可支付)
PayOrderRespDTO payOrder = validateDemoOrderCanPaid(id, payOrderId);
// 1.1 校验订单是否存在
PayDemoOrderDO order = payDemoOrderMapper.selectById(id);
if (order == null) {
log.error("[updateDemoOrderPaid][order({}) payOrder({}) 不存在订单,请进行处理!]", id, payOrderId);
throw exception(DEMO_ORDER_NOT_FOUND);
}
// 1.2 校验订单已支付
if (order.getPayStatus()) {
// 特殊:如果订单已支付,且支付单号相同,直接返回,说明重复回调
if (ObjectUtil.equals(order.getPayOrderId(), payOrderId)) {
log.warn("[updateDemoOrderPaid][order({}) 已支付,且支付单号相同({}),直接返回]", order, payOrderId);
return;
}
// 异常:支付单号不同,说明支付单号错误
log.error("[updateDemoOrderPaid][order({}) 支付单不匹配({})请进行处理order 数据是:{}]",
order, payOrderId, toJsonString(order));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR);
}
// 更新 PayDemoOrderDO 状态为已支付
// 2. 校验支付订单的合法性
PayOrderRespDTO payOrder = validatePayOrderPaid(order, payOrderId);
// 3. 更新 PayDemoOrderDO 状态为已支付
int updateCount = payDemoOrderMapper.updateByIdAndPayed(id, false,
new PayDemoOrderDO().setPayStatus(true).setPayTime(LocalDateTime.now())
.setPayChannelCode(payOrder.getChannelCode()));
@@ -124,56 +144,35 @@ public class PayDemoOrderServiceImpl implements PayDemoOrderService {
}
/**
* 校验交易订单满足被支付的条件
* 校验支付订单的合法性
*
* 1. 交易订单未支付
* 2. 支付单已支付
*
* @param id 交易订单编号
* @param order 交易订单
* @param payOrderId 支付订单编号
* @return 交易订单
* @return 支付订单
*/
private PayOrderRespDTO validateDemoOrderCanPaid(Long id, Long payOrderId) {
// 1.1 校验单是否存在
PayDemoOrderDO order = payDemoOrderMapper.selectById(id);
if (order == null) {
throw exception(DEMO_ORDER_NOT_FOUND);
}
// 1.2 校验订单未支付
if (order.getPayStatus()) {
log.error("[validateDemoOrderCanPaid][order({}) 不处于待支付状态请进行处理order 数据是:{}]",
id, toJsonString(order));
throw exception(DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID);
}
// 1.3 校验支付订单匹配
if (notEqual(order.getPayOrderId(), payOrderId)) { // 支付单号
log.error("[validateDemoOrderCanPaid][order({}) 支付单不匹配({})请进行处理order 数据是:{}]",
id, payOrderId, toJsonString(order));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR);
}
// 2.1 校验支付单是否存在
private PayOrderRespDTO validatePayOrderPaid(PayDemoOrderDO order, Long payOrderId) {
// 1. 校验支付单是否存在
PayOrderRespDTO payOrder = payOrderApi.getOrder(payOrderId);
if (payOrder == null) {
log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 不存在,请进行处理!]", id, payOrderId);
log.error("[validatePayOrderPaid][order({}) payOrder({}) 不存在,请进行处理!]", order.getId(), payOrderId);
throw exception(PAY_ORDER_NOT_FOUND);
}
// 2.2 校验支付单已支付
// 2.1 校验支付单已支付
if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) {
log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
id, payOrderId, toJsonString(payOrder));
log.error("[validatePayOrderPaid][order({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
order.getId(), payOrderId, toJsonString(payOrder));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_STATUS_NOT_SUCCESS);
}
// 2.3 校验支付金额一致
// 2.1 校验支付金额一致
if (notEqual(payOrder.getPrice(), order.getPrice())) {
log.error("[validateDemoOrderCanPaid][order({}) payOrder({}) 支付金额不匹配请进行处理order 数据是:{}payOrder 数据是:{}]",
id, payOrderId, toJsonString(order), toJsonString(payOrder));
log.error("[validatePayOrderPaid][order({}) payOrder({}) 支付金额不匹配请进行处理order 数据是:{}payOrder 数据是:{}]",
order.getId(), payOrderId, toJsonString(order), toJsonString(payOrder));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_PRICE_NOT_MATCH);
}
// 2.4 校验支付订单匹配(二次)
if (notEqual(payOrder.getMerchantOrderId(), id.toString())) {
log.error("[validateDemoOrderCanPaid][order({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
id, payOrderId, toJsonString(payOrder));
// 2.2 校验支付订单匹配(二次)
if (notEqual(payOrder.getMerchantOrderId(), order.getId().toString())) {
log.error("[validatePayOrderPaid][order({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
order.getId(), payOrderId, toJsonString(payOrder));
throw exception(DEMO_ORDER_UPDATE_PAID_FAIL_PAY_ORDER_ID_ERROR);
}
return payOrder;

View File

@@ -139,6 +139,16 @@ public interface PayOrderService {
*/
int syncOrder(LocalDateTime minCreateTime);
/**
* 同步订单的支付状态
*
* 1. Quietly 表示,即使同步失败,也不会抛出异常
* 2. 什么时候回出现异常?因为是主动同步,可能和支付渠道的异步回调存在并发冲突,导致抛出异常
*
* @param id 订单编号
*/
void syncOrderQuietly(Long id);
/**
* 将已过期的订单,状态修改为已关闭
*

View File

@@ -163,7 +163,14 @@ public class PayOrderServiceImpl implements PayOrderService {
// 4. 如果调用直接支付成功,则直接更新支付单状态为成功。例如说:付款码支付,免密支付时,就直接验证支付成功
if (unifiedOrderResp != null) {
getSelf().notifyOrder(channel, unifiedOrderResp);
try {
getSelf().notifyOrder(channel, unifiedOrderResp);
} catch (Exception e) {
// 兼容 https://gitee.com/zhijiantianya/yudao-cloud/issues/I8SM9H 场景
// 支付宝或微信扫码之后时,由于 PayClient 是直接返回支付成功,而支付也会有回调,导致存在并发更新问题,此时一般是可以 try catch 直接忽略
log.warn("[submitOrder][order({}) channel({}) 支付结果({}) 通知时发生异常,可能是并发问题]",
order, channel, unifiedOrderResp, e);
}
// 如有渠道错误码,则抛出业务异常,提示用户
if (StrUtil.isNotEmpty(unifiedOrderResp.getChannelErrorCode())) {
throw exception(PAY_ORDER_SUBMIT_CHANNEL_ERROR, unifiedOrderResp.getChannelErrorCode(),
@@ -431,9 +438,7 @@ public class PayOrderServiceImpl implements PayOrderService {
return;
}
// TODO 芋艿:应该 new 出来更新
order.setPrice(payPrice);
orderMapper.updateById(order);
orderMapper.updateById(new PayOrderDO().setId(order.getId()).setPrice(payPrice));
}
@Override
@@ -462,6 +467,18 @@ public class PayOrderServiceImpl implements PayOrderService {
return count;
}
@Override
public void syncOrderQuietly(Long id) {
// 1. 查询待支付订单
List<PayOrderExtensionDO> orderExtensions = orderExtensionMapper.selectListByOrderIdAndStatus(id,
PayOrderStatusEnum.WAITING.getStatus());
// 2. 遍历执行
for (PayOrderExtensionDO orderExtension : orderExtensions) {
syncOrder(orderExtension);
}
}
/**
* 同步单个支付拓展单
*

View File

@@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.pay.service.transfer;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO;
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferPageReqVO;
@@ -54,4 +55,13 @@ public interface PayTransferService {
* @return 同步到状态的转账数量,包括转账成功、转账失败、转账中的
*/
int syncTransfer();
/**
* 渠道的转账通知
*
* @param channelId 渠道编号
* @param notify 通知
*/
void notifyTransfer(Long channelId, PayTransferRespDTO notify);
}

View File

@@ -21,6 +21,7 @@ import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferMapper;
import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
import cn.iocoder.yudao.module.pay.enums.notify.PayNotifyTypeEnum;
import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum;
import cn.iocoder.yudao.module.pay.framework.pay.config.PayProperties;
import cn.iocoder.yudao.module.pay.service.app.PayAppService;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.pay.service.notify.PayNotifyService;
@@ -50,6 +51,9 @@ public class PayTransferServiceImpl implements PayTransferService {
private static final String TRANSFER_NO_PREFIX = "T";
@Resource
private PayProperties payProperties;
@Resource
private PayTransferMapper transferMapper;
@Resource
@@ -96,13 +100,15 @@ public class PayTransferServiceImpl implements PayTransferService {
transfer = INSTANCE.convert(reqDTO)
.setChannelId(channel.getId())
.setNo(no).setStatus(WAITING.getStatus())
.setNotifyUrl(payApp.getTransferNotifyUrl());
.setNotifyUrl(payApp.getTransferNotifyUrl())
.setAppId(channel.getAppId());
transferMapper.insert(transfer);
}
try {
// 3. 调用三方渠道发起转账
PayTransferUnifiedReqDTO transferUnifiedReq = INSTANCE.convert2(transfer)
.setOutTransferNo(transfer.getNo());
transferUnifiedReq.setNotifyUrl(genChannelTransferNotifyUrl(channel));
PayTransferRespDTO unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq);
// 4. 通知转账结果
getSelf().notifyTransfer(channel, unifiedTransferResp);
@@ -116,6 +122,16 @@ public class PayTransferServiceImpl implements PayTransferService {
return transfer.getId();
}
/**
* 根据支付渠道的编码,生成支付渠道的回调地址
*
* @param channel 支付渠道
* @return 支付渠道的回调地址 配置地址 + "/" + channel id
*/
private String genChannelTransferNotifyUrl(PayChannelDO channel) {
return payProperties.getTransferNotifyUrl() + "/" + channel.getId();
}
private PayTransferDO validateTransferCanCreate(PayTransferCreateReqDTO dto, Long appId) {
PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantTransferId(appId, dto.getMerchantTransferId());
if (transfer != null) {
@@ -154,7 +170,7 @@ public class PayTransferServiceImpl implements PayTransferService {
private void notifyTransferInProgress(PayChannelDO channel, PayTransferRespDTO notify) {
// 1.校验
PayTransferDO transfer = transferMapper.selectByNo(notify.getOutTransferNo());
PayTransferDO transfer = transferMapper.selectByAppIdAndNo(channel.getAppId(), notify.getOutTransferNo());
if (transfer == null) {
throw exception(PAY_TRANSFER_NOT_FOUND);
}
@@ -172,16 +188,12 @@ public class PayTransferServiceImpl implements PayTransferService {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING);
}
log.info("[notifyTransferInProgress][transfer({}) 更新为转账进行中状态]", transfer.getId());
// 3. 插入转账通知记录
notifyService.createPayNotifyTask(PayNotifyTypeEnum.TRANSFER.getType(),
transfer.getId());
}
private void notifyTransferSuccess(PayChannelDO channel, PayTransferRespDTO notify) {
// 1.校验
PayTransferDO transfer = transferMapper.selectByNo(notify.getOutTransferNo());
PayTransferDO transfer = transferMapper.selectByAppIdAndNo(channel.getAppId(), notify.getOutTransferNo());
if (transfer == null) {
throw exception(PAY_TRANSFER_NOT_FOUND);
}
@@ -210,7 +222,7 @@ public class PayTransferServiceImpl implements PayTransferService {
private void notifyTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) {
// 1.校验
PayTransferDO transfer = transferMapper.selectByNo(notify.getOutTransferNo());
PayTransferDO transfer = transferMapper.selectByAppIdAndNo(channel.getAppId(), notify.getOutTransferNo());
if (transfer == null) {
throw exception(PAY_TRANSFER_NOT_FOUND);
}
@@ -283,7 +295,7 @@ public class PayTransferServiceImpl implements PayTransferService {
}
}
private void notifyTransfer(Long channelId, PayTransferRespDTO notify) {
public void notifyTransfer(Long channelId, PayTransferRespDTO notify) {
// 校验渠道是否有效
PayChannelDO channel = channelService.validPayChannel(channelId);
// 通知转账结果给对应的业务

View File

@@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.pay.service.wallet;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
@@ -113,16 +114,28 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
@Override
@Transactional(rollbackFor = Exception.class)
public void updateWalletRechargerPaid(Long id, Long payOrderId) {
// 1.1 获取钱包充值记录
PayWalletRechargeDO walletRecharge = walletRechargeMapper.selectById(id);
if (walletRecharge == null) {
log.error("[updateWalletRechargerPaid][钱包充值记录不存在,钱包充值记录 id({})]", id);
// 1.1 校验钱包充值是否存在
PayWalletRechargeDO recharge = walletRechargeMapper.selectById(id);
if (recharge == null) {
log.error("[updateWalletRechargerPaid][recharge({}) payOrder({}) 不存在充值订单,请进行处理!]", id, payOrderId);
throw exception(WALLET_RECHARGE_NOT_FOUND);
}
// 1.2 校验钱包充值是否可以支付
PayOrderDO payOrderDO = validateWalletRechargerCanPaid(walletRecharge, payOrderId);
if (recharge.getPayStatus()) {
// 特殊:如果订单已支付,且支付单号相同,直接返回,说明重复回调
if (ObjectUtil.equals(recharge.getPayOrderId(), payOrderId)) {
log.warn("[updateWalletRechargerPaid][recharge({}) 已支付,且支付单号相同({}),直接返回]", recharge, payOrderId);
return;
}
// 异常:支付单号不同,说明支付单号错误
log.error("[updateWalletRechargerPaid][recharge({}) 已支付,但是支付单号不同({}),请进行处理!]", recharge, payOrderId);
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
// 2. 更新钱包充值的支付状态
// 2. 校验支付订单的合法性
PayOrderDO payOrderDO = validatePayOrderPaid(recharge, payOrderId);
// 3. 更新钱包充值的支付状态
int updateCount = walletRechargeMapper.updateByIdAndPaid(id, false,
new PayWalletRechargeDO().setId(id).setPayStatus(true).setPayTime(LocalDateTime.now())
.setPayChannelCode(payOrderDO.getChannelCode()));
@@ -130,14 +143,14 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID);
}
// 3. 更新钱包余额
// 4. 更新钱包余额
// TODO @jason这样的话未来提现会不会把充值的也提现走哈。类似先充 100送 110然后提现 110
// TODO 需要钱包中加个可提现余额
payWalletService.addWalletBalance(walletRecharge.getWalletId(), String.valueOf(id),
PayWalletBizTypeEnum.RECHARGE, walletRecharge.getTotalPrice());
payWalletService.addWalletBalance(recharge.getWalletId(), String.valueOf(id),
PayWalletBizTypeEnum.RECHARGE, recharge.getTotalPrice());
// 4. 发送订阅消息
getSelf().sendWalletRechargerPaidMessage(payOrderId, walletRecharge);
// 5. 发送订阅消息
getSelf().sendWalletRechargerPaidMessage(payOrderId, recharge);
}
@Async
@@ -180,7 +193,6 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
.setReason("想退钱").setPrice(walletRecharge.getPayPrice()));
// 4. 更新充值记录退款单号
// TODO @jaosn一般新建这种 update 对象,建议是,第一个 set id 属性,容易知道以它为更新
walletRechargeMapper.updateById(new PayWalletRechargeDO().setPayRefundId(payRefundId)
.setRefundStatus(WAITING.getStatus()).setId(walletRecharge.getId()));
}
@@ -267,43 +279,38 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
return wallet;
}
private PayOrderDO validateWalletRechargerCanPaid(PayWalletRechargeDO walletRecharge, Long payOrderId) {
// 1.1 校验充值记录的支付状态
if (walletRecharge.getPayStatus()) {
log.error("[validateWalletRechargerCanPaid][钱包({}) 不处于未支付状态! 钱包数据是:{}]",
walletRecharge.getId(), toJsonString(walletRecharge));
throw exception(WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID);
}
// 1.2 校验支付订单匹配
if (notEqual(walletRecharge.getPayOrderId(), payOrderId)) { // 支付单号
log.error("[validateWalletRechargerCanPaid][钱包({}) 支付单不匹配({}),请进行处理! 钱包数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
// 2.1 校验支付单是否存在
/**
* 校验支付订单的合法性
*
* @param recharge 充值订单
* @param payOrderId 支付订单编号
* @return 支付订单
*/
private PayOrderDO validatePayOrderPaid(PayWalletRechargeDO recharge, Long payOrderId) {
// 1. 校验支付单是否存在
PayOrderDO payOrder = payOrderService.getOrder(payOrderId);
if (payOrder == null) {
log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 不存在,请进行处理!]",
walletRecharge.getId(), payOrderId);
log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 不存在,请进行处理!]",
recharge.getId(), payOrderId);
throw exception(PAY_ORDER_NOT_FOUND);
}
// 2.2 校验支付单已支付
// 2.1 校验支付单已支付
if (!PayOrderStatusEnum.isSuccess(payOrder.getStatus())) {
log.error("[validateWalletRechargerCanPaid][钱包({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(payOrder));
log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 未支付请进行处理payOrder 数据是:{}]",
recharge.getId(), payOrderId, toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_STATUS_NOT_SUCCESS);
}
// 2.3 校验支付金额一致
if (notEqual(payOrder.getPrice(), walletRecharge.getPayPrice())) {
log.error("[validateDemoOrderCanPaid][钱包({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{}payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(walletRecharge), toJsonString(payOrder));
// 2.2 校验支付金额一致
if (notEqual(payOrder.getPrice(), recharge.getPayPrice())) {
log.error("[validatePayOrderPaid][充值订单({}) payOrder({}) 支付金额不匹配,请进行处理!钱包 数据是:{}payOrder 数据是:{}]",
recharge.getId(), payOrderId, toJsonString(recharge), toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_PRICE_NOT_MATCH);
}
// 2.4 校验支付订单的商户订单匹配
if (notEqual(payOrder.getMerchantOrderId(), walletRecharge.getId().toString())) {
log.error("[validateDemoOrderCanPaid][钱包({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
walletRecharge.getId(), payOrderId, toJsonString(payOrder));
// 2.3 校验支付订单的商户订单匹配
if (notEqual(payOrder.getMerchantOrderId(), recharge.getId().toString())) {
log.error("[validatePayOrderPaid][充值订单({}) 支付单不匹配({})请进行处理payOrder 数据是:{}]",
recharge.getId(), payOrderId, toJsonString(payOrder));
throw exception(WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR);
}
return payOrder;

View File

@@ -2,17 +2,20 @@ package cn.iocoder.yudao.module.pay.service.wallet;
import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.wallet.PayWalletPageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderExtensionDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
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.wallet.PayWalletLockRedisDAO;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
import cn.iocoder.yudao.module.pay.service.wallet.bo.WalletTransactionCreateReqBO;
import jakarta.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
@@ -32,10 +35,17 @@ import static cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum.PAYM
*/
@Service
@Slf4j
public class PayWalletServiceImpl implements PayWalletService {
public class PayWalletServiceImpl implements PayWalletService {
/**
* 通知超时时间,单位:毫秒
*/
public static final long UPDATE_TIMEOUT_MILLIS = 120 * DateUtils.SECOND_MILLIS;
@Resource
private PayWalletMapper walletMapper;
@Resource
private PayWalletLockRedisDAO lockRedisDAO;
@Resource
@Lazy // 延迟加载,避免循环依赖
@@ -121,75 +131,87 @@ public class PayWalletServiceImpl implements PayWalletService {
}
@Override
@Transactional(rollbackFor = Exception.class)
@SneakyThrows
public PayWalletTransactionDO reduceWalletBalance(Long walletId, Long bizId,
PayWalletBizTypeEnum bizType, Integer price) {
// 1. 获取钱包
PayWalletDO payWallet = getWallet(walletId);
if (payWallet == null) {
log.error("[reduceWalletBalance]用户钱包({})不存在.", walletId);
log.error("[reduceWalletBalance][用户钱包({})不存在]", walletId);
throw exception(WALLET_NOT_FOUND);
}
// 2.1 扣除余额
int updateCounts;
switch (bizType) {
case PAYMENT: {
updateCounts = walletMapper.updateWhenConsumption(payWallet.getId(), price);
break;
// 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯)
return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> {
// 2. 扣除余额
int updateCounts;
switch (bizType) {
case PAYMENT: {
updateCounts = walletMapper.updateWhenConsumption(payWallet.getId(), price);
break;
}
case RECHARGE_REFUND: {
updateCounts = walletMapper.updateWhenRechargeRefund(payWallet.getId(), price);
break;
}
default: {
// TODO 其它类型待实现
throw new UnsupportedOperationException("待实现");
}
}
case RECHARGE_REFUND: {
updateCounts = walletMapper.updateWhenRechargeRefund(payWallet.getId(), price);
break;
if (updateCounts == 0) {
throw exception(WALLET_BALANCE_NOT_ENOUGH);
}
default: {
// TODO 其它类型待实现
throw new UnsupportedOperationException("待实现");
}
}
if (updateCounts == 0) {
throw exception(WALLET_BALANCE_NOT_ENOUGH);
}
// 2.2 生成钱包流水
Integer afterBalance = payWallet.getBalance() - price;
WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId())
.setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId))
.setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(bo);
// 3. 生成钱包流水
Integer afterBalance = payWallet.getBalance() - price;
WalletTransactionCreateReqBO bo = new WalletTransactionCreateReqBO().setWalletId(payWallet.getId())
.setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId))
.setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(bo);
});
}
@Override
@Transactional(rollbackFor = Exception.class)
@SneakyThrows
public PayWalletTransactionDO addWalletBalance(Long walletId, String bizId,
PayWalletBizTypeEnum bizType, Integer price) {
// 1.1 获取钱包
// 1. 获取钱包
PayWalletDO payWallet = getWallet(walletId);
if (payWallet == null) {
log.error("[addWalletBalance]用户钱包({})不存在.", walletId);
log.error("[addWalletBalance][用户钱包({})不存在]", walletId);
throw exception(WALLET_NOT_FOUND);
}
// 1.2 更新钱包金额
switch (bizType) {
case PAYMENT_REFUND: { // 退款更新
walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price);
break;
}
case RECHARGE: { // 充值更新
walletMapper.updateWhenRecharge(payWallet.getId(), price);
break;
}
case UPDATE_BALANCE: // 更新余额
walletMapper.updateWhenRecharge(payWallet.getId(), price);
break;
default: {
// TODO 其它类型待实现
throw new UnsupportedOperationException("待实现");
}
}
// 2. 生成钱包流水
WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO()
.setWalletId(payWallet.getId()).setPrice(price).setBalance(payWallet.getBalance() + price)
.setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(transactionCreateReqBO);
// 2. 加锁,更新钱包余额(目的:避免钱包流水的并发更新时,余额变化不连贯)
return lockRedisDAO.lock(walletId, UPDATE_TIMEOUT_MILLIS, () -> {
// 3. 更新钱包金额
switch (bizType) {
case PAYMENT_REFUND: { // 退款更新
walletMapper.updateWhenConsumptionRefund(payWallet.getId(), price);
break;
}
case RECHARGE: { // 充值更新
walletMapper.updateWhenRecharge(payWallet.getId(), price);
break;
}
case UPDATE_BALANCE: // 更新余额
case BROKERAGE_WITHDRAW: // 分佣提现
walletMapper.updateWhenAdd(payWallet.getId(), price);
break;
default: {
throw new UnsupportedOperationException("待实现:" + bizType);
}
}
// 4. 生成钱包流水
WalletTransactionCreateReqBO transactionCreateReqBO = new WalletTransactionCreateReqBO()
.setWalletId(payWallet.getId()).setPrice(price).setBalance(payWallet.getBalance() + price)
.setBizId(bizId).setBizType(bizType.getType()).setTitle(bizType.getDescription());
return walletTransactionService.createWalletTransaction(transactionCreateReqBO);
});
}
@Override

View File

@@ -87,8 +87,6 @@ public class PayWalletTransactionServiceImpl implements PayWalletTransactionServ
@Override
public AppPayWalletTransactionSummaryRespVO getWalletTransactionSummary(Long userId, Integer userType, LocalDateTime[] createTime) {
PayWalletDO wallet = payWalletService.getOrCreateWallet(userId, userType);
AppPayWalletTransactionSummaryRespVO summary = new AppPayWalletTransactionSummaryRespVO()
.setTotalExpense(1).setTotalIncome(100);
return new AppPayWalletTransactionSummaryRespVO()
.setTotalExpense(payWalletTransactionMapper.selectPriceSum(wallet.getId(), TYPE_EXPENSE, createTime))
.setTotalIncome(payWalletTransactionMapper.selectPriceSum(wallet.getId(), TYPE_INCOME, createTime));