|
@@ -0,0 +1,655 @@
|
|
|
+package com.caimei.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.caimei.mapper.PayOrderMapper;
|
|
|
+import com.caimei.model.ResponseJson;
|
|
|
+import com.caimei.model.dto.PaymentDto;
|
|
|
+import com.caimei.model.po.*;
|
|
|
+import com.caimei.model.vo.*;
|
|
|
+import com.caimei.service.PayOrderService;
|
|
|
+import com.caimei.util.MathUtil;
|
|
|
+import com.caimei.util.Md5Util;
|
|
|
+import com.caimei.util.PayUtils;
|
|
|
+import com.caimei.util.RandomCodeGenerator;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.net.URI;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : plf
|
|
|
+ * @date : 2020/5/9
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class PayOrderServiceImpl implements PayOrderService {
|
|
|
+ @Resource
|
|
|
+ private PayOrderMapper payOrderMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 商户标识
|
|
|
+ */
|
|
|
+ String merAccount = "aa9aee6a148843a6a9e4ea117df4454b";
|
|
|
+ /**
|
|
|
+ * 私钥,商户密钥
|
|
|
+ */
|
|
|
+ String merKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAM0qCTZFdi1I59/Jeis+8KVhzSNhsRrKiOlHazIVyxNLzUQvFpWN5PlfVKlnJToorURTStfjAv01HD0Z4ZvMauuHhDT0bptiDln928Ld6SzX889X2nsCxl0Q+WzrkFsnT6gotvGnTeQGUgbBV3SQD3IUOwRwxoWYbrZqxtqHFxqRAgMBAAECgYEAvsHx9MMbAToDVmEXtXP8/lh0Cwy/RgDA0d30voni2pslTNtXbVCUcIUBy8y6oVvG1nt3YEmTsuiZy/nvehPT6GV1Gqj8T6lqWQ8KQKsDGlubvh23tzNM90me2TLZEPdNqC5CPRrwYrjght4BXgzu7s2+5FpeYCob1gVNi+w0Jz0CQQD0dXb9Oez+Ybnxb3rCghGWM6cxi8fsqk6MuKaleg53qfXrkgAVH78faeWzRaeSbVOh5+Z9kX5HUeynfM7E/f4nAkEA1tmnvJp4JQaouO1Trzbnkhowjea5daK/tDE8K0hIMHUjAw+c1QTteGOVGBFBHWPkUwkSCd2HKmk4URkp/snMhwJAO32+qF+Jclq8EqqLmHxo5UHKxX7793d2yD5Dp++tR6fgBiUwyfNA4tc1pEwmPLdIbBVwfUyEC70/N39jHoOlbwI/dX6SPJI9IgKCQp+HJEriWQP5iaCjy7E1JVXHkeP1lop4mzPukJAhTbUn1AGbmncGZmKPetWrFYZ1ReR9EtlJAkEA2kBmARWyOTt8fbikImuHr65M+BDgD+2fkuDb5+tqJljNmSqAIRRwcXj1Je6vkzlDJuSw3wlG6iliFtwe0cq7+w==";
|
|
|
+ /**
|
|
|
+ * 公钥
|
|
|
+ */
|
|
|
+ String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNKgk2RXYtSOffyXorPvClYc0jYbEayojpR2syFcsTS81ELxaVjeT5X1SpZyU6KK1EU0rX4wL9NRw9GeGbzGrrh4Q09G6bYg5Z/dvC3eks1/PPV9p7AsZdEPls65BbJ0+oKLbxp03kBlIGwVd0kA9yFDsEcMaFmG62asbahxcakQIDAQAB";
|
|
|
+ /**
|
|
|
+ * 用户编号
|
|
|
+ */
|
|
|
+ String merNo = "10001720";
|
|
|
+ /**
|
|
|
+ * 公账-专票,子商户商编
|
|
|
+ */
|
|
|
+ String publicAccountNo = "20001793";
|
|
|
+ /**
|
|
|
+ * 公账-普票,子商户商编
|
|
|
+ */
|
|
|
+ String commonInvoiceNo = "20001754";
|
|
|
+ /**
|
|
|
+ * 私账-无票,子商户商编
|
|
|
+ */
|
|
|
+ String privateAccountNo = "20001924";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取线上支付开关状态
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> getPayOnLineSwitch() {
|
|
|
+ Integer status = payOrderMapper.getPayOnLineSwitch();
|
|
|
+ return ResponseJson.success(status);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Map<String, Object>> checkoutCounter(Integer orderId) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ OrderVo order = payOrderMapper.findOrder(orderId);
|
|
|
+ if (null == order) {
|
|
|
+ return ResponseJson.error("订单不存在", null);
|
|
|
+ }
|
|
|
+ map.put("order", order);
|
|
|
+ //支付记录
|
|
|
+ List<DiscernReceiptVo> discernReceiptList = payOrderMapper.getDiscernReceipt(order);
|
|
|
+ BigDecimal receiptAmount = BigDecimal.ZERO;
|
|
|
+ if (null != discernReceiptList && discernReceiptList.size() > 0) {
|
|
|
+ for (DiscernReceiptVo discernReceipt : discernReceiptList) {
|
|
|
+ if ("2".equals(discernReceipt.getPayWay())) {
|
|
|
+ return ResponseJson.error("线下支付过只能走线下", null);
|
|
|
+ }
|
|
|
+ if ("3".equals(discernReceipt.getReceiptStatus())) {
|
|
|
+ receiptAmount = receiptAmount.add(discernReceipt.getAssociateAmount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ order.setReceiptAmount(receiptAmount);
|
|
|
+ map.put("discernReceipt", discernReceiptList);
|
|
|
+ //商品数据
|
|
|
+ List<OrderProductVo> orderProductList = payOrderMapper.findAllOrderProduct(orderId);
|
|
|
+ orderProductList.forEach(o -> {
|
|
|
+ if (StringUtils.isBlank(o.getIncludedTax()) || StringUtils.isBlank(o.getInvoiceType()) || "2".equals(o.getIncludedTax())
|
|
|
+ || ("2".equals(order.getInvoiceFlag()) && "0".equals(o.getIncludedTax()) && "3".equals(o.getInvoiceType()))) {
|
|
|
+ //订单选择开发票,商品属性:不含税-不能开票
|
|
|
+ order.setInvoiceStatus(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //过滤运费商品
|
|
|
+ orderProductList.removeIf(orderProductVo -> orderProductVo.getShopId() == 998);
|
|
|
+ map.put("orderProductList", orderProductList);
|
|
|
+ //机构信息
|
|
|
+ UserPo user = payOrderMapper.findUser(order.getUserID());
|
|
|
+ map.put("userName", user.getName());
|
|
|
+ return ResponseJson.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized ResponseJson<JSONObject> pay(PaymentDto payment) {
|
|
|
+ JSONObject result = null;
|
|
|
+ OrderVo order = payOrderMapper.findOrder(payment.getOrderId());
|
|
|
+ if (null == order) {
|
|
|
+ return ResponseJson.error("订单不存在", null);
|
|
|
+ }
|
|
|
+ if ("0".equals(order.getStatus()) || "33".equals(order.getStatus()) || "4".equals(order.getStatus()) || "5".equals(order.getStatus())
|
|
|
+ || "6".equals(order.getStatus()) || "7".equals(order.getStatus())) {
|
|
|
+ return ResponseJson.error("订单状态错误", null);
|
|
|
+ }
|
|
|
+ List<DiscernReceiptVo> discernReceiptList = payOrderMapper.getDiscernReceipt(order);
|
|
|
+ if (null != discernReceiptList && discernReceiptList.size() > 0) {
|
|
|
+ for (DiscernReceiptVo discernReceipt : discernReceiptList) {
|
|
|
+ if ("2".equals(discernReceipt.getPayWay())) {
|
|
|
+ return ResponseJson.error("线下支付过只能走线下", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("本次付款金额>>>>>" + payment.getPayAmount());
|
|
|
+ if (MathUtil.compare(MathUtil.mul(order.getPayableAmount(), 100), payment.getPayAmount()) < 0) {
|
|
|
+ return ResponseJson.error("付款金额错误", null);
|
|
|
+ }
|
|
|
+ //List<SplitAccountVo> splitBillDetail = null;
|
|
|
+ try {
|
|
|
+ // 时间戳
|
|
|
+ long time = System.currentTimeMillis() / 1000;
|
|
|
+ JSONObject json = getPayParameter(payment, time);
|
|
|
+ // 商户订单号
|
|
|
+ String orderId = order.getOrderNo() + "T" + time;
|
|
|
+ json.put("orderId", orderId);
|
|
|
+ //商品名称
|
|
|
+ String product = "采美订单" + order.getOrderNo();
|
|
|
+ json.put("product", product);
|
|
|
+ //支付类型
|
|
|
+ String payType = getPayType(payment);
|
|
|
+ String attach = order.getOrderId() + "," + payType;
|
|
|
+ json.put("attach", attach);
|
|
|
+ //分账详情
|
|
|
+ /*splitBillDetail = splitBillDetail(order, payment);
|
|
|
+ String parameters = ledgerParameters(splitBillDetail, order.getOrderID());
|
|
|
+ log.info("分账参数: " + parameters);
|
|
|
+ json.put("splitBillDetail", parameters);*/
|
|
|
+ String sign = PayUtils.buildSign(json, merKey);
|
|
|
+ json.put("sign", sign);
|
|
|
+ String data = PayUtils.buildDataPrivate(json, merKey);
|
|
|
+ result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/v1", merAccount, data);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("错误信息", e);
|
|
|
+ return ResponseJson.error("支付失败", null);
|
|
|
+ }
|
|
|
+ String code = result.getString("code");
|
|
|
+ if (!"000000".equals(code)) {
|
|
|
+ String msg = result.getString("msg");
|
|
|
+ log.info("第三方支付失败>>>>>>>msg:" + msg);
|
|
|
+ return ResponseJson.error(msg, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ //保存分账信息
|
|
|
+ //saveSplitBillDetail(splitBillDetail, result);
|
|
|
+ return ResponseJson.success(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 整理第三方支付详情参数
|
|
|
+ */
|
|
|
+ private String ledgerParameters(List<SplitAccountVo> splitBillDetail, Integer orderId) {
|
|
|
+ List<Map<String, String>> maps = new ArrayList<>();
|
|
|
+ List<ShopOrderVo> shopOrderList = payOrderMapper.findShopOrder(orderId);
|
|
|
+ //供应商子商户总金额
|
|
|
+ for (ShopOrderVo shopOrder : shopOrderList) {
|
|
|
+ BigDecimal shopTotalAmount = BigDecimal.ZERO;
|
|
|
+ String subUserNo = "";
|
|
|
+ for (SplitAccountVo account : splitBillDetail) {
|
|
|
+ if ("4".equals(account.getType()) && shopOrder.getShopID().equals(account.getShopId())) {
|
|
|
+ shopTotalAmount = MathUtil.add(shopTotalAmount, account.getSplitAccount());
|
|
|
+ subUserNo = account.getSubUserNo();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ addMaps(maps, shopTotalAmount, subUserNo);
|
|
|
+ }
|
|
|
+ //公账-专票总金额,私账-无票总金额,公账-普票总金额
|
|
|
+ BigDecimal totalAmount1 = BigDecimal.ZERO;
|
|
|
+ BigDecimal totalAmount2 = BigDecimal.ZERO;
|
|
|
+ BigDecimal totalAmount3 = BigDecimal.ZERO;
|
|
|
+ for (SplitAccountVo account : splitBillDetail) {
|
|
|
+ if ("1".equals(account.getType())) {
|
|
|
+ totalAmount1 = MathUtil.add(totalAmount1, account.getSplitAccount());
|
|
|
+ } else if ("2".equals(account.getType())) {
|
|
|
+ totalAmount2 = MathUtil.add(totalAmount2, account.getSplitAccount());
|
|
|
+ } else if ("3".equals(account.getType())) {
|
|
|
+ totalAmount3 = MathUtil.add(totalAmount3, account.getSplitAccount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ addMaps(maps, totalAmount1, publicAccountNo);
|
|
|
+ addMaps(maps, totalAmount2, privateAccountNo);
|
|
|
+ addMaps(maps, totalAmount3, commonInvoiceNo);
|
|
|
+ return JSON.toJSONString(maps);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addMaps(List<Map<String, String>> maps, BigDecimal shopTotalAmount, String subUserNo) {
|
|
|
+ if (MathUtil.compare(shopTotalAmount, 0) > 0) {
|
|
|
+ Map<String, String> map = new HashMap<>(3);
|
|
|
+ map.put("subUserNo", subUserNo);
|
|
|
+ map.put("splitBillType", "2");
|
|
|
+ map.put("splitBillValue", MathUtil.mul(shopTotalAmount, 100).toString());
|
|
|
+ maps.add(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存分账信息
|
|
|
+ */
|
|
|
+ private void saveSplitBillDetail(List<SplitAccountVo> splitBillDetail, JSONObject result) {
|
|
|
+ JSONObject data = result.getJSONObject("data");
|
|
|
+ String mbOrderId = data.getString("mbOrderId");
|
|
|
+ String orderRequestNo = data.getString("orderId");
|
|
|
+ for (SplitAccountVo splitAccount : splitBillDetail) {
|
|
|
+ splitAccount.setMbOrderId(mbOrderId);
|
|
|
+ splitAccount.setOrderRequestNo(orderRequestNo);
|
|
|
+ splitAccount.setPayStatus("0");
|
|
|
+ payOrderMapper.insertSplitAccount(splitAccount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置第三方支付参数
|
|
|
+ */
|
|
|
+ private JSONObject getPayParameter(PaymentDto payment, long time) throws Exception {
|
|
|
+ String userType = "ENTERPRISE";
|
|
|
+ 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", payment.getClientIp());
|
|
|
+ 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 List<SplitAccountVo> splitBillDetail(OrderVo order, PaymentDto payment) {
|
|
|
+ List<SplitAccountVo> list = new ArrayList<>();
|
|
|
+ //本次支付金额,单位/元
|
|
|
+ BigDecimal payAmount = MathUtil.div(payment.getPayAmount(), 100);
|
|
|
+ //待分账总金额
|
|
|
+ BigDecimal splitAmount = payAmount;
|
|
|
+ //总手续费
|
|
|
+ BigDecimal procedureFee = BigDecimal.ZERO;
|
|
|
+ if ("UNIONPAY".equals(payment.getPayWay())) {
|
|
|
+ procedureFee = new BigDecimal(8);
|
|
|
+ } else {
|
|
|
+ //手续费
|
|
|
+ procedureFee = MathUtil.mul(payAmount, 0.0038, 2);
|
|
|
+ if (MathUtil.compare(procedureFee, 0) == 0) {
|
|
|
+ procedureFee = new BigDecimal("0.01");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ splitAmount = MathUtil.sub(splitAmount, procedureFee);
|
|
|
+ List<OrderProductVo> orderProductList = payOrderMapper.findAllOrderProduct(order.getOrderId());
|
|
|
+ for (OrderProductVo orderProduct : orderProductList) {
|
|
|
+ BigDecimal costPrice = MathUtil.mul(orderProduct.getCostPrice(), orderProduct.getNum());
|
|
|
+ //不含税能开发票
|
|
|
+ if ("0".equals(orderProduct.getIncludedTax()) && !"3".equals(orderProduct.getInvoiceType())) {
|
|
|
+ //应付总税费
|
|
|
+ BigDecimal payableTax = MathUtil.mul(orderProduct.getSingleShouldPayTotalTax(), orderProduct.getNum());
|
|
|
+ costPrice = MathUtil.add(costPrice, payableTax);
|
|
|
+ }
|
|
|
+ //判断是否支付过
|
|
|
+ BigDecimal paidAmount = payOrderMapper.findPaidAmount(orderProduct.getOrderProductId());
|
|
|
+ if (paidAmount == null || MathUtil.compare(paidAmount, costPrice) < 0) {
|
|
|
+ if (paidAmount != null && MathUtil.compare(paidAmount, 0) > 0) {
|
|
|
+ costPrice = MathUtil.sub(costPrice, paidAmount);
|
|
|
+ }
|
|
|
+ //待分账金额>=本次待分账金额
|
|
|
+ if (MathUtil.compare(splitAmount, costPrice) > -1) {
|
|
|
+ splitAmount = MathUtil.sub(splitAmount, costPrice);
|
|
|
+ } else {
|
|
|
+ costPrice = splitAmount;
|
|
|
+ splitAmount = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ String commercialCode = payOrderMapper.findCommercialCode(orderProduct.getShopId());
|
|
|
+ SplitAccountVo splitAccount = new SplitAccountVo();
|
|
|
+ splitAccount.setOrderId(order.getOrderId());
|
|
|
+ splitAccount.setOrderProductId(orderProduct.getOrderProductId());
|
|
|
+ splitAccount.setShopId(orderProduct.getShopId().intValue());
|
|
|
+ splitAccount.setSplitAccount(costPrice);
|
|
|
+ splitAccount.setProductType("1");
|
|
|
+ if (StringUtils.isNotBlank(commercialCode)) {
|
|
|
+ //供应商拥有子商户号
|
|
|
+ splitAccount.setType("4");
|
|
|
+ splitAccount.setSubUserNo(commercialCode);
|
|
|
+ } else {
|
|
|
+ if ("3".equals(orderProduct.getInvoiceType())) {
|
|
|
+ //不能开票,则分账到私账-无票
|
|
|
+ splitAccount.setType("2");
|
|
|
+ splitAccount.setSubUserNo(privateAccountNo);
|
|
|
+ } else if ("1".equals(orderProduct.getInvoiceType())) {
|
|
|
+ //开增值税发票,则分账到公账-专票
|
|
|
+ splitAccount.setType("1");
|
|
|
+ splitAccount.setSubUserNo(publicAccountNo);
|
|
|
+ } else if ("2".equals(orderProduct.getInvoiceType())) {
|
|
|
+ //开普通发票,则分账到公账-普票
|
|
|
+ splitAccount.setType("3");
|
|
|
+ splitAccount.setSubUserNo(commonInvoiceNo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list.add(splitAccount);
|
|
|
+ if (MathUtil.compare(splitAmount, 0) == 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //付供应商运费,是以供应商为单位的
|
|
|
+ if (MathUtil.compare(splitAmount, 0) > 0) {
|
|
|
+ List<ShopOrderVo> shopOrderList = payOrderMapper.findShopOrder(order.getOrderId());
|
|
|
+ for (ShopOrderVo shopOrder : shopOrderList) {
|
|
|
+ //运费
|
|
|
+ BigDecimal shopPostFee = shopOrder.getShopPostFee();
|
|
|
+ if (MathUtil.compare(shopPostFee, 0) > 0) {
|
|
|
+ BigDecimal shipping = payOrderMapper.findShipping(order.getOrderId(), shopOrder.getShopID());
|
|
|
+ shopPostFee = MathUtil.sub(shopPostFee, shipping);
|
|
|
+ if (MathUtil.compare(splitAmount, shopPostFee) > -1) {
|
|
|
+ splitAmount = MathUtil.sub(splitAmount, shipping);
|
|
|
+ } else {
|
|
|
+ shopPostFee = splitAmount;
|
|
|
+ splitAmount = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ String commercialCode = payOrderMapper.findCommercialCode(Long.valueOf(shopOrder.getShopID()));
|
|
|
+ SplitAccountVo splitAccount = new SplitAccountVo();
|
|
|
+ splitAccount.setOrderId(order.getOrderId());
|
|
|
+ splitAccount.setShopId(shopOrder.getShopID());
|
|
|
+ splitAccount.setSplitAccount(shopPostFee);
|
|
|
+ splitAccount.setProductType("2");
|
|
|
+ if (StringUtils.isNotBlank(commercialCode)) {
|
|
|
+ //供应商拥有子商户号
|
|
|
+ splitAccount.setType("4");
|
|
|
+ splitAccount.setSubUserNo(commercialCode);
|
|
|
+ } else {
|
|
|
+ //私账
|
|
|
+ splitAccount.setType("2");
|
|
|
+ splitAccount.setSubUserNo(privateAccountNo);
|
|
|
+ }
|
|
|
+ list.add(splitAccount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //佣金,私账
|
|
|
+ if (MathUtil.compare(splitAmount, 0) > 0) {
|
|
|
+ SplitAccountVo splitAccount = new SplitAccountVo();
|
|
|
+ splitAccount.setOrderId(order.getOrderId());
|
|
|
+ splitAccount.setSplitAccount(splitAmount);
|
|
|
+ splitAccount.setProductType("3");
|
|
|
+ splitAccount.setType("2");
|
|
|
+ splitAccount.setSubUserNo(privateAccountNo);
|
|
|
+ list.add(splitAccount);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public String paymentCallback(String data) throws Exception {
|
|
|
+ //公钥解密
|
|
|
+ JSONObject json = PayUtils.decryptDataPublic(data, publicKey);
|
|
|
+ log.info("公钥解密>>>>>>" + json);
|
|
|
+ //公钥验签
|
|
|
+ String signaa = json.getString("sign");
|
|
|
+ json.remove("sign");
|
|
|
+ String signbb = PayUtils.buildSign(json, publicKey);
|
|
|
+ if (!signaa.equals(signbb)) {
|
|
|
+ return "验签失败";
|
|
|
+ }
|
|
|
+ //订单状态
|
|
|
+ String orderStatus = json.getString("orderStatus");
|
|
|
+ //附加数据,下单时若有传输则原样返回,下单时为空,则不返回该数据
|
|
|
+ String attach = json.getString("attach");
|
|
|
+ //平台唯一流水号
|
|
|
+ String mbOrderId = json.getString("mbOrderId");
|
|
|
+ //商户唯一订单号
|
|
|
+ String orderRequestNo = json.getString("orderId");
|
|
|
+ //订单金额,以元为单位
|
|
|
+ BigDecimal amount = json.getBigDecimal("amount");
|
|
|
+ log.info("订单状态>>>>>>" + orderStatus);
|
|
|
+ if ("FAILED".equals(orderStatus)) {
|
|
|
+ return "支付失败";
|
|
|
+ }
|
|
|
+ String[] split = attach.split(",");
|
|
|
+ //订单id
|
|
|
+ Integer orderId = Integer.valueOf(split[0]);
|
|
|
+ //支付类型
|
|
|
+ String payType = split[1];
|
|
|
+ OrderVo order = payOrderMapper.findOrder(orderId);
|
|
|
+ //支付记录
|
|
|
+ List<DiscernReceiptVo> discernReceiptList = payOrderMapper.getDiscernReceipt(order);
|
|
|
+ BigDecimal receiptAmount = BigDecimal.ZERO;
|
|
|
+ if (null != discernReceiptList && discernReceiptList.size() > 0) {
|
|
|
+ for (DiscernReceiptVo discernReceipt : discernReceiptList) {
|
|
|
+ receiptAmount = receiptAmount.add(discernReceipt.getAssociateAmount());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //已付金额+本次支付金额
|
|
|
+ receiptAmount = MathUtil.add(receiptAmount, amount);
|
|
|
+ log.info("已付金额+本次支付金额>>>>>>>" + receiptAmount);
|
|
|
+ if (MathUtil.compare(order.getPayableAmount(), receiptAmount) == 0) {
|
|
|
+ /*
|
|
|
+ * 订单全部支付
|
|
|
+ * 0待确认,11待收待发,12待收部发,13待收全发,21部收待发,22部收部发,23部收全发,
|
|
|
+ * 31已收待发,32已收部发,33已收全发,4交易完成,5订单完成,6已关闭,7交易全退
|
|
|
+ */
|
|
|
+ if ("11".equals(order.getStatus()) || "21".equals(order.getStatus())) {
|
|
|
+ order.setStatus("31");
|
|
|
+ } else if ("12".equals(order.getStatus()) || "22".equals(order.getStatus())) {
|
|
|
+ order.setStatus("32");
|
|
|
+ } else {
|
|
|
+ order.setStatus("33");
|
|
|
+ }
|
|
|
+ order.setPayFlag("1");
|
|
|
+ order.setOnlinePayFlag("0");
|
|
|
+ //(收款买家)收款状态:1待收款、2部分收款、3已收款
|
|
|
+ order.setReceiptStatus("3");
|
|
|
+ log.info("订单全部支付,修改订单状态>>>>>>" + order.getStatus());
|
|
|
+ } else {
|
|
|
+ //部分支付
|
|
|
+ if ("11".equals(order.getStatus()) || "21".equals(order.getStatus())) {
|
|
|
+ order.setStatus("21");
|
|
|
+ } else if ("12".equals(order.getStatus()) || "22".equals(order.getStatus())) {
|
|
|
+ order.setStatus("22");
|
|
|
+ } else {
|
|
|
+ order.setStatus("23");
|
|
|
+ }
|
|
|
+ order.setOnlinePayFlag("0");
|
|
|
+ //(收款买家)收款状态:1待收款、2部分收款、3已收款
|
|
|
+ order.setReceiptStatus("2");
|
|
|
+ }
|
|
|
+ //更新付款次数
|
|
|
+ order.setPaySuccessCounter(order.getPaySuccessCounter() + 1);
|
|
|
+ payOrderMapper.updateSelective(order);
|
|
|
+ //修改支付链接状态
|
|
|
+ OrderPayLinkVo orderPayLink = payOrderMapper.findOrderPayLink(order.getOrderId(), amount);
|
|
|
+ if (null != orderPayLink && ("12".equals(payType) || "17".equals(payType))) {
|
|
|
+ orderPayLink.setPayStatus("1");
|
|
|
+ payOrderMapper.updateOrderPayLink(orderPayLink);
|
|
|
+ }
|
|
|
+ //保存收款记录
|
|
|
+ String curDateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
|
|
|
+ CmDiscernReceiptPo discernReceipt = new CmDiscernReceiptPo();
|
|
|
+ discernReceipt.setPayWay("1");
|
|
|
+ discernReceipt.setPayType(payType);
|
|
|
+ discernReceipt.setReceiptType("1");
|
|
|
+ discernReceipt.setReceiptStatus("3");
|
|
|
+ discernReceipt.setReceiptAmount(amount);
|
|
|
+ discernReceipt.setConfirmType("4");
|
|
|
+ discernReceipt.setRePayFlag("1");
|
|
|
+ discernReceipt.setFormData(json.toJSONString());
|
|
|
+ discernReceipt.setReceiptDate(curDateStr);
|
|
|
+ discernReceipt.setConfirmDate(curDateStr);
|
|
|
+ discernReceipt.setReviewDate(curDateStr);
|
|
|
+ discernReceipt.setUpdateDate(curDateStr);
|
|
|
+ discernReceipt.setDelFlag("0");
|
|
|
+ payOrderMapper.insertDiscernReceipt(discernReceipt);
|
|
|
+ CmReceiptOrderRelationPo relation = new CmReceiptOrderRelationPo();
|
|
|
+ relation.setReceiptID(discernReceipt.getId().intValue());
|
|
|
+ relation.setOrderID(order.getOrderId());
|
|
|
+ relation.setAssociateAmount(amount);
|
|
|
+ relation.setRelationType("2");
|
|
|
+ relation.setMbOrderId(mbOrderId);
|
|
|
+ relation.setOrderRequestNo(orderRequestNo);
|
|
|
+ relation.setDelFlag("0");
|
|
|
+ relation.setSplitStatus("0");
|
|
|
+ payOrderMapper.insertOrderRelation(relation);
|
|
|
+ log.info(">>>>>>>>>>>>>>>>>>>>>>>保存付款金额到收款记录," + amount);
|
|
|
+ //修改分账付款状态
|
|
|
+ //payOrderMapper.updateSplitAccountByPay(mbOrderId);
|
|
|
+ return "SUCCESS";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 支付状态转换
|
|
|
+ */
|
|
|
+ private String getPayType(PaymentDto payment) {
|
|
|
+ String payType = payment.getPayType();
|
|
|
+ if ("ALIPAY_H5".equals(payType)) {
|
|
|
+ //支付宝H5
|
|
|
+ return "14";
|
|
|
+ } else if ("JSAPI_WEIXIN".equals(payType)) {
|
|
|
+ //微信公众号支付
|
|
|
+ return "13";
|
|
|
+ } else if ("MINIAPP_WEIXIN".equals(payType)) {
|
|
|
+ //微信小程序支付
|
|
|
+ return "15";
|
|
|
+ } else if ("GATEWAY_UNIONPAY".equals(payType) && "ENTERPRISE".equals(payment.getUserType())) {
|
|
|
+ //企业网银
|
|
|
+ return "12";
|
|
|
+ } else {
|
|
|
+ //个人网银
|
|
|
+ return "17";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<String> payLink(OrderPayLinkVo orderPayLink) {
|
|
|
+ if (null == orderPayLink) {
|
|
|
+ return ResponseJson.error("参数异常", null);
|
|
|
+ }
|
|
|
+ OrderVo order = payOrderMapper.findOrder(orderPayLink.getOrderId().intValue());
|
|
|
+ if (null == order) {
|
|
|
+ return ResponseJson.error("订单不存在", null);
|
|
|
+ }
|
|
|
+ // 时间戳
|
|
|
+ long time = System.currentTimeMillis() / 1000;
|
|
|
+ String linkLogo = RandomCodeGenerator.generateCodeString(4);
|
|
|
+ try {
|
|
|
+ linkLogo = Md5Util.MD5To16Bit(linkLogo + time);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ orderPayLink.setLinkLogo(linkLogo);
|
|
|
+ Date date = new Date();
|
|
|
+ GregorianCalendar calendar = new GregorianCalendar();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.add(Calendar.DATE, 1);
|
|
|
+ orderPayLink.setGenerateTime(date);
|
|
|
+ orderPayLink.setEffectiveTime(calendar.getTime());
|
|
|
+ orderPayLink.setPayStatus("0");
|
|
|
+ orderPayLink.setDelFlag("0");
|
|
|
+ OrderPayLinkVo orderPayLinkVo = payOrderMapper.findOrderPayLink(orderPayLink.getOrderId().intValue(), null);
|
|
|
+ if (null == orderPayLinkVo) {
|
|
|
+ payOrderMapper.insertOrderPayLink(orderPayLink);
|
|
|
+ } else {
|
|
|
+ orderPayLink.setId(orderPayLinkVo.getId());
|
|
|
+ payOrderMapper.updateOrderPayLink(orderPayLink);
|
|
|
+ }
|
|
|
+ String link = orderPayLink.getRedirectLink() + "?linkLogo=" + linkLogo;
|
|
|
+ return ResponseJson.success(link);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void jumpPage(String linkLogo, String linkPage, ServerHttpResponse response) throws IOException {
|
|
|
+ String linkUrl = linkPage + "?linkLogo=" + linkLogo;
|
|
|
+ response.setStatusCode(HttpStatus.FOUND);
|
|
|
+ response.getHeaders().setLocation(URI.create(linkUrl));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Map<String, Object>> linkData(String linkLogo) {
|
|
|
+ Map<String, Object> map = new HashMap<>(3);
|
|
|
+ OrderPayLinkVo orderPayLink = payOrderMapper.getOrderPayLink(linkLogo);
|
|
|
+ //链接状态,0成功
|
|
|
+ int code = 0;
|
|
|
+ if (null == orderPayLink) {
|
|
|
+ //已重新生成链接
|
|
|
+ return ResponseJson.error("链接更新,请重新获取");
|
|
|
+ } else {
|
|
|
+ OrderVo order = payOrderMapper.findOrder(orderPayLink.getOrderId().intValue());
|
|
|
+ List<DiscernReceiptVo> discernReceiptList = payOrderMapper.getDiscernReceipt(order);
|
|
|
+ if (orderPayLink.getEffectiveTime().compareTo(new Date()) < 0) {
|
|
|
+ //链接失效
|
|
|
+ code = -3;
|
|
|
+ }
|
|
|
+ if (null != discernReceiptList && discernReceiptList.size() > 0) {
|
|
|
+ for (DiscernReceiptVo discernReceipt : discernReceiptList) {
|
|
|
+ if ("2".equals(discernReceipt.getPayWay())) {
|
|
|
+ // 已线下支付
|
|
|
+ code = -2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ("1".equals(orderPayLink.getPayStatus())) {
|
|
|
+ //链接已支付
|
|
|
+ code = 5;
|
|
|
+ }
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String time = dateFormat.format(orderPayLink.getEffectiveTime());
|
|
|
+ map.put("orderPayLink", orderPayLink);
|
|
|
+ map.put("code", code);
|
|
|
+ map.put("time", time);
|
|
|
+ }
|
|
|
+ return ResponseJson.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<String> payWhetherSuccess(Integer orderId, Integer paySuccessCounter) {
|
|
|
+ OrderVo order = payOrderMapper.findOrder(orderId);
|
|
|
+ if (order.getPaySuccessCounter().equals(paySuccessCounter)) {
|
|
|
+ return ResponseJson.error(-2, "支付失败", null);
|
|
|
+ } else if (order.getPaySuccessCounter() > paySuccessCounter) {
|
|
|
+ return ResponseJson.success("支付成功");
|
|
|
+ } else {
|
|
|
+ return ResponseJson.error("支付异常>>>>>>>" + order, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ResponseJson<JSONObject> findOrderStatus(String mbOrderId) {
|
|
|
+ // 时间戳
|
|
|
+ long time = System.currentTimeMillis() / 1000;
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put("merAccount", merAccount);
|
|
|
+ json.put("mbOrderId", mbOrderId);
|
|
|
+ json.put("time", time);
|
|
|
+ String sign = PayUtils.buildSign(json, merKey);
|
|
|
+ json.put("sign", sign);
|
|
|
+ String data = "";
|
|
|
+ try {
|
|
|
+ data = PayUtils.buildDataPrivate(json, merKey);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("错误信息", e);
|
|
|
+ }
|
|
|
+ JSONObject result = PayUtils.httpGet("https://platform.mhxxkj.com/paygateway/mbpay/order/query/v1_1", merAccount, data);
|
|
|
+ String code = result.getString("code");
|
|
|
+ if (!"000000".equals(code)) {
|
|
|
+ String msg = result.getString("msg");
|
|
|
+ log.info("第三方查询订单失败>>>>>>>msg:" + msg);
|
|
|
+ return ResponseJson.error(msg, null);
|
|
|
+ }
|
|
|
+ return ResponseJson.success(result);
|
|
|
+ }
|
|
|
+}
|