diff --git a/daxpay-single-sdk/src/test/java/cn/daxpay/single/sdk/allocation/AllocationTest.java b/daxpay-single-sdk/src/test/java/cn/daxpay/single/sdk/allocation/AllocationTest.java index 329f551aa..e51b65ba5 100644 --- a/daxpay-single-sdk/src/test/java/cn/daxpay/single/sdk/allocation/AllocationTest.java +++ b/daxpay-single-sdk/src/test/java/cn/daxpay/single/sdk/allocation/AllocationTest.java @@ -9,6 +9,7 @@ import cn.daxpay.single.sdk.model.sync.AllocSyncModel; import cn.daxpay.single.sdk.net.DaxPayConfig; import cn.daxpay.single.sdk.net.DaxPayKit; import cn.daxpay.single.sdk.param.allocation.AllocFinishParam; +import cn.daxpay.single.sdk.param.allocation.AllocReceiverParam; import cn.daxpay.single.sdk.param.allocation.AllocSyncParam; import cn.daxpay.single.sdk.param.allocation.AllocationParam; import cn.daxpay.single.sdk.param.pay.PayParam; @@ -18,6 +19,8 @@ import cn.hutool.json.JSONUtil; import org.junit.Before; import org.junit.Test; +import java.util.Collections; + /** * 支付分账测试 * @author xxm @@ -53,8 +56,6 @@ public class AllocationTest { param.setMethod(PayMethodEnum.QRCODE.getCode()); param.setAttach("{回调参数}"); param.setAllocation(true); -// param.setReturnUrl("https://abc.com/returnurl"); -// param.setNotifyUrl("https://abc.com/callback"); DaxPayResult execute = DaxPayKit.execute(param); System.out.println(JSONUtil.toJsonStr(execute)); @@ -79,8 +80,6 @@ public class AllocationTest { param.setAttach("{回调参数}"); param.setAllocation(true); param.setAutoAllocation(true); -// param.setReturnUrl("https://abc.com/returnurl"); -// param.setNotifyUrl("https://abc.com/callback"); DaxPayResult execute = DaxPayKit.execute(param); System.out.println(JSONUtil.toJsonStr(execute)); @@ -104,6 +103,45 @@ public class AllocationTest { System.out.println(JSONUtil.toJsonStr(execute)); } + /** + * 手动发起分账, 使用指定分账组 + */ + @Test + public void allocationOpenByGroup() { + // 分账参数 + AllocationParam param = new AllocationParam(); + param.setBizAllocationNo("A"+ RandomUtil.randomNumbers(5)); + param.setAttach("88899"); + param.setBizOrderNo("SDK_1717257064753"); + param.setGroupNo("123"); + param.setDescription("测试分账"); + param.setClientIp("127.0.0.1"); + + DaxPayResult execute = DaxPayKit.execute(param); + System.out.println(JSONUtil.toJsonStr(execute)); + } + + /** + * 手动发起分账, 手动传入接收方 + */ + @Test + public void allocationOpenByReceivers() { + // 分账参数 + AllocationParam param = new AllocationParam(); + param.setBizAllocationNo("A"+ RandomUtil.randomNumbers(5)); + param.setAttach("88899"); + param.setOrderNo("DEVP24060123574963000003"); + AllocReceiverParam allocReceiverParam = new AllocReceiverParam(); + allocReceiverParam.setReceiverNo("123"); + allocReceiverParam.setAmount(6000); + param.setReceivers(Collections.singletonList(allocReceiverParam)); + param.setDescription("测试分账"); + param.setClientIp("127.0.0.1"); + + DaxPayResult execute = DaxPayKit.execute(param); + System.out.println(JSONUtil.toJsonStr(execute)); + } + /** * 分账完结 */ diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/payment/allocation/AllocReceiverParam.java b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/payment/allocation/AllocReceiverParam.java index 15901f541..7ab1fb8e1 100644 --- a/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/payment/allocation/AllocReceiverParam.java +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/daxpay/single/param/payment/allocation/AllocReceiverParam.java @@ -6,6 +6,7 @@ import lombok.experimental.Accessors; import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; /** * 分账接收方列表 @@ -24,7 +25,7 @@ public class AllocReceiverParam { /** 分账金额 */ @Schema(description = "分账金额") - @NotEmpty(message = "分账金额必填") + @NotNull(message = "分账金额必填") @Min(value = 1,message = "分账金额至少为0.01元") private Integer amount; } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/order/allocation/service/AllocationOrderService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/order/allocation/service/AllocationOrderService.java index fce3cd667..c6cf4b27c 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/order/allocation/service/AllocationOrderService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/order/allocation/service/AllocationOrderService.java @@ -32,6 +32,7 @@ import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -106,12 +107,19 @@ public class AllocationOrderService { Map receiverNoMap = param.getReceivers() .stream() .collect(Collectors.toMap(AllocReceiverParam::getReceiverNo, AllocReceiverParam::getAmount)); - // 查询分账接收方信息 List receivers = receiverManager.findAllByReceiverNos(receiverNos); if (receivers.size() != receiverNos.size()){ - throw new PayFailureException("分账接收方列表存在无效的分账接收方"); + throw new PayFailureException("分账接收方列表存在重复或无效的数据"); } + // 判断分账接收方类型是否都与分账订单类型匹配 + boolean anyMatch = receivers.stream() + .anyMatch(o -> !Objects.equals(o.getChannel(), payOrder.getChannel())); + if (anyMatch){ + throw new PayFailureException("分账接收方列表存在非本通道的数据"); + } + + long allocId = IdUtil.getSnowflakeNextId(); // 订单明细 diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/allocation/service/AllocationService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/allocation/service/AllocationService.java index 5be391862..6eecbb28c 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/allocation/service/AllocationService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/daxpay/single/service/core/payment/allocation/service/AllocationService.java @@ -4,6 +4,7 @@ import cn.bootx.platform.common.core.exception.DataNotExistException; import cn.bootx.platform.common.core.exception.RepetitiveOperationException; import cn.bootx.platform.common.core.util.CollUtil; import cn.daxpay.single.code.AllocDetailResultEnum; +import cn.daxpay.single.code.AllocOrderResultEnum; import cn.daxpay.single.code.AllocOrderStatusEnum; import cn.daxpay.single.code.PayOrderAllocStatusEnum; import cn.daxpay.single.exception.pay.PayFailureException; @@ -103,7 +104,7 @@ public class AllocationService { } try { // 构建分账订单相关信息 - OrderAndDetail orderAndDetail = this.createAlloc(param, payOrder); + OrderAndDetail orderAndDetail = this.checkAndCreateAlloc(param, payOrder); // 检查是否需要进行分账 AllocationOrder order = orderAndDetail.getOrder(); List details = orderAndDetail.getDetails(); @@ -125,6 +126,7 @@ public class AllocationService { allocationStrategy.allocation(); // 执行中 order.setStatus(AllocOrderStatusEnum.ALLOCATION_PROCESSING.getCode()) + .setResult(AllocOrderResultEnum.PART_SUCCESS.getCode()) .setErrorMsg(null); } catch (Exception e) { log.error("分账出现错误:", e); @@ -285,20 +287,26 @@ public class AllocationService { /** * 构建分账订单相关信息 */ - private OrderAndDetail createAlloc(AllocationParam param, PayOrder payOrder){ + private OrderAndDetail checkAndCreateAlloc(AllocationParam param, PayOrder payOrder){ // 创建分账单和明细并保存, 同时更新支付订单状态 使用事务 OrderAndDetail orderAndDetail; // 判断是否传输了分账接收方列表 if (CollUtil.isNotEmpty(param.getReceivers())) { orderAndDetail = allocationOrderService.createAndUpdate(param, payOrder); - } else if (Objects.nonNull(param.getGroupNo())){ - // 指定分账组 - AllocationGroup allocationGroup = groupManager.findByGroupNo(param.getGroupNo()).orElseThrow(() -> new DataNotExistException("未查询到分账组")); - List receiversByGroups = allocationGroupService.findReceiversByGroups(allocationGroup.getId()); - orderAndDetail = allocationOrderService.createAndUpdate(param ,payOrder, receiversByGroups); } else { - // 默认分账组 - AllocationGroup allocationGroup = groupManager.findDefaultGroup(payOrder.getChannel()).orElseThrow(() -> new PayFailureException("未查询到默认分账组")); + AllocationGroup allocationGroup; + if (Objects.nonNull(param.getGroupNo())){ + // 指定分账组 + allocationGroup = groupManager.findByGroupNo(param.getGroupNo()).orElseThrow(() -> new DataNotExistException("未查询到分账组")); + } else { + // 默认分账组 + allocationGroup = groupManager.findDefaultGroup(payOrder.getChannel()).orElseThrow(() -> new PayFailureException("未查询到默认分账组")); + } + // 判断通道类型是否一致 + if (!Objects.equals(allocationGroup.getChannel(), payOrder.getChannel())){ + throw new PayFailureException("分账接收方列表存在非本通道的数据"); + } + List receiversByGroups = allocationGroupService.findReceiversByGroups(allocationGroup.getId()); orderAndDetail = allocationOrderService.createAndUpdate(param ,payOrder, receiversByGroups); }