feat 分账同步

This commit is contained in:
xxm1995
2024-04-12 18:01:19 +08:00
parent eecc228399
commit 625c5c301a
21 changed files with 294 additions and 49 deletions

View File

@@ -1,29 +0,0 @@
package cn.bootx.platform.daxpay.service.code;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 分账状态枚举
* @author xxm
* @since 2024/4/7
*/
@Getter
@AllArgsConstructor
public enum AllocationStatusEnum {
// 待分账
WAITING("waiting", "待分账"),
PARTIAL_PROCESSING("partial_processing", "分账处理中"),
PARTIAL_SUCCESS("partial_success", "部分分账完成"),
FINISH_PROCESSING("finish_processing", "分账完结处理中"),
FINISH_SUCCESS("finish_success", "分账完成"),
PARTIAL_FAILED("partial_failed", "部分分账完成"),
FINISH_FAILED("finish_failed", "分账失败"),
CLOSED("closed", "分账关闭"),
UNKNOWN("unknown", "分账状态未知");
final String code;
final String name;
}

View File

@@ -82,7 +82,7 @@ public class AliPayAllocationService {
* 分账状态查询
*/
@SneakyThrows
public void query(AllocationOrder allocationOrder){
public void queryStatus(AllocationOrder allocationOrder){
AlipayTradeOrderSettleQueryModel model = new AlipayTradeOrderSettleQueryModel();
model.setTradeNo(allocationOrder.getGatewayPayOrderNo());
model.setOutRequestNo(allocationOrder.getOrderNo());
@@ -95,6 +95,13 @@ public class AliPayAllocationService {
System.out.println(royaltyDetailList);
}
/**
* 分账剩余金额查询
*/
public void queryAmount(AllocationOrder allocationOrder){
}
/**
* 验证错误信息
*/

View File

@@ -92,6 +92,24 @@ public class WeChatPayAllocationService {
this.verifyErrorMsg(result);
}
/**
* 查询分账状态
*/
public void queryStatus(AllocationOrder allocationOrder, WeChatPayConfig config){
Map<String, String> params = ProfitSharingModel.builder()
.mch_id(config.getWxMchId())
.appid(config.getWxAppId())
.nonce_str(WxPayKit.generateStr())
.transaction_id(allocationOrder.getGatewayPayOrderNo())
.out_order_no(allocationOrder.getOrderNo())
.build()
.createSign(config.getApiKeyV2(), SignType.HMACSHA256);
String xmlResult = WxPayApi.profitSharingQuery(params);
Map<String, String> result = WxPayKit.xmlToMap(xmlResult);
this.verifyErrorMsg(result);
}
/**
* 验证错误信息
*/

View File

@@ -2,8 +2,8 @@ package cn.bootx.platform.daxpay.service.core.order.allocation.entity;
import cn.bootx.platform.common.core.function.EntityBaseFunction;
import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.core.order.allocation.convert.AllocationConvert;
import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDto;
import cn.bootx.table.modify.annotation.DbColumn;
@@ -33,11 +33,19 @@ import java.time.LocalDateTime;
public class AllocationOrder extends MpBaseEntity implements EntityBaseFunction<AllocationOrderDto> {
/**
* 分账订单号
* 分账订单号(传输给三方支付系统做关联)
*/
@DbColumn(comment = "分账订单号")
private String orderNo;
/**
* 分账单号
*/
@DbMySqlIndex(comment = "分账单号索引", type = MySqlIndexType.UNIQUE)
@DbColumn(comment = "分账单号")
private String allocationNo;
/**
* 支付订单ID
*/
@@ -62,14 +70,6 @@ public class AllocationOrder extends MpBaseEntity implements EntityBaseFunction<
@DbColumn(comment = "网关分账单号")
private String gatewayAllocationNo;
/**
* 分账单号
*/
@DbMySqlIndex(comment = "分账单号索引", type = MySqlIndexType.UNIQUE)
@DbColumn(comment = "分账单号")
private String allocationNo;
/**
* 所属通道
* @see PayChannelEnum

View File

@@ -8,7 +8,7 @@ import cn.bootx.platform.common.core.util.ResultConvertUtil;
import cn.bootx.platform.common.mybatisplus.util.MpUtil;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.param.pay.allocation.AllocationStartParam;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.core.order.allocation.dao.AllocationOrderDetailManager;
import cn.bootx.platform.daxpay.service.core.order.allocation.dao.AllocationOrderManager;
import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrder;

View File

@@ -4,7 +4,7 @@ import cn.bootx.platform.common.core.function.EntityBaseFunction;
import cn.bootx.platform.common.mybatisplus.base.MpBaseEntity;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.code.PayStatusEnum;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.core.order.pay.convert.PayOrderConvert;
import cn.bootx.platform.daxpay.service.dto.order.pay.PayOrderDto;
import cn.bootx.table.modify.annotation.DbColumn;

View File

@@ -0,0 +1,39 @@
package cn.bootx.platform.daxpay.service.core.payment.allocation.factory;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.exception.pay.PayUnsupportedMethodException;
import cn.bootx.platform.daxpay.service.core.payment.sync.strategy.allocation.AliPayAllocationSyncStrategy;
import cn.bootx.platform.daxpay.service.core.payment.sync.strategy.allocation.WechatPayAllocationSyncStrategy;
import cn.bootx.platform.daxpay.service.func.AbsAllocationSyncStrategy;
import cn.hutool.extra.spring.SpringUtil;
import lombok.experimental.UtilityClass;
/**
* 分账同步策略工厂
* @author xxm
* @since 2024/4/12
*/
@UtilityClass
public class AllocationSyncFactory {
/**
* 根据传入的支付通道创建策略
* @return 支付策略
*/
public static AbsAllocationSyncStrategy create(String channel) {
PayChannelEnum channelEnum = PayChannelEnum.findByCode(channel);
AbsAllocationSyncStrategy strategy;
switch (channelEnum) {
case ALI:
strategy = SpringUtil.getBean(AliPayAllocationSyncStrategy.class);
break;
case WECHAT:
strategy = SpringUtil.getBean(WechatPayAllocationSyncStrategy.class);
break;
default:
throw new PayUnsupportedMethodException();
}
return strategy;
}
}

View File

@@ -5,7 +5,7 @@ import cn.bootx.platform.daxpay.exception.pay.PayFailureException;
import cn.bootx.platform.daxpay.param.pay.allocation.AllocationFinishParam;
import cn.bootx.platform.daxpay.param.pay.allocation.AllocationStartParam;
import cn.bootx.platform.daxpay.result.allocation.AllocationResult;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.core.order.allocation.dao.AllocationOrderManager;
import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrder;
import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrderDetail;

View File

@@ -0,0 +1,50 @@
package cn.bootx.platform.daxpay.service.core.payment.allocation.service;
import cn.bootx.platform.common.core.exception.DataNotExistException;
import cn.bootx.platform.daxpay.param.pay.AllocationSyncParam;
import cn.bootx.platform.daxpay.result.pay.SyncResult;
import cn.bootx.platform.daxpay.service.core.order.allocation.dao.AllocationOrderManager;
import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrder;
import cn.bootx.platform.daxpay.service.core.payment.allocation.factory.AllocationSyncFactory;
import cn.bootx.platform.daxpay.service.func.AbsAllocationSyncStrategy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Objects;
/**
* 分账状态同步
* @author xxm
* @since 2024/4/12
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class AllocationSyncService {
private final AllocationOrderManager allocationOrderManager;
/**
* 支付同步, 开启一个新的事务, 不受外部抛出异常的影响
*/
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
public SyncResult sync(AllocationSyncParam param) {
// 获取分账订单
AllocationOrder allocationOrder = null;
if (Objects.nonNull(param.getAllocationId())){
allocationOrder = allocationOrderManager.findById(param.getAllocationId())
.orElseThrow(() -> new DataNotExistException("分账单不存在"));
}
if (Objects.isNull(allocationOrder)){
allocationOrder = allocationOrderManager.findByAllocationNo(param.getAllocationNo())
.orElseThrow(() -> new DataNotExistException("分账单不存在"));
}
// 获取分账策略
AbsAllocationSyncStrategy allocationSyncStrategy = AllocationSyncFactory.create(allocationOrder.getChannel());
allocationSyncStrategy.initPayParam(allocationOrder);
allocationSyncStrategy.doSyncStatus();
return new SyncResult();
}
}

View File

@@ -68,4 +68,5 @@ public class WeChatPayAllocationStrategy extends AbsAllocationStrategy {
weChatPayAllocationService.finish(getAllocationOrder(), weChatPayConfig);
}
}

View File

@@ -7,7 +7,7 @@ import cn.bootx.platform.daxpay.param.pay.PayChannelParam;
import cn.bootx.platform.daxpay.param.pay.PayParam;
import cn.bootx.platform.daxpay.param.pay.SimplePayParam;
import cn.bootx.platform.daxpay.result.pay.PayResult;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.service.common.context.PayLocal;
import cn.bootx.platform.daxpay.service.common.local.PaymentContextLocal;
import cn.bootx.platform.daxpay.service.core.order.pay.builder.PayBuilder;

View File

@@ -0,0 +1,44 @@
package cn.bootx.platform.daxpay.service.core.payment.sync.strategy.allocation;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.service.core.channel.alipay.entity.AliPayConfig;
import cn.bootx.platform.daxpay.service.core.channel.alipay.service.AliPayAllocationService;
import cn.bootx.platform.daxpay.service.core.channel.alipay.service.AliPayConfigService;
import cn.bootx.platform.daxpay.service.func.AbsAllocationSyncStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;
/**
* 支付宝分账状态同步策略
* @author xxm
* @since 2024/4/12
*/
@Scope(SCOPE_PROTOTYPE)
@Component
@RequiredArgsConstructor
public class AliPayAllocationSyncStrategy extends AbsAllocationSyncStrategy {
private final AliPayConfigService aliPayConfigService;
private final AliPayAllocationService aliPayAllocationService;
/**
* 策略标识
*/
@Override
public PayChannelEnum getChannel() {
return PayChannelEnum.ALI;
}
/**
* 同步状态
*/
@Override
public void doSyncStatus() {
AliPayConfig aliPayConfig = aliPayConfigService.getAndCheckConfig();
aliPayConfigService.initConfig(aliPayConfig);
aliPayAllocationService.queryStatus(this.getAllocationOrder());
}
}

View File

@@ -0,0 +1,45 @@
package cn.bootx.platform.daxpay.service.core.payment.sync.strategy.allocation;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.service.core.channel.wechat.entity.WeChatPayConfig;
import cn.bootx.platform.daxpay.service.core.channel.wechat.service.WeChatPayAllocationService;
import cn.bootx.platform.daxpay.service.core.channel.wechat.service.WeChatPayConfigService;
import cn.bootx.platform.daxpay.service.func.AbsAllocationSyncStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;
/**
* 微信分账状态同步策略
* @author xxm
* @since 2024/4/12
*/
@Scope(SCOPE_PROTOTYPE)
@Component
@RequiredArgsConstructor
public class WechatPayAllocationSyncStrategy extends AbsAllocationSyncStrategy {
private final WeChatPayAllocationService weChatPayAllocationService;
private final WeChatPayConfigService weChatPayConfigService;
/**
* 同步状态
*/
@Override
public void doSyncStatus() {
WeChatPayConfig wechatPayConfig = weChatPayConfigService.getAndCheckConfig();
weChatPayAllocationService.queryStatus(this.getAllocationOrder(),wechatPayConfig);
}
/**
* 策略标识
*/
@Override
public PayChannelEnum getChannel() {
return PayChannelEnum.WECHAT;
}
}

View File

@@ -1,6 +1,7 @@
package cn.bootx.platform.daxpay.service.dto.order.allocation;
import cn.bootx.platform.common.core.rest.dto.BaseDto;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.v3.oas.annotations.media.Schema;
@@ -77,7 +78,7 @@ public class AllocationOrderDto extends BaseDto {
/**
* 状态
* @see cn.bootx.platform.daxpay.service.code.AllocationStatusEnum
* @see AllocationStatusEnum
*/
@Schema(description = "状态")
private String status;

View File

@@ -3,7 +3,7 @@ package cn.bootx.platform.daxpay.service.dto.order.pay;
import cn.bootx.platform.common.core.rest.dto.BaseDto;
import cn.bootx.platform.daxpay.code.PayChannelEnum;
import cn.bootx.platform.daxpay.code.PayStatusEnum;
import cn.bootx.platform.daxpay.service.code.AllocationStatusEnum;
import cn.bootx.platform.daxpay.code.AllocationStatusEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

View File

@@ -0,0 +1,33 @@
package cn.bootx.platform.daxpay.service.func;
import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* 抽象的分账状态同步策略
* @author xxm
* @since 2024/4/12
*/
@Slf4j
@Getter
@Setter
public abstract class AbsAllocationSyncStrategy implements PayStrategy{
private AllocationOrder allocationOrder;
/**
* 初始化参数
*/
public void initPayParam(AllocationOrder allocationOrder) {
this.allocationOrder = allocationOrder;
}
/**
* 同步状态
*/
public abstract void doSyncStatus();
}