Commit aa2f651124fcef7f1f1176fa26a24a9f31e6d37b

Authored by jason
1 parent 437185cb

Pay: 转账单实现

Showing 20 changed files with 777 additions and 12 deletions
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/dto/transfer/PayTransferRespDTO.java
@@ -64,12 +64,12 @@ public class PayTransferRespDTO { @@ -64,12 +64,12 @@ public class PayTransferRespDTO {
64 } 64 }
65 65
66 /** 66 /**
67 - * 创建【FAILURE】状态的转账返回 67 + * 创建【CLOSED】状态的转账返回
68 */ 68 */
69 - public static PayTransferRespDTO failureOf(String channelErrorCode, String channelErrorMsg,  
70 - String outTransferNo, Object rawData) { 69 + public static PayTransferRespDTO closedOf(String channelErrorCode, String channelErrorMsg,
  70 + String outTransferNo, Object rawData) {
71 PayTransferRespDTO respDTO = new PayTransferRespDTO(); 71 PayTransferRespDTO respDTO = new PayTransferRespDTO();
72 - respDTO.status = PayTransferStatusRespEnum.FAILURE.getStatus(); 72 + respDTO.status = PayTransferStatusRespEnum.CLOSED.getStatus();
73 respDTO.channelErrorCode = channelErrorCode; 73 respDTO.channelErrorCode = channelErrorCode;
74 respDTO.channelErrorMsg = channelErrorMsg; 74 respDTO.channelErrorMsg = channelErrorMsg;
75 // 相对通用的字段 75 // 相对通用的字段
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/dto/transfer/PayTransferUnifiedReqDTO.java
@@ -58,7 +58,6 @@ public class PayTransferUnifiedReqDTO { @@ -58,7 +58,6 @@ public class PayTransferUnifiedReqDTO {
58 58
59 /** 59 /**
60 * 支付渠道的额外参数 60 * 支付渠道的额外参数
61 - *  
62 */ 61 */
63 private Map<String, String> channelExtras; 62 private Map<String, String> channelExtras;
64 63
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/alipay/AlipayTransferClient.java
@@ -51,7 +51,7 @@ public class AlipayTransferClient extends AbstractAlipayPayClient { @@ -51,7 +51,7 @@ public class AlipayTransferClient extends AbstractAlipayPayClient {
51 model.setProductCode("TRANS_ACCOUNT_NO_PWD"); // 销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD 51 model.setProductCode("TRANS_ACCOUNT_NO_PWD"); // 销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD
52 model.setBizScene("DIRECT_TRANSFER"); // 业务场景 单笔无密转账固定为 DIRECT_TRANSFER。 52 model.setBizScene("DIRECT_TRANSFER"); // 业务场景 单笔无密转账固定为 DIRECT_TRANSFER。
53 model.setBusinessParams(JsonUtils.toJsonString(reqDTO.getChannelExtras())); 53 model.setBusinessParams(JsonUtils.toJsonString(reqDTO.getChannelExtras()));
54 - PayTransferTypeEnum transferType = PayTransferTypeEnum.valueOf(reqDTO.getType()); 54 + PayTransferTypeEnum transferType = PayTransferTypeEnum.ofType(reqDTO.getType());
55 switch(transferType){ 55 switch(transferType){
56 case WX_BALANCE : 56 case WX_BALANCE :
57 case WALLET_BALANCE : { 57 case WALLET_BALANCE : {
@@ -84,7 +84,7 @@ public class AlipayTransferClient extends AbstractAlipayPayClient { @@ -84,7 +84,7 @@ public class AlipayTransferClient extends AbstractAlipayPayClient {
84 if (ObjectUtils.equalsAny(response.getSubCode(), "SYSTEM_ERROR", "ACQ.SYSTEM_ERROR")) { 84 if (ObjectUtils.equalsAny(response.getSubCode(), "SYSTEM_ERROR", "ACQ.SYSTEM_ERROR")) {
85 return PayTransferRespDTO.waitingOf(null, reqDTO.getOutTransferNo(), response); 85 return PayTransferRespDTO.waitingOf(null, reqDTO.getOutTransferNo(), response);
86 } 86 }
87 - return PayTransferRespDTO.failureOf(response.getSubCode(), response.getSubMsg(), 87 + return PayTransferRespDTO.closedOf(response.getSubCode(), response.getSubMsg(),
88 reqDTO.getOutTransferNo(), response); 88 reqDTO.getOutTransferNo(), response);
89 } 89 }
90 return PayTransferRespDTO.successOf(response.getOrderId(), parseTime(response.getTransDate()), 90 return PayTransferRespDTO.successOf(response.getOrderId(), parseTime(response.getTransDate()),
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/enums/transfer/PayTransferStatusRespEnum.java
@@ -3,6 +3,8 @@ package cn.iocoder.yudao.framework.pay.core.enums.transfer; @@ -3,6 +3,8 @@ package cn.iocoder.yudao.framework.pay.core.enums.transfer;
3 import lombok.AllArgsConstructor; 3 import lombok.AllArgsConstructor;
4 import lombok.Getter; 4 import lombok.Getter;
5 5
  6 +import java.util.Objects;
  7 +
6 /** 8 /**
7 * 渠道的转账状态枚举 9 * 渠道的转账状态枚举
8 * 10 *
@@ -12,10 +14,28 @@ import lombok.Getter; @@ -12,10 +14,28 @@ import lombok.Getter;
12 @AllArgsConstructor 14 @AllArgsConstructor
13 public enum PayTransferStatusRespEnum { 15 public enum PayTransferStatusRespEnum {
14 16
15 - WAITING(0, "等待转账"),  
16 - SUCCESS(10, "转账成功"),  
17 - FAILURE(20, "转账失败"); 17 + WAITING(0, "转账中"),
  18 +
  19 + /**
  20 + * TODO 转账到银行卡. 会有T+0 T+1 到账的请情况。 还未实现
  21 + */
  22 + IN_PROGRESS(10, "转账进行中"),
  23 +
  24 +
  25 + SUCCESS(20, "转账成功"),
  26 + /**
  27 + * 转账关闭 (失败,或者其它情况)
  28 + */
  29 + CLOSED(30, "转账关闭");
18 30
19 private final Integer status; 31 private final Integer status;
20 private final String name; 32 private final String name;
  33 +
  34 + public static boolean isSuccess(Integer status) {
  35 + return Objects.equals(status, SUCCESS.getStatus());
  36 + }
  37 +
  38 + public static boolean isClosed(Integer status) {
  39 + return Objects.equals(status, CLOSED.getStatus());
  40 + }
21 } 41 }
yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/enums/transfer/PayTransferTypeEnum.java
@@ -20,6 +20,9 @@ public enum PayTransferTypeEnum implements IntArrayValuable { @@ -20,6 +20,9 @@ public enum PayTransferTypeEnum implements IntArrayValuable {
20 BANK_CARD(3, "银行卡"), 20 BANK_CARD(3, "银行卡"),
21 WALLET_BALANCE(4, "钱包余额"); 21 WALLET_BALANCE(4, "钱包余额");
22 22
  23 + public static final String ALIPAY_LOGON_ID = "ALIPAY_LOGON_ID";
  24 + public static final String ALIPAY_ACCOUNT_NAME = "ALIPAY_ACCOUNT_NAME";
  25 +
23 private final Integer type; 26 private final Integer type;
24 private final String name; 27 private final String name;
25 28
@@ -30,7 +33,7 @@ public enum PayTransferTypeEnum implements IntArrayValuable { @@ -30,7 +33,7 @@ public enum PayTransferTypeEnum implements IntArrayValuable {
30 return ARRAYS; 33 return ARRAYS;
31 } 34 }
32 35
33 - public static PayTransferTypeEnum valueOf(Integer type) { 36 + public static PayTransferTypeEnum ofType(Integer type) {
34 return ArrayUtil.firstMatch(item -> item.getType().equals(type), values()); 37 return ArrayUtil.firstMatch(item -> item.getType().equals(type), values());
35 } 38 }
36 } 39 }
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/api/transfer/PayTransferApi.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.api.transfer;
  2 +
  3 +import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
  4 +
  5 +import javax.validation.Valid;
  6 +
  7 +/**
  8 + * 转账单 API 接口
  9 + *
  10 + * @author jason
  11 + */
  12 +public interface PayTransferApi {
  13 +
  14 + /**
  15 + * 创建转账单
  16 + *
  17 + * @param reqDTO 创建请求
  18 + * @return 转账单编号
  19 + */
  20 + Long createTransfer(@Valid PayTransferCreateReqDTO reqDTO);
  21 +}
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/api/transfer/dto/PayTransferCreateReqDTO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.api.transfer.dto;
  2 +
  3 +import lombok.Data;
  4 +
  5 +import javax.validation.constraints.Min;
  6 +import javax.validation.constraints.NotEmpty;
  7 +import javax.validation.constraints.NotNull;
  8 +import java.util.Map;
  9 +
  10 +/**
  11 + * @author jason
  12 + */
  13 +@Data
  14 +public class PayTransferCreateReqDTO {
  15 +
  16 + /**
  17 + * 应用编号
  18 + */
  19 + @NotNull(message = "应用编号不能为空")
  20 + private Long appId;
  21 +
  22 + /**
  23 + * 类型
  24 + */
  25 + @NotNull(message = "转账类型不能为空")
  26 + private Integer type;
  27 +
  28 + /**
  29 + * 商户订单编号
  30 + */
  31 + @NotEmpty(message = "商户订单编号不能为空")
  32 + private String merchantOrderId;
  33 +
  34 + /**
  35 + * 转账金额,单位:分
  36 + */
  37 + @Min(value = 1, message = "转账金额必须大于零")
  38 + private Integer price;
  39 +
  40 + /**
  41 + * 转账标题
  42 + */
  43 + private String title;
  44 +
  45 + @NotEmpty(message = "收款方信息不能为空")
  46 + private Map<String, String> payeeInfo;
  47 +}
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/enums/ErrorCodeConstants.java
@@ -59,6 +59,19 @@ public interface ErrorCodeConstants { @@ -59,6 +59,19 @@ public interface ErrorCodeConstants {
59 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_ORDER_ID_ERROR = new ErrorCode(1007008008, "钱包退款更新失败,钱包退款单编号不匹配"); 59 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_ORDER_ID_ERROR = new ErrorCode(1007008008, "钱包退款更新失败,钱包退款单编号不匹配");
60 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_NOT_FOUND = new ErrorCode(1007008009, "钱包退款更新失败,退款订单不存在"); 60 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_NOT_FOUND = new ErrorCode(1007008009, "钱包退款更新失败,退款订单不存在");
61 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_PRICE_NOT_MATCH = new ErrorCode(1007008010, "钱包退款更新失败,退款单金额不匹配"); 61 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_PRICE_NOT_MATCH = new ErrorCode(1007008010, "钱包退款更新失败,退款单金额不匹配");
  62 +
  63 + // ========== 转账模块 1007009000 ==========
  64 + ErrorCode PAY_TRANSFER_SUBMIT_CHANNEL_ERROR = new ErrorCode(1007009000, "发起转账报错,错误码:{},错误提示:{}");
  65 + ErrorCode PAY_TRANSFER_ALIPAY_LOGIN_ID_IS_EMPTY = new ErrorCode(1007009001, "支付宝登录 ID 不能为空");
  66 + ErrorCode PAY_TRANSFER_ALIPAY_ACCOUNT_NAME_IS_EMPTY = new ErrorCode(1007009002, "支付宝账号名称不能为空");
  67 + ErrorCode PAY_TRANSFER_NOT_FOUND = new ErrorCode(1007009003, "转账交易单不存在");
  68 + ErrorCode PAY_TRANSFER_STATUS_IS_SUCCESS = new ErrorCode(1007009004, "转账单已成功转账");
  69 + ErrorCode PAY_TRANSFER_STATUS_IS_NOT_WAITING = new ErrorCode(1007009005, "转账单不处于待转账");
  70 + ErrorCode PAY_TRANSFER_STATUS_IS_NOT_PENDING = new ErrorCode(1007009006, "转账单不处于待转账或转账中");
  71 + ErrorCode PAY_TRANSFER_EXTENSION_NOT_FOUND = new ErrorCode(1007009007, "转账交易拓展单不存在");
  72 + ErrorCode PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH = new ErrorCode(1007009008, "转账类型和转账渠道不匹配");
  73 + ErrorCode PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING = new ErrorCode(1007009009, "转账拓展单不处于待转账或转账中");
  74 +
62 // ========== 示例订单 1007900000 ========== 75 // ========== 示例订单 1007900000 ==========
63 ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在"); 76 ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在");
64 ErrorCode DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007900001, "示例订单更新支付状态失败,订单不是【未支付】状态"); 77 ErrorCode DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007900001, "示例订单更新支付状态失败,订单不是【未支付】状态");
@@ -71,5 +84,4 @@ public interface ErrorCodeConstants { @@ -71,5 +84,4 @@ public interface ErrorCodeConstants {
71 ErrorCode DEMO_ORDER_REFUND_FAIL_REFUND_NOT_SUCCESS = new ErrorCode(1007900008, "发起退款失败,退款订单未退款成功"); 84 ErrorCode DEMO_ORDER_REFUND_FAIL_REFUND_NOT_SUCCESS = new ErrorCode(1007900008, "发起退款失败,退款订单未退款成功");
72 ErrorCode DEMO_ORDER_REFUND_FAIL_REFUND_ORDER_ID_ERROR = new ErrorCode(1007900009, "发起退款失败,退款单编号不匹配"); 85 ErrorCode DEMO_ORDER_REFUND_FAIL_REFUND_ORDER_ID_ERROR = new ErrorCode(1007900009, "发起退款失败,退款单编号不匹配");
73 ErrorCode DEMO_ORDER_REFUND_FAIL_REFUND_PRICE_NOT_MATCH = new ErrorCode(1007900010, "发起退款失败,退款单金额不匹配"); 86 ErrorCode DEMO_ORDER_REFUND_FAIL_REFUND_PRICE_NOT_MATCH = new ErrorCode(1007900010, "发起退款失败,退款单金额不匹配");
74 -  
75 } 87 }
yudao-module-pay/yudao-module-pay-api/src/main/java/cn/iocoder/yudao/module/pay/enums/transfer/PayTransferStatusEnum.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.enums.transfer;
  2 +
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Getter;
  5 +
  6 +import java.util.Objects;
  7 +
  8 +/**
  9 + * @author jason
  10 + */
  11 +@Getter
  12 +@AllArgsConstructor
  13 +public enum PayTransferStatusEnum {
  14 +
  15 + WAITING(0, "待转账"),
  16 + /**
  17 + * TODO 转账到银行卡. 会有T+0 T+1 到账的请情况。 还未实现
  18 + */
  19 + IN_PROGRESS(10, "转账进行中"),
  20 +
  21 + SUCCESS(20, "转账成功"),
  22 + /**
  23 + * 转账关闭 (失败,或者其它情况)
  24 + */
  25 + CLOSED(30, "转账关闭");
  26 +
  27 + private final Integer status;
  28 + private final String name;
  29 +
  30 + public static boolean isSuccess(Integer status) {
  31 + return Objects.equals(status, SUCCESS.getStatus());
  32 + }
  33 +
  34 + public static boolean isClosed(Integer status) {
  35 + return Objects.equals(status, CLOSED.getStatus());
  36 + }
  37 + public static boolean isWaiting(Integer status) {
  38 + return Objects.equals(status, WAITING.getStatus());
  39 + }
  40 +
  41 + /**
  42 + * 是否处于待转账或者转账中的状态
  43 + * @param status 状态
  44 + */
  45 + public static boolean isPendingStatus(Integer status) {
  46 + return Objects.equals(status, WAITING.getStatus()) || Objects.equals(status, IN_PROGRESS.status);
  47 + }
  48 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/api/transfer/PayTransferApiImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.api.transfer;
  2 +
  3 +import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
  4 +import org.springframework.stereotype.Service;
  5 +import org.springframework.validation.annotation.Validated;
  6 +
  7 +/**
  8 + * @author jason
  9 + */
  10 +@Service
  11 +@Validated
  12 +public class PayTransferApiImpl implements PayTransferApi {
  13 + @Override
  14 + public Long createTransfer(PayTransferCreateReqDTO reqDTO) {
  15 +
  16 + return null;
  17 + }
  18 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/transfer/PayTransferController.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.controller.admin.transfer;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
  5 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
  6 +import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
  7 +import io.swagger.v3.oas.annotations.Operation;
  8 +import io.swagger.v3.oas.annotations.tags.Tag;
  9 +import org.springframework.validation.annotation.Validated;
  10 +import org.springframework.web.bind.annotation.PostMapping;
  11 +import org.springframework.web.bind.annotation.RequestBody;
  12 +import org.springframework.web.bind.annotation.RequestMapping;
  13 +import org.springframework.web.bind.annotation.RestController;
  14 +
  15 +import javax.annotation.Resource;
  16 +import javax.validation.Valid;
  17 +
  18 +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
  19 +import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
  20 +
  21 +@Tag(name = "管理后台 - 转账单")
  22 +@RestController
  23 +@RequestMapping("/pay/transfer")
  24 +@Validated
  25 +public class PayTransferController {
  26 +
  27 + @Resource
  28 + private PayTransferService payTransferService;
  29 +
  30 + @PostMapping("/submit")
  31 + @Operation(summary = "提交转账订单")
  32 + public CommonResult<PayTransferSubmitRespVO> submitPayTransfer(@Valid @RequestBody PayTransferSubmitReqVO reqVO) {
  33 + PayTransferSubmitRespVO respVO = payTransferService.submitTransfer(reqVO, getClientIP());
  34 + return success(respVO);
  35 + }
  36 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/transfer/vo/PayTransferSubmitReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.controller.admin.transfer.vo;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import lombok.Data;
  5 +
  6 +import javax.validation.constraints.NotEmpty;
  7 +import javax.validation.constraints.NotNull;
  8 +import java.util.Map;
  9 +
  10 +@Schema(description = "管理后台 - 转账单提交 Request VO")
  11 +@Data
  12 +public class PayTransferSubmitReqVO {
  13 +
  14 + @Schema(description = "转账单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
  15 + @NotNull(message = "转账单编号不能为空")
  16 + private Long id;
  17 +
  18 + @Schema(description = "转账渠道", requiredMode = Schema.RequiredMode.REQUIRED, example = "alipay_transfer")
  19 + @NotEmpty(message = "转账渠道不能为空")
  20 + private String channelCode;
  21 +
  22 + @Schema(description = "转账渠道的额外参数")
  23 + private Map<String, String> channelExtras;
  24 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/transfer/vo/PayTransferSubmitRespVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.controller.admin.transfer.vo;
  2 +
  3 +import io.swagger.v3.oas.annotations.media.Schema;
  4 +import lombok.Data;
  5 +
  6 +@Schema(description = "管理后台 - 转账单提交 Response VO")
  7 +@Data
  8 +public class PayTransferSubmitRespVO {
  9 +
  10 + @Schema(description = "转账状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") // 参见 PayTransferStatusEnum 枚举
  11 + private Integer status;
  12 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/convert/transfer/PayTransferConvert.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.convert.transfer;
  2 +
  3 +import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
  4 +import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
  5 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
  6 +import org.mapstruct.Mapper;
  7 +import org.mapstruct.Mapping;
  8 +import org.mapstruct.factory.Mappers;
  9 +
  10 +/**
  11 + * @author jason
  12 + */
  13 +@Mapper
  14 +public interface PayTransferConvert {
  15 +
  16 + PayTransferConvert INSTANCE = Mappers.getMapper(PayTransferConvert.class);
  17 +
  18 + PayTransferDO convert(PayTransferCreateReqDTO dto);
  19 + @Mapping(source = "transferType", target = "type")
  20 + PayTransferCreateReqDTO convert(PayDemoTransferCreateReqVO vo);
  21 +
  22 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/dataobject/transfer/PayTransferDO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.dal.dataobject.transfer;
  2 +
  3 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  4 +import com.baomidou.mybatisplus.annotation.KeySequence;
  5 +import com.baomidou.mybatisplus.annotation.TableField;
  6 +import com.baomidou.mybatisplus.annotation.TableId;
  7 +import com.baomidou.mybatisplus.annotation.TableName;
  8 +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
  9 +import lombok.Data;
  10 +
  11 +import java.time.LocalDateTime;
  12 +import java.util.Map;
  13 +
  14 +/**
  15 + * 转账单 DO
  16 + *
  17 + * @author jason
  18 + */
  19 +@TableName(value ="pay_transfer", autoResultMap = true)
  20 +@KeySequence("pay_transfer_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
  21 +@Data
  22 +public class PayTransferDO extends BaseDO {
  23 +
  24 + /**
  25 + * 编号
  26 + */
  27 + @TableId
  28 + private Long id;
  29 +
  30 + /**
  31 + * 类型
  32 + */
  33 + private Integer type;
  34 +
  35 + /**
  36 + * 应用编号
  37 + */
  38 + private Long appId;
  39 +
  40 + /**
  41 + * 商户订单编号
  42 + */
  43 + private String merchantOrderId;
  44 +
  45 + /**
  46 + * 转账金额,单位:分
  47 + */
  48 + private Integer price;
  49 +
  50 + /**
  51 + * 转账标题
  52 + */
  53 + private String title;
  54 +
  55 + /**
  56 + * 收款人信息,不同类型和渠道不同
  57 + */
  58 + @TableField(typeHandler = JacksonTypeHandler.class)
  59 + private Map<String, String> payeeInfo;
  60 +
  61 + /**
  62 + * 转账状态
  63 + */
  64 + private Integer status;
  65 +
  66 + /**
  67 + * 订单转账成功时间
  68 + *
  69 + */
  70 + private LocalDateTime successTime;
  71 +
  72 + /**
  73 + * 转账成功的转账拓展单编号
  74 + *
  75 + * 关联 {@link PayTransferExtensionDO#getId()}
  76 + */
  77 + private Long extensionId;
  78 +
  79 + /**
  80 + * 转账成功的转账拓展单号
  81 + *
  82 + * 关联 {@link PayTransferExtensionDO#getNo()}
  83 + */
  84 + private String no;
  85 +
  86 + /**
  87 + * 转账渠道编号
  88 + */
  89 + private Long channelId;
  90 +
  91 + /**
  92 + * 转账渠道编码
  93 + */
  94 + private String channelCode;
  95 +}
0 \ No newline at end of file 96 \ No newline at end of file
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/dataobject/transfer/PayTransferExtensionDO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.dal.dataobject.transfer;
  2 +
  3 +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
  4 +import com.baomidou.mybatisplus.annotation.KeySequence;
  5 +import com.baomidou.mybatisplus.annotation.TableField;
  6 +import com.baomidou.mybatisplus.annotation.TableId;
  7 +import com.baomidou.mybatisplus.annotation.TableName;
  8 +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
  9 +import lombok.Data;
  10 +
  11 +import java.util.Map;
  12 +
  13 +/**
  14 + * 转账拓展单 DO
  15 + *
  16 + * @author jason
  17 + */
  18 +@TableName(value ="pay_transfer_extension",autoResultMap = true)
  19 +@KeySequence("pay_transfer_extension_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
  20 +@Data
  21 +public class PayTransferExtensionDO extends BaseDO {
  22 +
  23 + /**
  24 + * 编号
  25 + */
  26 + @TableId
  27 + private Long id;
  28 +
  29 + /**
  30 + * 转账单号
  31 + */
  32 + private String no;
  33 +
  34 + /**
  35 + * 转账单编号
  36 + */
  37 + private Long transferId;
  38 +
  39 + /**
  40 + * 转账渠道编号
  41 + */
  42 + private Long channelId;
  43 +
  44 + /**
  45 + * 转账渠道编码
  46 + */
  47 + private String channelCode;
  48 +
  49 + /**
  50 + * 支付渠道的额外参数
  51 + */
  52 + @TableField(typeHandler = JacksonTypeHandler.class)
  53 + private Map<String, String> channelExtras;
  54 +
  55 + /**
  56 + * 转账状态
  57 + */
  58 + private Integer status;
  59 +
  60 + /**
  61 + * 支付渠道异步通知的内容
  62 + */
  63 + private String channelNotifyData;
  64 +}
0 \ No newline at end of file 65 \ No newline at end of file
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/transfer/PayTransferExtensionMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.dal.mysql.transfer;
  2 +
  3 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferExtensionDO;
  4 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  5 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6 +import org.apache.ibatis.annotations.Mapper;
  7 +
  8 +import java.util.List;
  9 +
  10 +@Mapper
  11 +public interface PayTransferExtensionMapper extends BaseMapperX<PayTransferExtensionDO> {
  12 +
  13 + default PayTransferExtensionDO selectByNo(String no){
  14 + return selectOne(PayTransferExtensionDO::getNo, no);
  15 + }
  16 +
  17 + default int updateByIdAndStatus(Long id, List<Integer> status, PayTransferExtensionDO updateObj){
  18 + return update(updateObj, new LambdaQueryWrapper<PayTransferExtensionDO>()
  19 + .eq(PayTransferExtensionDO::getId, id).in(PayTransferExtensionDO::getStatus, status));
  20 + }
  21 +}
  22 +
  23 +
  24 +
  25 +
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/transfer/PayTransferMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.dal.mysql.transfer;
  2 +
  3 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
  4 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  5 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6 +import org.apache.ibatis.annotations.Mapper;
  7 +
  8 +import java.util.List;
  9 +
  10 +@Mapper
  11 +public interface PayTransferMapper extends BaseMapperX<PayTransferDO> {
  12 +
  13 + default int updateByIdAndStatus(Long id, List<Integer> status, PayTransferDO updateObj) {
  14 + return update(updateObj, new LambdaQueryWrapper<PayTransferDO>()
  15 + .eq(PayTransferDO::getId, id).in(PayTransferDO::getStatus, status));
  16 + }
  17 +}
  18 +
  19 +
  20 +
  21 +
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/transfer/PayTransferService.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.service.transfer;
  2 +
  3 +import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
  4 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
  5 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
  6 +
  7 +import javax.validation.Valid;
  8 +
  9 +/**
  10 + * @author jason
  11 + */
  12 +public interface PayTransferService {
  13 +
  14 + /**
  15 + * 提交转账单
  16 + *
  17 + * 此时,会发起支付渠道的调用
  18 + *
  19 + * @param reqVO 请求
  20 + * @param userIp 用户 ip
  21 + * @return 渠道的返回结果
  22 + */
  23 + PayTransferSubmitRespVO submitTransfer(@Valid PayTransferSubmitReqVO reqVO, String userIp);
  24 +
  25 + /**
  26 + * 创建转账单
  27 + *
  28 + * @param reqDTO 创建请求
  29 + * @return 转账单编号
  30 + */
  31 + Long createTransfer(@Valid PayTransferCreateReqDTO reqDTO);
  32 +}
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/transfer/PayTransferServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.service.transfer;
  2 +
  3 +import cn.hutool.core.collection.CollUtil;
  4 +import cn.hutool.core.util.StrUtil;
  5 +import cn.hutool.extra.spring.SpringUtil;
  6 +import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
  7 +import cn.iocoder.yudao.framework.pay.core.client.PayClient;
  8 +import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory;
  9 +import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO;
  10 +import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO;
  11 +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
  12 +import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferStatusRespEnum;
  13 +import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
  14 +import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
  15 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
  16 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
  17 +import cn.iocoder.yudao.module.pay.convert.transfer.PayTransferConvert;
  18 +import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO;
  19 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
  20 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferExtensionDO;
  21 +import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferExtensionMapper;
  22 +import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferMapper;
  23 +import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
  24 +import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum;
  25 +import cn.iocoder.yudao.module.pay.service.app.PayAppService;
  26 +import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
  27 +import lombok.extern.slf4j.Slf4j;
  28 +import org.springframework.stereotype.Service;
  29 +import org.springframework.transaction.annotation.Transactional;
  30 +
  31 +import javax.annotation.Resource;
  32 +import java.util.Objects;
  33 +
  34 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  35 +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
  36 +import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum.*;
  37 +
  38 +/**
  39 + * @author jason
  40 + */
  41 +@Service
  42 +@Slf4j
  43 +public class PayTransferServiceImpl implements PayTransferService {
  44 + private static final String TRANSFER_NO_PREFIX = "T";
  45 +
  46 + @Resource
  47 + private PayTransferMapper transferMapper;
  48 + @Resource
  49 + private PayTransferExtensionMapper transferExtensionMapper;
  50 + @Resource
  51 + private PayAppService appService;
  52 + @Resource
  53 + private PayChannelService channelService;
  54 + @Resource
  55 + private PayClientFactory payClientFactory;
  56 + @Resource
  57 + private PayNoRedisDAO noRedisDAO;
  58 +
  59 + @Override
  60 + public PayTransferSubmitRespVO submitTransfer(PayTransferSubmitReqVO reqVO, String userIp) {
  61 + // 1.1 校验转账单是否可以提交
  62 + PayTransferDO transfer = validateTransferCanSubmit(reqVO.getId());
  63 + // 1.2 校验转账类型和渠道是否匹配
  64 + validateChannelCodeAndTypeMatch(reqVO.getChannelCode(), transfer.getType());
  65 + // 1.3 校验支付渠道是否有效
  66 + PayChannelDO channel = validateChannelCanSubmit(transfer.getAppId(), reqVO.getChannelCode());
  67 + PayClient client = payClientFactory.getPayClient(channel.getId());
  68 +
  69 + // 2 新增转账拓展单
  70 + String no = noRedisDAO.generate(TRANSFER_NO_PREFIX);
  71 + PayTransferExtensionDO transferExtension = new PayTransferExtensionDO().setNo(no)
  72 + .setTransferId(transfer.getId()).setChannelId(channel.getId())
  73 + .setChannelCode(channel.getCode()).setStatus(WAITING.getStatus());
  74 + transferExtensionMapper.insert(transferExtension);
  75 +
  76 + // 3. 调用三方渠道发起转账
  77 + PayTransferUnifiedReqDTO transferUnifiedReq = new PayTransferUnifiedReqDTO()
  78 + .setOutTransferNo(transferExtension.getNo()).setPrice(transfer.getPrice())
  79 + .setType(transfer.getType()).setTitle(transfer.getTitle())
  80 + .setPayeeInfo(transfer.getPayeeInfo()).setUserIp(userIp)
  81 + .setChannelExtras(reqVO.getChannelExtras());
  82 + PayTransferRespDTO unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq);
  83 +
  84 + // 4. 通知转账结果
  85 + getSelf().notifyTransfer(channel, unifiedTransferResp);
  86 + // 如有渠道错误码,则抛出业务异常,提示用户
  87 + if (StrUtil.isNotEmpty(unifiedTransferResp.getChannelErrorCode())) {
  88 + throw exception(PAY_TRANSFER_SUBMIT_CHANNEL_ERROR, unifiedTransferResp.getChannelErrorCode(),
  89 + unifiedTransferResp.getChannelErrorMsg());
  90 + }
  91 + return new PayTransferSubmitRespVO().setStatus(unifiedTransferResp.getStatus());
  92 + }
  93 +
  94 + @Override
  95 + public Long createTransfer(PayTransferCreateReqDTO reqDTO) {
  96 + // 校验 App
  97 + appService.validPayApp(reqDTO.getAppId());
  98 + // 创建转账单
  99 + PayTransferDO transfer = PayTransferConvert.INSTANCE.convert(reqDTO)
  100 + .setStatus(WAITING.getStatus());
  101 + transferMapper.insert(transfer);
  102 + return transfer.getId();
  103 + }
  104 +
  105 + @Transactional(rollbackFor = Exception.class)
  106 + // 注意,如果是方法内调用该方法,需要通过 getSelf().notifyTransfer(channel, notify) 调用,否则事务不生效
  107 + public void notifyTransfer(PayChannelDO channel, PayTransferRespDTO notify) {
  108 + // 转账成功的回调
  109 + if (PayTransferStatusRespEnum.isSuccess(notify.getStatus())) {
  110 + notifyTransferSuccess(channel, notify);
  111 + }
  112 + // 转账关闭的回调
  113 + if (PayTransferStatusRespEnum.isClosed(notify.getStatus())) {
  114 + notifyTransferClosed(channel, notify);
  115 + }
  116 + // WAITING 状态无需处理
  117 + // TODO IN_PROGRESS 待处理
  118 + }
  119 + private void notifyTransferSuccess(PayChannelDO channel, PayTransferRespDTO notify) {
  120 + // 1. 更新 PayTransferExtensionDO 转账成功
  121 + PayTransferExtensionDO transferExtension = updateTransferExtensionSuccess(notify);
  122 +
  123 + // 2. 更新 PayTransferDO 转账成功
  124 + Boolean transferred = updateTransferSuccess(channel,transferExtension, notify);
  125 + if (transferred) {
  126 + return ;
  127 + }
  128 + // 3. TODO 插入转账通知记录
  129 + }
  130 +
  131 + private Boolean updateTransferSuccess(PayChannelDO channel, PayTransferExtensionDO transferExtension,
  132 + PayTransferRespDTO notify) {
  133 + // 1.校验
  134 + PayTransferDO transfer = transferMapper.selectById(transferExtension.getTransferId());
  135 + if (transfer == null) {
  136 + throw exception(PAY_TRANSFER_NOT_FOUND);
  137 + }
  138 + if (isSuccess(transfer.getStatus()) && Objects.equals(transfer.getExtensionId(), transferExtension.getId())) {
  139 + log.info("[updateTransferSuccess][transfer({}) 已经是已转账,无需更新]", transfer.getId());
  140 + return true;
  141 + }
  142 + if (!isPendingStatus(transfer.getStatus())) {
  143 + throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
  144 + }
  145 + // 2.更新
  146 + int updateCounts = transferMapper.updateByIdAndStatus(transfer.getId(),
  147 + CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
  148 + new PayTransferDO().setStatus(SUCCESS.getStatus()).setSuccessTime(notify.getSuccessTime())
  149 + .setChannelId(channel.getId()).setChannelCode(channel.getCode())
  150 + .setExtensionId(transferExtension.getId()).setNo(transferExtension.getNo()));
  151 + if (updateCounts == 0) {
  152 + throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
  153 + }
  154 + log.info("[updateTransferSuccess][transfer({}) 更新为已转账]", transfer.getId());
  155 + return false;
  156 + }
  157 +
  158 + private PayTransferExtensionDO updateTransferExtensionSuccess(PayTransferRespDTO notify) {
  159 + // 1 校验
  160 + PayTransferExtensionDO transferExtension = transferExtensionMapper.selectByNo(notify.getOutTransferNo());
  161 + if (transferExtension == null) {
  162 + throw exception(PAY_TRANSFER_EXTENSION_NOT_FOUND);
  163 + }
  164 + if (isSuccess(transferExtension.getStatus())) { // 如果已成功,直接返回,不用重复更新
  165 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 已经是成功状态,无需更新]", transferExtension.getId());
  166 + return transferExtension;
  167 + }
  168 + if (!isPendingStatus(transferExtension.getStatus())) {
  169 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  170 + }
  171 + // 2. 更新 PayTransferExtensionDO
  172 + int updateCount = transferExtensionMapper.updateByIdAndStatus(transferExtension.getId(),
  173 + CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
  174 + new PayTransferExtensionDO().setStatus(SUCCESS.getStatus())
  175 + .setChannelNotifyData(JsonUtils.toJsonString(notify)));
  176 + if (updateCount == 0) {
  177 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  178 + }
  179 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 更新为已转账]", transferExtension.getId());
  180 + return transferExtension;
  181 + }
  182 +
  183 + private void notifyTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) {
  184 + // 更新 PayTransferExtensionDO 转账关闭
  185 + updateTransferExtensionClosed(notify);
  186 + }
  187 +
  188 + private void updateTransferExtensionClosed(PayTransferRespDTO notify) {
  189 + // 1 校验
  190 + PayTransferExtensionDO transferExtension = transferExtensionMapper.selectByNo(notify.getOutTransferNo());
  191 + if (transferExtension == null) {
  192 + throw exception(PAY_TRANSFER_EXTENSION_NOT_FOUND);
  193 + }
  194 + if (isClosed(transferExtension.getStatus())) { // 如果已是关闭状态,直接返回,不用重复更新
  195 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 已经是关闭状态,无需更新]", transferExtension.getId());
  196 + return;
  197 + }
  198 + if (!isPendingStatus(transferExtension.getStatus())) {
  199 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  200 + }
  201 + // 2. 更新 PayTransferExtensionDO
  202 + int updateCount = transferExtensionMapper.updateByIdAndStatus(transferExtension.getId(),
  203 + CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
  204 + new PayTransferExtensionDO().setStatus(CLOSED.getStatus())
  205 + .setChannelNotifyData(JsonUtils.toJsonString(notify)));
  206 + if (updateCount == 0) {
  207 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  208 + }
  209 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 更新为关闭状态]", transferExtension.getId());
  210 + }
  211 +
  212 + private void validateChannelCodeAndTypeMatch(String channelCode, Integer type) {
  213 + PayTransferTypeEnum transferType = PayTransferTypeEnum.ofType(type);
  214 + PayChannelEnum payChannel = PayChannelEnum.getByCode(channelCode);
  215 + switch (transferType) {
  216 + case ALIPAY_BALANCE: {
  217 + if (payChannel != PayChannelEnum.ALIPAY_TRANSFER) {
  218 + throw exception(PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH);
  219 + }
  220 + break;
  221 + }
  222 + case WX_BALANCE:
  223 + case BANK_CARD:
  224 + case WALLET_BALANCE: {
  225 + throw new UnsupportedOperationException("待实现");
  226 + }
  227 + }
  228 + }
  229 +
  230 + private PayChannelDO validateChannelCanSubmit(Long appId, String channelCode) {
  231 + // 校验 App
  232 + appService.validPayApp(appId);
  233 + // 校验支付渠道是否有效
  234 + PayChannelDO channel = channelService.validPayChannel(appId, channelCode);
  235 + PayClient client = payClientFactory.getPayClient(channel.getId());
  236 + if (client == null) {
  237 + log.error("[validateChannelCanSubmit][渠道编号({}) 找不到对应的支付客户端]", channel.getId());
  238 + throw exception(CHANNEL_NOT_FOUND);
  239 + }
  240 + return channel;
  241 + }
  242 +
  243 + private PayTransferDO validateTransferCanSubmit(Long id) {
  244 + PayTransferDO transfer = transferMapper.selectById(id);
  245 + if (transfer == null) { // 是否存在
  246 + throw exception(PAY_TRANSFER_NOT_FOUND);
  247 + }
  248 + if (PayTransferStatusEnum.isSuccess(transfer.getStatus())) {
  249 + throw exception(PAY_TRANSFER_STATUS_IS_SUCCESS);
  250 + }
  251 + if (!PayTransferStatusEnum.isWaiting(transfer.getStatus())) {
  252 + throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING);
  253 + }
  254 + // TODO 查询拓展单是否未已转账和转账中
  255 + return transfer;
  256 + }
  257 +
  258 + /**
  259 + * 获得自身的代理对象,解决 AOP 生效问题
  260 + *
  261 + * @return 自己
  262 + */
  263 + private PayTransferServiceImpl getSelf() {
  264 + return SpringUtil.getBean(getClass());
  265 + }
  266 +}