Browse Source

feat(order):优化订单支付与分账逻辑

- 添加订单不存在时的异常处理
- 添加商户不存在时的异常处理
- 调整微信支付手续费计算逻辑,移除重复扣费
- 更新分账金额计算方式,剔除保险金额影响
- 移除冗余的手续费设置逻辑- 优化SM3工具类,增加金额计算与临界值处理方法
- 调整main方法测试用例及分配比例
wzq 1 tuần trước cách đây
mục cha
commit
0a1bd51ece

+ 3 - 2
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/app/service/WeChatPayService.java

@@ -307,9 +307,10 @@ public class WeChatPayService {
             //分账金额(支付金额-保险金额)
             BigDecimal price = appOrder.getPrice().subtract(insurePrice);
             //微信手续费,不足1分按1分算(当计算值小于0.01时强制提升至0.01)
-            BigDecimal FEE = calculate(price, new BigDecimal("0.006"));
+//            BigDecimal FEE = calculate(price, new BigDecimal("0.006"));
 
-            price = price.subtract(FEE).subtract(insurePrice);
+//            price = price.subtract(FEE).subtract(insurePrice);
+            price = price.subtract(insurePrice);
             log.info("------------------------------------------------分账金额:{}", price);
 
             //商户(分账给平台)

+ 12 - 0
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/app/service/impl/OrderServiceImpl.java

@@ -1819,10 +1819,22 @@ public class OrderServiceImpl extends ServiceImpl<AppOrderMapper, AppOrder> impl
 
         //查询订单
         AppOrder appOrder = appOrderMapper.selectOne(Wrappers.<AppOrder>lambdaQuery().eq(AppOrder::getOrderCode, orderCode).last("limit 1"));
+
+        if (ObjectUtil.isEmpty(appOrder)) {
+            throw new RuntimeException("当前订单不存在");
+        }
+
+        if (appOrder.getOrderOrFree() == 1) {
+            return "100001";//支付成功
+        }
         //查询订单子商户号
         String orgCode = appOrder.getOrgCode();
         SysDepart depart = sysDepartMapper.selectOne(Wrappers.lambdaQuery(SysDepart.class).eq(SysDepart::getOrgCode, orgCode).last("limit 1"));
 
+        if (ObjectUtil.isEmpty(depart)) {
+            throw new RuntimeException("当前商品所属商户不存在");
+        }
+
         //null代表查询失败 SUCCESS-成功 USERPAYING和ACCEPT为中间态 其他为支付失败
         JSONObject res = weChatPayService.orderQueryByOutTradeNo(orderCode,depart.getMchId());
         String s = res == null ? null : res.getString("trade_state");

+ 1 - 1
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/config/WeChatProfitSharingService.java

@@ -116,7 +116,7 @@ public class WeChatProfitSharingService {
         appProfitSharingRecords.setPrice(RatiosUtil.amount_fee(appOrder.getPrice()));
         appProfitSharingRecords.setOrderAmount(orderAmount);
         appProfitSharingRecords.setAmount(amount);
-        appProfitSharingRecords.setProcedureFee(orderAmount - amount);
+//        appProfitSharingRecords.setProcedureFee(orderAmount - amount);
         appProfitSharingRecords.setProfitSharingTime(new Date());
 
         List<AppProfitSharingInfo> receiverRecords = appProfitSharingInfos.stream().filter(e -> e.getType() == 1).collect(Collectors.toList());

+ 33 - 2
national-motion-module-system/national-motion-system-biz/src/main/java/org/jeecg/modules/pay/unionPay/SM3Util.java

@@ -46,12 +46,43 @@ public class SM3Util {
 //
 //    }
 
+    private static final BigDecimal MIN_THRESHOLD = new BigDecimal("0.01"); // 最小阈值
+
+    /**
+     * 计算千分之6并处理临界值
+     *
+     * @param input 输入金额(单位:元)
+     * @return 计算结果(保留两位小数,不足0.01按0.01计算)
+     * @throws IllegalArgumentException 输入为null或负数时抛出
+     */
+    public static BigDecimal calculate(BigDecimal input, BigDecimal multiplier) {
+        if (input == null) {
+            throw new IllegalArgumentException("输入金额不能为null");
+        }
+        if (input.compareTo(BigDecimal.ZERO) < 0) {
+            throw new IllegalArgumentException("输入金额不能为负数");
+        }
+
+        // 精确乘法运算(保留4位中间精度)
+        BigDecimal product = input.multiply(multiplier)
+                .setScale(4, RoundingMode.HALF_UP);
+
+        // 临界值判断与补偿
+        return product.compareTo(MIN_THRESHOLD) < 0
+                ? MIN_THRESHOLD
+                : product.setScale(2, RoundingMode.DOWN);
+    }
+
     public static void main(String[] args) throws Exception {
 
+        BigDecimal price = new BigDecimal("0.3");
+
+        BigDecimal FEE = calculate(price, new BigDecimal("0.006"));
 
+        price = price.subtract(FEE);
 
-        BigDecimal total1 = new BigDecimal("0.09");
-        BigDecimal[] ratios1 = {new BigDecimal("69.4"), new BigDecimal("20"), new BigDecimal("10.6")}; // 总和100 → 直接分配
+        BigDecimal total1 = price;
+        BigDecimal[] ratios1 = {new BigDecimal("89.4"), new BigDecimal("0"), new BigDecimal("10.6")}; // 总和100 → 直接分配
         System.out.println("示例1结果:" + Arrays.toString(allocate(total1, ratios1)));
         System.out.println(amount_fee(new BigDecimal(0.01)).multiply(new BigDecimal(0.06)).setScale(0, RoundingMode.UP));
     }