feat:【PAY 支付】提现示例,拆分 create 和 transfer 两个状态,更符合实际场景

This commit is contained in:
YunaiV 2025-05-09 22:03:01 +08:00
parent 38c76806a3
commit d2b668a676
11 changed files with 128 additions and 40 deletions

View File

@ -52,5 +52,15 @@ public class PayTransferRespDTO {
*/
private LocalDateTime successTime;
// ========== 其它字段 ==========
/**
* 调用渠道的错误码
*/
private String channelErrorCode;
/**
* 调用渠道的错误提示
*/
private String channelErrorMsg;
}

View File

@ -68,9 +68,9 @@ public interface ErrorCodeConstants {
ErrorCode PAY_TRANSFER_NOT_FOUND = new ErrorCode(1_007_009_001, "转账单不存在");
ErrorCode PAY_TRANSFER_CREATE_CHANNEL_NOT_MATCH = new ErrorCode(1_007_009_002, "转账发起失败,原因:两次相同转账请求的类型不匹配");
ErrorCode PAY_TRANSFER_CREATE_PRICE_NOT_MATCH = new ErrorCode(1_007_009_003, "转账发起失败,原因:两次相同转账请求的金额不匹配");
ErrorCode PAY_TRANSFER_CREATE_MERCHANT_EXISTS = new ErrorCode(1_007_009_004, "转账发起失败,原因:该笔业务的转账已经发起,请查询转账订单相关状态");
ErrorCode PAY_TRANSFER_STATUS_IS_NOT_WAITING = new ErrorCode(1_007_009_005, "转账单不处于待转账");
ErrorCode PAY_TRANSFER_STATUS_IS_NOT_WAITING_OR_PROCESSING = new ErrorCode(1_007_009_006, "转账单不处于待转账或转账中");
ErrorCode PAY_TRANSFER_CREATE_FAIL_STATUS_NOT_CLOSED = new ErrorCode(1_007_009_004, "转账发起失败,原因:已经存在相同的转账单,且状态不是已关闭");
ErrorCode PAY_TRANSFER_NOTIFY_FAIL_STATUS_IS_NOT_WAITING = new ErrorCode(1_007_009_006, "通知转账结果失败,原因:转账单不处于待转账");
ErrorCode PAY_TRANSFER_NOTIFY_FAIL_STATUS_NOT_WAITING_OR_PROCESSING = new ErrorCode(1_007_009_007, "通知转账结果失败,原因:转账单不处于待转账或转账中");
// ========== 示例订单 1-007-900-000 ==========
ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1_007_900_000, "示例订单不存在");
@ -92,5 +92,6 @@ public interface ErrorCodeConstants {
ErrorCode DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_PRICE_NOT_MATCH = new ErrorCode(1_007_901_003, "更新示例提现单状态失败,支付转账单金额不匹配");
ErrorCode DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_MERCHANT_EXISTS = new ErrorCode(1_007_901_004, "更新示例提现单状态失败,支付转账单商户订单号不匹配");
ErrorCode DEMO_WITHDRAW_UPDATE_STATUS_FAIL_PAY_CHANNEL_NOT_MATCH = new ErrorCode(1_007_901_005, "更新示例提现单状态失败,支付转账单渠道不匹配");
ErrorCode DEMO_WITHDRAW_TRANSFER_FAIL_STATUS_NOT_WAITING_OR_CLOSED = new ErrorCode(1_007_901_008, "发起转账失败,原因:示例提现单状态不是【等待提现】或【提现关闭】");
}

View File

@ -1,4 +1,4 @@
### 请求 /pay/pay/demo-order 接口(支付宝) => 成功
### 请求 /pay/demo-withdraw/create 接口(支付宝) => 成功
POST {{baseUrl}}/pay/demo-withdraw/create
Authorization: Bearer {{token}}
Content-Type: application/json
@ -12,7 +12,7 @@ tenant-id: {{adminTenantId}}
"userName": "oespxk7368"
}
### 请求 /pay/pay/demo-order 接口(微信余额) => 成功
### 请求 /pay/demo-withdraw/create 接口(微信余额) => 成功
POST {{baseUrl}}/pay/demo-withdraw/create
Authorization: Bearer {{token}}
Content-Type: application/json
@ -26,7 +26,7 @@ tenant-id: {{adminTenantId}}
"userName": "芋艿"
}
### 请求 /pay/pay/demo-order 接口(钱包余额) => 成功
### 请求 /pay/demo-withdraw/create 接口(钱包余额) => 成功
POST {{baseUrl}}/pay/demo-withdraw/create
Authorization: Bearer {{token}}
Content-Type: application/json
@ -37,4 +37,14 @@ tenant-id: {{adminTenantId}}
"subject": "测试转账",
"price": 1,
"userAccount": "1"
}
}
### 请求 /pay/demo-withdraw/transfer 接口(发起转账) => 成功
POST {{baseUrl}}/pay/demo-withdraw/transfer?id=1
Authorization: Bearer {{token}}
tenant-id: {{adminTenantId}}
### 请求 /pay/demo-withdraw/page 接口(查询分页) => 成功
GET {{baseUrl}}/pay/demo-withdraw/page?pageNo=1&pageSize=10
Authorization: Bearer {{token}}
tenant-id: {{adminTenantId}}

View File

@ -10,6 +10,7 @@ import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.withdraw.PayDemoWith
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoWithdrawDO;
import cn.iocoder.yudao.module.pay.service.demo.PayDemoWithdrawService;
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 jakarta.annotation.security.PermitAll;
@ -30,14 +31,22 @@ public class PayDemoWithdrawController {
@PostMapping("/create")
@Operation(summary = "创建示例提现单")
public CommonResult<Long> createDemoTransfer(@Valid @RequestBody PayDemoWithdrawCreateReqVO createReqVO) {
public CommonResult<Long> createDemoWithdraw(@Valid @RequestBody PayDemoWithdrawCreateReqVO createReqVO) {
Long id = demoWithdrawService.createDemoWithdraw(createReqVO);
return success(id);
}
@PostMapping("/transfer")
@Operation(summary = "提现单转账")
@Parameter(name = "id", required = true, description = "提现单编号", example = "1024")
public CommonResult<Long> transferDemoWithdraw(@RequestParam("id") Long id) {
Long payTransferId = demoWithdrawService.transferDemoWithdraw(id);
return success(payTransferId);
}
@GetMapping("/page")
@Operation(summary = "获得示例提现单分页")
public CommonResult<PageResult<PayDemoWithdrawRespVO>> getDemoTransferPage(@Valid PageParam pageVO) {
public CommonResult<PageResult<PayDemoWithdrawRespVO>> getDemoWithdrawPage(@Valid PageParam pageVO) {
PageResult<PayDemoWithdrawDO> pageResult = demoWithdrawService.getDemoWithdrawPage(pageVO);
return success(BeanUtils.toBean(pageResult, PayDemoWithdrawRespVO.class));
}
@ -45,7 +54,7 @@ public class PayDemoWithdrawController {
@PostMapping("/update-status")
@Operation(summary = "更新示例提现单的转账状态") // pay-module 转账服务进行回调
@PermitAll // 无需登录安全由 PayDemoTransferService 内部校验实现
public CommonResult<Boolean> updateDemoTransferStatus(@RequestBody PayTransferNotifyReqDTO notifyReqDTO) {
public CommonResult<Boolean> updateDemoWithdrawStatus(@RequestBody PayTransferNotifyReqDTO notifyReqDTO) {
demoWithdrawService.updateDemoWithdrawStatus(Long.valueOf(notifyReqDTO.getMerchantOrderId()),
notifyReqDTO.getPayTransferId());
return success(true);

View File

@ -1,12 +1,10 @@
package cn.iocoder.yudao.module.pay.controller.admin.demo.vo.withdraw;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.pay.enums.demo.PayDemoWithdrawTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.*;
import lombok.Data;
@Schema(description = "管理后台 - 示例提现单创建 Request VO")
@ -34,4 +32,11 @@ public class PayDemoWithdrawCreateReqVO {
@InEnum(PayDemoWithdrawTypeEnum.class)
private Integer type;
@AssertTrue(message = "收款人姓名")
public boolean isUserNameValid() {
// 特殊支付宝必须填写用户名
return ObjectUtil.notEqual(type, PayDemoWithdrawTypeEnum.ALIPAY.getType())
|| ObjectUtil.isNotEmpty(userName);
}
}

View File

@ -35,10 +35,13 @@ public class PayDemoWithdrawRespVO {
@Schema(description = "转账单编号", example = "23695")
private Long payTransferId;
@Schema(description = "转账渠道")
@Schema(description = "转账渠道", example = "wx_lite")
private String transferChannelCode;
@Schema(description = "转账成功时间")
private LocalDateTime transferTime;
@Schema(description = "转账失败原因", example = "IP 不正确")
private String transferErrorMsg;
}

View File

@ -76,5 +76,9 @@ public class PayDemoWithdrawDO extends BaseDO {
* 转账成功时间
*/
private LocalDateTime transferTime;
/**
* 转账错误提示
*/
private String transferErrorMsg;
}

View File

@ -15,10 +15,10 @@ public interface PayDemoWithdrawMapper extends BaseMapperX<PayDemoWithdrawDO> {
.orderByDesc(PayDemoWithdrawDO::getId));
}
default int updateByIdAndStatus(Long id, Integer status, PayDemoWithdrawDO updateObj) {
default int updateByIdAndStatus(Long id, Integer whereStatus, PayDemoWithdrawDO updateObj) {
return update(updateObj, new LambdaQueryWrapperX<PayDemoWithdrawDO>()
.eq(PayDemoWithdrawDO::getId, id)
.eq(PayDemoWithdrawDO::getStatus, status));
.eq(PayDemoWithdrawDO::getStatus, whereStatus));
}
}

View File

@ -1,5 +1,6 @@
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;
@ -50,27 +51,58 @@ public class PayDemoTransferServiceImpl implements PayDemoWithdrawService {
@Override
public Long createDemoWithdraw(@Valid PayDemoWithdrawCreateReqVO reqVO) {
// 1. 保存示例提现单
PayDemoWithdrawDO withdraw = BeanUtils.toBean(reqVO, PayDemoWithdrawDO.class)
.setTransferChannelCode(getTransferChannelCode(reqVO.getType()))
.setStatus(PayTransferStatusEnum.WAITING.getStatus());
.setStatus(PayDemoWithdrawStatusEnum.WAITING.getStatus());
demoTransferMapper.insert(withdraw);
return withdraw.getId();
}
@Override
public Long transferDemoWithdraw(Long id) {
// 1.1 校验提现单
PayDemoWithdrawDO withdraw = validateDemoWithdrawCanTransfer(id);
// 1.2 特殊如果是转账失败的情况需要充值下
if (PayDemoWithdrawStatusEnum.isClosed(withdraw.getStatus())) {
int updateCount = demoTransferMapper.updateByIdAndStatus(withdraw.getId(), withdraw.getStatus(),
new PayDemoWithdrawDO().setStatus(PayDemoWithdrawStatusEnum.WAITING.getStatus()).setTransferErrorMsg(""));
if (updateCount == 0) {
throw exception(DEMO_WITHDRAW_TRANSFER_FAIL_STATUS_NOT_WAITING_OR_CLOSED);
}
withdraw.setStatus(PayDemoWithdrawStatusEnum.WAITING.getStatus());
}
// 2.1 创建支付单
PayTransferCreateReqDTO transferReqDTO = new PayTransferCreateReqDTO()
.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()); // 收款信息
if (ObjectUtil.equal(reqVO.getType(), PayDemoWithdrawTypeEnum.WECHAT.getType())) {
.setSubject(withdraw.getSubject()).setPrice(withdraw.getPrice()) // 价格信息
.setUserAccount(withdraw.getUserAccount()).setUserName(withdraw.getUserName()); // 收款信息
if (ObjectUtil.equal(withdraw.getType(), PayDemoWithdrawTypeEnum.WECHAT.getType())) {
// 注意微信很特殊提现需要写明用途
transferReqDTO.setChannelExtras(PayTransferCreateReqDTO.buildWeiXinChannelExtra1000(
"测试活动", "测试奖励"));
}
Long payTransferId = payTransferApi.createTransfer(transferReqDTO);
// 2.2 更新转账单到 demo 示例提现单
demoTransferMapper.updateById(new PayDemoWithdrawDO().setId(withdraw.getId())
.setPayTransferId(payTransferId));
return withdraw.getId();
// 2.2 更新转账单到 demo 示例提现单并将状态更新为转账中
demoTransferMapper.updateByIdAndStatus(withdraw.getId(), withdraw.getStatus(),
new PayDemoWithdrawDO().setPayTransferId(payTransferId));
return payTransferId;
}
private PayDemoWithdrawDO validateDemoWithdrawCanTransfer(Long id) {
// 校验存在
PayDemoWithdrawDO withdraw = demoTransferMapper.selectById(id);
if (withdraw == null) {
throw exception(DEMO_WITHDRAW_NOT_FOUND);
}
// 校验状态只有等待中或转账失败的订单才能发起转账
if (!PayDemoWithdrawStatusEnum.isWaiting(withdraw.getStatus())
&& !PayDemoWithdrawStatusEnum.isClosed(withdraw.getStatus())) {
throw exception(DEMO_WITHDRAW_TRANSFER_FAIL_STATUS_NOT_WAITING_OR_CLOSED);
}
return withdraw;
}
private String getTransferChannelCode(Integer type) {
@ -116,10 +148,12 @@ public class PayDemoTransferServiceImpl implements PayDemoWithdrawService {
PayTransferRespDTO payTransfer = validateDemoTransferStatusCanUpdate(withdraw, payTransferId);
// 3. 更新示例订单状态
demoTransferMapper.updateById(new PayDemoWithdrawDO().setId(id)
.setPayTransferId(payTransferId)
.setStatus(payTransfer.getStatus())
.setTransferTime(payTransfer.getSuccessTime()));
Integer newStatus = PayTransferStatusEnum.isSuccess(payTransfer.getStatus()) ? PayDemoWithdrawStatusEnum.SUCCESS.getStatus() :
PayTransferStatusEnum.isClosed(payTransfer.getStatus()) ? PayDemoWithdrawStatusEnum.CLOSED.getStatus() : null;
Assert.notNull(newStatus, "转账单状态({}) 不合法", payTransfer.getStatus());
demoTransferMapper.updateByIdAndStatus(withdraw.getId(), withdraw.getStatus(),
new PayDemoWithdrawDO().setStatus(newStatus).setTransferTime(payTransfer.getSuccessTime())
.setTransferErrorMsg(payTransfer.getChannelErrorMsg()));
}
private PayTransferRespDTO validateDemoTransferStatusCanUpdate(PayDemoWithdrawDO withdraw, Long payTransferId) {

View File

@ -22,6 +22,14 @@ public interface PayDemoWithdrawService {
*/
Long createDemoWithdraw(@Valid PayDemoWithdrawCreateReqVO createReqVO);
/**
* 提现单转账
*
* @param id 提现单编号
* @return 转账编号
*/
Long transferDemoWithdraw(Long id);
/**
* 获得示例提现单分页
*

View File

@ -75,7 +75,7 @@ public class PayTransferServiceImpl implements PayTransferService {
// 1.3 校验转账单已经发起过转账
PayTransferDO transfer = validateTransferCanCreate(reqDTO, payApp.getId());
// 2. 不存在创建转账单否则允许使用相同的 no 再次发起转账
// 2.1 情况一不存在创建转账单则进行创建
if (transfer == null) {
String no = noRedisDAO.generate(TRANSFER_NO_PREFIX);
transfer = BeanUtils.toBean(reqDTO, PayTransferDO.class)
@ -83,6 +83,10 @@ public class PayTransferServiceImpl implements PayTransferService {
.setNo(no).setStatus(PayTransferStatusEnum.WAITING.getStatus())
.setNotifyUrl(payApp.getTransferNotifyUrl());
transferMapper.insert(transfer);
} else {
// 2.2 情况二存在创建转账单但是状态为关闭则更新为等待中
transferMapper.updateByIdAndStatus(transfer.getId(), transfer.getStatus(),
new PayTransferDO().setStatus(PayTransferStatusEnum.WAITING.getStatus()));
}
try {
// 3. 调用三方渠道发起转账
@ -114,9 +118,9 @@ public class PayTransferServiceImpl implements PayTransferService {
private PayTransferDO validateTransferCanCreate(PayTransferCreateReqDTO reqDTO, Long appId) {
PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantOrderId(appId, reqDTO.getMerchantOrderId());
if (transfer != null) {
// 已经存在并且状态不为等待状态说明已经调用渠道转账并返回结果
if (!PayTransferStatusEnum.isWaiting(transfer.getStatus())) {
throw exception(PAY_TRANSFER_CREATE_MERCHANT_EXISTS);
// 只有转账单状态为关闭才能再次发起转账
if (!PayTransferStatusEnum.isClosed(transfer.getStatus())) {
throw exception(PAY_TRANSFER_CREATE_FAIL_STATUS_NOT_CLOSED);
}
// 校验参数是否一致
if (ObjectUtil.notEqual(reqDTO.getPrice(), transfer.getPrice())) {
@ -160,7 +164,7 @@ public class PayTransferServiceImpl implements PayTransferService {
return;
}
if (!PayTransferStatusEnum.isWaiting(transfer.getStatus())) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING);
throw exception(PAY_TRANSFER_NOTIFY_FAIL_STATUS_IS_NOT_WAITING);
}
// 2. 更新状态
@ -168,7 +172,7 @@ public class PayTransferServiceImpl implements PayTransferService {
PayTransferStatusEnum.WAITING.getStatus(),
new PayTransferDO().setStatus(PayTransferStatusEnum.PROCESSING.getStatus()));
if (updateCounts == 0) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING);
throw exception(PAY_TRANSFER_NOTIFY_FAIL_STATUS_IS_NOT_WAITING);
}
log.info("[notifyTransferProgressing][transfer({}) 更新为转账进行中状态]", transfer.getId());
}
@ -184,7 +188,7 @@ public class PayTransferServiceImpl implements PayTransferService {
return;
}
if (!PayTransferStatusEnum.isWaitingOrProcessing(transfer.getStatus())) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING_OR_PROCESSING);
throw exception(PAY_TRANSFER_NOTIFY_FAIL_STATUS_NOT_WAITING_OR_PROCESSING);
}
// 2. 更新状态
@ -195,7 +199,7 @@ public class PayTransferServiceImpl implements PayTransferService {
.setChannelTransferNo(notify.getChannelTransferNo())
.setChannelNotifyData(JsonUtils.toJsonString(notify)));
if (updateCounts == 0) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING_OR_PROCESSING);
throw exception(PAY_TRANSFER_NOTIFY_FAIL_STATUS_NOT_WAITING_OR_PROCESSING);
}
log.info("[notifyTransferSuccess][transfer({}) 更新为已转账]", transfer.getId());
@ -214,7 +218,7 @@ public class PayTransferServiceImpl implements PayTransferService {
return;
}
if (!PayTransferStatusEnum.isWaitingOrProcessing(transfer.getStatus())) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING_OR_PROCESSING);
throw exception(PAY_TRANSFER_NOTIFY_FAIL_STATUS_NOT_WAITING_OR_PROCESSING);
}
// 2. 更新状态
@ -225,7 +229,7 @@ public class PayTransferServiceImpl implements PayTransferService {
.setChannelNotifyData(JsonUtils.toJsonString(notify))
.setChannelErrorCode(notify.getChannelErrorCode()).setChannelErrorMsg(notify.getChannelErrorMsg()));
if (updateCount == 0) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING_OR_PROCESSING);
throw exception(PAY_TRANSFER_NOTIFY_FAIL_STATUS_NOT_WAITING_OR_PROCESSING);
}
log.info("[notifyTransferClosed][transfer({}) 更新为关闭状态]", transfer.getId());