转账 - 转账代码修改,新增转账示例

This commit is contained in:
jason 2023-10-29 23:36:06 +08:00
parent 5f29399cf4
commit ccccd9f226
25 changed files with 262 additions and 319 deletions

View File

@ -15,10 +15,9 @@ CREATE TABLE `pay_transfer`
`success_time` datetime NULL COMMENT '转账成功时间', `success_time` datetime NULL COMMENT '转账成功时间',
`price` int NOT NULL COMMENT '转账金额单位', `price` int NOT NULL COMMENT '转账金额单位',
`subject` varchar(512) NOT NULL COMMENT '转账标题', `subject` varchar(512) NOT NULL COMMENT '转账标题',
`user_name` varchar(64) NULL COMMENT '收款人姓名',
`alipay_logon_id` varchar(64) NULL COMMENT '支付宝登录号', `alipay_logon_id` varchar(64) NULL COMMENT '支付宝登录号',
`alipay_account_name` varchar(64) NULL COMMENT '支付宝账号名称',
`openid` varchar(64) NULL COMMENT '微信 openId', `openid` varchar(64) NULL COMMENT '微信 openId',
`wx_account_name` varchar(64) NULL COMMENT '微信账号名称',
`notify_url` varchar(1024) NOT NULL COMMENT '异步通知商户地址', `notify_url` varchar(1024) NOT NULL COMMENT '异步通知商户地址',
`user_ip` varchar(50) NOT NULL COMMENT '用户 IP', `user_ip` varchar(50) NOT NULL COMMENT '用户 IP',
`channel_extras` varchar(512) NULL DEFAULT NULL COMMENT '渠道的额外参数', `channel_extras` varchar(512) NULL DEFAULT NULL COMMENT '渠道的额外参数',
@ -35,43 +34,18 @@ CREATE TABLE `pay_transfer`
PRIMARY KEY (`id`) USING BTREE PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='转账单表'; ) ENGINE=InnoDB COMMENT='转账单表';
-- ----------------------------
-- 转账扩展单
-- ----------------------------
DROP TABLE IF EXISTS `pay_transfer_extension`;
CREATE TABLE `pay_transfer_extension`
(
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
`no` varchar(64) NOT NULL COMMENT '转账单号',
`transfer_id` bigint NOT NULL COMMENT '转账单编号',
`channel_id` bigint NOT NULL COMMENT '转账渠道编号',
`channel_code` varchar(32) NOT NULL COMMENT '转账渠道编码',
`channel_extras` varchar(512) NULL DEFAULT NULL COMMENT '支付渠道的额外参数',
`status` tinyint NOT NULL COMMENT '转账状态',
`channel_notify_data` varchar(4096) NULL DEFAULT NULL COMMENT '支付渠道异步通知的内容',
`creator` varchar(64) NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB COMMENT='转账拓展单表';
-- ---------------------------- -- ----------------------------
-- Table structure for pay_demo_transfer -- Table structure for pay_demo_transfer
-- ---------------------------- -- ----------------------------
DROP TABLE IF EXISTS `pay_demo_transfer`; DROP TABLE IF EXISTS `pay_demo_transfer`;
CREATE TABLE `pay_demo_transfer` ( CREATE TABLE `pay_demo_transfer` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '订单编号', `id` bigint NOT NULL AUTO_INCREMENT COMMENT '订单编号',
`user_id` bigint UNSIGNED NOT NULL COMMENT '用户编号',
`app_id` bigint NOT NULL COMMENT '应用编号', `app_id` bigint NOT NULL COMMENT '应用编号',
`price` int NOT NULL COMMENT '转账金额单位',
`type` int NOT NULL COMMENT '转账类型', `type` int NOT NULL COMMENT '转账类型',
`price` int NOT NULL COMMENT '转账金额单位',
`user_name` varchar(64) NULL COMMENT '收款人姓名',
`alipay_logon_id` varchar(64) NULL COMMENT '支付宝登录号', `alipay_logon_id` varchar(64) NULL COMMENT '支付宝登录号',
`alipay_account_name` varchar(64) NULL COMMENT '支付宝账号名称',
`openid` varchar(64) NULL COMMENT '微信 openId', `openid` varchar(64) NULL COMMENT '微信 openId',
`wx_account_name` varchar(64) NULL COMMENT '微信账号名称',
`transfer_status` tinyint NOT NULL DEFAULT 0 COMMENT '转账状态', `transfer_status` tinyint NOT NULL DEFAULT 0 COMMENT '转账状态',
`pay_transfer_id` bigint NULL DEFAULT NULL COMMENT '转账订单编号', `pay_transfer_id` bigint NULL DEFAULT NULL COMMENT '转账订单编号',
`pay_channel_code` varchar(16) NULL DEFAULT NULL COMMENT '转账支付成功渠道', `pay_channel_code` varchar(16) NULL DEFAULT NULL COMMENT '转账支付成功渠道',
@ -216,3 +190,48 @@ VALUES (
'', '', '', 0 '', '', '', 0
); );
-- 支付实战和转账实战数据库脚本
update system_menu set deleted = 1 where id = 2161;
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status, component_name
)
VALUES (
'接入示例', '', 1, 99, 1117,
'demo', 'ep:caret-right', '', 0, ''
);
SELECT @parentId1 := LAST_INSERT_ID();
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status, component_name
)
VALUES (
'支付实战', '', 2, 1, @parentId1,
'demo-order', 'fa:leaf', 'pay/demo/order/index', 0, NULL
);
INSERT INTO system_menu(
name, permission, type, sort, parent_id,
path, icon, component, status, component_name
)
VALUES (
'转账实战', '', 2, 1, @parentId1,
'demo-transfer', 'fa:leaf', 'pay/demo/transfer/index', 0, NULL
);
-- 转账状态和转账类型数据字典
INSERT INTO `system_dict_type`(`id`, `name`, `type`, `status`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `deleted_time`) VALUES (171, '支付转账类型', 'pay_transfer_type', 0, '', '1', '2023-10-28 16:27:18', '1', '2023-10-28 16:27:18', b'0', '1970-01-01 00:00:00');
INSERT INTO `system_dict_type`(`id`, `name`, `type`, `status`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`, `deleted_time`) VALUES (170, '转账订单状态', 'pay_transfer_status', 0, '', '1', '2023-10-28 16:18:32', '1', '2023-10-28 16:18:32', b'0', '1970-01-01 00:00:00');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1245, 4, '钱包余额', '4', 'pay_transfer_type', 0, 'info', '', '', '1', '2023-10-28 16:28:37', '1', '2023-10-28 16:28:37', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1244, 3, '银行卡', '3', 'pay_transfer_type', 0, 'default', '', '', '1', '2023-10-28 16:28:21', '1', '2023-10-28 16:28:21', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1243, 2, '微信余额', '2', 'pay_transfer_type', 0, 'info', '', '', '1', '2023-10-28 16:28:07', '1', '2023-10-28 16:28:07', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1242, 1, '支付宝余额', '1', 'pay_transfer_type', 0, 'default', '', '', '1', '2023-10-28 16:27:44', '1', '2023-10-28 16:27:44', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1241, 4, '转账失败', '30', 'pay_transfer_status', 0, 'warning', '', '', '1', '2023-10-28 16:24:16', '1', '2023-10-28 16:24:16', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1240, 3, '转账成功', '20', 'pay_transfer_status', 0, 'success', '', '', '1', '2023-10-28 16:23:50', '1', '2023-10-28 16:23:50', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1239, 2, '转账进行中', '10', 'pay_transfer_status', 0, 'info', '', '', '1', '2023-10-28 16:23:12', '1', '2023-10-28 16:23:12', b'0');
INSERT INTO `system_dict_data`(`id`, `sort`, `label`, `value`, `dict_type`, `status`, `color_type`, `css_class`, `remark`, `creator`, `create_time`, `updater`, `update_time`, `deleted`) VALUES (1238, 1, '等待转账', '0', 'pay_transfer_status', 0, 'default', '', '', '1', '2023-10-28 16:21:43', '1', '2023-10-28 16:23:22', b'0');

View File

@ -30,6 +30,7 @@ public interface GlobalErrorCodeConstants {
ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "系统异常"); ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "系统异常");
ErrorCode NOT_IMPLEMENTED = new ErrorCode(501, "功能未实现/未开启"); ErrorCode NOT_IMPLEMENTED = new ErrorCode(501, "功能未实现/未开启");
ErrorCode ERROR_CONFIGURATION = new ErrorCode(502, "错误的配置项");
// ========== 自定义错误段 ========== // ========== 自定义错误段 ==========
ErrorCode REPEATED_REQUESTS = new ErrorCode(900, "重复请求,请稍后重试"); // 重复请求 ErrorCode REPEATED_REQUESTS = new ErrorCode(900, "重复请求,请稍后重试"); // 重复请求

View File

@ -11,6 +11,8 @@ import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.Map; import java.util.Map;
import static cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum.*;
/** /**
* 统一转账 Request DTO * 统一转账 Request DTO
* *
@ -49,32 +51,26 @@ public class PayTransferUnifiedReqDTO {
*/ */
@NotEmpty(message = "转账标题不能为空") @NotEmpty(message = "转账标题不能为空")
@Length(max = 128, message = "转账标题不能超过 128") @Length(max = 128, message = "转账标题不能超过 128")
private String title; private String subject;
/**
* 收款人姓名
*/
@NotBlank(message = "收款人姓名不能为空", groups = {Alipay.class})
private String userName;
/** /**
* 支付宝登录号 * 支付宝登录号
*/ */
@NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class}) @NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class})
private String alipayLogonId; private String alipayLogonId;
/**
* 支付宝账号名称
*/
@NotBlank(message = "支付宝账号名称不能为空", groups = {PayTransferTypeEnum.Alipay.class})
private String alipayAccountName;
/** /**
* 微信 openId * 微信 openId
*/ */
@NotBlank(message = "微信 openId 不能为空", groups = {PayTransferTypeEnum.WxPay.class}) @NotBlank(message = "微信 openId 不能为空", groups = {WxPay.class})
private String openid; private String openid;
/**
* 微信账号名称
*/
private String wxAccountName;
/** /**
* 支付渠道的额外参数 * 支付渠道的额外参数
*/ */

View File

@ -16,6 +16,8 @@ import lombok.extern.slf4j.Slf4j;
import java.util.Map; import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
/** /**
@ -186,9 +188,9 @@ public abstract class AbstractPayClient<Config extends PayClientConfig> implemen
@Override @Override
public final PayTransferRespDTO unifiedTransfer(PayTransferUnifiedReqDTO reqDTO) { public final PayTransferRespDTO unifiedTransfer(PayTransferUnifiedReqDTO reqDTO) {
validatePayTransferReqDTO(reqDTO);
PayTransferRespDTO resp; PayTransferRespDTO resp;
try{ try{
validatePayTransferReqDTO(reqDTO);
resp = doUnifiedTransfer(reqDTO); resp = doUnifiedTransfer(reqDTO);
}catch (ServiceException ex) { // 业务异常都是实现类已经翻译所以直接抛出即可 }catch (ServiceException ex) { // 业务异常都是实现类已经翻译所以直接抛出即可
throw ex; throw ex;
@ -212,7 +214,7 @@ public abstract class AbstractPayClient<Config extends PayClientConfig> implemen
break; break;
} }
default: { default: {
throw new UnsupportedOperationException("待实现"); throw exception(NOT_IMPLEMENTED);
} }
} }
} }

View File

@ -43,8 +43,7 @@ import java.util.Objects;
import java.util.function.Supplier; import java.util.function.Supplier;
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMATTER; import static cn.hutool.core.date.DatePattern.NORM_DATETIME_FORMATTER;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST; import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception0;
import static cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig.MODE_CERTIFICATE; import static cn.iocoder.yudao.framework.pay.core.client.impl.alipay.AlipayPayClientConfig.MODE_CERTIFICATE;
@ -229,13 +228,13 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient<AlipayPa
protected PayTransferRespDTO doUnifiedTransfer(PayTransferUnifiedReqDTO reqDTO) throws AlipayApiException { protected PayTransferRespDTO doUnifiedTransfer(PayTransferUnifiedReqDTO reqDTO) throws AlipayApiException {
// 1.1 校验公钥类型 必须使用公钥证书模式 // 1.1 校验公钥类型 必须使用公钥证书模式
if (!Objects.equals(config.getMode(), MODE_CERTIFICATE)) { if (!Objects.equals(config.getMode(), MODE_CERTIFICATE)) {
throw exception0(BAD_REQUEST.getCode(),"支付宝单笔转账必须使用公钥证书模式"); throw exception0(ERROR_CONFIGURATION.getCode(),"支付宝单笔转账必须使用公钥证书模式");
} }
// 1.2 构建 AlipayFundTransUniTransferModel // 1.2 构建 AlipayFundTransUniTransferModel
AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel(); AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
// 通用的参数 // 通用的参数
model.setTransAmount(formatAmount(reqDTO.getPrice())); // 转账金额 model.setTransAmount(formatAmount(reqDTO.getPrice())); // 转账金额
model.setOrderTitle(reqDTO.getTitle()); // 转账业务的标题用于在支付宝用户的账单里显示 model.setOrderTitle(reqDTO.getSubject()); // 转账业务的标题用于在支付宝用户的账单里显示
model.setOutBizNo(reqDTO.getOutTransferNo()); model.setOutBizNo(reqDTO.getOutTransferNo());
model.setProductCode("TRANS_ACCOUNT_NO_PWD"); // 销售产品码单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD model.setProductCode("TRANS_ACCOUNT_NO_PWD"); // 销售产品码单笔无密转账固定为 TRANS_ACCOUNT_NO_PWD
model.setBizScene("DIRECT_TRANSFER"); // 业务场景 单笔无密转账固定为 DIRECT_TRANSFER model.setBizScene("DIRECT_TRANSFER"); // 业务场景 单笔无密转账固定为 DIRECT_TRANSFER
@ -249,7 +248,7 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient<AlipayPa
Participant payeeInfo = new Participant(); Participant payeeInfo = new Participant();
payeeInfo.setIdentityType("ALIPAY_LOGON_ID"); payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
payeeInfo.setIdentity(reqDTO.getAlipayLogonId()); // 支付宝登录号 payeeInfo.setIdentity(reqDTO.getAlipayLogonId()); // 支付宝登录号
payeeInfo.setName(reqDTO.getAlipayAccountName()); // 支付宝账号姓名 payeeInfo.setName(reqDTO.getUserName()); // 支付宝账号姓名
model.setPayeeInfo(payeeInfo); model.setPayeeInfo(payeeInfo);
// 1.3 构建 AlipayFundTransUniTransferRequest // 1.3 构建 AlipayFundTransUniTransferRequest
AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest(); AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();

View File

@ -14,7 +14,7 @@ import java.util.Objects;
@AllArgsConstructor @AllArgsConstructor
public enum PayTransferStatusRespEnum { public enum PayTransferStatusRespEnum {
WAITING(0, "转账"), WAITING(0, "等待转账"),
/** /**
* TODO 转账到银行卡. 会有T+0 T+1 到账的请情况 还未实现 * TODO 转账到银行卡. 会有T+0 T+1 到账的请情况 还未实现

View File

@ -60,19 +60,16 @@ public class PayTransferCreateReqDTO {
@NotEmpty(message = "转账标题不能为空") @NotEmpty(message = "转账标题不能为空")
private String subject; private String subject;
@NotEmpty(message = "收款方信息不能为空") /**
private Map<String, String> payeeInfo; * 收款人姓名
*/
@NotBlank(message = "收款人姓名不能为空", groups = {PayTransferTypeEnum.Alipay.class})
private String userName;
@NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class}) @NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class})
private String alipayLogonId; private String alipayLogonId;
@NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class})
private String alipayAccountName;
// ========== 微信转账相关字段 ========== // ========== 微信转账相关字段 ==========
@NotBlank(message = "微信 openId 不能为空", groups = {PayTransferTypeEnum.WxPay.class}) @NotBlank(message = "微信 openId 不能为空", groups = {PayTransferTypeEnum.WxPay.class})
private String openid; private String openid;
private String wxAccountName;
} }

View File

@ -68,11 +68,9 @@ public interface ErrorCodeConstants {
ErrorCode PAY_TRANSFER_NOT_FOUND = new ErrorCode(1_007_009_001, "转账交易单不存在"); ErrorCode PAY_TRANSFER_NOT_FOUND = new ErrorCode(1_007_009_001, "转账交易单不存在");
ErrorCode PAY_TRANSFER_STATUS_IS_SUCCESS = new ErrorCode(1_007_009_002, "转账单已成功转账"); ErrorCode PAY_TRANSFER_STATUS_IS_SUCCESS = new ErrorCode(1_007_009_002, "转账单已成功转账");
ErrorCode PAY_TRANSFER_EXISTS = new ErrorCode(1_007_009_003, "已经存在转账单"); ErrorCode PAY_TRANSFER_EXISTS = new ErrorCode(1_007_009_003, "已经存在转账单");
ErrorCode PAY_MERCHANT_TRANSFER_EXISTS = new ErrorCode(1_007_009_004, "该笔业务的转账已经存在,请查询相关状态"); ErrorCode PAY_MERCHANT_TRANSFER_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 = new ErrorCode(1_007_009_005, "转账单不处于待转账");
ErrorCode PAY_TRANSFER_STATUS_IS_NOT_PENDING = new ErrorCode(1_007_009_006, "转账单不处于待转账或转账中"); ErrorCode PAY_TRANSFER_STATUS_IS_NOT_PENDING = new ErrorCode(1_007_009_006, "转账单不处于待转账或转账中");
ErrorCode PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH = new ErrorCode(1_007_009_008, "转账类型和转账渠道不匹配");
// ========== 示例订单 1-007-900-000 ========== // ========== 示例订单 1-007-900-000 ==========
ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1_007_900_000, "示例订单不存在"); ErrorCode DEMO_ORDER_NOT_FOUND = new ErrorCode(1_007_900_000, "示例订单不存在");
ErrorCode DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1_007_900_001, "示例订单更新支付状态失败,订单不是【未支付】状态"); ErrorCode DEMO_ORDER_UPDATE_PAID_STATUS_NOT_UNPAID = new ErrorCode(1_007_900_001, "示例订单更新支付状态失败,订单不是【未支付】状态");

View File

@ -12,7 +12,7 @@ import java.util.Objects;
@AllArgsConstructor @AllArgsConstructor
public enum PayTransferStatusEnum { public enum PayTransferStatusEnum {
WAITING(0, "待转账"), WAITING(0, "待转账"),
/** /**
* TODO 转账到银行卡. 会有T+0 T+1 到账的请情况 还未实现 * TODO 转账到银行卡. 会有T+0 T+1 到账的请情况 还未实现
*/ */
@ -20,7 +20,7 @@ public enum PayTransferStatusEnum {
SUCCESS(20, "转账成功"), SUCCESS(20, "转账成功"),
/** /**
* 转账关闭 (失败或者其它情况) * 转账关闭 (失败或者其它情况) // TODO 改成 转账失败状态
*/ */
CLOSED(30, "转账关闭"); CLOSED(30, "转账关闭");

View File

@ -1,21 +1,22 @@
package cn.iocoder.yudao.module.pay.controller.admin.demo; package cn.iocoder.yudao.module.pay.controller.admin.demo;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO; import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferRespVO;
import cn.iocoder.yudao.module.pay.convert.demo.PayDemoTransferConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
import cn.iocoder.yudao.module.pay.service.demo.PayDemoTransferService; import cn.iocoder.yudao.module.pay.service.demo.PayDemoTransferService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Valid; import javax.validation.Valid;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "管理后台 - 示例转账单") @Tag(name = "管理后台 - 示例转账单")
@RestController @RestController
@ -27,7 +28,14 @@ public class PayDemoTransferController {
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "创建示例转账订单") @Operation(summary = "创建示例转账订单")
public CommonResult<Long> createDemoOrder(@Valid @RequestBody PayDemoTransferCreateReqVO createReqVO) { public CommonResult<Long> createDemoTransfer(@Valid @RequestBody PayDemoTransferCreateReqVO createReqVO) {
return success(demoTransferService.createDemoTransfer(getLoginUserId(), createReqVO)); return success(demoTransferService.createDemoTransfer(createReqVO));
}
@GetMapping("/page")
@Operation(summary = "获得示例订单分页")
public CommonResult<PageResult<PayDemoTransferRespVO>> getDemoTransferPage(@Valid PageParam pageVO) {
PageResult<PayDemoTransferDO> pageResult = demoTransferService.getDemoTransferPage(pageVO);
return success(PayDemoTransferConvert.INSTANCE.convertPage(pageResult));
} }
} }

View File

@ -11,6 +11,8 @@ import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferTypeEnum.*;
/** /**
* @author jason * @author jason
*/ */
@ -28,29 +30,22 @@ public class PayDemoTransferCreateReqVO {
@Min(value = 1, message = "转账金额必须大于零") @Min(value = 1, message = "转账金额必须大于零")
private Integer price; private Integer price;
@Schema(description = "收款人姓名", example = "test1")
@NotBlank(message = "收款人姓名不能为空", groups = {Alipay.class})
private String userName;
// ========== 支付宝转账相关字段 ========== // ========== 支付宝转账相关字段 ==========
@Schema(description = "支付宝登录号,支持邮箱和手机号格式", example = "test1@@sandbox.com") @Schema(description = "支付宝登录号,支持邮箱和手机号格式", example = "test1@@sandbox.com")
@NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class}) @NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class})
private String alipayLogonId; private String alipayLogonId;
@Schema(description = "支付宝账号名称", example = "test1")
@NotBlank(message = "支付宝账号名称不能为空", groups = {Alipay.class})
private String alipayAccountName;
// ========== 微信转账相关字段 ========== // ========== 微信转账相关字段 ==========
@Schema(description = "微信 openId", example = "oLefc4g5Gxx") @Schema(description = "微信 openId", example = "oLefc4g5Gxx")
@NotBlank(message = "微信 openId 不能为空", groups = {WxPay.class}) @NotBlank(message = "微信 openId 不能为空", groups = {WxPay.class})
private String openid; private String openid;
@Schema(description = "微信账号名称", example = "test2")
private String wxAccountName;
// ========== 转账到银行卡和钱包相关字段 待补充 ========== // ========== 转账到银行卡和钱包相关字段 待补充 ==========
public interface WxPay {
}
public interface Alipay {
}
public void validate(Validator validator) { public void validate(Validator validator) {
PayTransferTypeEnum transferType = PayTransferTypeEnum.typeOf(type); PayTransferTypeEnum transferType = PayTransferTypeEnum.typeOf(type);

View File

@ -0,0 +1,47 @@
package cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 示例业务转账订单 Base VO提供给添加修改详细的子 VO 使用
* 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成
*/
@Data
public class PayDemoTransferRespVO {
@Schema(description = "订单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
private Long id;
@Schema(description = "应用编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Long appId;
@Schema(description = "转账金额,单位:分", requiredMode = Schema.RequiredMode.REQUIRED, example = "22338")
private Integer price;
@Schema(description = "转账类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer type;
@Schema(description = "收款人姓名", example = "test")
private String userName;
@Schema(description = "支付宝登录号", example = "32167")
private String alipayLogonId;
@Schema(description = "微信 openId", example = "31139")
private String openid;
@Schema(description = "转账状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
private Integer transferStatus;
@Schema(description = "转账订单编号", example = "23695")
private Long payTransferId;
@Schema(description = "转账支付成功渠道")
private String payChannelCode;
@Schema(description = "转账支付时间")
private LocalDateTime transferTime;
}

View File

@ -28,11 +28,11 @@ public class PayTransferController {
@Resource @Resource
private PayTransferService payTransferService; private PayTransferService payTransferService;
@PostMapping("/initiate") @PostMapping("/create")
@Operation(summary = "发起转账") @Operation(summary = "创建转账单,发起转账")
// TODO @jason权限的设置 管理后台页面加的时候加一下 // TODO @jason权限的设置 管理后台页面加的时候加一下
public CommonResult<PayTransferCreateRespVO> initiatePayTransfer(@Valid @RequestBody PayTransferCreateReqVO reqVO) { public CommonResult<PayTransferCreateRespVO> createPayTransfer(@Valid @RequestBody PayTransferCreateReqVO reqVO) {
PayTransferDO payTransfer = payTransferService.initiateTransfer(reqVO, getClientIP()); PayTransferDO payTransfer = payTransferService.createTransfer(reqVO, getClientIP());
return success(new PayTransferCreateRespVO().setId(payTransfer.getId()).setStatus(payTransfer.getStatus())); return success(new PayTransferCreateRespVO().setId(payTransfer.getId()).setStatus(payTransfer.getStatus()));
} }

View File

@ -2,23 +2,24 @@ package cn.iocoder.yudao.module.pay.controller.admin.transfer.vo;
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
import cn.iocoder.yudao.framework.common.validation.InEnum; import cn.iocoder.yudao.framework.common.validation.InEnum;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO; import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferTypeEnum; import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferTypeEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.constraints.Min; import javax.validation.constraints.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map; import java.util.Map;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferTypeEnum.*;
@Schema(description = "管理后台 - 发起转账 Request VO") @Schema(description = "管理后台 - 发起转账 Request VO")
@Data @Data
public class PayTransferCreateReqVO { public class PayTransferCreateReqVO {
@Schema(description = "应用编号不能为空", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "应用编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "应用编号不能为空") @NotNull(message = "应用编号不能为空")
private Long appId; private Long appId;
@ -43,33 +44,30 @@ public class PayTransferCreateReqVO {
@NotEmpty(message = "转账标题不能为空") @NotEmpty(message = "转账标题不能为空")
private String subject; private String subject;
@Schema(description = "收款人姓名", example = "test1")
@NotBlank(message = "收款人姓名不能为空", groups = {Alipay.class})
private String userName;
@Schema(description = "支付宝登录号", example = "test1@sandbox.com") @Schema(description = "支付宝登录号", example = "test1@sandbox.com")
@NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class}) @NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class})
private String alipayLogonId; private String alipayLogonId;
@Schema(description = "支付宝账号名称", example = "test1")
@NotBlank(message = "支付宝账号名称不能为空", groups = {PayTransferTypeEnum.Alipay.class})
private String alipayAccountName;
@Schema(description = "微信 openId", example = "oLefc4g5Gxx") @Schema(description = "微信 openId", example = "oLefc4g5Gxx")
@NotBlank(message = "微信 openId 不能为空", groups = {PayTransferTypeEnum.WxPay.class}) @NotBlank(message = "微信 openId 不能为空", groups = {WxPay.class})
private String openid; private String openid;
@Schema(description = "微信账号名称", example = "test2")
private String wxAccountName;
@Schema(description = "转账渠道的额外参数") @Schema(description = "转账渠道的额外参数")
private Map<String, String> channelExtras; private Map<String, String> channelExtras;
public void validate(Validator validator) { public void validate(Validator validator) {
PayTransferTypeEnum transferType = PayTransferTypeEnum.typeOf(type); PayTransferTypeEnum transferType = typeOf(type);
switch (transferType) { switch (transferType) {
case ALIPAY_BALANCE: { case ALIPAY_BALANCE: {
ValidationUtils.validate(validator, this, PayDemoTransferCreateReqVO.Alipay.class); ValidationUtils.validate(validator, this, Alipay.class);
break; break;
} }
case WX_BALANCE: { case WX_BALANCE: {
ValidationUtils.validate(validator, this, PayDemoTransferCreateReqVO.WxPay.class); ValidationUtils.validate(validator, this, WxPay.class);
break; break;
} }
default: { default: {
@ -78,4 +76,20 @@ public class PayTransferCreateReqVO {
} }
} }
@AssertTrue(message = "转账类型和转账渠道不匹配")
public boolean isValidChannelCode() {
PayTransferTypeEnum transferType = typeOf(type);
switch (transferType) {
case ALIPAY_BALANCE: {
return PayChannelEnum.isAlipay(channelCode);
}
case WX_BALANCE:
case BANK_CARD:
case WALLET_BALANCE: {
throw exception(NOT_IMPLEMENTED);
}
}
return Boolean.FALSE;
}
} }

View File

@ -1,6 +1,8 @@
package cn.iocoder.yudao.module.pay.convert.demo; package cn.iocoder.yudao.module.pay.convert.demo;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO; import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferRespVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO; import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@ -14,4 +16,6 @@ public interface PayDemoTransferConvert {
PayDemoTransferConvert INSTANCE = Mappers.getMapper(PayDemoTransferConvert.class); PayDemoTransferConvert INSTANCE = Mappers.getMapper(PayDemoTransferConvert.class);
PayDemoTransferDO convert(PayDemoTransferCreateReqVO bean); PayDemoTransferDO convert(PayDemoTransferCreateReqVO bean);
PageResult<PayDemoTransferRespVO> convertPage(PageResult<PayDemoTransferDO> pageResult);
} }

View File

@ -6,7 +6,6 @@ import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTran
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO; import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO; import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@Mapper @Mapper
@ -16,7 +15,6 @@ public interface PayTransferConvert {
PayTransferDO convert(PayTransferCreateReqDTO dto); PayTransferDO convert(PayTransferCreateReqDTO dto);
@Mapping(source = "subject", target = "title")
PayTransferUnifiedReqDTO convert2(PayTransferCreateReqDTO dto); PayTransferUnifiedReqDTO convert2(PayTransferCreateReqDTO dto);
PayTransferCreateReqDTO convert(PayTransferCreateReqVO vo); PayTransferCreateReqDTO convert(PayTransferCreateReqVO vo);

View File

@ -26,11 +26,6 @@ public class PayDemoTransferDO extends BaseDO {
@TableId @TableId
private Long id; private Long id;
/**
* 用户编号
*/
private Long userId;
/** /**
* 应用编号 * 应用编号
* *
@ -38,11 +33,6 @@ public class PayDemoTransferDO extends BaseDO {
*/ */
private Long appId; private Long appId;
/**
* 转账金额单位
*/
private Integer price;
/** /**
* 转账类型 * 转账类型
* <p> * <p>
@ -50,33 +40,33 @@ public class PayDemoTransferDO extends BaseDO {
*/ */
private Integer type; private Integer type;
/**
* 转账金额单位
*/
private Integer price;
/**
* 收款人姓名
*/
private String userName;
/** /**
* 支付宝登录号 * 支付宝登录号
*/ */
private String alipayLogonId; private String alipayLogonId;
/**
* 支付宝账号名称
*/
private String alipayAccountName;
/** /**
* 微信 openId * 微信 openId
*/ */
private String openid; private String openid;
/**
* 微信账号名称
*/
private String wxAccountName;
/** /**
* 转账状态 * 转账状态
*/ */
private Integer transferStatus; private Integer transferStatus;
/** /**
* 转账单编号 * 转账单编号
*/ */
private Long payTransferId; private Long payTransferId;

View File

@ -85,6 +85,12 @@ public class PayTransferDO extends BaseDO {
* 转账金额单位 * 转账金额单位
*/ */
private Integer price; private Integer price;
/**
* 收款人姓名
*/
private String userName;
/** /**
* 转账状态 * 转账状态
* *
@ -103,10 +109,6 @@ public class PayTransferDO extends BaseDO {
*/ */
private String alipayLogonId; private String alipayLogonId;
/**
* 支付宝账号名称
*/
private String alipayAccountName;
// ========== 微信转账相关字段 ========== // ========== 微信转账相关字段 ==========
/** /**
@ -114,11 +116,6 @@ public class PayTransferDO extends BaseDO {
*/ */
private String openid; private String openid;
/**
* 微信账号名称
*/
private String wxAccountName;
// ========== 其它字段 ========== // ========== 其它字段 ==========
/** /**

View File

@ -1,68 +0,0 @@
package cn.iocoder.yudao.module.pay.dal.dataobject.transfer;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
import java.util.Map;
// TODO @jason转账是不是类似 refund不用拓展单呀支付做拓展单的原因是因为它存在不确定性可以切换多种转账和退款都是明确方式的
// @芋艿 转账是不是也存在多种方式 例如转账到银行卡 可以使用微信也可以使用支付宝 支付宝账号余额不够可以切换到微信
// TODO @jason发起了就不允许调整了类似退款哈
/**
* 转账拓展单 DO
*
* @author jason
*/
@TableName(value ="pay_transfer_extension",autoResultMap = true)
@KeySequence("pay_transfer_extension_seq") // 用于 OraclePostgreSQLKingbaseDB2H2 数据库的主键自增如果是 MySQL 等数据库可不写
@Data
public class PayTransferExtensionDO extends BaseDO {
/**
* 编号
*/
@TableId
private Long id;
/**
* 转账单号
*/
private String no;
/**
* 转账单编号
*/
private Long transferId;
/**
* 转账渠道编号
*/
private Long channelId;
/**
* 转账渠道编码
*/
private String channelCode;
/**
* 支付渠道的额外参数
*/
@TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, String> channelExtras;
/**
* 转账状态
*/
private Integer status;
/**
* 支付渠道异步通知的内容
*/
private String channelNotifyData;
}

View File

@ -1,10 +1,17 @@
package cn.iocoder.yudao.module.pay.dal.mysql.demo; package cn.iocoder.yudao.module.pay.dal.mysql.demo;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@Mapper @Mapper
public interface PayDemoTransferMapper extends BaseMapperX<PayDemoTransferDO> { public interface PayDemoTransferMapper extends BaseMapperX<PayDemoTransferDO> {
default PageResult<PayDemoTransferDO> selectPage(PageParam pageParam){
return selectPage(pageParam, new LambdaQueryWrapperX<PayDemoTransferDO>()
.orderByDesc(PayDemoTransferDO::getId));
}
} }

View File

@ -1,26 +0,0 @@
package cn.iocoder.yudao.module.pay.dal.mysql.transfer;
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferExtensionDO;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface PayTransferExtensionMapper extends BaseMapperX<PayTransferExtensionDO> {
default PayTransferExtensionDO selectByNo(String no){
return selectOne(PayTransferExtensionDO::getNo, no);
}
default int updateByIdAndStatus(Long id, List<Integer> whereStatuses, PayTransferExtensionDO updateObj) {
return update(updateObj, new LambdaQueryWrapper<PayTransferExtensionDO>()
.eq(PayTransferExtensionDO::getId, id).in(PayTransferExtensionDO::getStatus, whereStatuses));
}
}

View File

@ -1,6 +1,9 @@
package cn.iocoder.yudao.module.pay.service.demo; package cn.iocoder.yudao.module.pay.service.demo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO; import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
import javax.validation.Valid; import javax.validation.Valid;
@ -12,11 +15,17 @@ import javax.validation.Valid;
public interface PayDemoTransferService { public interface PayDemoTransferService {
/** /**
* 创建转账 * 创建转账业务示例订
* *
* @param userId 用户编号
* @param createReqVO 创建信息 * @param createReqVO 创建信息
* @return 编号 * @return 编号
*/ */
Long createDemoTransfer(Long userId, @Valid PayDemoTransferCreateReqVO createReqVO); Long createDemoTransfer(@Valid PayDemoTransferCreateReqVO createReqVO);
/**
* 获得转账业务示例订单分页
*
* @param pageVO 分页查询参数
*/
PageResult<PayDemoTransferDO> getDemoTransferPage(PageParam pageVO);
} }

View File

@ -1,11 +1,12 @@
package cn.iocoder.yudao.module.pay.service.demo; package cn.iocoder.yudao.module.pay.service.demo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO; import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.convert.demo.PayDemoTransferConvert; import cn.iocoder.yudao.module.pay.convert.demo.PayDemoTransferConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO; import cn.iocoder.yudao.module.pay.dal.dataobject.demo.PayDemoTransferDO;
import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoTransferMapper; import cn.iocoder.yudao.module.pay.dal.mysql.demo.PayDemoTransferMapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -35,14 +36,18 @@ public class PayDemoTransferServiceImpl implements PayDemoTransferService {
private Validator validator; private Validator validator;
@Override @Override
@Transactional(rollbackFor = Exception.class) public Long createDemoTransfer(@Valid PayDemoTransferCreateReqVO vo) {
public Long createDemoTransfer(Long userId, @Valid PayDemoTransferCreateReqVO vo) {
// 1 校验参数 // 1 校验参数
vo.validate(validator); vo.validate(validator);
// 2 保存示例转账业务表 // 2 保存示例转账业务表
PayDemoTransferDO demoTransfer = PayDemoTransferConvert.INSTANCE.convert(vo) PayDemoTransferDO demoTransfer = PayDemoTransferConvert.INSTANCE.convert(vo)
.setUserId(userId).setAppId(TRANSFER_APP_ID).setTransferStatus(WAITING.getStatus()); .setAppId(TRANSFER_APP_ID).setTransferStatus(WAITING.getStatus());
demoTransferMapper.insert(demoTransfer); demoTransferMapper.insert(demoTransfer);
return demoTransfer.getId(); return demoTransfer.getId();
} }
@Override
public PageResult<PayDemoTransferDO> getDemoTransferPage(PageParam pageVO) {
return demoTransferMapper.selectPage(pageVO);
}
} }

View File

@ -14,18 +14,18 @@ import javax.validation.Valid;
public interface PayTransferService { public interface PayTransferService {
/** /**
* 发起转账 * 创建转账单发起转账
* *
* 此时会发起支付渠道的调用 * 此时会发起转账渠道的调用
* *
* @param reqVO 请求 * @param reqVO 请求
* @param userIp 用户 ip * @param userIp 用户 ip
* @return 渠道的返回结果 * @return 渠道的返回结果
*/ */
PayTransferDO initiateTransfer(@Valid PayTransferCreateReqVO reqVO, String userIp); PayTransferDO createTransfer(@Valid PayTransferCreateReqVO reqVO, String userIp);
/** /**
* 创建转账单 * 创建转账单并发起转账
* *
* @param reqDTO 创建请求 * @param reqDTO 创建请求
* @return 转账单编号 * @return 转账单编号

View File

@ -2,22 +2,18 @@ package cn.iocoder.yudao.module.pay.service.transfer;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.pay.core.client.PayClient; import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferRespDTO;
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.client.exception.PayException;
import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum;
import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferStatusRespEnum; import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferStatusRespEnum;
import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO; import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO; import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO; import cn.iocoder.yudao.module.pay.dal.dataobject.channel.PayChannelDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO; import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferMapper; import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferMapper;
import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO; import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum;
import cn.iocoder.yudao.module.pay.service.app.PayAppService; import cn.iocoder.yudao.module.pay.service.app.PayAppService;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService; import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -26,6 +22,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.Validator; import javax.validation.Validator;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.convert.transfer.PayTransferConvert.INSTANCE; import static cn.iocoder.yudao.module.pay.convert.transfer.PayTransferConvert.INSTANCE;
@ -33,6 +30,7 @@ import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum.*; import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum.*;
// TODO @jason等彻底实现完单测写写 // TODO @jason等彻底实现完单测写写
/** /**
* 转账 Service 实现类 * 转账 Service 实现类
* *
@ -56,28 +54,24 @@ public class PayTransferServiceImpl implements PayTransferService {
private Validator validator; private Validator validator;
@Override @Override
@Transactional(rollbackFor = Exception.class) public PayTransferDO createTransfer(PayTransferCreateReqVO reqVO, String userIp) {
public PayTransferDO initiateTransfer(PayTransferCreateReqVO reqVO, String userIp) { // 1. 校验参数
// 1.1 校验参数
reqVO.validate(validator); reqVO.validate(validator);
// todo 使用 AssertTrue
validateChannelCodeAndTypeMatch(reqVO.getChannelCode(), reqVO.getType());
// 1.2 校验转账单是否可以创建
validateTransferCanCreate(reqVO.getAppId(), reqVO.getMerchantTransferId());
// 2. 创建转账单发起转账 // 2. 创建转账单发起转账
PayTransferCreateReqDTO req = INSTANCE.convert(reqVO).setUserIp(userIp); PayTransferCreateReqDTO req = INSTANCE.convert(reqVO).setUserIp(userIp);
Long transferId = createTransfer(req); Long transferId = createTransfer(req);
// 3. 返回转账单 // 3. 返回转账单
return getTransfer(transferId); return getTransfer(transferId);
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Long createTransfer(PayTransferCreateReqDTO reqDTO) { public Long createTransfer(PayTransferCreateReqDTO reqDTO) {
// 1.1 校验转账单是否可以提交 // 1.1 校验转账单是否可以提交
validateTransferExist(reqDTO.getAppId(), reqDTO.getMerchantTransferId()); validateTransferCanCreate(reqDTO.getAppId(), reqDTO.getMerchantTransferId());
// 1.2 校验 App // 1.2 校验 App
PayAppDO appDO = appService.validPayApp(reqDTO.getAppId()); appService.validPayApp(reqDTO.getAppId());
// 1.3 校验支付渠道是否有效 // 1.3 校验支付渠道是否有效
PayChannelDO channel = channelService.validPayChannel(reqDTO.getAppId(), reqDTO.getChannelCode()); PayChannelDO channel = channelService.validPayChannel(reqDTO.getAppId(), reqDTO.getChannelCode());
PayClient client = channelService.getPayClient(channel.getId()); PayClient client = channelService.getPayClient(channel.getId());
@ -90,19 +84,28 @@ public class PayTransferServiceImpl implements PayTransferService {
PayTransferDO transfer = INSTANCE.convert(reqDTO) PayTransferDO transfer = INSTANCE.convert(reqDTO)
.setChannelId(channel.getId()) .setChannelId(channel.getId())
.setNo(no).setStatus(WAITING.getStatus()) .setNo(no).setStatus(WAITING.getStatus())
.setNotifyUrl(appDO.getRefundNotifyUrl()); // TODO 需要加个transfer Notify url .setNotifyUrl("http://127.0.0.1:48080/admin-api/pay/todo"); // TODO 需要加个transfer Notify url
transferMapper.insert(transfer); transferMapper.insert(transfer);
PayTransferRespDTO unifiedTransferResp = null;
try { try {
// 3. 调用三方渠道发起转账 // 3. 调用三方渠道发起转账
PayTransferUnifiedReqDTO transferUnifiedReq = INSTANCE.convert2(reqDTO) PayTransferUnifiedReqDTO transferUnifiedReq = INSTANCE.convert2(reqDTO)
.setOutTransferNo(no); .setOutTransferNo(no);
PayTransferRespDTO unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq); unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq);
} catch (ServiceException ex) {
// 业务异常.直接返回转账失败的结果
log.error("[createTransfer][转账 id({}) requestDTO({}) 发生业务异常]", transfer.getId(), reqDTO, ex);
unifiedTransferResp = PayTransferRespDTO.closedOf("", "", no, ex);
} catch (Throwable e) {
// 注意这里仅打印异常不进行抛出
// 原因是虽然调用支付渠道进行转账发生异常网络请求超时实际转账成功这个结果后续通过转账回调或者转账轮询可以拿到
// TODO 需要加转账回调业务接口 转账轮询未实现
// 最终在异常的情况下支付中心会异步回调业务的转账回调接口提供转账结果
log.error("[createTransfer][转账 id({}) requestDTO({}) 发生异常]", transfer.getId(), reqDTO, e);
}
if (Objects.nonNull(unifiedTransferResp)) {
// 4. 通知转账结果 // 4. 通知转账结果
getSelf().notifyTransfer(channel, unifiedTransferResp); getSelf().notifyTransfer(channel, unifiedTransferResp);
} catch (PayException e) {
// 这里仅打印异常不进行抛出
// TODO 说明
log.error("[createTransfer][转账 id({}) requestDTO({}) 发生异常]", transfer.getId(), reqDTO, e);
} }
return transfer.getId(); return transfer.getId();
} }
@ -127,16 +130,9 @@ public class PayTransferServiceImpl implements PayTransferService {
// TODO IN_PROGRESS 待处理 // TODO IN_PROGRESS 待处理
} }
private void validateTransferExist(Long appId, String merchantTransferId ) { private void validateTransferCanCreate(Long appId, String merchantTransferId) {
PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantTransferId(appId, merchantTransferId); PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantTransferId(appId, merchantTransferId);
if (transfer != null) { // 是否存在 if (transfer != null) { // 是否存在
throw exception(PAY_TRANSFER_EXISTS);
}
}
private void validateTransferCanCreate(Long appId, String merchantTransferId ) {
PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantTransferId(appId, merchantTransferId);
if (transfer != null) { // 是否存在
throw exception(PAY_MERCHANT_TRANSFER_EXISTS); throw exception(PAY_MERCHANT_TRANSFER_EXISTS);
} }
} }
@ -192,7 +188,7 @@ public class PayTransferServiceImpl implements PayTransferService {
// 2.更新 // 2.更新
int updateCount = transferMapper.updateByIdAndStatus(transfer.getId(), int updateCount = transferMapper.updateByIdAndStatus(transfer.getId(),
CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()), CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
new PayTransferDO().setStatus(CLOSED.getStatus()) .setChannelId(channel.getId()) new PayTransferDO().setStatus(CLOSED.getStatus()).setChannelId(channel.getId())
.setChannelCode(channel.getCode()).setChannelTransferNo(notify.getChannelTransferNo()) .setChannelCode(channel.getCode()).setChannelTransferNo(notify.getChannelTransferNo())
.setChannelErrorCode(notify.getChannelErrorCode()).setChannelErrorMsg(notify.getChannelErrorMsg()) .setChannelErrorCode(notify.getChannelErrorCode()).setChannelErrorMsg(notify.getChannelErrorMsg())
.setChannelNotifyData(JsonUtils.toJsonString(notify))); .setChannelNotifyData(JsonUtils.toJsonString(notify)));
@ -204,52 +200,7 @@ public class PayTransferServiceImpl implements PayTransferService {
private void notifyTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) { private void notifyTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) {
// 更新 PayTransferDO 转账关闭 // 更新 PayTransferDO 转账关闭
updateTransferClosed(channel,notify); updateTransferClosed(channel, notify);
}
private void validateChannelCodeAndTypeMatch(String channelCode, Integer type) {
PayTransferTypeEnum transferType = PayTransferTypeEnum.typeOf(type);
switch (transferType) {
case ALIPAY_BALANCE: {
if (!PayChannelEnum.isAlipay(channelCode)) {
throw exception(PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH);
}
break;
}
case WX_BALANCE:
case BANK_CARD:
case WALLET_BALANCE: {
throw new UnsupportedOperationException("待实现");
}
}
}
private PayChannelDO validateChannelCanSubmit(Long appId, String channelCode) {
// 校验 App
appService.validPayApp(appId);
// 校验支付渠道是否有效
PayChannelDO channel = channelService.validPayChannel(appId, channelCode);
PayClient client = channelService.getPayClient(channel.getId());
if (client == null) {
log.error("[validateChannelCanSubmit][渠道编号({}) 找不到对应的支付客户端]", channel.getId());
throw exception(CHANNEL_NOT_FOUND);
}
return channel;
}
private PayTransferDO validateTransferExist(Long id) {
PayTransferDO transfer = transferMapper.selectById(id);
if (transfer == null) { // 是否存在
throw exception(PAY_TRANSFER_NOT_FOUND);
}
if (PayTransferStatusEnum.isSuccess(transfer.getStatus())) {
throw exception(PAY_TRANSFER_STATUS_IS_SUCCESS);
}
if (!PayTransferStatusEnum.isWaiting(transfer.getStatus())) {
throw exception(PAY_TRANSFER_STATUS_IS_NOT_WAITING);
}
// TODO 查询拓展单是否未已转账和转账中
return transfer;
} }
/** /**