yanderong
2026-07-23 393c3178a7beeb39915aff302d9f30ca2b6e5247
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/OrderController.java
@@ -5,10 +5,14 @@
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.CharteredCar.server.IOrderCharteredCarService;
import com.stylefeng.guns.modular.account.controller.AppOrderController;
import com.stylefeng.guns.modular.call.server.IOrderCallService;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.shunfeng.model.Financial;
import com.stylefeng.guns.modular.shunfeng.model.OrderRide;
import com.stylefeng.guns.modular.shunfeng.service.IDriverRideService;
import com.stylefeng.guns.modular.shunfeng.service.IFinancialService;
import com.stylefeng.guns.modular.shunfeng.service.IOrderRideService;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
@@ -39,7 +43,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@@ -149,6 +159,9 @@
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private RestTemplate internalRestTemplate;
    @Resource
    private ISystemPriceCityService systemPriceCityService;
    @Value("${trip.sheet.filePath}")
@@ -163,6 +176,10 @@
    @Autowired
    private IOrderRideService orderRideService;
    @Autowired
    private IFinancialService financialService;
    @Autowired
    private ISystemNoticeService systemNoticeService;
    /*顺风车司机*/
    @Autowired
    private IDriverRideService driverRideService;
@@ -170,6 +187,9 @@
    private IOrderPrivateCarService iOrderPrivateCarService;
    @Autowired
    private IIncomeService incomeService;
    @Autowired
    private AppOrderController appOrderController;
    /**
@@ -638,6 +658,7 @@
            }
            return ResultUtil.success(orderInfoWarpper);
        } catch (Exception e) {
            log.error("获取服务中的详情订单数据异常,接口:{}, 错误信息:", "/api/order/queryOrderInfo", e);
            e.printStackTrace();
            return ResultUtil.runErr();
        }
@@ -1502,6 +1523,7 @@
            }
            return ResultUtil.success();
        } catch (Exception e) {
            log.error("订单完成支付订单操作,异常,接口:/api/taxi/payTaxiOrder1,错误信息:", e);
            e.printStackTrace();
            return ResultUtil.runErr();
        }
@@ -1702,7 +1724,121 @@
            e.printStackTrace();
        }
    }
    /**
     * 取消订单微信回调
     *
     * @param request
     */
    @ResponseBody
    @PostMapping("/base/cbyzxCancelOrderTaxi")
    public Map<String, Object> cbyzxCancelOrderTaxi(@RequestBody(required = false) String params,
                                                    HttpServletRequest request, HttpServletResponse response) {
        try {
            JSONObject payload = parseCbyzxCallbackPayload(params, request);
            String refundNo = firstNotBlank(payload,
                    "refundOutRequestNo", "refund_out_request_no", "outRefundNo", "out_refund_no",
                    "merchantRefundSerialNo", "merchant_refund_serial_no");
            if (StringUtils.isBlank(refundNo)) {
                return buildCbyzxCallbackResponse(0, "success");
            }
            if (!refundNo.startsWith("PRF_")) {
                return buildCbyzxCallbackResponse(0, "success");
            }
            String[] split = refundNo.split("_");
            if (split.length < 4 || !StringUtils.isNumeric(split[1]) || !StringUtils.isNumeric(split[2])) {
                return buildCbyzxCallbackResponse(1, "invalid refundOutRequestNo");
            }
            Integer orderType = Integer.valueOf(split[1]);
            Integer orderId = Integer.valueOf(split[2]);
            String refundId = firstNotBlank(payload,
                    "refundId", "refund_id", "gatewaySerialNo", "gateway_serial_no",
                    "thirdRefundNo", "third_refund_no", "merchantRefundSerialNo", "merchant_refund_serial_no");
            Map<String, Object> driverResult = notifyDriverPrepayRefund(orderId, orderType, refundNo, refundId);
            Object code = driverResult == null ? null : driverResult.get("code");
            if (code != null && "200".equals(String.valueOf(code))) {
                return buildCbyzxCallbackResponse(0, "success");
            }
            log.error("创博优退款回调转发司机端失败,payload={}, driverResult={}", payload, driverResult);
            return buildCbyzxCallbackResponse(1, "driver callback fail");
        } catch (Exception e) {
            log.error("创博优退款回调处理失败", e);
            return buildCbyzxCallbackResponse(1, "fail");
        }
//        try {
//            Map<String, String> map = payMoneyUtil.weixinpayCallback(request);
//            if (null != map) {
//                String order_id = map.get("transaction_id");
//                String out_trade_no = map.get("out_trade_no");
//                String result = map.get("result");
//                if (ToolUtil.isNotEmpty(out_trade_no) && ToolUtil.isNotEmpty(order_id)) {
//                    PrintWriter out = response.getWriter();
//                    out.write(result);
//                    out.flush();
//                    out.close();
//
//                    String[] split = out_trade_no.split("_");
//                    Integer id = Integer.valueOf(split[0]);
//                    Integer type = Integer.valueOf(split[1]);
//                    switch (type) {
//                        case 1:
//                            orderPrivateCarService.payCancelOrderPrivateCar(id, order_id, 1);
//                            break;
//                        case 2:
//                            orderTaxiService.payCancelOrderTaxi(id, order_id, 1);
//                            break;
//                        case 3:
//                            orderCrossCityService.payCancelOrderCrossCity(id, order_id, 1);
//                            break;
//                    }
//                }
////                cloudPay(map.get("total_fee"),map.get("transaction_id"),1);
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
    /**
     * 内部服务调用:创博优微信先付订单退款。
     * 只负责调用退款网关并返回原始结果,不修改司机端订单业务状态。
     */
    @ResponseBody
    @PostMapping("/base/internal/cbyzxPrepayRefund")
    public JSONObject cbyzxPrepayRefund(String outRequestNo, String refundOutRequestNo,
                                        Integer refundAmount, String refundDesc) {
        JSONObject error = new JSONObject();
        if (ToolUtil.isEmpty(outRequestNo)) {
            error.put("code", 1);
            error.put("msg", "missing outRequestNo");
            return error;
        }
        if (ToolUtil.isEmpty(refundOutRequestNo)) {
            error.put("code", 1);
            error.put("msg", "missing refundOutRequestNo");
            return error;
        }
        if (refundAmount == null || refundAmount <= 0) {
            error.put("code", 1);
            error.put("msg", "invalid refundAmount");
            return error;
        }
        try {
            CbyzxPayUtil.ClosePayOrderRequest closeRequest = new CbyzxPayUtil.ClosePayOrderRequest();
            closeRequest.setOutRequestNo(outRequestNo);
            closeRequest.setRefundOutRequestNo(refundOutRequestNo);
            closeRequest.setRefundAmount(refundAmount);
            closeRequest.setRefundDesc(ToolUtil.isNotEmpty(refundDesc) ? refundDesc : "微信先付订单退款");
            return CbyzxPayUtil.closePayOrder(closeRequest);
        } catch (Exception e) {
            log.error("内部创博优先付退款失败,outRequestNo={}, refundOutRequestNo={}",
                    outRequestNo, refundOutRequestNo, e);
            error.put("code", 1);
            error.put("msg", e.getMessage());
            return error;
        }
    }
    /**
     * 取消订单支付宝回调
@@ -1803,59 +1939,173 @@
    /**
     * 创博优完成订单支付回调。
     * out_trade_no 为平台订单号,用于反解订单id和订单类型;
     * trade_no 为创博优支付流水号,用于标记真实支付流水;
     * trade_status=TRADE_SUCCESS 才认为支付成功。
     * outRequestNo 为平台订单号,用于反解订单id和订单类型;
     * paymentOrderId 为创博优支付流水号,用于标记真实支付流水;
     * paidTime 为支付完成时间,paidAmount 为实付金额。
     */
    @ResponseBody
    @PostMapping("/base/cbyzxPayOrderTaxi")
    public Map<String, Object> cbyzxPayOrderTaxi(HttpServletRequest request) {
    public Map<String, Object> cbyzxPayOrderTaxi(@RequestBody String params, HttpServletRequest request) {
        try {
            JSONObject payload = cbyzxOrderPayHelper.parseCallbackRequest(request);
            log.info("创博优完成订单支付回调,notify_type={}, out_trade_no={}, trade_no={}, trade_status={}",
                    cbyzxOrderPayHelper.extractNotifyType(payload),
            JSONObject payload = JSONObject.parseObject(params);
            log.error("创博优完成订单支付回调,outRequestNo={}, paymentOrderId={}, paidTime={}, paidAmount={}",
                    cbyzxOrderPayHelper.extractOutRequestNo(payload),
                    cbyzxOrderPayHelper.extractThirdPartyOrderNo(payload),
                    payload == null ? null : payload.getString("trade_status"));
                    payload == null ? null : payload.getString("paidTime"),
                    payload == null ? null : payload.getString("paidAmount"));
            if (payload == null) {
                return buildCbyzxCallbackResponse(1, "invalid request");
            }
            if (!cbyzxOrderPayHelper.isPaySuccess(payload)) {
                return buildCbyzxCallbackResponse(1, cbyzxOrderPayHelper.extractMessage(payload));
            }
            String outRequestNo = cbyzxOrderPayHelper.extractOutRequestNo(payload);
            if (StringUtils.isBlank(outRequestNo)) {
                return buildCbyzxCallbackResponse(1, "missing outRequestNo");
            }
            String paymentOrderId = cbyzxOrderPayHelper.extractThirdPartyOrderNo(payload);
            if (StringUtils.isBlank(paymentOrderId)) {
                return buildCbyzxCallbackResponse(1, "missing paymentOrderId");
            }
            if (payload.get("paidAmount") == null) {
                return buildCbyzxCallbackResponse(1, "missing paidAmount");
            }
            String[] split = outRequestNo.split("_");
            if (split.length < 2) {
                return buildCbyzxCallbackResponse(1, "invalid outRequestNo");
            }
            Integer id = Integer.valueOf(split[0]);
            Integer type = Integer.valueOf(split[1]);
            String recordCode = StringUtils.defaultIfBlank(cbyzxOrderPayHelper.extractRecordCode(payload), outRequestNo);
            String orderId = StringUtils.defaultIfBlank(cbyzxOrderPayHelper.extractThirdPartyOrderNo(payload), recordCode);
            String orderId = paymentOrderId;
            switch (type) {
                case 1:
                    orderPrivateCarService.payOrderPrivateCarCallback(id, orderId, 1, recordCode);
            if (StringUtils.isNumeric(split[0])) {
                Integer id = Integer.valueOf(split[0]);
                Integer type = Integer.valueOf(split[1]);
                switch (type) {
                    case 1:
                        orderPrivateCarService.payOrderPrivateCarCallback(id, orderId, 1, recordCode);
                        break;
                    case 2:
                        orderTaxiService.payOrderTaxiCallback(id, orderId, 1, 0, recordCode);
                        break;
                    case 3:
                        orderCrossCityService.payOrderCrossCityCallback(id, orderId, 1, recordCode);
                        break;
                    case 4:
                        orderLogisticsService.payOrderLogisticsCallback(id, orderId, 1);
                        break;
                    case 5:
                        orderLogisticsService.payOrderLogisticsCallback(id, orderId, 1);
                        break;
                    case 8:
                        OrderRide orderRide = orderRideService.selectById(id);
                        if (orderRide == null) {
                            return buildCbyzxCallbackResponse(1, "order not found");
                        }
                        if (orderRide.getState() == null || orderRide.getState() != 2) {
//                            orderRide.setOutNum(orderId);
                            orderRide.setPayTime(new Date());
                            orderRide.setPayType(2);
                            orderRide.setState(2);
                            Financial financial = new Financial();
                            financial.setType(1);
                            financial.setPayType("2");
                            financial.setMoney(orderRide.getMoney());
                            financial.setOrderNum(orderRide.getOrderNum());
                            financial.setLsType(orderId);
                            financial.setAddTime(new Date());
                            financial.setTradeStatus("SUCCESS");
                            financial.setPwType(8);
                            financial.setUserId(orderRide.getUserId());
                            financial.setLx(1);
                            financial.setOrderType(4);
                            financialService.insert(financial);
                            orderRideService.updateById(orderRide);
                            systemNoticeService.addSystemNotice(1, "您成功支付从" + orderRide.getStartName() + "到" + orderRide.getEndName() + "的顺风车订单", orderRide.getUserId(), 1);
                        }
                        break;
                    default:
                        return buildCbyzxCallbackResponse(1, "unsupported orderType");
                }
                return buildCbyzxCallbackResponse(0, "success");
            }
            switch (split[0]) {
                case "CANCEL":
                    if (split.length < 3) {
                        return buildCbyzxCallbackResponse(1, "invalid cancel outRequestNo");
                    }
                    Integer cancelId = Integer.valueOf(split[1]);
                    Integer cancelType = Integer.valueOf(split[2]);
                    switch (cancelType) {
                        case 1:
                            orderPrivateCarService.payCancelOrderPrivateCar(cancelId, orderId, 1);
                            break;
                        case 2:
                            orderTaxiService.payCancelOrderTaxi(cancelId, orderId, 1);
                            break;
                        case 3:
                            orderCrossCityService.payCancelOrderCrossCity(cancelId, orderId, 1);
                            break;
                        default:
                            return buildCbyzxCallbackResponse(1, "unsupported cancel orderType");
                    }
                    break;
                case 2:
                    orderTaxiService.payOrderTaxiCallback(id, orderId, 1, 0, recordCode);
                case "THANK":
                    if (split.length < 3) {
                        return buildCbyzxCallbackResponse(1, "invalid thank outRequestNo");
                    }
                    Integer thankId = Integer.valueOf(split[1]);
                    Integer thankType = Integer.valueOf(split[2]);
                    switch (thankType) {
                        case 1:
                            orderPrivateCarService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        case 2:
                            orderTaxiService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        case 3:
                            orderCrossCityService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        case 4:
                            orderLogisticsService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        case 5:
                            orderLogisticsService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        case 6:
                            orderCharteredCarService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        case 7:
                            orderCallService.payThankYouFeeCallback(thankId, orderId, 1);
                            break;
                        default:
                            return buildCbyzxCallbackResponse(1, "unsupported thank orderType");
                    }
                    break;
                case 3:
                    orderCrossCityService.payOrderCrossCityCallback(id, orderId, 1, recordCode);
                case "SPREAD":
                    if (split.length < 3) {
                        return buildCbyzxCallbackResponse(1, "invalid spread outRequestNo");
                    }
                    orderLogisticsService.payOrderLogisticsSpreadCallback(Integer.valueOf(split[1]), orderId, 1);
                    break;
                case 4:
                    orderLogisticsService.payOrderLogisticsCallback(id, orderId, 1);
                case "CARD":
                    taxiCardService.payTaxiCardPaymentSpread(Integer.valueOf(split[1]), orderId);
                    break;
                case 5:
                    orderLogisticsService.payOrderLogisticsCallback(id, orderId, 1);
                case "BALANCE":
                    if (split.length < 3) {
                        return buildCbyzxCallbackResponse(1, "invalid balance outRequestNo");
                    }
                    Integer uid = Integer.valueOf(split[1]);
                    Integer paymentRecordId = Integer.valueOf(split[2]);
                    PaymentRecord paymentRecord = paymentRecordService.selectById(paymentRecordId);
                    if (paymentRecord == null) {
                        return buildCbyzxCallbackResponse(1, "paymentRecord not found");
                    }
                    if (paymentRecord.getState() == 1) {
                        userInfoService.payCancelUserBalance(uid, orderId, paymentRecordId, 1);
                        appOrderController.createAllocationReqUser(1, uid, 1, paymentRecord.getAmount());
                    }
                    break;
                default:
                    return buildCbyzxCallbackResponse(1, "unsupported orderType");
                    return buildCbyzxCallbackResponse(1, "unsupported scene");
            }
            return buildCbyzxCallbackResponse(0, "success");
        } catch (Exception e) {
@@ -1918,6 +2168,69 @@
        return result;
    }
    private JSONObject parseCbyzxCallbackPayload(String params, HttpServletRequest request) {
        JSONObject payload = new JSONObject();
        if (StringUtils.isNotBlank(params)) {
            try {
                JSONObject body = JSONObject.parseObject(params);
                if (body != null) {
                    payload.putAll(body);
                }
            } catch (Exception e) {
                log.warn("创博优回调body不是JSON,params={}", params);
            }
        }
        if (request != null && request.getParameterMap() != null) {
            for (String key : request.getParameterMap().keySet()) {
                String[] values = request.getParameterValues(key);
                if (values == null || values.length == 0) {
                    continue;
                }
                payload.put(key, values.length == 1 ? values[0] : Arrays.asList(values));
            }
        }
        return payload;
    }
    private String firstNotBlank(JSONObject payload, String... keys) {
        if (payload == null || keys == null) {
            return null;
        }
        for (String key : keys) {
            Object value = payload.get(key);
            if (value == null) {
                continue;
            }
            if (value instanceof Collection) {
                for (Object item : (Collection<?>) value) {
                    String text = item == null ? null : String.valueOf(item);
                    if (StringUtils.isNotBlank(text)) {
                        return text;
                    }
                }
            } else {
                String text = String.valueOf(value);
                if (StringUtils.isNotBlank(text)) {
                    return text;
                }
            }
        }
        return null;
    }
    private Map<String, Object> notifyDriverPrepayRefund(Integer orderId, Integer orderType, String refundNo, String refundId) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, Object> postParams = new LinkedMultiValueMap<>();
        postParams.add("orderId", orderId);
        postParams.add("orderType", orderType);
        postParams.add("refundNo", refundNo);
        postParams.add("refundId", refundId);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(postParams, headers);
        return internalRestTemplate.postForObject(
                "http://driver-server/base/internal/prepayRefundCallback", requestEntity, Map.class);
    }
    /**
     * 小件物流差价微信支付回调