From e0c495d94b7bc930e887063fce5be10360168a74 Mon Sep 17 00:00:00 2001 From: xxm1995 Date: Mon, 15 Apr 2024 18:30:38 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=88=86=E8=B4=A6=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++-- .../allocation/AllocationOrderController.java | 12 ++++++ .../pay/allocation/AllocationResetParam.java | 22 +++++++++++ .../daxpay/service/code/AliPayCode.java | 3 ++ .../common/context/AllocationLocal.java | 17 +++++++++ .../common/context/PaymentContext.java | 3 ++ .../service/AliPayAllocationService.java | 20 ++++++++-- .../service/WeChatPayAllocationService.java | 4 +- .../result/AllocationGatewaySyncResult.java | 17 +++++++++ .../allocation/service/AllocationService.java | 37 ++++++++++++++++++- .../allocation/AliPayAllocationStrategy.java | 2 +- .../WeChatPayAllocationStrategy.java | 4 +- 12 files changed, 139 insertions(+), 14 deletions(-) create mode 100644 daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/param/pay/allocation/AllocationResetParam.java create mode 100644 daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/AllocationLocal.java create mode 100644 daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/result/AllocationGatewaySyncResult.java diff --git a/README.md b/README.md index e94c2f58..0d39c19c 100644 --- a/README.md +++ b/README.md @@ -179,15 +179,21 @@ public class SimplePayOrderTest { ## 🥪 关于我们 -钉钉扫码加入钉钉交流群(推荐) +扫码加入QQ交流群 +

+ +

+ + +扫码加入钉钉交流群

-QQ扫码加入QQ交流群 +扫码加入飞书交流群

- +

diff --git a/daxpay-single/daxpay-single-admin/src/main/java/cn/bootx/platform/daxpay/admin/controller/allocation/AllocationOrderController.java b/daxpay-single/daxpay-single-admin/src/main/java/cn/bootx/platform/daxpay/admin/controller/allocation/AllocationOrderController.java index 391f94fe..77173adb 100644 --- a/daxpay-single/daxpay-single-admin/src/main/java/cn/bootx/platform/daxpay/admin/controller/allocation/AllocationOrderController.java +++ b/daxpay-single/daxpay-single-admin/src/main/java/cn/bootx/platform/daxpay/admin/controller/allocation/AllocationOrderController.java @@ -6,7 +6,9 @@ import cn.bootx.platform.common.core.rest.ResResult; import cn.bootx.platform.common.core.rest.dto.LabelValue; import cn.bootx.platform.common.core.rest.param.PageParam; import cn.bootx.platform.daxpay.param.pay.AllocationSyncParam; +import cn.bootx.platform.daxpay.param.pay.allocation.AllocationFinishParam; import cn.bootx.platform.daxpay.service.core.order.allocation.service.AllocationOrderService; +import cn.bootx.platform.daxpay.service.core.payment.allocation.service.AllocationService; import cn.bootx.platform.daxpay.service.core.payment.allocation.service.AllocationSyncService; import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDetailDto; import cn.bootx.platform.daxpay.service.dto.order.allocation.AllocationOrderDto; @@ -35,6 +37,7 @@ public class AllocationOrderController { private final AllocationOrderService allocationOrderService; private final AllocationSyncService allocationSyncService; + private final AllocationService allocationService; @Operation(summary = "分页") @GetMapping("/page") @@ -75,4 +78,13 @@ public class AllocationOrderController { allocationSyncService.sync(param); return Res.ok(); } + + @Operation(summary = "分账完结") + @PostMapping("/finish") + public ResResult finish(Long id){ + AllocationFinishParam param = new AllocationFinishParam(); + param.setOrderId(id); + allocationService.finish(param); + return Res.ok(); + } } diff --git a/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/param/pay/allocation/AllocationResetParam.java b/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/param/pay/allocation/AllocationResetParam.java new file mode 100644 index 00000000..3dbf900c --- /dev/null +++ b/daxpay-single/daxpay-single-core/src/main/java/cn/bootx/platform/daxpay/param/pay/allocation/AllocationResetParam.java @@ -0,0 +1,22 @@ +package cn.bootx.platform.daxpay.param.pay.allocation; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 重新分账参数 + * @author xxm + * @since 2024/4/15 + */ +@Data +@Accessors(chain = true) +@Schema(title = "重新分账参数") +public class AllocationResetParam { + + @Schema(description = "分账订单ID") + private Long orderId; + + @Schema(description = "分账单号") + private String allocationNo; +} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/code/AliPayCode.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/code/AliPayCode.java index 5af159df..2eddbdee 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/code/AliPayCode.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/code/AliPayCode.java @@ -102,4 +102,7 @@ public interface AliPayCode { /** 分账接收方不存在 */ String USER_NOT_EXIST = "USER_NOT_EXIST"; + /** 分账金额超过最大可分账金额 */ + String ALLOC_AMOUNT_VALIDATE_ERROR = "ACQ.ALLOC_AMOUNT_VALIDATE_ERROR"; + } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/AllocationLocal.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/AllocationLocal.java new file mode 100644 index 00000000..e043239a --- /dev/null +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/AllocationLocal.java @@ -0,0 +1,17 @@ +package cn.bootx.platform.daxpay.service.common.context; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 分账上下文 + * @author xxm + * @since 2024/4/15 + */ +@Data +@Accessors(chain = true) +public class AllocationLocal { + + /** 网关分账号 */ + private String gatewayNo; +} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/PaymentContext.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/PaymentContext.java index b5110f2e..9b75c9be 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/PaymentContext.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/common/context/PaymentContext.java @@ -42,4 +42,7 @@ public class PaymentContext { /** 对账相关信息 */ private final ReconcileLocal reconcileInfo = new ReconcileLocal(); + /** 分账相关信息 */ + private final AllocationLocal allocationInfo = new AllocationLocal(); + } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/alipay/service/AliPayAllocationService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/alipay/service/AliPayAllocationService.java index 5c37c056..b8e4e889 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/alipay/service/AliPayAllocationService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/alipay/service/AliPayAllocationService.java @@ -2,6 +2,7 @@ package cn.bootx.platform.daxpay.service.core.channel.alipay.service; import cn.bootx.platform.daxpay.exception.pay.PayFailureException; import cn.bootx.platform.daxpay.service.code.AliPayCode; +import cn.bootx.platform.daxpay.service.common.local.PaymentContextLocal; import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrder; import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrderDetail; import cn.hutool.core.util.StrUtil; @@ -35,7 +36,6 @@ public class AliPayAllocationService { */ @SneakyThrows public void allocation(AllocationOrder allocationOrder, List orderDetails){ - // 分账主体参数 AlipayTradeOrderSettleModel model = new AlipayTradeOrderSettleModel(); model.setOutRequestNo(String.valueOf(allocationOrder.getOrderNo())); @@ -54,26 +54,38 @@ public class AliPayAllocationService { model.setRoyaltyParameters(royaltyParameters); AlipayTradeOrderSettleResponse response = AliPayApi.tradeOrderSettleToResponse(model); - // TODO 需要写入到分账订单中 + // 需要写入到分账订单中 String settleNo = response.getSettleNo(); + PaymentContextLocal.get().getAllocationInfo().setGatewayNo(settleNo); this.verifyErrorMsg(response); - } /** * 分账完结 */ @SneakyThrows - public void finish(AllocationOrder allocationOrder){ + public void finish(AllocationOrder allocationOrder, List orderDetails ){ // 分账主体参数 AlipayTradeOrderSettleModel model = new AlipayTradeOrderSettleModel(); model.setOutRequestNo(String.valueOf(allocationOrder.getOrderNo())); model.setTradeNo(allocationOrder.getGatewayPayOrderNo()); + model.setRoyaltyMode("async"); // 分账完结参数 SettleExtendParams extendParams = new SettleExtendParams(); extendParams.setRoyaltyFinish("true"); model.setExtendParams(extendParams); + // 分账子参数 + List royaltyParameters = orderDetails.stream() + .map(o -> { + OpenApiRoyaltyDetailInfoPojo infoPojo = new OpenApiRoyaltyDetailInfoPojo(); + infoPojo.setAmount(String.valueOf(o.getAmount() / 100.0)); + infoPojo.setTransIn(o.getReceiverAccount()); + return infoPojo; + }) + .collect(Collectors.toList()); + model.setRoyaltyParameters(royaltyParameters); + AlipayTradeOrderSettleResponse response = AliPayApi.tradeOrderSettleToResponse(model); this.verifyErrorMsg(response); } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wechat/service/WeChatPayAllocationService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wechat/service/WeChatPayAllocationService.java index 76fd837d..d95fd355 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wechat/service/WeChatPayAllocationService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/channel/wechat/service/WeChatPayAllocationService.java @@ -59,7 +59,7 @@ public class WeChatPayAllocationService { .appid(config.getWxAppId()) .nonce_str(WxPayKit.generateStr()) .transaction_id(allocationOrder.getGatewayPayOrderNo()) - .out_order_no(WxPayKit.generateStr()) + .out_order_no(allocationOrder.getOrderNo()) .receivers(JSON.toJSONString(list)) .build() .createSign(config.getApiKeyV2(), SignType.HMACSHA256); @@ -74,7 +74,7 @@ public class WeChatPayAllocationService { /** * 完成分账 */ - public void finish(AllocationOrder allocationOrder, WeChatPayConfig config){ + public void finish(AllocationOrder allocationOrder, List allocationOrderDetails, WeChatPayConfig config){ Map params = ProfitSharingModel.builder() .mch_id(config.getWxMchId()) .appid(config.getWxAppId()) diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/result/AllocationGatewaySyncResult.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/result/AllocationGatewaySyncResult.java new file mode 100644 index 00000000..aeaa2ce5 --- /dev/null +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/result/AllocationGatewaySyncResult.java @@ -0,0 +1,17 @@ +package cn.bootx.platform.daxpay.service.core.payment.allocation.result; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 分账网关同步结果 + * @author xxm + * @since 2024/4/15 + */ +@Data +@Accessors(chain = true) +@Schema(title = "分账网关同步结果") +public class AllocationGatewaySyncResult { + +} diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/service/AllocationService.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/service/AllocationService.java index 37503ad2..7f6a8e72 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/service/AllocationService.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/service/AllocationService.java @@ -1,11 +1,13 @@ package cn.bootx.platform.daxpay.service.core.payment.allocation.service; import cn.bootx.platform.common.core.exception.DataNotExistException; +import cn.bootx.platform.daxpay.code.AllocationStatusEnum; 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.AllocationResetParam; import cn.bootx.platform.daxpay.param.pay.allocation.AllocationStartParam; import cn.bootx.platform.daxpay.result.allocation.AllocationResult; -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; import cn.bootx.platform.daxpay.service.core.order.allocation.entity.AllocationOrderDetail; @@ -50,6 +52,8 @@ public class AllocationService { private final AllocationOrderService allocationOrderService; + private final AllocationOrderDetailManager allocationOrderDetailManager; + /** * 开启分账, 使用分账组进行分账 */ @@ -99,6 +103,34 @@ public class AllocationService { .setStatus(order.getStatus()); } + /** + * 重新分账 + */ + public void resetAllocation(AllocationResetParam param){ + AllocationOrder allocationOrder; + if (Objects.nonNull(param.getOrderId())){ + allocationOrder = allocationOrderManager.findById(param.getOrderId()) + .orElseThrow(() -> new DataNotExistException("未查询到分账单信息")); + } else { + allocationOrder = allocationOrderManager.findByAllocationNo(param.getAllocationNo()) + .orElseThrow(() -> new DataNotExistException("未查询到分账单信息")); + } + List details = allocationOrderDetailManager.findAllByOrderId(allocationOrder.getId()); + + // 创建分账策略并初始化 + AbsAllocationStrategy allocationStrategy = AllocationFactory.create(allocationOrder.getChannel()); + allocationStrategy.initParam(allocationOrder, details); + + // 分账预处理 + allocationStrategy.doBeforeHandler(); + try { + // 重复分账处理 + allocationStrategy.allocation(); + } catch (Exception e) { + // 失败 + } + } + /** * 分账完结 */ @@ -111,10 +143,11 @@ public class AllocationService { allocationOrder = allocationOrderManager.findByAllocationNo(param.getAllocationNo()) .orElseThrow(() -> new DataNotExistException("未查询到分账单信息")); } + List details = allocationOrderDetailManager.findAllByOrderId(allocationOrder.getId()); // 创建分账策略并初始化 AbsAllocationStrategy allocationStrategy = AllocationFactory.create(allocationOrder.getChannel()); - allocationStrategy.initParam(allocationOrder, null); + allocationStrategy.initParam(allocationOrder, details); // 分账完结预处理 allocationStrategy.doBeforeHandler(); diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/AliPayAllocationStrategy.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/AliPayAllocationStrategy.java index 16859021..e5fe9998 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/AliPayAllocationStrategy.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/AliPayAllocationStrategy.java @@ -67,7 +67,7 @@ public class AliPayAllocationStrategy extends AbsAllocationStrategy { */ @Override public void finish() { - aliPayAllocationService.finish(this.getAllocationOrder()); + aliPayAllocationService.finish(this.getAllocationOrder(), this.getAllocationOrderDetails()); } } diff --git a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/WeChatPayAllocationStrategy.java b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/WeChatPayAllocationStrategy.java index ebd649d7..0a7e40a9 100644 --- a/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/WeChatPayAllocationStrategy.java +++ b/daxpay-single/daxpay-single-service/src/main/java/cn/bootx/platform/daxpay/service/core/payment/allocation/strategy/allocation/WeChatPayAllocationStrategy.java @@ -57,7 +57,7 @@ public class WeChatPayAllocationStrategy extends AbsAllocationStrategy { */ @Override public void allocation() { - weChatPayAllocationService.allocation(getAllocationOrder(), getAllocationOrderDetails(), weChatPayConfig); + weChatPayAllocationService.allocation(getAllocationOrder(), this.getAllocationOrderDetails(), weChatPayConfig); } /** @@ -65,7 +65,7 @@ public class WeChatPayAllocationStrategy extends AbsAllocationStrategy { */ @Override public void finish() { - weChatPayAllocationService.finish(getAllocationOrder(), weChatPayConfig); + weChatPayAllocationService.finish(getAllocationOrder(), this.getAllocationOrderDetails(), weChatPayConfig); }