mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 16:35:06 +08:00
转账 - code review 修改
This commit is contained in:
@ -28,13 +28,13 @@ public class PayDemoTransferCreateReqVO {
|
||||
@Min(value = 1, message = "转账金额必须大于零")
|
||||
private Integer price;
|
||||
|
||||
// ========== 支付宝,微信转账相关字段 ==========
|
||||
// ========== 支付宝转账相关字段 ==========
|
||||
@Schema(description = "支付宝登录号,支持邮箱和手机号格式", example = "test1@@sandbox.com")
|
||||
@NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class})
|
||||
private String alipayLogonId;
|
||||
|
||||
@Schema(description = "支付宝账号名称", example = "test1")
|
||||
@NotBlank(message = "支付宝登录号不能为空", groups = {Alipay.class})
|
||||
@NotBlank(message = "支付宝账号名称不能为空", groups = {Alipay.class})
|
||||
private String alipayAccountName;
|
||||
|
||||
// ========== 微信转账相关字段 ==========
|
||||
@ -42,7 +42,7 @@ public class PayDemoTransferCreateReqVO {
|
||||
@NotBlank(message = "微信 openId 不能为空", groups = {WxPay.class})
|
||||
private String openid;
|
||||
|
||||
@Schema(description = "微信账号名称", example = "oLefc4g5Gjxxxxxx")
|
||||
@Schema(description = "微信账号名称", example = "test2")
|
||||
private String wxAccountName;
|
||||
|
||||
// ========== 转账到银行卡和钱包相关字段 待补充 ==========
|
||||
|
@ -1,8 +1,9 @@
|
||||
package cn.iocoder.yudao.module.pay.controller.admin.transfer;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateRespVO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
|
||||
import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@ -27,12 +28,12 @@ public class PayTransferController {
|
||||
@Resource
|
||||
private PayTransferService payTransferService;
|
||||
|
||||
@PostMapping("/submit")
|
||||
@Operation(summary = "提交转账订单")
|
||||
@PostMapping("/initiate")
|
||||
@Operation(summary = "发起转账")
|
||||
// TODO @jason:权限的设置, 管理后台页面加的时候加一下
|
||||
public CommonResult<PayTransferSubmitRespVO> submitPayTransfer(@Valid @RequestBody PayTransferSubmitReqVO reqVO) {
|
||||
PayTransferSubmitRespVO respVO = payTransferService.submitTransfer(reqVO, getClientIP());
|
||||
return success(respVO);
|
||||
public CommonResult<PayTransferCreateRespVO> initiatePayTransfer(@Valid @RequestBody PayTransferCreateReqVO reqVO) {
|
||||
PayTransferDO payTransfer = payTransferService.initiateTransfer(reqVO, getClientIP());
|
||||
return success(new PayTransferCreateRespVO().setId(payTransfer.getId()).setStatus(payTransfer.getStatus()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,81 @@
|
||||
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.validation.InEnum;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
|
||||
import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferTypeEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "管理后台 - 发起转账 Request VO")
|
||||
@Data
|
||||
public class PayTransferCreateReqVO {
|
||||
|
||||
@Schema(description = "应用编号不能为空", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "应用编号不能为空")
|
||||
private Long appId;
|
||||
|
||||
@Schema(description = "商户转账单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "商户转账单编号不能为空")
|
||||
private String merchantTransferId;
|
||||
|
||||
@Schema(description = "转账类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "转账类型不能为空")
|
||||
@InEnum(PayTransferTypeEnum.class)
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "转账渠道", requiredMode = Schema.RequiredMode.REQUIRED, example = "alipay_pc")
|
||||
@NotEmpty(message = "转账渠道不能为空")
|
||||
private String channelCode;
|
||||
|
||||
@Min(value = 1, message = "转账金额必须大于零")
|
||||
@NotNull(message = "转账金额不能为空")
|
||||
private Integer price;
|
||||
|
||||
@Schema(description = "转账标题", requiredMode = Schema.RequiredMode.REQUIRED, example = "示例转账")
|
||||
@NotEmpty(message = "转账标题不能为空")
|
||||
private String subject;
|
||||
|
||||
@Schema(description = "支付宝登录号", example = "test1@sandbox.com")
|
||||
@NotBlank(message = "支付宝登录号不能为空", groups = {PayTransferTypeEnum.Alipay.class})
|
||||
private String alipayLogonId;
|
||||
|
||||
@Schema(description = "支付宝账号名称", example = "test1")
|
||||
@NotBlank(message = "支付宝账号名称不能为空", groups = {PayTransferTypeEnum.Alipay.class})
|
||||
private String alipayAccountName;
|
||||
|
||||
@Schema(description = "微信 openId", example = "oLefc4g5Gxx")
|
||||
@NotBlank(message = "微信 openId 不能为空", groups = {PayTransferTypeEnum.WxPay.class})
|
||||
private String openid;
|
||||
|
||||
@Schema(description = "微信账号名称", example = "test2")
|
||||
private String wxAccountName;
|
||||
|
||||
@Schema(description = "转账渠道的额外参数")
|
||||
private Map<String, String> channelExtras;
|
||||
|
||||
public void validate(Validator validator) {
|
||||
PayTransferTypeEnum transferType = PayTransferTypeEnum.typeOf(type);
|
||||
switch (transferType) {
|
||||
case ALIPAY_BALANCE: {
|
||||
ValidationUtils.validate(validator, this, PayDemoTransferCreateReqVO.Alipay.class);
|
||||
break;
|
||||
}
|
||||
case WX_BALANCE: {
|
||||
ValidationUtils.validate(validator, this, PayDemoTransferCreateReqVO.WxPay.class);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new UnsupportedOperationException("待实现");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -3,9 +3,12 @@ package cn.iocoder.yudao.module.pay.controller.admin.transfer.vo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Schema(description = "管理后台 - 转账单提交 Response VO")
|
||||
@Schema(description = "管理后台 - 发起转账 Response VO")
|
||||
@Data
|
||||
public class PayTransferSubmitRespVO {
|
||||
public class PayTransferCreateRespVO {
|
||||
|
||||
@Schema(description = "转账单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "转账状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") // 参见 PayTransferStatusEnum 枚举
|
||||
private Integer status;
|
@ -1,25 +0,0 @@
|
||||
package cn.iocoder.yudao.module.pay.controller.admin.transfer.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "管理后台 - 转账单提交 Request VO")
|
||||
@Data
|
||||
public class PayTransferSubmitReqVO {
|
||||
|
||||
@Schema(description = "转账单编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "转账单编号不能为空")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "转账渠道", requiredMode = Schema.RequiredMode.REQUIRED, example = "alipay_transfer")
|
||||
@NotEmpty(message = "转账渠道不能为空")
|
||||
private String channelCode;
|
||||
|
||||
@Schema(description = "转账渠道的额外参数")
|
||||
private Map<String, String> channelExtras;
|
||||
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
package cn.iocoder.yudao.module.pay.convert.transfer;
|
||||
|
||||
import cn.iocoder.yudao.framework.pay.core.client.dto.transfer.PayTransferUnifiedReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTransferCreateReqVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
@ -12,9 +14,13 @@ public interface PayTransferConvert {
|
||||
|
||||
PayTransferConvert INSTANCE = Mappers.getMapper(PayTransferConvert.class);
|
||||
|
||||
@Mapping(source = "title", target = "subject") // TODO @jason:是不是都改成 subject 完事呀?
|
||||
PayTransferDO convert(PayTransferCreateReqDTO dto);
|
||||
|
||||
@Mapping(source = "subject", target = "title")
|
||||
PayTransferUnifiedReqDTO convert2(PayTransferCreateReqDTO dto);
|
||||
|
||||
PayTransferCreateReqDTO convert(PayTransferCreateReqVO vo);
|
||||
|
||||
PayTransferCreateReqDTO convert(PayDemoTransferCreateReqVO vo);
|
||||
|
||||
}
|
||||
|
@ -22,14 +22,10 @@ public interface PayWalletConvert {
|
||||
|
||||
PageResult<PayWalletRespVO> convertPage(PageResult<PayWalletDO> page);
|
||||
|
||||
default PageResult<PayWalletRespVO> convertPage(PageResult<PayWalletDO> page, Map<Long, MemberUserRespDTO> userMap){
|
||||
default PageResult<PayWalletRespVO> convertPage(PageResult<PayWalletDO> page, Map<Long, MemberUserRespDTO> userMap) {
|
||||
PageResult<PayWalletRespVO> pageResult = convertPage(page);
|
||||
pageResult.getList().forEach( wallet -> MapUtils.findAndThen(userMap, wallet.getUserId(),
|
||||
user -> {
|
||||
// TODO @jason:可以链式调用哈;
|
||||
wallet.setNickname(user.getNickname());
|
||||
wallet.setAvatar(user.getAvatar());
|
||||
}));
|
||||
pageResult.getList().forEach(wallet -> MapUtils.findAndThen(userMap, wallet.getUserId(),
|
||||
user -> wallet.setNickname(user.getNickname()).setAvatar(user.getAvatar())));
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.pay.dal.dataobject.demo;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import cn.iocoder.yudao.framework.pay.core.enums.transfer.PayTransferTypeEnum;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.app.PayAppDO;
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
@ -30,6 +31,13 @@ public class PayDemoTransferDO extends BaseDO {
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 应用编号
|
||||
*
|
||||
* 关联 {@link PayAppDO#getId()}
|
||||
*/
|
||||
private Long appId;
|
||||
|
||||
/**
|
||||
* 转账金额,单位:分
|
||||
*/
|
||||
|
@ -61,11 +61,11 @@ public class PayTransferDO extends BaseDO {
|
||||
|
||||
// ========== 商户相关字段 ==========
|
||||
/**
|
||||
* 商户订单编号
|
||||
* 商户转账单编号
|
||||
*
|
||||
* 例如说,内部系统 A 的订单号,需要保证每个 PayAppDO 唯一
|
||||
*/
|
||||
private String merchantOrderId;
|
||||
private String merchantTransferId;
|
||||
|
||||
// ========== 转账相关字段 ==========
|
||||
|
||||
|
@ -15,6 +15,14 @@ public interface PayTransferMapper extends BaseMapperX<PayTransferDO> {
|
||||
.eq(PayTransferDO::getId, id).in(PayTransferDO::getStatus, status));
|
||||
}
|
||||
|
||||
default PayTransferDO selectByAppIdAndMerchantTransferId(Long appId, String merchantTransferId){
|
||||
return selectOne(PayTransferDO::getAppId, appId,
|
||||
PayTransferDO::getMerchantTransferId, merchantTransferId);
|
||||
}
|
||||
|
||||
default PayTransferDO selectByNo(String no){
|
||||
return selectOne(PayTransferDO::getNo, no);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,7 +4,6 @@ import cn.iocoder.yudao.module.pay.controller.admin.demo.vo.transfer.PayDemoTran
|
||||
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.mysql.demo.PayDemoTransferMapper;
|
||||
import cn.iocoder.yudao.module.pay.service.transfer.PayTransferService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
@ -33,8 +32,6 @@ public class PayDemoTransferServiceImpl implements PayDemoTransferService {
|
||||
@Resource
|
||||
private PayDemoTransferMapper demoTransferMapper;
|
||||
@Resource
|
||||
private PayTransferService transferService;
|
||||
@Resource
|
||||
private Validator validator;
|
||||
|
||||
@Override
|
||||
@ -44,7 +41,7 @@ public class PayDemoTransferServiceImpl implements PayDemoTransferService {
|
||||
vo.validate(validator);
|
||||
// 2 保存示例转账业务表
|
||||
PayDemoTransferDO demoTransfer = PayDemoTransferConvert.INSTANCE.convert(vo)
|
||||
.setUserId(userId).setTransferStatus(WAITING.getStatus());
|
||||
.setUserId(userId).setAppId(TRANSFER_APP_ID).setTransferStatus(WAITING.getStatus());
|
||||
demoTransferMapper.insert(demoTransfer);
|
||||
return demoTransfer.getId();
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
package cn.iocoder.yudao.module.pay.service.transfer;
|
||||
|
||||
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferCreateReqVO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
@ -14,7 +14,7 @@ import javax.validation.Valid;
|
||||
public interface PayTransferService {
|
||||
|
||||
/**
|
||||
* 提交转账单
|
||||
* 发起转账
|
||||
*
|
||||
* 此时,会发起支付渠道的调用
|
||||
*
|
||||
@ -22,7 +22,7 @@ public interface PayTransferService {
|
||||
* @param userIp 用户 ip
|
||||
* @return 渠道的返回结果
|
||||
*/
|
||||
PayTransferSubmitRespVO submitTransfer(@Valid PayTransferSubmitReqVO reqVO, String userIp);
|
||||
PayTransferDO initiateTransfer(@Valid PayTransferCreateReqVO reqVO, String userIp);
|
||||
|
||||
/**
|
||||
* 创建转账单
|
||||
@ -32,4 +32,10 @@ public interface PayTransferService {
|
||||
*/
|
||||
Long createTransfer(@Valid PayTransferCreateReqDTO reqDTO);
|
||||
|
||||
/**
|
||||
* 获取转账单
|
||||
* @param id 转账单编号
|
||||
*/
|
||||
PayTransferDO getTransfer(Long id);
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,20 @@
|
||||
package cn.iocoder.yudao.module.pay.service.transfer;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
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.dto.transfer.PayTransferRespDTO;
|
||||
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.PayTransferTypeEnum;
|
||||
import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitReqVO;
|
||||
import cn.iocoder.yudao.module.pay.controller.admin.transfer.vo.PayTransferSubmitRespVO;
|
||||
import cn.iocoder.yudao.module.pay.convert.transfer.PayTransferConvert;
|
||||
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.transfer.PayTransferDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.dataobject.transfer.PayTransferExtensionDO;
|
||||
import cn.iocoder.yudao.module.pay.dal.mysql.transfer.PayTransferExtensionMapper;
|
||||
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.enums.transfer.PayTransferStatusEnum;
|
||||
@ -28,8 +25,10 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Validator;
|
||||
|
||||
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.enums.ErrorCodeConstants.*;
|
||||
import static cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum.*;
|
||||
|
||||
@ -47,63 +46,72 @@ public class PayTransferServiceImpl implements PayTransferService {
|
||||
|
||||
@Resource
|
||||
private PayTransferMapper transferMapper;
|
||||
@Resource
|
||||
private PayTransferExtensionMapper transferExtensionMapper;
|
||||
|
||||
@Resource
|
||||
private PayAppService appService;
|
||||
@Resource
|
||||
private PayChannelService channelService;
|
||||
|
||||
@Resource
|
||||
private PayNoRedisDAO noRedisDAO;
|
||||
@Resource
|
||||
private Validator validator;
|
||||
|
||||
@Override
|
||||
public PayTransferSubmitRespVO submitTransfer(PayTransferSubmitReqVO reqVO, String userIp) {
|
||||
// 1.1 校验转账单是否可以提交
|
||||
PayTransferDO transfer = validateTransferCanSubmit(reqVO.getId());
|
||||
// 1.2 校验转账类型和渠道是否匹配
|
||||
validateChannelCodeAndTypeMatch(reqVO.getChannelCode(), transfer.getType());
|
||||
// 1.3 校验支付渠道是否有效
|
||||
PayChannelDO channel = validateChannelCanSubmit(transfer.getAppId(), reqVO.getChannelCode());
|
||||
PayClient client = channelService.getPayClient(channel.getId());
|
||||
|
||||
// 2. 新增转账拓展单
|
||||
String no = noRedisDAO.generate(TRANSFER_NO_PREFIX);
|
||||
PayTransferExtensionDO transferExtension = new PayTransferExtensionDO().setNo(no)
|
||||
.setTransferId(transfer.getId()).setChannelId(channel.getId())
|
||||
.setChannelCode(channel.getCode()).setStatus(WAITING.getStatus());
|
||||
transferExtensionMapper.insert(transferExtension);
|
||||
|
||||
// 3. 调用三方渠道发起转账
|
||||
PayTransferUnifiedReqDTO transferUnifiedReq = new PayTransferUnifiedReqDTO()
|
||||
.setOutTransferNo(transferExtension.getNo()).setPrice(transfer.getPrice())
|
||||
.setType(transfer.getType()).setTitle(transfer.getSubject())
|
||||
.setUserIp(userIp)
|
||||
.setChannelExtras(reqVO.getChannelExtras());
|
||||
PayTransferRespDTO unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq);
|
||||
|
||||
// 4. 通知转账结果
|
||||
getSelf().notifyTransfer(channel, unifiedTransferResp);
|
||||
// 如有渠道错误码,则抛出业务异常,提示用户
|
||||
if (StrUtil.isNotEmpty(unifiedTransferResp.getChannelErrorCode())) {
|
||||
throw exception(PAY_TRANSFER_SUBMIT_CHANNEL_ERROR, unifiedTransferResp.getChannelErrorCode(),
|
||||
unifiedTransferResp.getChannelErrorMsg());
|
||||
}
|
||||
return new PayTransferSubmitRespVO().setStatus(unifiedTransferResp.getStatus());
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PayTransferDO initiateTransfer(PayTransferCreateReqVO reqVO, String userIp) {
|
||||
// 1.1 校验参数
|
||||
reqVO.validate(validator);
|
||||
// todo 使用 AssertTrue
|
||||
validateChannelCodeAndTypeMatch(reqVO.getChannelCode(), reqVO.getType());
|
||||
// 1.2 校验转账单是否可以创建
|
||||
validateTransferCanCreate(reqVO.getAppId(), reqVO.getMerchantTransferId());
|
||||
// 2. 创建转账单,发起转账
|
||||
PayTransferCreateReqDTO req = INSTANCE.convert(reqVO).setUserIp(userIp);
|
||||
Long transferId = createTransfer(req);
|
||||
// 3. 返回转账单
|
||||
return getTransfer(transferId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Long createTransfer(PayTransferCreateReqDTO reqDTO) {
|
||||
// 校验 App
|
||||
appService.validPayApp(reqDTO.getAppId());
|
||||
// 创建转账单
|
||||
PayTransferDO transfer = PayTransferConvert.INSTANCE.convert(reqDTO)
|
||||
.setStatus(WAITING.getStatus());
|
||||
// 1.1 校验转账单是否可以提交
|
||||
validateTransferExist(reqDTO.getAppId(), reqDTO.getMerchantTransferId());
|
||||
// 1.2 校验 App
|
||||
PayAppDO appDO = appService.validPayApp(reqDTO.getAppId());
|
||||
// 1.3 校验支付渠道是否有效
|
||||
PayChannelDO channel = channelService.validPayChannel(reqDTO.getAppId(), reqDTO.getChannelCode());
|
||||
PayClient client = channelService.getPayClient(channel.getId());
|
||||
if (client == null) {
|
||||
log.error("[createTransfer][渠道编号({}) 找不到对应的支付客户端]", channel.getId());
|
||||
throw exception(CHANNEL_NOT_FOUND);
|
||||
}
|
||||
// 2.创建转账单
|
||||
String no = noRedisDAO.generate(TRANSFER_NO_PREFIX);
|
||||
PayTransferDO transfer = INSTANCE.convert(reqDTO)
|
||||
.setChannelId(channel.getId())
|
||||
.setNo(no).setStatus(WAITING.getStatus())
|
||||
.setNotifyUrl(appDO.getRefundNotifyUrl()); // TODO 需要加个transfer Notify url
|
||||
transferMapper.insert(transfer);
|
||||
try {
|
||||
// 3. 调用三方渠道发起转账
|
||||
PayTransferUnifiedReqDTO transferUnifiedReq = INSTANCE.convert2(reqDTO)
|
||||
.setOutTransferNo(no);
|
||||
PayTransferRespDTO unifiedTransferResp = client.unifiedTransfer(transferUnifiedReq);
|
||||
// 4. 通知转账结果
|
||||
getSelf().notifyTransfer(channel, unifiedTransferResp);
|
||||
} catch (PayException e) {
|
||||
// 这里仅打印异常,不进行抛出。
|
||||
// TODO 说明
|
||||
log.error("[createTransfer][转账 id({}) requestDTO({}) 发生异常]", transfer.getId(), reqDTO, e);
|
||||
}
|
||||
return transfer.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PayTransferDO getTransfer(Long id) {
|
||||
return transferMapper.selectById(id);
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
// 注意,如果是方法内调用该方法,需要通过 getSelf().notifyTransfer(channel, notify) 调用,否则事务不生效
|
||||
public void notifyTransfer(PayChannelDO channel, PayTransferRespDTO notify) {
|
||||
@ -119,26 +127,38 @@ public class PayTransferServiceImpl implements PayTransferService {
|
||||
// TODO IN_PROGRESS 待处理
|
||||
}
|
||||
|
||||
private void notifyTransferSuccess(PayChannelDO channel, PayTransferRespDTO notify) {
|
||||
// 1. 更新 PayTransferExtensionDO 转账成功
|
||||
PayTransferExtensionDO transferExtension = updateTransferExtensionSuccess(notify);
|
||||
private void validateTransferExist(Long appId, String merchantTransferId ) {
|
||||
PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantTransferId(appId, merchantTransferId);
|
||||
if (transfer != null) { // 是否存在
|
||||
throw exception(PAY_TRANSFER_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 更新 PayTransferDO 转账成功
|
||||
Boolean transferred = updateTransferSuccess(channel,transferExtension, notify);
|
||||
private void validateTransferCanCreate(Long appId, String merchantTransferId ) {
|
||||
PayTransferDO transfer = transferMapper.selectByAppIdAndMerchantTransferId(appId, merchantTransferId);
|
||||
if (transfer != null) { // 是否存在
|
||||
throw exception(PAY_MERCHANT_TRANSFER_EXISTS);
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyTransferSuccess(PayChannelDO channel, PayTransferRespDTO notify) {
|
||||
// 1. 更新 PayTransferDO 转账成功
|
||||
Boolean transferred = updateTransferSuccess(channel, notify);
|
||||
if (transferred) {
|
||||
return;
|
||||
}
|
||||
// 3. TODO 插入转账通知记录
|
||||
// 2. TODO 插入转账通知记录
|
||||
}
|
||||
|
||||
private Boolean updateTransferSuccess(PayChannelDO channel, PayTransferExtensionDO transferExtension,
|
||||
PayTransferRespDTO notify) {
|
||||
private Boolean updateTransferSuccess(PayChannelDO channel, PayTransferRespDTO notify) {
|
||||
// 1.校验
|
||||
PayTransferDO transfer = transferMapper.selectById(transferExtension.getTransferId());
|
||||
PayTransferDO transfer = transferMapper.selectByNo(notify.getOutTransferNo());
|
||||
if (transfer == null) {
|
||||
throw exception(PAY_TRANSFER_NOT_FOUND);
|
||||
}
|
||||
|
||||
if (isSuccess(transfer.getStatus())) { // 如果已成功,直接返回,不用重复更新
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
if (!isPendingStatus(transfer.getStatus())) {
|
||||
throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
|
||||
}
|
||||
@ -146,76 +166,52 @@ public class PayTransferServiceImpl implements PayTransferService {
|
||||
int updateCounts = transferMapper.updateByIdAndStatus(transfer.getId(),
|
||||
CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
|
||||
new PayTransferDO().setStatus(SUCCESS.getStatus()).setSuccessTime(notify.getSuccessTime())
|
||||
.setChannelTransferNo(notify.getChannelTransferNo())
|
||||
.setChannelId(channel.getId()).setChannelCode(channel.getCode())
|
||||
.setNo(transferExtension.getNo()));
|
||||
.setChannelNotifyData(JsonUtils.toJsonString(notify)));
|
||||
if (updateCounts == 0) {
|
||||
throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
|
||||
}
|
||||
log.info("[updateTransferSuccess][transfer({}) 更新为已转账]", transfer.getId());
|
||||
return false;
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
|
||||
private PayTransferExtensionDO updateTransferExtensionSuccess(PayTransferRespDTO notify) {
|
||||
// 1 校验
|
||||
PayTransferExtensionDO transferExtension = transferExtensionMapper.selectByNo(notify.getOutTransferNo());
|
||||
if (transferExtension == null) {
|
||||
throw exception(PAY_TRANSFER_EXTENSION_NOT_FOUND);
|
||||
private void updateTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) {
|
||||
// 1.校验
|
||||
PayTransferDO transfer = transferMapper.selectByNo(notify.getOutTransferNo());
|
||||
if (transfer == null) {
|
||||
throw exception(PAY_TRANSFER_NOT_FOUND);
|
||||
}
|
||||
if (isSuccess(transferExtension.getStatus())) { // 如果已成功,直接返回,不用重复更新
|
||||
log.info("[updateTransferExtensionSuccess][transferExtension({}) 已经是成功状态,无需更新]", transferExtension.getId());
|
||||
return transferExtension;
|
||||
if (isClosed(transfer.getStatus())) { // 如果已是关闭状态,直接返回,不用重复更新
|
||||
log.info("[updateTransferClosed][transfer({}) 已经是关闭状态,无需更新]", transfer.getId());
|
||||
return;
|
||||
}
|
||||
if (!isPendingStatus(transferExtension.getStatus())) {
|
||||
throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
|
||||
if (!isPendingStatus(transfer.getStatus())) {
|
||||
throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
|
||||
}
|
||||
// 2. 更新 PayTransferExtensionDO
|
||||
int updateCount = transferExtensionMapper.updateByIdAndStatus(transferExtension.getId(),
|
||||
// 2.更新
|
||||
int updateCount = transferMapper.updateByIdAndStatus(transfer.getId(),
|
||||
CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
|
||||
new PayTransferExtensionDO().setStatus(SUCCESS.getStatus())
|
||||
new PayTransferDO().setStatus(CLOSED.getStatus()) .setChannelId(channel.getId())
|
||||
.setChannelCode(channel.getCode()).setChannelTransferNo(notify.getChannelTransferNo())
|
||||
.setChannelErrorCode(notify.getChannelErrorCode()).setChannelErrorMsg(notify.getChannelErrorMsg())
|
||||
.setChannelNotifyData(JsonUtils.toJsonString(notify)));
|
||||
if (updateCount == 0) {
|
||||
throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
|
||||
throw exception(PAY_TRANSFER_STATUS_IS_NOT_PENDING);
|
||||
}
|
||||
log.info("[updateTransferExtensionSuccess][transferExtension({}) 更新为已转账]", transferExtension.getId());
|
||||
return transferExtension;
|
||||
log.info("[updateTransferClosed][transfer({}) 更新为关闭状态]", transfer.getId());
|
||||
}
|
||||
|
||||
private void notifyTransferClosed(PayChannelDO channel, PayTransferRespDTO notify) {
|
||||
// 更新 PayTransferExtensionDO 转账关闭
|
||||
updateTransferExtensionClosed(notify);
|
||||
}
|
||||
|
||||
private void updateTransferExtensionClosed(PayTransferRespDTO notify) {
|
||||
// 1 校验
|
||||
PayTransferExtensionDO transferExtension = transferExtensionMapper.selectByNo(notify.getOutTransferNo());
|
||||
if (transferExtension == null) {
|
||||
throw exception(PAY_TRANSFER_EXTENSION_NOT_FOUND);
|
||||
}
|
||||
if (isClosed(transferExtension.getStatus())) { // 如果已是关闭状态,直接返回,不用重复更新
|
||||
log.info("[updateTransferExtensionSuccess][transferExtension({}) 已经是关闭状态,无需更新]", transferExtension.getId());
|
||||
return;
|
||||
}
|
||||
if (!isPendingStatus(transferExtension.getStatus())) {
|
||||
throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
|
||||
}
|
||||
// 2. 更新 PayTransferExtensionDO
|
||||
int updateCount = transferExtensionMapper.updateByIdAndStatus(transferExtension.getId(),
|
||||
CollUtil.newArrayList(WAITING.getStatus(), IN_PROGRESS.getStatus()),
|
||||
new PayTransferExtensionDO().setStatus(CLOSED.getStatus())
|
||||
.setChannelNotifyData(JsonUtils.toJsonString(notify)));
|
||||
if (updateCount == 0) {
|
||||
throw exception(PAY_TRANSFER_EXTENSION_STATUS_IS_NOT_PENDING);
|
||||
}
|
||||
log.info("[updateTransferExtensionSuccess][transferExtension({}) 更新为关闭状态]", transferExtension.getId());
|
||||
// 更新 PayTransferDO 转账关闭
|
||||
updateTransferClosed(channel,notify);
|
||||
}
|
||||
|
||||
private void validateChannelCodeAndTypeMatch(String channelCode, Integer type) {
|
||||
PayTransferTypeEnum transferType = PayTransferTypeEnum.typeOf(type);
|
||||
PayChannelEnum payChannel = PayChannelEnum.getByCode(channelCode);
|
||||
switch (transferType) {
|
||||
case ALIPAY_BALANCE: {
|
||||
// TODO @jason:可以抽到 PayChannelEnum 里,isAlipay? 类似这种哈
|
||||
if (!payChannel.getCode().startsWith("alipay")) {
|
||||
if (!PayChannelEnum.isAlipay(channelCode)) {
|
||||
throw exception(PAY_TRANSFER_TYPE_AND_CHANNEL_NOT_MATCH);
|
||||
}
|
||||
break;
|
||||
@ -241,7 +237,7 @@ public class PayTransferServiceImpl implements PayTransferService {
|
||||
return channel;
|
||||
}
|
||||
|
||||
private PayTransferDO validateTransferCanSubmit(Long id) {
|
||||
private PayTransferDO validateTransferExist(Long id) {
|
||||
PayTransferDO transfer = transferMapper.selectById(id);
|
||||
if (transfer == null) { // 是否存在
|
||||
throw exception(PAY_TRANSFER_NOT_FOUND);
|
||||
|
Reference in New Issue
Block a user