Commit 96b1662e4a89490ac1bfffba1ff61daa7c25f9da

Authored by YunaiV
2 parents 9ee0aebd 71ec0639

Merge remote-tracking branch 'origin/feature/mall_product' into feature/mall_product

Showing 27 changed files with 1110 additions and 13 deletions
sql/mysql/pay_wallet.sql
... ... @@ -73,3 +73,81 @@ CREATE TABLE `pay_wallet_recharge`
73 73 PRIMARY KEY (`id`) USING BTREE
74 74 ) ENGINE=InnoDB COMMENT='会员钱包充值';
75 75  
  76 +-- ----------------------------
  77 +-- 转账单表
  78 +-- ----------------------------
  79 +DROP TABLE IF EXISTS `pay_transfer`;
  80 +CREATE TABLE `pay_transfer`
  81 +(
  82 + `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
  83 + `type` int NOT NULL COMMENT '类型',
  84 + `app_id` bigint NOT NULL COMMENT '应用编号',
  85 + `merchant_order_id` varchar(64) NOT NULL COMMENT '商户订单编号',
  86 + `price` int NOT NULL COMMENT '转账金额,单位:分',
  87 + `title` varchar(512) NOT NULL COMMENT '转账标题',
  88 + `payee_info` varchar(512) NOT NULL COMMENT '收款人信息,不同类型和渠道不同',
  89 + `status` tinyint NOT NULL COMMENT '转账状态',
  90 + `success_time` datetime NULL COMMENT '转账成功时间',
  91 + `extension_id` bigint NULL COMMENT '转账渠道编号',
  92 + `no` varchar(64) NULL COMMENT '转账单号',
  93 + `channel_id` bigint NULL COMMENT '转账渠道编号',
  94 + `channel_code` varchar(32) NULL COMMENT '转账渠道编码',
  95 + `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',
  96 + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  97 + `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',
  98 + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  99 + `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  100 + `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  101 + PRIMARY KEY (`id`) USING BTREE
  102 +) ENGINE=InnoDB COMMENT='转账单表';
  103 +
  104 +-- ----------------------------
  105 +-- 转账扩展单
  106 +-- ----------------------------
  107 +DROP TABLE IF EXISTS `pay_transfer_extension`;
  108 +CREATE TABLE `pay_transfer_extension`
  109 +(
  110 + `id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
  111 + `no` varchar(64) NOT NULL COMMENT '转账单号',
  112 + `transfer_id` bigint NOT NULL COMMENT '转账单编号',
  113 + `channel_id` bigint NOT NULL COMMENT '转账渠道编号',
  114 + `channel_code` varchar(32) NOT NULL COMMENT '转账渠道编码',
  115 + `channel_extras` varchar(512) NULL DEFAULT NULL COMMENT '支付渠道的额外参数',
  116 + `status` tinyint NOT NULL COMMENT '转账状态',
  117 + `channel_notify_data` varchar(4096) NULL DEFAULT NULL COMMENT '支付渠道异步通知的内容',
  118 + `creator` varchar(64) NULL DEFAULT '' COMMENT '创建者',
  119 + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  120 + `updater` varchar(64) NULL DEFAULT '' COMMENT '更新者',
  121 + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  122 + `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  123 + `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  124 + PRIMARY KEY (`id`) USING BTREE
  125 +) ENGINE=InnoDB COMMENT='转账拓展单表';
  126 +
  127 +-- ----------------------------
  128 +-- Table structure for pay_demo_transfer
  129 +-- ----------------------------
  130 +DROP TABLE IF EXISTS `pay_demo_transfer`;
  131 +CREATE TABLE `pay_demo_transfer` (
  132 + `id` bigint NOT NULL AUTO_INCREMENT COMMENT '订单编号',
  133 + `user_id` bigint UNSIGNED NOT NULL COMMENT '用户编号',
  134 + `price` int NOT NULL COMMENT '转账金额,单位:分',
  135 + `type` int NOT NULL COMMENT '转账类型',
  136 + `payee_info` varchar(512) NOT NULL COMMENT '收款人信息,不同类型和渠道不同',
  137 + `transfer_status` tinyint NOT NULL DEFAULT 0 COMMENT '转账状态',
  138 + `pay_transfer_id` bigint NULL DEFAULT NULL COMMENT '转账订单编号',
  139 + `pay_channel_code` varchar(16) NULL DEFAULT NULL COMMENT '转账支付成功渠道',
  140 + `transfer_time` datetime NULL DEFAULT NULL COMMENT '转账支付时间',
  141 + `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT '' COMMENT '创建者',
  142 + `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  143 + `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT '' COMMENT '更新者',
  144 + `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  145 + `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  146 + `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  147 + PRIMARY KEY (`id`) USING BTREE
  148 +) ENGINE = InnoDB COMMENT = '示例业务转账订单\n';
  149 +
  150 +
  151 +ALTER TABLE `pay_channel`
  152 + MODIFY COLUMN `config` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '支付渠道配置' AFTER `app_id`;
  153 +
... ...
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 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 71 PayTransferRespDTO respDTO = new PayTransferRespDTO();
72   - respDTO.status = PayTransferStatusRespEnum.FAILURE.getStatus();
  72 + respDTO.status = PayTransferStatusRespEnum.CLOSED.getStatus();
73 73 respDTO.channelErrorCode = channelErrorCode;
74 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 58  
59 59 /**
60 60 * 支付渠道的额外参数
61   - *
62 61 */
63 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 51 model.setProductCode("TRANS_ACCOUNT_NO_PWD"); // 销售产品码。单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD
52 52 model.setBizScene("DIRECT_TRANSFER"); // 业务场景 单笔无密转账固定为 DIRECT_TRANSFER。
53 53 model.setBusinessParams(JsonUtils.toJsonString(reqDTO.getChannelExtras()));
54   - PayTransferTypeEnum transferType = PayTransferTypeEnum.valueOf(reqDTO.getType());
  54 + PayTransferTypeEnum transferType = PayTransferTypeEnum.ofType(reqDTO.getType());
55 55 switch(transferType){
56 56 case WX_BALANCE :
57 57 case WALLET_BALANCE : {
... ... @@ -84,7 +84,7 @@ public class AlipayTransferClient extends AbstractAlipayPayClient {
84 84 if (ObjectUtils.equalsAny(response.getSubCode(), "SYSTEM_ERROR", "ACQ.SYSTEM_ERROR")) {
85 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 88 reqDTO.getOutTransferNo(), response);
89 89 }
90 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 3 import lombok.AllArgsConstructor;
4 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 14 @AllArgsConstructor
13 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 31 private final Integer status;
20 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 20 BANK_CARD(3, "银行卡"),
21 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 26 private final Integer type;
24 27 private final String name;
25 28  
... ... @@ -30,7 +33,7 @@ public enum PayTransferTypeEnum implements IntArrayValuable {
30 33 return ARRAYS;
31 34 }
32 35  
33   - public static PayTransferTypeEnum valueOf(Integer type) {
  36 + public static PayTransferTypeEnum ofType(Integer type) {
34 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
... ... @@ -42,14 +42,14 @@ public interface ErrorCodeConstants {
42 42 ErrorCode REFUND_NOT_FOUND = new ErrorCode(1_007_006_004, "支付退款单不存在");
43 43 ErrorCode REFUND_STATUS_IS_NOT_WAITING = new ErrorCode(1_007_006_005, "支付退款单不处于待退款");
44 44  
45   - // ========== 钱包模块 1007007000 ==========
  45 + // ========== 钱包模块 1-007-007-000 ==========
46 46 ErrorCode WALLET_NOT_FOUND = new ErrorCode(1007007000, "用户钱包不存在");
47 47 ErrorCode WALLET_BALANCE_NOT_ENOUGH = new ErrorCode(1007007001, "钱包余额不足");
48 48 ErrorCode WALLET_TRANSACTION_NOT_FOUND = new ErrorCode(1007007002, "未找到对应的钱包交易");
49 49 ErrorCode WALLET_REFUND_EXIST = new ErrorCode(1007007003, "已经存在钱包退款");
50 50 ErrorCode WALLET_FREEZE_PRICE_NOT_ENOUGH = new ErrorCode(1007007004, "钱包冻结余额不足");
51 51  
52   - // ========== 钱包模块 1-007-007-000 ==========
  52 + // ========== 钱包充值模块 1-007-008-000 ==========
53 53 ErrorCode WALLET_RECHARGE_NOT_FOUND = new ErrorCode(1007008000, "钱包充值记录不存在");
54 54 ErrorCode WALLET_RECHARGE_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007008001, "钱包充值更新支付状态失败,钱包充值记录不是【未支付】状态");
55 55 ErrorCode WALLET_RECHARGE_UPDATE_PAID_PAY_ORDER_ID_ERROR = new ErrorCode(1007008002, "钱包充值更新支付状态失败,支付单编号不匹配");
... ... @@ -62,6 +62,18 @@ public interface ErrorCodeConstants {
62 62 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_NOT_FOUND = new ErrorCode(1007008009, "钱包退款更新失败,退款订单不存在");
63 63 ErrorCode WALLET_RECHARGE_REFUND_FAIL_REFUND_PRICE_NOT_MATCH = new ErrorCode(1007008010, "钱包退款更新失败,退款单金额不匹配");
64 64  
  65 + // ========== 转账模块 1-007-009-000 ==========
  66 + ErrorCode PAY_TRANSFER_SUBMIT_CHANNEL_ERROR = new ErrorCode(1007009000, "发起转账报错,错误码:{},错误提示:{}");
  67 + ErrorCode PAY_TRANSFER_ALIPAY_LOGIN_ID_IS_EMPTY = new ErrorCode(1007009001, "支付宝登录 ID 不能为空");
  68 + ErrorCode PAY_TRANSFER_ALIPAY_ACCOUNT_NAME_IS_EMPTY = new ErrorCode(1007009002, "支付宝账号名称不能为空");
  69 + ErrorCode PAY_TRANSFER_NOT_FOUND = new ErrorCode(1007009003, "转账交易单不存在");
  70 + ErrorCode PAY_TRANSFER_STATUS_IS_SUCCESS = new ErrorCode(1007009004, "转账单已成功转账");
  71 + ErrorCode PAY_TRANSFER_STATUS_IS_NOT_WAITING = new ErrorCode(1007009005, "转账单不处于待转账");
  72 + ErrorCode PAY_TRANSFER_STATUS_IS_NOT_PENDING = new ErrorCode(1007009006, "转账单不处于待转账或转账中");
  73 + ErrorCode PAY_TRANSFER_EXTENSION_NOT_FOUND = new ErrorCode(1007009007, "转账交易拓展单不存在");
  74 + ErrorCode PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH = new ErrorCode(1007009008, "转账类型和转账渠道不匹配");
  75 + ErrorCode PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING = new ErrorCode(1007009009, "转账拓展单不处于待转账或转账中");
  76 +
65 77 // ========== 示例订单 1-007-900-000 ==========
66 78 ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1007900000, "示例订单不存在");
67 79 ErrorCode DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1007900001, "示例订单更新支付状态失败,订单不是【未支付】状态");
... ...
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/demo/PayDemoTransferController.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.controller.admin.demo;
  2 +
  3 +import cn.iocoder.yudao.framework.common.pojo.CommonResult;
  4 +import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
  5 +import cn.iocoder.yudao.module.pay.service.demo.PayDemoTransferService;
  6 +import io.swagger.v3.oas.annotations.Operation;
  7 +import io.swagger.v3.oas.annotations.tags.Tag;
  8 +import org.springframework.validation.annotation.Validated;
  9 +import org.springframework.web.bind.annotation.PostMapping;
  10 +import org.springframework.web.bind.annotation.RequestBody;
  11 +import org.springframework.web.bind.annotation.RequestMapping;
  12 +import org.springframework.web.bind.annotation.RestController;
  13 +
  14 +import javax.annotation.Resource;
  15 +import javax.validation.Valid;
  16 +
  17 +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
  18 +import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
  19 +
  20 +@Tag(name = "管理后台 - 示例转账单")
  21 +@RestController
  22 +@RequestMapping("/pay/demo-transfer")
  23 +@Validated
  24 +public class PayDemoTransferController {
  25 + @Resource
  26 + private PayDemoTransferService demoTransferService;
  27 +
  28 + @PostMapping("/create")
  29 + @Operation(summary = "创建示例转账订单")
  30 + public CommonResult<Long> createDemoOrder(@Valid @RequestBody PayDemoTransferCreateReqVO createReqVO) {
  31 + return success(demoTransferService.createDemoTransfer(getLoginUserId(), createReqVO));
  32 + }
  33 +}
... ...
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/controller/admin/demo/vo/transfer/PayDemoTransferCreateReqVO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer;
  2 +
  3 +import cn.iocoder.yudao.framework.common.validation.InEnum;
  4 +import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
  5 +import io.swagger.v3.oas.annotations.media.Schema;
  6 +import lombok.Data;
  7 +
  8 +import javax.validation.constraints.Min;
  9 +import javax.validation.constraints.NotEmpty;
  10 +import javax.validation.constraints.NotNull;
  11 +import java.util.Map;
  12 +
  13 +/**
  14 + * @author jason
  15 + */
  16 +@Schema(description = "管理后台 - 示例转账单创建 Request VO")
  17 +@Data
  18 +public class PayDemoTransferCreateReqVO {
  19 +
  20 + @Schema(description = "转账类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
  21 + @NotNull(message = "转账类型不能为空")
  22 + @InEnum(PayTransferTypeEnum.class)
  23 + private Integer transferType;
  24 +
  25 + @NotNull(message = "转账金额不能为空")
  26 + @Min(value = 1, message = "转账金额必须大于零")
  27 + private Integer price;
  28 +
  29 + @Schema(description = "收款方信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "{'ALIPAY_LOGON_ID':'xxxx'}")
  30 + @NotEmpty(message = "收款方信息不能为空")
  31 + private Map<String, String> payeeInfo;
  32 +}
... ...
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/demo/PayDemoTransferDO.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.dal.dataobject.demo;
  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 + * 示例转账订单
  16 + *
  17 + * 演示业务系统的转账业务
  18 + */
  19 +@TableName(value ="pay_demo_transfer", autoResultMap = true)
  20 +@KeySequence("pay_demo_transfer_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
  21 +@Data
  22 +public class PayDemoTransferDO extends BaseDO {
  23 + /**
  24 + * 订单编号
  25 + */
  26 + @TableId
  27 + private Long id;
  28 +
  29 + /**
  30 + * 用户编号
  31 + */
  32 + private Long userId;
  33 +
  34 + /**
  35 + * 转账金额,单位:分
  36 + */
  37 + private Integer price;
  38 +
  39 + /**
  40 + * 转账类型
  41 + */
  42 + private Integer type;
  43 +
  44 + /**
  45 + * 收款人信息,不同类型和渠道不同
  46 + */
  47 + @TableField(typeHandler = JacksonTypeHandler.class)
  48 + private Map<String, String> payeeInfo;
  49 +
  50 + /**
  51 + * 转账状态
  52 + */
  53 + private Integer transferStatus;
  54 +
  55 + /**
  56 + * 转账订单编号
  57 + */
  58 + private Long payTransferId;
  59 +
  60 + /**
  61 + * 转账支付成功渠道
  62 + */
  63 + private String payChannelCode;
  64 +
  65 + /**
  66 + * 转账支付时间
  67 + */
  68 + private LocalDateTime transferTime;
  69 +}
0 70 \ No newline at end of file
... ...
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 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 65 \ No newline at end of file
... ...
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/dal/mysql/demo/PayDemoTransferMapper.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.dal.mysql.demo;
  2 +
  3 +import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
  4 +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
  5 +import org.apache.ibatis.annotations.Mapper;
  6 +
  7 +@Mapper
  8 +public interface PayDemoTransferMapper extends BaseMapperX<PayDemoTransferDO> {
  9 +
  10 +}
  11 +
  12 +
  13 +
  14 +
... ...
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/demo/PayDemoTransferService.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.service.demo;
  2 +
  3 +import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
  4 +
  5 +import javax.validation.Valid;
  6 +
  7 +/**
  8 + * 示例转账业务 Service 接口
  9 + *
  10 + * @author jason
  11 + */
  12 +public interface PayDemoTransferService {
  13 +
  14 + /**
  15 + * 创建转账单
  16 + *
  17 + * @param userId 用户编号
  18 + * @param createReqVO 创建信息
  19 + * @return 编号
  20 + */
  21 + Long createDemoTransfer(Long userId, @Valid PayDemoTransferCreateReqVO createReqVO);
  22 +}
... ...
yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/demo/PayDemoTransferServiceImpl.java 0 → 100644
  1 +package cn.iocoder.yudao.module.pay.service.demo;
  2 +
  3 +import cn.hutool.core.map.MapUtil;
  4 +import cn.hutool.core.util.StrUtil;
  5 +import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
  6 +import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
  7 +import cn.iocoder.yudao.module.pay.convert.transfer.PayTransferConvert;
  8 +import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
  9 +import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoTransferMapper;
  10 +import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
  11 +import org.springframework.stereotype.Service;
  12 +import org.springframework.transaction.annotation.Transactional;
  13 +import org.springframework.validation.annotation.Validated;
  14 +
  15 +import javax.annotation.Resource;
  16 +import javax.validation.Valid;
  17 +import java.util.Map;
  18 +
  19 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  20 +import static cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum.*;
  21 +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.PAY_TRANSFER_ALIPAY_ACCOUNT_NAME_IS_EMPTY;
  22 +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.PAY_TRANSFER_ALIPAY_LOGIN_ID_IS_EMPTY;
  23 +import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum.WAITING;
  24 +
  25 +/**
  26 + * 示例转账业务 Service 实现类
  27 + *
  28 + * @author jason
  29 + */
  30 +@Service
  31 +@Validated
  32 +public class PayDemoTransferServiceImpl implements PayDemoTransferService {
  33 +
  34 + /**
  35 + * 接入的实力应用编号
  36 +
  37 + * 从 [支付管理 -> 应用信息] 里添加
  38 + */
  39 + private static final Long TRANSFER_APP_ID = 8L;
  40 + @Resource
  41 + private PayDemoTransferMapper demoTransferMapper;
  42 + @Resource
  43 + private PayTransferService transferService;
  44 +
  45 + @Override
  46 + @Transactional(rollbackFor = Exception.class)
  47 + public Long createDemoTransfer(Long userId, @Valid PayDemoTransferCreateReqVO vo) {
  48 + // 1 校验收款账号
  49 + validatePayeeInfo(vo.getTransferType(), vo.getPayeeInfo());
  50 +
  51 + // 2 保存示例转账业务表
  52 + PayDemoTransferDO demoTransfer = new PayDemoTransferDO().setUserId(userId).setType(vo.getTransferType())
  53 + .setPrice(vo.getPrice()).setPayeeInfo(vo.getPayeeInfo())
  54 + .setTransferStatus(WAITING.getStatus());
  55 + demoTransferMapper.insert(demoTransfer);
  56 +
  57 + // 3.1 创建转账单
  58 + Long transferId = transferService.createTransfer(PayTransferConvert.INSTANCE.convert(vo)
  59 + .setAppId(TRANSFER_APP_ID).setTitle("示例转账")
  60 + .setMerchantOrderId(String.valueOf(demoTransfer.getId())));
  61 + // 3.2 更新转账单编号
  62 + demoTransferMapper.updateById(new PayDemoTransferDO().setId(demoTransfer.getId())
  63 + .setPayTransferId(transferId));
  64 + return demoTransfer.getId();
  65 + }
  66 +
  67 + private void validatePayeeInfo(Integer transferType, Map<String, String> payeeInfo) {
  68 + PayTransferTypeEnum transferTypeEnum = ofType(transferType);
  69 + switch (transferTypeEnum) {
  70 + case ALIPAY_BALANCE: {
  71 + if (StrUtil.isEmpty(MapUtil.getStr(payeeInfo, ALIPAY_LOGON_ID))) {
  72 + throw exception(PAY_TRANSFER_ALIPAY_LOGIN_ID_IS_EMPTY);
  73 + }
  74 + if (StrUtil.isEmpty(MapUtil.getStr(payeeInfo, ALIPAY_ACCOUNT_NAME))) {
  75 + throw exception(PAY_TRANSFER_ALIPAY_ACCOUNT_NAME_IS_EMPTY);
  76 + }
  77 + break;
  78 + }
  79 + case WX_BALANCE:
  80 + case BANK_CARD:
  81 + case WALLET_BALANCE: {
  82 + throw new UnsupportedOperationException("待实现");
  83 + }
  84 + }
  85 + }
  86 +}
... ...
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.dto.transfer.PayTransferRespDTO;
  9 +import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO;
  10 +import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
  11 +import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferStatusRespEnum;
  12 +import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
  13 +import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
  14 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
  15 +import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
  16 +import cn.iocoder.yudao.module.pay.convert.transfer.PayTransferConvert;
  17 +import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO;
  18 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
  19 +import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferExtensionDO;
  20 +import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferExtensionMapper;
  21 +import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferMapper;
  22 +import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
  23 +import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum;
  24 +import cn.iocoder.yudao.module.pay.service.app.PayAppService;
  25 +import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
  26 +import lombok.extern.slf4j.Slf4j;
  27 +import org.springframework.stereotype.Service;
  28 +import org.springframework.transaction.annotation.Transactional;
  29 +
  30 +import javax.annotation.Resource;
  31 +import java.util.Objects;
  32 +
  33 +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
  34 +import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
  35 +import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum.*;
  36 +
  37 +/**
  38 + * @author jason
  39 + */
  40 +@Service
  41 +@Slf4j
  42 +public class PayTransferServiceImpl implements PayTransferService {
  43 +
  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 PayNoRedisDAO noRedisDAO;
  56 +
  57 + @Override
  58 + public PayTransferSubmitRespVO submitTransfer(PayTransferSubmitReqVO reqVO, String userIp) {
  59 + // 1.1 校验转账单是否可以提交
  60 + PayTransferDO transfer = validateTransferCanSubmit(reqVO.getId());
  61 + // 1.2 校验转账类型和渠道是否匹配
  62 + validateChannelCodeAndTypeMatch(reqVO.getChannelCode(), transfer.getType());
  63 + // 1.3 校验支付渠道是否有效
  64 + PayChannelDO channel = validateChannelCanSubmit(transfer.getAppId(), reqVO.getChannelCode());
  65 + PayClient client = channelService.getPayClient(channel.getId());
  66 +
  67 + // 2 新增转账拓展单
  68 + String no = noRedisDAO.generate(TRANSFER_NO_PREFIX);
  69 + PayTransferExtensionDO transferExtension = new PayTransferExtensionDO().setNo(no)
  70 + .setTransferId(transfer.getId()).setChannelId(channel.getId())
  71 + .setChannelCode(channel.getCode()).setStatus(WAITING.getStatus());
  72 + transferExtensionMapper.insert(transferExtension);
  73 +
  74 + // 3. 调用三方渠道发起转账
  75 + PayTransferUnifiedReqDTO transferUnifiedReq = new PayTransferUnifiedReqDTO()
  76 + .setOutTransferNo(transferExtension.getNo()).setPrice(transfer.getPrice())
  77 + .setType(transfer.getType()).setTitle(transfer.getTitle())
  78 + .setPayeeInfo(transfer.getPayeeInfo()).setUserIp(userIp)
  79 + .setChannelExtras(reqVO.getChannelExtras());
  80 + PayTransferRespDTO unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq);
  81 +
  82 + // 4. 通知转账结果
  83 + getSelf().notifyTransfer(channel, unifiedTransferResp);
  84 + // 如有渠道错误码,则抛出业务异常,提示用户
  85 + if (StrUtil.isNotEmpty(unifiedTransferResp.getChannelErrorCode())) {
  86 + throw exception(PAY_TRANSFER_SUBMIT_CHANNEL_ERROR, unifiedTransferResp.getChannelErrorCode(),
  87 + unifiedTransferResp.getChannelErrorMsg());
  88 + }
  89 + return new PayTransferSubmitRespVO().setStatus(unifiedTransferResp.getStatus());
  90 + }
  91 +
  92 + @Override
  93 + public Long createTransfer(PayTransferCreateReqDTO reqDTO) {
  94 + // 校验 App
  95 + appService.validPayApp(reqDTO.getAppId());
  96 + // 创建转账单
  97 + PayTransferDO transfer = PayTransferConvert.INSTANCE.convert(reqDTO)
  98 + .setStatus(WAITING.getStatus());
  99 + transferMapper.insert(transfer);
  100 + return transfer.getId();
  101 + }
  102 +
  103 + @Transactional(rollbackFor = Exception.class)
  104 + // 注意,如果是方法内调用该方法,需要通过 getSelf().notifyTransfer(channel, notify) 调用,否则事务不生效
  105 + public void notifyTransfer(PayChannelDO channel, PayTransferRespDTO notify) {
  106 + // 转账成功的回调
  107 + if (PayTransferStatusRespEnum.isSuccess(notify.getStatus())) {
  108 + notifyTransferSuccess(channel, notify);
  109 + }
  110 + // 转账关闭的回调
  111 + if (PayTransferStatusRespEnum.isClosed(notify.getStatus())) {
  112 + notifyTransferClosed(channel, notify);
  113 + }
  114 + // WAITING 状态无需处理
  115 + // TODO IN_PROGRESS 待处理
  116 + }
  117 + private void notifyTransferSuccess(PayChannelDO channel, PayTransferRespDTO notify) {
  118 + // 1. 更新 PayTransferExtensionDO 转账成功
  119 + PayTransferExtensionDO transferExtension = updateTransferExtensionSuccess(notify);
  120 +
  121 + // 2. 更新 PayTransferDO 转账成功
  122 + Boolean transferred = updateTransferSuccess(channel,transferExtension, notify);
  123 + if (transferred) {
  124 + return ;
  125 + }
  126 + // 3. TODO 插入转账通知记录
  127 + }
  128 +
  129 + private Boolean updateTransferSuccess(PayChannelDO channel, PayTransferExtensionDO transferExtension,
  130 + PayTransferRespDTO notify) {
  131 + // 1.校验
  132 + PayTransferDO transfer = transferMapper.selectById(transferExtension.getTransferId());
  133 + if (transfer == null) {
  134 + throw exception(PAY_TRANSFER_NOT_FOUND);
  135 + }
  136 + if (isSuccess(transfer.getStatus()) && Objects.equals(transfer.getExtensionId(), transferExtension.getId())) {
  137 + log.info("[updateTransferSuccess][transfer({}) 已经是已转账,无需更新]", transfer.getId());
  138 + return true;
  139 + }
  140 + if (!isPendingStatus(transfer.getStatus())) {
  141 + throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
  142 + }
  143 + // 2.更新
  144 + int updateCounts = transferMapper.updateByIdAndStatus(transfer.getId(),
  145 + CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
  146 + new PayTransferDO().setStatus(SUCCESS.getStatus()).setSuccessTime(notify.getSuccessTime())
  147 + .setChannelId(channel.getId()).setChannelCode(channel.getCode())
  148 + .setExtensionId(transferExtension.getId()).setNo(transferExtension.getNo()));
  149 + if (updateCounts == 0) {
  150 + throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
  151 + }
  152 + log.info("[updateTransferSuccess][transfer({}) 更新为已转账]", transfer.getId());
  153 + return false;
  154 + }
  155 +
  156 + private PayTransferExtensionDO updateTransferExtensionSuccess(PayTransferRespDTO notify) {
  157 + // 1 校验
  158 + PayTransferExtensionDO transferExtension = transferExtensionMapper.selectByNo(notify.getOutTransferNo());
  159 + if (transferExtension == null) {
  160 + throw exception(PAY_TRANSFER_EXTENSION_NOT_FOUND);
  161 + }
  162 + if (isSuccess(transferExtension.getStatus())) { // 如果已成功,直接返回,不用重复更新
  163 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 已经是成功状态,无需更新]", transferExtension.getId());
  164 + return transferExtension;
  165 + }
  166 + if (!isPendingStatus(transferExtension.getStatus())) {
  167 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  168 + }
  169 + // 2. 更新 PayTransferExtensionDO
  170 + int updateCount = transferExtensionMapper.updateByIdAndStatus(transferExtension.getId(),
  171 + CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
  172 + new PayTransferExtensionDO().setStatus(SUCCESS.getStatus())
  173 + .setChannelNotifyData(JsonUtils.toJsonString(notify)));
  174 + if (updateCount == 0) {
  175 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  176 + }
  177 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 更新为已转账]", transferExtension.getId());
  178 + return transferExtension;
  179 + }
  180 +
  181 + private void notifyTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) {
  182 + // 更新 PayTransferExtensionDO 转账关闭
  183 + updateTransferExtensionClosed(notify);
  184 + }
  185 +
  186 + private void updateTransferExtensionClosed(PayTransferRespDTO notify) {
  187 + // 1 校验
  188 + PayTransferExtensionDO transferExtension = transferExtensionMapper.selectByNo(notify.getOutTransferNo());
  189 + if (transferExtension == null) {
  190 + throw exception(PAY_TRANSFER_EXTENSION_NOT_FOUND);
  191 + }
  192 + if (isClosed(transferExtension.getStatus())) { // 如果已是关闭状态,直接返回,不用重复更新
  193 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 已经是关闭状态,无需更新]", transferExtension.getId());
  194 + return;
  195 + }
  196 + if (!isPendingStatus(transferExtension.getStatus())) {
  197 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  198 + }
  199 + // 2. 更新 PayTransferExtensionDO
  200 + int updateCount = transferExtensionMapper.updateByIdAndStatus(transferExtension.getId(),
  201 + CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
  202 + new PayTransferExtensionDO().setStatus(CLOSED.getStatus())
  203 + .setChannelNotifyData(JsonUtils.toJsonString(notify)));
  204 + if (updateCount == 0) {
  205 + throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
  206 + }
  207 + log.info("[updateTransferExtensionSuccess][transferExtension({}) 更新为关闭状态]", transferExtension.getId());
  208 + }
  209 +
  210 + private void validateChannelCodeAndTypeMatch(String channelCode, Integer type) {
  211 + PayTransferTypeEnum transferType = PayTransferTypeEnum.ofType(type);
  212 + PayChannelEnum payChannel = PayChannelEnum.getByCode(channelCode);
  213 + switch (transferType) {
  214 + case ALIPAY_BALANCE: {
  215 + if (payChannel != PayChannelEnum.ALIPAY_TRANSFER) {
  216 + throw exception(PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH);
  217 + }
  218 + break;
  219 + }
  220 + case WX_BALANCE:
  221 + case BANK_CARD:
  222 + case WALLET_BALANCE: {
  223 + throw new UnsupportedOperationException("待实现");
  224 + }
  225 + }
  226 + }
  227 +
  228 + private PayChannelDO validateChannelCanSubmit(Long appId, String channelCode) {
  229 + // 校验 App
  230 + appService.validPayApp(appId);
  231 + // 校验支付渠道是否有效
  232 + PayChannelDO channel = channelService.validPayChannel(appId, channelCode);
  233 + PayClient client = channelService.getPayClient(channel.getId());
  234 + if (client == null) {
  235 + log.error("[validateChannelCanSubmit][渠道编号({}) 找不到对应的支付客户端]", channel.getId());
  236 + throw exception(CHANNEL_NOT_FOUND);
  237 + }
  238 + return channel;
  239 + }
  240 +
  241 + private PayTransferDO validateTransferCanSubmit(Long id) {
  242 + PayTransferDO transfer = transferMapper.selectById(id);
  243 + if (transfer == null) { // 是否存在
  244 + throw exception(PAY_TRANSFER_NOT_FOUND);
  245 + }
  246 + if (PayTransferStatusEnum.isSuccess(transfer.getStatus())) {
  247 + throw exception(PAY_TRANSFER_STATUS_IS_SUCCESS);
  248 + }
  249 + if (!PayTransferStatusEnum.isWaiting(transfer.getStatus())) {
  250 + throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING);
  251 + }
  252 + // TODO 查询拓展单是否未已转账和转账中
  253 + return transfer;
  254 + }
  255 +
  256 + /**
  257 + * 获得自身的代理对象,解决 AOP 生效问题
  258 + *
  259 + * @return 自己
  260 + */
  261 + private PayTransferServiceImpl getSelf() {
  262 + return SpringUtil.getBean(getClass());
  263 + }
  264 +}
... ...