Ver código fonte

线上支付分账

plf 4 anos atrás
pai
commit
dacb0d62d1

+ 1 - 1
base-module/src/main/java/com/caimei/module/base/entity/po/Product.java

@@ -563,7 +563,7 @@ public class Product implements Serializable {
     private String qualificationImg;
 
     /**
-     * 是否含税   0不含税,1含税
+     * 是否含税   0不含税,1含税,2未知
      */
     private String includedTax;
 

+ 10 - 0
base-module/src/main/java/com/caimei/module/base/entity/vo/OrderProductVo.java

@@ -38,6 +38,16 @@ public class OrderProductVo extends CmOrderProduct {
      */
     private String productCategory;
 
+    /**
+     * 是否含税 0不含税,1含税,2未知
+     */
+    private String includedTax;
+
+    /**
+     * 发票类型(基于是否含税基础) 1增值税票,2普通票, 3不能开票
+     */
+    private String invoiceType;
+
     /**
      * 二手商品分类,1二手仪器,2临期产品,3其他
      */

+ 5 - 0
base-module/src/main/java/com/caimei/module/base/entity/vo/OrderVo.java

@@ -56,4 +56,9 @@ public class OrderVo extends CmOrder {
      * 机构名称
      */
     private String clubName;
+
+    /**
+     * 订单内是否包含确定能否开发票的商品
+     */
+    private boolean invoiceStatus = false;
 }

+ 125 - 56
pay/src/main/java/com/caimei/module/pay/service/impl/PayServiceImpl.java

@@ -32,7 +32,6 @@ public class PayServiceImpl implements PayService {
     @Resource
     private PayDao payDao;
 
-
     /**
      * 商户标识
      */
@@ -49,6 +48,14 @@ public class PayServiceImpl implements PayService {
      * 用户编号
      */
     String merNo = "10001720";
+    /**
+     * 公账,子商户商编
+     */
+    String publicAccountNo = "20001793";
+    /**
+     * 私账,子商户商编
+     */
+    String privateAccountNo = "20001924";
 
     JsonModel model = JsonModel.newInstance();
 
@@ -80,6 +87,11 @@ public class PayServiceImpl implements PayService {
         map.put("discernReceipt", discernReceiptList);
         //商品数据
         List<OrderProductVo> orderProductList = payDao.fandAllOrderProduct(orderId);
+        orderProductList.forEach(o -> {
+            if ("2".equals(o.getIncludedTax())) {
+                order.setInvoiceStatus(true);
+            }
+        });
         //过滤运费商品
         orderProductList.removeIf(orderProductVo -> orderProductVo.getShopID() == 998);
         map.put("orderProductList", orderProductList);
@@ -115,43 +127,25 @@ public class PayServiceImpl implements PayService {
         try {
             // 时间戳
             long time = System.currentTimeMillis() / 1000;
+            JSONObject json = getPayParameter(payment, request, time);
             // 商户订单号
             String orderId = order.getOrderNo() + "#" + time;
+            json.put("orderId", orderId);
             //商品名称
             String product = "采美订单" + order.getOrderNo();
-            String userType = "ENTERPRISE";
-            //用户IP地址
-            String userIp = IPUtil.getIpAddr(request);
-            JSONObject json = new JSONObject();
-            json.put("merAccount", merAccount);
-            json.put("merNo", merNo);
-            json.put("orderId", orderId);
-            json.put("time", time);
-            //支付金额
-            json.put("amount", payment.getPayAmount());
             json.put("product", product);
-            json.put("payWay", payment.getPayWay());
-            json.put("payType", payment.getPayType());
-            json.put("userIp", userIp);
-            json.put("returnUrl", payment.getReturnUrl());
-            json.put("notifyUrl", payment.getNotifyUrl());
             String attach = order.getOrderID() + "," + payment.getPayType();
             json.put("attach", attach);
-            if (null != payment.getBankCode()) {
-                json.put("bankCode", payment.getBankCode());
-            }
-            json.put("userType", userType);
-            if (null != payment.getOrderId()) {
-                json.put("openId", payment.getOpenid());
+            //分账详情
+            String splitBillDetail = splitBillDetail(order, payment);
+            if ("分账失败".equals(splitBillDetail)) {
+                return model.error("本次付款金额过小,分账失败");
             }
+            log.info("分账详情>>>>>>>单位/分>>>" + splitBillDetail);
+            json.put("splitBillDetail", splitBillDetail);
             String sign = PayUtils.buildSign(json, merKey);
             json.put("sign", sign);
-            String data = "";
-            try {
-                data = PayUtils.buildDataPrivate(json, merKey);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
+            String data = PayUtils.buildDataPrivate(json, merKey);
             result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/v1", merAccount, data);
         } catch (Exception e) {
             e.printStackTrace();
@@ -166,6 +160,97 @@ public class PayServiceImpl implements PayService {
         return model.success(result);
     }
 
+    /**
+     * 设置第三方支付参数
+     */
+    private JSONObject getPayParameter(Payment payment, HttpServletRequest request, long time) throws Exception {
+        String userType = "ENTERPRISE";
+        //用户IP地址
+        String userIp = IPUtil.getIpAddr(request);
+        JSONObject json = new JSONObject();
+        json.put("merAccount", merAccount);
+        json.put("merNo", merNo);
+        json.put("time", time);
+        //支付金额
+        json.put("amount", payment.getPayAmount());
+        json.put("payWay", payment.getPayWay());
+        json.put("payType", payment.getPayType());
+        json.put("userIp", userIp);
+        json.put("returnUrl", payment.getReturnUrl());
+        json.put("notifyUrl", payment.getNotifyUrl());
+        if (null != payment.getBankCode()) {
+            json.put("bankCode", payment.getBankCode());
+        }
+        json.put("userType", userType);
+        if (null != payment.getOrderId()) {
+            json.put("openId", payment.getOpenid());
+        }
+        return json;
+    }
+
+    /**
+     * 分账详情
+     */
+    private String splitBillDetail(OrderVo order, Payment payment) {
+        List<Map<String, String>> list = new ArrayList<>();
+        //分账金额
+        BigDecimal fashionableAmount = BigDecimal.ZERO;
+        //本次支付金额,单位/元
+        BigDecimal payAmount = MathUtil.div(payment.getPayAmount(), 100);
+        if ("UNIONPAY".equals(payment.getPayWay())) {
+            fashionableAmount = MathUtil.sub(payAmount, 8);
+        } else {
+            //手续费
+            BigDecimal procedureFee = MathUtil.mul(payAmount, 0.0038, 2);
+            if (MathUtil.compare(procedureFee, 0) == 0) {
+                fashionableAmount = MathUtil.sub(payAmount, 0.01);
+            } else {
+                fashionableAmount = MathUtil.sub(payAmount, procedureFee);
+            }
+        }
+        List<OrderProductVo> orderProductList = payDao.fandAllOrderProduct(order.getOrderID().intValue());
+        //私账应付总金额
+        BigDecimal privateAccountAmount = BigDecimal.ZERO;
+        for (OrderProductVo orderProduct : orderProductList) {
+            //不含税-不能开票,则分账到私账银行卡
+            if ("0".equals(orderProduct.getIncludedTax()) && "3".equals(orderProduct.getInvoiceType())) {
+                privateAccountAmount = MathUtil.add(privateAccountAmount, orderProduct.getShouldPayFee());
+            }
+        }
+        if (MathUtil.compare(privateAccountAmount, order.getPayTotalFee()) == 0 || "1".equals(order.getSecondHandOrderFlag())) {
+            //二手订单与不含税-不能开票,全部分给私账
+            Map<String, String> map = new HashMap<>(3);
+            map.put("subUserNo", privateAccountNo);
+            map.put("splitBillType", "2");
+            map.put("splitBillValue", MathUtil.mul(fashionableAmount, 100).toString());
+            list.add(map);
+        } else {
+            //分给私账金额
+            BigDecimal privateAccount = MathUtil.div(MathUtil.mul(fashionableAmount, privateAccountAmount), order.getPayTotalFee());
+            if (MathUtil.compare(privateAccount, 0) == 0 && MathUtil.compare(privateAccountAmount, 0) > 0) {
+                return "分账失败";
+            }
+            if (MathUtil.compare(privateAccount, 0) != 0) {
+                //私账
+                Map<String, String> map1 = new HashMap<>(3);
+                map1.put("subUserNo", privateAccountNo);
+                map1.put("splitBillType", "2");
+                //分账金额,单位/分
+                BigDecimal splitBillValue = MathUtil.round(MathUtil.mul(privateAccount, 100), 0);
+                map1.put("splitBillValue", String.valueOf(splitBillValue));
+                list.add(map1);
+            }
+            //公账
+            Map<String, String> map2 = new HashMap<>(3);
+            map2.put("subUserNo", publicAccountNo);
+            map2.put("splitBillType", "2");
+            BigDecimal splitBillValue = MathUtil.round(MathUtil.mul(MathUtil.sub(fashionableAmount, privateAccount), 100), 0);
+            map2.put("splitBillValue", String.valueOf(splitBillValue));
+            list.add(map2);
+        }
+        return JSONObject.toJSONString(list);
+    }
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public String paymentCallback(String data) throws Exception {
@@ -415,43 +500,27 @@ public class PayServiceImpl implements PayService {
         try {
             // 时间戳
             long time = System.currentTimeMillis() / 1000;
+            JSONObject json = getPayParameter(payment, request, time);
             // 商户订单号
             String orderId = payment.getProductId() + "#" + time;
+            json.put("orderId", orderId);
             //商品名称
             String product = "采美订单" + orderId;
-            String userType = "ENTERPRISE";
-            //用户IP地址
-            String userIp = IPUtil.getIpAddr(request);
-            JSONObject json = new JSONObject();
-            json.put("merAccount", merAccount);
-            json.put("merNo", merNo);
-            json.put("orderId", orderId);
-            json.put("time", time);
-            //支付金额
-            json.put("amount", payment.getPayAmount());
             json.put("product", product);
-            json.put("payWay", payment.getPayWay());
-            json.put("payType", payment.getPayType());
-            json.put("userIp", userIp);
-            json.put("returnUrl", payment.getReturnUrl());
-            json.put("notifyUrl", payment.getNotifyUrl());
             String attach = payment.getProductId() + "," + payment.getPayType();
             json.put("attach", attach);
-            if (null != payment.getBankCode()) {
-                json.put("bankCode", payment.getBankCode());
-            }
-            json.put("userType", userType);
-            if (null != payment.getOrderId()) {
-                json.put("openId", payment.getOpenid());
-            }
+            //二手发布100,默认私账
+            List<Map<String, String>> list = new ArrayList<>();
+            Map<String, String> map = new HashMap<>(3);
+            map.put("subUserNo", privateAccountNo);
+            map.put("splitBillType", "1");
+            map.put("splitBillValue", "1");
+            list.add(map);
+            String splitBillDetail = JSONObject.toJSONString(list);
+            json.put("splitBillDetail", splitBillDetail);
             String sign = PayUtils.buildSign(json, merKey);
             json.put("sign", sign);
-            String data = "";
-            try {
-                data = PayUtils.buildDataPrivate(json, merKey);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
+            String data = PayUtils.buildDataPrivate(json, merKey);
             result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/v1", merAccount, data);
         } catch (Exception e) {
             e.printStackTrace();

+ 9 - 1
pay/src/main/resources/com-caimei-module-pay/PayMapper.xml

@@ -352,7 +352,15 @@
     </select>
 
     <select id="fandAllOrderProduct" resultType="com.caimei.module.base.entity.vo.OrderProductVo">
-        SELECT * FROM cm_order_product WHERE orderID =  #{orderId}
+        SELECT
+          cop.*,
+          p.includedTax,
+          p.invoiceType
+        FROM
+          cm_order_product cop
+          LEFT JOIN product p ON cop.productID = p.productID
+        WHERE
+          orderID = #{orderId}
     </select>
 
     <select id="findUser" resultType="com.caimei.module.base.entity.vo.UserVo">