Kaynağa Gözat

fix:"优化支付接口,改用商品编号"

soobin 4 ay önce
ebeveyn
işleme
d6189f58d1

+ 507 - 0
src/main/java/com/szwl/controller/TOrderController.java

@@ -4898,5 +4898,512 @@ public class TOrderController {
         return orderService.huifuPayQuery(queryId, sn, companyType);
     }
 
+    @ApiOperation(value = "购物车 请求在线支付 二码合一")
+    @PostMapping("/newCarsPay")
+    public Object newCarsPay(String clientId, @RequestBody Map<String, String> productNameMap) {
+        if (productNameMap == null) {
+            return JsonMessage.error("数据出错");
+        }
+
+        StringBuffer note = new StringBuffer();
+        StringBuffer productName = new StringBuffer();
+        String productNo = "";
+        Map<String, Integer> productMap = new HashMap<>();
+        Integer productNumber = 0;
+
+        TEquipment equipment = R.getDataIfSuccess(szwlFeign.findEquipmentByClientId(clientId));
+        if (equipment == null || equipment.getId() == null) {
+            return JsonMessage.error("找不到设备");
+        }
+
+        BigDecimal price = new BigDecimal("0.00");
+        TEquipmentDesc equipmentDesc = R.getDataIfSuccess(szwlFeign.findEquipmentById(equipment.getId()));
+        if (equipmentDesc == null || equipmentDesc.getEquipmentId() == null || equipmentDesc.getPayType() == null || equipmentDesc.getPayType().equals("0")) {
+            // 1、type=0,原始设定,没有第二件半价
+            for (String key : productNameMap.keySet()) {
+                String entryValue = String.valueOf(productNameMap.get(key));
+                List<String> value = JSON.parseArray(entryValue, String.class);
+                boolean hasDuplicate = hasDuplicateValue(value);
+                if (hasDuplicate) {
+                    return JsonMessage.error("优惠码输入重复,单个商品只能使用一个优惠码");
+                }
+                List<TPromoCode> promoCodeList = new ArrayList<>();
+                // 1、校验优惠码
+                if (value.size() > 0) {
+                    for (String code : value) {
+                        if (!code.equals("0")) {
+                            TPromoCode promoCode;
+                            ResponseModel<TPromoCode> tPromoCodeResponseModel = szwlFeign.selectTPromoCode(code, equipment.getAdminId().toString());
+                            TPromoCode tPromoCode = tPromoCodeResponseModel.getData();
+                            promoCode = tPromoCode;
+                            if (tPromoCode == null || tPromoCode.getId() == null) {
+                                //不存在
+                                return JsonMessage.success("1");
+                            }
+                            Date lastUseDate = null;
+                            if (promoCode != null) {
+                                lastUseDate = promoCode.getLastUseDate();
+                            }
+
+                            if (lastUseDate != null && lastUseDate.getTime() < ((new Date()).getTime())) {
+                                promoCode.setIsUse("2");
+                                szwlFeign.updatePromoCode(String.valueOf(promoCode.getId()), "2");
+                                //过期
+                                return JsonMessage.success("4");
+                            }
+                            if (promoCode.getId() == null) {
+                                //不存在
+                                return JsonMessage.success("1");
+                            }
+                            if (promoCode.getIsUse().equals("1")) {
+                                //被使用
+                                return JsonMessage.success("2");
+                            }
+                            if (!promoCode.getAdminId().equals("1")) {
+                                if (String.valueOf(equipment.getAdminId()).equals(promoCode.getAdminId())) {
+
+                                } else {
+                                    //不是本机
+                                    return JsonMessage.success("3");
+                                }
+                            }
+                            if (promoCode.getDiscount() == null) {
+                                //旧优惠码
+                                return JsonMessage.success("0");
+                            }
+                            promoCodeList.add(promoCode);
+                        }
+                    }
+                }
+                String[] productInfo = key.split("-");
+                String no = productInfo[0];
+                String num = productInfo[1];
+                //确定价格,然后叠加
+                TProduct product = R.getDataIfSuccess(szwlFeign.getProductByNo(no, String.valueOf(equipment.getId())));
+                if (product == null) {
+                    return JsonMessage.error("找不到商品");
+                }
+                String goodsName = product.getProductName();
+                productMap.put(goodsName, Integer.valueOf(num));
+                productNumber += Integer.valueOf(num);
+                productName.append(goodsName).append("x").append(num).append(",");
+                BigDecimal productPrice = product.getRmbPrice();
+                productNo = product.getNo();
+                if (promoCodeList.size() > 0) {
+                    //有优惠码
+                    BigDecimal onePrice = new BigDecimal("0.00");
+                    Map<BigDecimal, Integer> priceMap = new HashMap<>();
+                    int i;
+                    for (i = 0; i < promoCodeList.size(); i++) {
+                        BigDecimal discount = BigDecimal.valueOf(promoCodeList.get(i).getDiscount()).setScale(2, RoundingMode.HALF_DOWN);
+                        BigDecimal price1 = new BigDecimal(0.0);
+                        TPromoCode tPromoCode = promoCodeList.get(i);
+                        if (StringUtils.isNotEmpty(tPromoCode.getType()) && tPromoCode.getType().equals("1")) {
+                            if (productPrice.compareTo(discount) > 0) {
+                                // 如果 price 大于 discount,从 price 中减去 discount
+                                price1 = productPrice.subtract(discount).setScale(2, RoundingMode.HALF_DOWN);
+                            } else {
+                                // 如果 price 小于等于 discount,将 price 设置为 0
+                                price1 = BigDecimal.ZERO;
+                            }
+                        } else {
+                            if (tPromoCode.getDiscount() == 0) {
+                                price1 = BigDecimal.ZERO;
+                            } else {
+                                price1 = productPrice.multiply(discount).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN);
+                            }
+                        }
+                        price = price.add(price1);
+                        if (priceMap.containsKey(price1)) {
+                            priceMap.put(price1, priceMap.get(price1) + 1);
+                        } else {
+                            priceMap.put(price1, 1);
+                        }
+                    }
+                    if (i < Integer.valueOf(num)) {
+                        int a = Integer.valueOf(num) - i;
+                        BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
+                        price = price.add(price2);
+                        priceMap.put(price2, a);
+                    }
+
+                    for (BigDecimal priceKey : priceMap.keySet()) {
+                        note.append(goodsName).append("-").append(product.getNo()).append("-").append(RandomUtil.randomNumbers(5)).append(":");
+                        note.append(priceKey).append("-").append(priceMap.get(priceKey)).append(",");
+                    }
+
+                } else {
+                    //1,type=0,原始设定,没有第二件半价
+                    int a = Integer.valueOf(num);
+                    BigDecimal price2 = productPrice.multiply(new BigDecimal(a)).setScale(2, RoundingMode.HALF_DOWN);
+                    price = price.add(price2);
+                    note.append(goodsName).append("-").append(product.getNo()).append(":");
+                    note.append(price2).append("-").append(num).append(",");
+                }
+            }
+        } else {
+            //2,type=1,第二件半价
+            //2.1 判定是否有第二件,有多少个第二件半价
+            for (String key : productNameMap.keySet()) {
+                String[] productInfo = key.split("-");
+                String no = productInfo[0];
+                String num = productInfo[1];
+                //确定价格,然后叠加
+                TProduct product = R.getDataIfSuccess(szwlFeign.getProductByNo(no, String.valueOf(equipment.getId())));
+                if (product == null) {
+                    return JsonMessage.error("找不到商品");
+                }
+                String goodsName = product.getProductName();
+                BigDecimal productPrice = product.getRmbPrice();
+                productName.append(goodsName).append("x").append(num).append(",");
+                note.append(goodsName).append("-").append(product.getNo()).append(":");
+                productNo = product.getNo();
+                switch (num) {
+                    case "1":
+                        //1个  0个半价
+                        price = price.add(productPrice);
+                        note.append(price).append("-").append(num).append(",");
+                        break;
+                    case "2":
+                        //2个  1个半价
+                        price = price.add(productPrice);
+                        price = price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
+                        note.append(price).append("-").append(num).append(",");
+                        break;
+                    case "3":
+                        //3个 1个半价
+                        price = price.add(productPrice);
+                        price = price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
+                        price = price.add(productPrice);
+                        note.append(price).append("-").append(num).append(",");
+                        break;
+                    case "4":
+                        //4个 2个半价
+                        price = price.add(productPrice);
+                        price = price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
+                        price = price.add(productPrice);
+                        price = price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
+                        note.append(price).append("-").append(num).append(",");
+                        break;
+                    case "5":
+                        //5个 2个半价
+                        price = price.add(productPrice);
+                        price = price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
+                        price = price.add(productPrice);
+                        price = price.add(productPrice.multiply(new BigDecimal(5)).divide(new BigDecimal(10)).setScale(2, RoundingMode.HALF_DOWN));
+                        price = price.add(productPrice);
+                        note.append(price).append("-").append(num).append(",");
+                        break;
+                }
+            }
+        }
+
+        if (equipment == null) {
+            return JsonMessage.error("找不到设备");
+        }
+
+        Long equipmentId = equipment.getId();
+        //查找商品
+        if (BigDecimal.ZERO.compareTo(price) == 0) {
+            return JsonMessage.success("0");
+        }
+        if (BigDecimal.ZERO.compareTo(price) > 0) {
+            return JsonMessage.error("商品价格异常");
+        }
+        Long client6 = Long.parseLong(clientId.substring(clientId.length() - 6));
+        String sn1 = orderService.initSn(client6);
+        String sn2 = orderService.initSn(client6);
+        // 获取关联设备的分销
+        TProportion proportion = R.getDataIfSuccess(szwlFeign.getProportion(String.valueOf(equipmentId)));
+        if (proportion == null || proportion.getId() == null) {
+            return JsonMessage.error("设备商家未完成分销设置");
+        }
+        TAdmin admin = R.getDataIfSuccess(szwlFeign.getAdmin(String.valueOf(equipment.getAdminId())));
+        if (admin == null) {
+            return JsonMessage.error("找不到设备商家");
+        }
+
+        // 支付方式:0或null为汇聚,1为汇付支付
+        String payPlatform = admin.getPayPlatform();
+        String companyType = admin.getCompanyType();
+        // 汇聚分账参数
+        JSONArray altInfo = new JSONArray();
+        // 汇付分账明细参数
+        JSONObject acctSplitBunch = new JSONObject();
+        if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
+            acctSplitBunch = getAcctSplitBunch(proportion, price, companyType);
+            if (!(acctSplitBunch.size() > 0)) {
+                return JsonMessage.error("设备商家未注册结算账号");
+            }
+        } else {
+            altInfo = getAltInfo(proportion, price);
+            if (!(altInfo.size() > 0)) {
+                return JsonMessage.error("设备商家未注册结算账号");
+            }
+        }
+
+
+        String orderNo1 = sn1;
+        String orderNo2 = sn2;
+        BigDecimal amount = price.setScale(2, RoundingMode.HALF_DOWN);
+        String productDesc = "";
+        String commonParameter = "";
+        String returnUrl = null;
+        String notifyUrl = JoinpayConstant.Notify_Url;
+        String isShowPic = "1";
+        String openId = null;
+        String authCode = null;
+        String appid = null;
+        String transactionModel = null;
+        String tradeMerchantNo = admin.getTradeMerchantNo();
+        String buyerId = null;
+        String isAlt = "1";
+        String altType = "11";
+        String altUrl = null;
+        BigDecimal marketingAmount = null;
+        String frpCode1 = "WEIXIN_NATIVE";
+        String frpCode2 = "ALIPAY_NATIVE";
+
+        TOrder order = new TOrder();
+        order.setId(IDGenerator.orderID());
+        order.setCreateDate(new Date());
+        order.setModifyDate(new Date());
+        order.setAdminId(admin.getId());
+        order.setSn(sn1);
+        order.setProductNo(productNo);
+        // 去掉后面的逗号
+        if (productNumber == 1) {
+            productName.delete(productName.length() - 3, productName.length());
+        } else {
+            productName.deleteCharAt(productName.length() - 1);
+        }
+        order.setProductName(productName.toString());
+        order.setPrice(price);
+        order.setClientId(equipment.getClientId());
+        order.setEquipmentId(equipmentId);
+        order.setFrpCode(frpCode1);
+        order.setAltInfo(altInfo.toString());
+        order.setProductNumber(productNumber);
+        order.setStatus(0);
+        order.setType(admin.getType());
+        order.setCompanyType(companyType);
+        order.setMachineType(equipment.getMachineType());
+        if (admin.getType() == 0) {
+            order.setAdminProportion(proportion.getProportion());
+            order.setAgencyProportion(proportion.getAgencyProportion());
+            order.setAgencyId(proportion.getAgencyId());
+        }
+        if (admin.getType() == 1) {
+            order.setAdminProportion(proportion.getProportion());
+            order.setAgencyProportion(proportion.getAgencyProportion());
+            order.setAgencyId(proportion.getAgencyId());
+            order.setMerchantProportion(proportion.getMerchantProportion());
+            order.setMerchantId(proportion.getMerchantId());
+        }
+        if (admin.getType() == 2) {
+            order.setAdminProportion(proportion.getProportion());
+            order.setAgencyProportion(proportion.getAgencyProportion());
+            order.setAgencyId(proportion.getAgencyId());
+            order.setMerchantProportion(proportion.getMerchantProportion());
+            order.setMerchantId(proportion.getMerchantId());
+            order.setPersonageProportion(proportion.getPersonageProportion());
+            order.setPersonageId(proportion.getPersonageId());
+        }
+        order.setPayPlatform(payPlatform);
+        if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
+            order.setIsSettlement("0");
+        }
+        order.setNote(note.toString());
+        TOrder order2 = new TOrder();
+        order2.setId(IDGenerator.orderID());
+        order2.setCreateDate(new Date());
+        order2.setModifyDate(new Date());
+        order2.setAdminId(admin.getId());
+        order2.setSn(sn2);
+        order2.setProductName(productName.toString());
+        order2.setProductNo(productNo);
+        order2.setPrice(price);
+        order2.setClientId(equipment.getClientId());
+        order2.setEquipmentId(equipmentId);
+        order2.setFrpCode(frpCode2);
+        order2.setAltInfo(altInfo.toString());
+        order2.setStatus(0);
+        order2.setProductNumber(productNumber);
+        order2.setType(admin.getType());
+        order2.setCompanyType(companyType);
+        order2.setMachineType(equipment.getMachineType());
+        if (admin.getType() == 0) {
+            order2.setAdminProportion(proportion.getProportion());
+            order2.setAgencyProportion(proportion.getAgencyProportion());
+            order2.setAgencyId(proportion.getAgencyId());
+        }
+        if (admin.getType() == 1) {
+            order2.setAdminProportion(proportion.getProportion());
+            order2.setAgencyProportion(proportion.getAgencyProportion());
+            order2.setAgencyId(proportion.getAgencyId());
+            order2.setMerchantProportion(proportion.getMerchantProportion());
+            order2.setMerchantId(proportion.getMerchantId());
+        }
+        if (admin.getType() == 2) {
+            order2.setAdminProportion(proportion.getProportion());
+            order2.setAgencyProportion(proportion.getAgencyProportion());
+            order2.setAgencyId(proportion.getAgencyId());
+            order2.setMerchantProportion(proportion.getMerchantProportion());
+            order2.setMerchantId(proportion.getMerchantId());
+            order2.setPersonageProportion(proportion.getPersonageProportion());
+            order2.setPersonageId(proportion.getPersonageId());
+        }
+        order2.setPayPlatform(payPlatform);
+        if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
+            order2.setIsSettlement("0");
+        }
+        order2.setNote(note.toString());
+        // 订单明细表
+        TOrderDetails orderDetails = new TOrderDetails();
+        orderDetails.setAdminId(admin.getId());
+        orderDetails.setEquipmentId(equipmentId);
+        orderDetails.setCreateDate(new Date());
+        orderDetails.setCompanyType(companyType);
+        orderDetails.setRefundStatus("0");
+        orderDetails.setMachineType(equipment.getMachineType());
+        String productName1 = "";
+        if (productNumber > 1) {
+            if (equipment.getMachineType() == null || equipment.getMachineType().equals("0")) {
+                productName1 = "棉花糖-" + equipment.getName() + "-" + client6;
+            } else {
+                productName1 = "爆米花-" + equipment.getName() + "-" + client6;
+            }
+        } else {
+            productName1 = productName + "-" + equipment.getName() + "-" + client6;
+        }
+
+        Map<String, String> map = new HashMap<>();
+        String[] products = order.getNote().split(",");
+        for (String product : products) {
+            String[] parts = product.split(":");
+            // 商品名称-商品编号
+            String key = parts[0];
+            // 商品单价-商品数量
+            String value = parts[1];
+            map.put(key, value);
+        }
+
+        String result = null;
+        if (StringUtils.isNotEmpty(payPlatform) && payPlatform.equals("1")) {
+            // 汇付分账参数
+            order.setAcctSplitBunch(acctSplitBunch.toString());
+            order2.setAcctSplitBunch(acctSplitBunch.toString());
+            String result1 = "";
+            if (StrUtil.isNotEmpty(companyType) && companyType.equals("1")) {
+                result1 = HuifuConstant.SC_WECHAT_URL + "?price=" + price + "&sn=" + sn1;
+            } else {
+                result1 = HuifuConstant.SZ_WECHAT_URL + "?price=" + price + "&sn=" + sn1;
+            }
+            try {
+                result1 = URLEncoder.encode(result1, "UTF-8");
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+            }
+            // 支付宝
+            try {
+                result = huifuMchService.uniPay(
+                        sn2, amount, productName1, productDesc,
+                        notifyUrl, frpCode2, openId, appid, acctSplitBunch, companyType
+                );
+            } catch (Exception e) {
+                e.printStackTrace();
+                return JsonMessage.error("申请支付失败");
+            }
+            if (StrUtil.isEmpty(result)) {
+                return JsonMessage.error("找不到支付图片");
+            }
+            // 汇聚支付支付申请返回支付二维码图片
+            String code1 = "http://sz.sunzee.com.cn/PAY-SERVER/tOrder/aliPay?zfb=" + result + "&wx=" + result1;
+            String qrcode = toQrcode(code1);
+            JSONObject kindData = new JSONObject();
+            kindData.put("sn", sn1 + "-" + sn2);
+            kindData.put("rd_Pic", qrcode);
+            for (String key : map.keySet()) {
+                // key:"玫瑰精灵-A01",value:"0.50-1"
+                String value = map.get(key);
+                String[] GoodsInfo = key.split("-");
+                String[] GoodsValue = value.split("-");
+                orderDetails.setId(HuifuUtils.initDetailsId());
+                orderDetails.setProductName(GoodsInfo[0]);
+                orderDetails.setProductNo(GoodsInfo[1]);
+                orderDetails.setPrice(new BigDecimal(GoodsValue[0]).divide(new BigDecimal(GoodsValue[1])).setScale(2));
+                orderDetails.setProductNumber(Integer.valueOf(GoodsValue[1]));
+                orderDetails.setAmount(new BigDecimal(GoodsValue[0]).setScale(2));
+                orderDetails.setId(HuifuUtils.initDetailsId());
+                orderDetails.setOrderSn(sn1);
+                orderDetailsService.save(orderDetails);
+                orderDetails.setId(HuifuUtils.initDetailsId());
+                orderDetails.setOrderSn(sn2);
+                orderDetailsService.save(orderDetails);
+            }
+            orderService.save(order);
+            orderService.save(order2);
+            return JsonMessage.success(kindData.toString());
+        } else {
+            try {
+                result = joinpayMchService.uniPay(
+                        orderNo1, amount, productName1, productDesc,
+                        commonParameter, returnUrl, notifyUrl, frpCode1,
+                        isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
+                        buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
+                );
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+                return JsonMessage.error("申请支付失败");
+            }
+            JSONObject resultJson = JSONObject.parseObject(result);
+            String rc_Result1 = resultJson.getString("rc_Result");
+            String result1 = null;
+            try {
+                result1 = joinpayMchService.uniPay(
+                        orderNo2, amount, productName1, productDesc,
+                        commonParameter, returnUrl, notifyUrl, frpCode2,
+                        isShowPic, openId, authCode, appid, transactionModel, tradeMerchantNo,
+                        buyerId, isAlt, altType, altInfo, altUrl, marketingAmount
+                );
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+                return JsonMessage.error("申请支付失败");
+            }
+            JSONObject resultJson1 = JSONObject.parseObject(result1);
+            String rc_Result2 = resultJson1.getString("rc_Result");
+
+            // 汇聚支付支付申请返回支付二维码图片
+
+            String code1 = "http://app.sunzee.com.cn/" + "/api/order/aliPay.htm?wx=" + rc_Result1 + "&zfb=" + rc_Result2;
+            String rd_Pic = toQrcode(code1);
+            JSONObject kindData = new JSONObject();
+            kindData.put("sn", sn1 + "-" + sn2);
+            kindData.put("rd_Pic", rd_Pic);
+            for (String key : map.keySet()) {
+                // key:"玫瑰精灵-A01",value:"0.50-1"
+                String value = map.get(key);
+                String[] GoodsInfo = key.split("-");
+                String[] GoodsValue = value.split("-");
+                orderDetails.setId(HuifuUtils.initDetailsId());
+                orderDetails.setProductName(GoodsInfo[0]);
+                orderDetails.setProductNo(GoodsInfo[1]);
+                orderDetails.setPrice(new BigDecimal(GoodsValue[0]).divide(new BigDecimal(GoodsValue[1])).setScale(2));
+                orderDetails.setProductNumber(Integer.valueOf(GoodsValue[1]));
+                orderDetails.setAmount(new BigDecimal(GoodsValue[0]).setScale(2));
+                orderDetails.setId(HuifuUtils.initDetailsId());
+                orderDetails.setOrderSn(sn1);
+                orderDetailsService.save(orderDetails);
+                orderDetails.setId(HuifuUtils.initDetailsId());
+                orderDetails.setOrderSn(sn2);
+                orderDetailsService.save(orderDetails);
+            }
+            orderService.save(order);
+            orderService.save(order2);
+            return JsonMessage.success(kindData.toString());
+        }
+
+    }
+
 }
 

+ 5 - 0
src/main/java/com/szwl/feign/bean/SzwlFeign.java

@@ -3,6 +3,7 @@ package com.szwl.feign.bean;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.szwl.model.bo.ResponseModel;
 import com.szwl.model.entity.*;
+import io.swagger.annotations.ApiOperation;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -103,4 +104,8 @@ public interface SzwlFeign {
     @GetMapping("/tWechat/getOpenId")
     ResponseModel<String> getOpenId(@RequestParam Long adminId);
 
+    @ApiOperation(value = "通过编号获取商品")
+    @GetMapping("/tProduct/getProductByNo")
+    ResponseModel<TProduct> getProductByNo(@RequestParam(value = "no")String no, @RequestParam(value = "equipmentId")String equipmentId);
+
 }