|
|
@@ -3,22 +3,24 @@ package cn.iocoder.yudao.module.pay.service.demo;
|
|
|
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.framework.common.util.json.JsonUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
import cn.iocoder.yudao.module.pay.api.transfer.PayTransferApi;
|
|
|
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
|
|
|
-import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
|
|
|
-import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
|
|
|
-import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
|
|
|
-import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoTransferMapper;
|
|
|
+import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferRespDTO;
|
|
|
+import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.withdraw.PayDemoWithdrawCreateReqVO;
|
|
|
+import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoWithdrawDO;
|
|
|
+import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoWithdrawMapper;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.PayChannelEnum;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.demo.PayDemoWithdrawStatusEnum;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.demo.PayDemoWithdrawTypeEnum;
|
|
|
import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum;
|
|
|
-import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
|
|
|
import jakarta.annotation.Resource;
|
|
|
import jakarta.validation.Valid;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
-import java.util.Objects;
|
|
|
-
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
|
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
|
|
|
@@ -30,7 +32,8 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
|
|
|
*/
|
|
|
@Service
|
|
|
@Validated
|
|
|
-public class PayDemoTransferServiceImpl implements PayDemoTransferService {
|
|
|
+@Slf4j
|
|
|
+public class PayDemoTransferServiceImpl implements PayDemoWithdrawService {
|
|
|
|
|
|
/**
|
|
|
* 接入的支付应用标识
|
|
|
@@ -40,75 +43,113 @@ public class PayDemoTransferServiceImpl implements PayDemoTransferService {
|
|
|
private static final String PAY_APP_KEY = "demo";
|
|
|
|
|
|
@Resource
|
|
|
- private PayDemoTransferMapper demoTransferMapper;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private PayTransferService payTransferService;
|
|
|
+ private PayDemoWithdrawMapper demoTransferMapper;
|
|
|
|
|
|
@Resource
|
|
|
private PayTransferApi payTransferApi;
|
|
|
|
|
|
@Override
|
|
|
- public Long createDemoTransfer(@Valid PayDemoTransferCreateReqVO reqVO) {
|
|
|
- // 1. 保存示例转账业务表
|
|
|
- PayDemoTransferDO demoTransfer = BeanUtils.toBean(reqVO, PayDemoTransferDO.class)
|
|
|
- .setTransferStatus(PayTransferStatusEnum.WAITING.getStatus());
|
|
|
- demoTransferMapper.insert(demoTransfer);
|
|
|
+ public Long createDemoWithdraw(@Valid PayDemoWithdrawCreateReqVO reqVO) {
|
|
|
+ // 1. 保存示例提现单
|
|
|
+ PayDemoWithdrawDO withdraw = BeanUtils.toBean(reqVO, PayDemoWithdrawDO.class)
|
|
|
+ .setTransferChannelCode(getTransferChannelCode(reqVO.getType()))
|
|
|
+ .setStatus(PayTransferStatusEnum.WAITING.getStatus());
|
|
|
+ demoTransferMapper.insert(withdraw);
|
|
|
|
|
|
// 2.1 创建支付单
|
|
|
Long payTransferId = payTransferApi.createTransfer(new PayTransferCreateReqDTO()
|
|
|
- .setChannelCode(reqVO.getChannelCode())
|
|
|
- .setAppKey(PAY_APP_KEY).setUserIp(getClientIP()) // 支付应用
|
|
|
- .setMerchantTransferId(String.valueOf(demoTransfer.getId())) // 业务的订单编号
|
|
|
- .setSubject(reqVO.getSubject()).setPrice(demoTransfer.getPrice()) // 价格信息
|
|
|
+ .setAppKey(PAY_APP_KEY).setChannelCode(withdraw.getTransferChannelCode()).setUserIp(getClientIP()) // 支付应用
|
|
|
+ .setMerchantOrderId(String.valueOf(withdraw.getId())) // 业务的订单编号
|
|
|
+ .setSubject(reqVO.getSubject()).setPrice(withdraw.getPrice()) // 价格信息
|
|
|
.setUserAccount(reqVO.getUserAccount()).setUserName(reqVO.getUserName())); // 收款信息
|
|
|
- // 2.2 更新转账单到 demo 示例转账业务表
|
|
|
- demoTransferMapper.updateById(new PayDemoTransferDO().setId(demoTransfer.getId())
|
|
|
+ // 2.2 更新转账单到 demo 示例提现单
|
|
|
+ demoTransferMapper.updateById(new PayDemoWithdrawDO().setId(withdraw.getId())
|
|
|
.setPayTransferId(payTransferId));
|
|
|
- return demoTransfer.getId();
|
|
|
+ return withdraw.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getTransferChannelCode(Integer type) {
|
|
|
+ if (ObjectUtil.equal(type, PayDemoWithdrawTypeEnum.ALIPAY.getType())) {
|
|
|
+ return PayChannelEnum.ALIPAY_PC.getCode();
|
|
|
+ }
|
|
|
+ if (ObjectUtil.equal(type, PayDemoWithdrawTypeEnum.WECHAT.getType())) {
|
|
|
+ return PayChannelEnum.WX_LITE.getCode();
|
|
|
+ }
|
|
|
+ if (ObjectUtil.equal(type, PayDemoWithdrawTypeEnum.WALLET.getType())) {
|
|
|
+ return PayChannelEnum.WALLET.getCode();
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException("未知提现方式:" + type);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public PageResult<PayDemoTransferDO> getDemoTransferPage(PageParam pageVO) {
|
|
|
+ public PageResult<PayDemoWithdrawDO> getDemoWithdrawPage(PageParam pageVO) {
|
|
|
return demoTransferMapper.selectPage(pageVO);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateDemoTransferStatus(Long id, Long payTransferId) {
|
|
|
- PayTransferDO payTransfer = validateDemoTransferStatusCanUpdate(id, payTransferId);
|
|
|
- // TODO @芋艿:这块,需要在优化下;
|
|
|
- // 更新示例订单状态
|
|
|
- if (payTransfer != null) {
|
|
|
- demoTransferMapper.updateById(new PayDemoTransferDO().setId(id)
|
|
|
- .setPayTransferId(payTransferId)
|
|
|
- .setTransferStatus(payTransfer.getStatus())
|
|
|
- .setTransferTime(payTransfer.getSuccessTime()));
|
|
|
+ public void updateDemoWithdrawStatus(Long id, Long payTransferId) {
|
|
|
+ // 1.1 校验转账单是否存在
|
|
|
+ PayDemoWithdrawDO withdraw = demoTransferMapper.selectById(id);
|
|
|
+ if (withdraw == null) {
|
|
|
+ log.error("[updateDemoWithdrawStatus][withdraw({}) payOrder({}) 不存在提现单,请进行处理!]", id, payTransferId);
|
|
|
+ throw exception(DEMO_WITHDRAW_NOT_FOUND);
|
|
|
+ }
|
|
|
+ // 1.2 校验转账单已成结束(成功或失败)
|
|
|
+ if (PayDemoWithdrawStatusEnum.isSuccess(withdraw.getStatus())
|
|
|
+ || PayDemoWithdrawStatusEnum.isClosed(withdraw.getStatus())) {
|
|
|
+ // 特殊:转账单编号相同,直接返回,说明重复回调
|
|
|
+ if (ObjectUtil.equal(withdraw.getPayTransferId(), payTransferId)) {
|
|
|
+ log.warn("[updateDemoWithdrawStatus][withdraw({}) 已结束,且转账单编号相同({}),直接返回]", withdraw, payTransferId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 异常:转账单编号不同,说明转账单编号错误
|
|
|
+ log.error("[updateDemoWithdrawStatus][withdraw({}) 转账单不匹配({}),请进行处理!]", withdraw, payTransferId);
|
|
|
+ throw exception(DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_TRANSFER_ID_ERROR);
|
|
|
}
|
|
|
+
|
|
|
+ // 2. 校验提现单的合法性
|
|
|
+ PayTransferRespDTO payTransfer = validateDemoTransferStatusCanUpdate(withdraw, payTransferId);
|
|
|
+
|
|
|
+ // 3. 更新示例订单状态
|
|
|
+ demoTransferMapper.updateById(new PayDemoWithdrawDO().setId(id)
|
|
|
+ .setPayTransferId(payTransferId)
|
|
|
+ .setStatus(payTransfer.getStatus())
|
|
|
+ .setTransferTime(payTransfer.getSuccessTime()));
|
|
|
}
|
|
|
|
|
|
- private PayTransferDO validateDemoTransferStatusCanUpdate(Long id, Long payTransferId) {
|
|
|
- PayDemoTransferDO demoTransfer = demoTransferMapper.selectById(id);
|
|
|
- if (demoTransfer == null) {
|
|
|
- throw exception(DEMO_TRANSFER_NOT_FOUND);
|
|
|
+ private PayTransferRespDTO validateDemoTransferStatusCanUpdate(PayDemoWithdrawDO withdraw, Long payTransferId) {
|
|
|
+ // 1. 校验转账单是否存在
|
|
|
+ PayTransferRespDTO payTransfer = payTransferApi.getTransfer(payTransferId);
|
|
|
+ if (payTransfer == null) {
|
|
|
+ log.error("[validateDemoTransferStatusCanUpdate][withdraw({}) payTransfer({}) 不存在,请进行处理!]", withdraw.getId(), payTransferId);
|
|
|
+ throw exception(PAY_TRANSFER_NOT_FOUND);
|
|
|
}
|
|
|
- // TODO @芋艿:这里也要更新下;
|
|
|
- // 无需更新返回 null
|
|
|
- if (PayTransferStatusEnum.isSuccess(demoTransfer.getTransferStatus())
|
|
|
- || PayTransferStatusEnum.isClosed(demoTransfer.getTransferStatus())) {
|
|
|
- return null;
|
|
|
+
|
|
|
+ // 2.1 校验转账单已成功
|
|
|
+ if (!PayTransferStatusEnum.isSuccessOrClosed(payTransfer.getStatus())) {
|
|
|
+ log.error("[validateDemoTransferStatusCanUpdate][withdraw({}) payTransfer({}) 未结束,请进行处理!payTransfer 数据是:{}]",
|
|
|
+ withdraw.getId(), payTransferId, JsonUtils.toJsonString(payTransfer));
|
|
|
+ throw exception(DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_TRANSFER_STATUS_NOT_SUCCESS_OR_CLOSED);
|
|
|
}
|
|
|
- PayTransferDO transfer = payTransferService.getTransfer(payTransferId);
|
|
|
- if (transfer == null) {
|
|
|
- throw exception(PAY_TRANSFER_NOT_FOUND);
|
|
|
+ // 2.2 校验转账金额一致
|
|
|
+ if (ObjectUtil.notEqual(payTransfer.getPrice(), withdraw.getPrice())) {
|
|
|
+ log.error("[validateDemoTransferStatusCanUpdate][withdraw({}) payTransfer({}) 转账金额不匹配,请进行处理!withdraw 数据是:{},payTransfer 数据是:{}]",
|
|
|
+ withdraw.getId(), payTransferId, JsonUtils.toJsonString(withdraw), JsonUtils.toJsonString(payTransfer));
|
|
|
+ throw exception(DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_PRICE_NOT_MATCH);
|
|
|
}
|
|
|
- if (!Objects.equals(demoTransfer.getPrice(), transfer.getPrice())) {
|
|
|
- throw exception(DEMO_TRANSFER_FAIL_PRICE_NOT_MATCH);
|
|
|
+ // 2.3 校验转账订单匹配(二次)
|
|
|
+ if (ObjectUtil.notEqual(payTransfer.getMerchantOrderId(), withdraw.getId().toString())) {
|
|
|
+ log.error("[validateDemoTransferStatusCanUpdate][withdraw({}) 转账单不匹配({}),请进行处理!payTransfer 数据是:{}]",
|
|
|
+ withdraw.getId(), payTransferId, JsonUtils.toJsonString(payTransfer));
|
|
|
+ throw exception(DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_MERCHANT_EXISTS);
|
|
|
}
|
|
|
- if (ObjectUtil.notEqual(transfer.getMerchantTransferId(), id.toString())) {
|
|
|
- throw exception(DEMO_TRANSFER_FAIL_TRANSFER_ID_ERROR);
|
|
|
+ // 2.4 校验转账渠道一致
|
|
|
+ if (ObjectUtil.notEqual(payTransfer.getChannelCode(), withdraw.getTransferChannelCode())) {
|
|
|
+ log.error("[validateDemoTransferStatusCanUpdate][withdraw({}) payTransfer({}) 转账渠道不匹配,请进行处理!withdraw 数据是:{},payTransfer 数据是:{}]",
|
|
|
+ withdraw.getId(), payTransferId, JsonUtils.toJsonString(withdraw), JsonUtils.toJsonString(payTransfer));
|
|
|
+ throw exception(DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_CHANNEL_NOT_MATCH);
|
|
|
}
|
|
|
- // TODO 校验账号
|
|
|
- return transfer;
|
|
|
+ return payTransfer;
|
|
|
}
|
|
|
|
|
|
}
|