yanderong
2026-06-09 b19474186df25762866bd9323c3f3ac8be4de9b9
feat:司机APP充值对接微信支付和数据上报
5个文件已修改
88 ■■■■■ 已修改文件
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/CallbackController.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/PayController.java 14 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/PayServiceImpl.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverOKTravel/guns-admin/src/main/resources/application.yml 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ManagementOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/PushEvaluationImpl.java 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/CallbackController.java
@@ -4,10 +4,11 @@
import com.stylefeng.guns.modular.system.model.Driver;
import com.stylefeng.guns.modular.system.model.TransactionDetails;
import com.stylefeng.guns.modular.system.service.IDriverService;
import com.stylefeng.guns.modular.system.service.IPayService;
import com.stylefeng.guns.modular.system.service.IReassignService;
import com.stylefeng.guns.modular.system.service.ITransactionDetailsService;
import com.stylefeng.guns.modular.system.util.PayMoneyUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -22,6 +23,7 @@
/**
 * 第三方支付回调控制器
 */
@Slf4j
@RestController
@RequestMapping("/base")
public class CallbackController {
@@ -36,6 +38,8 @@
    private ITransactionDetailsService transactionDetailsService;
    @Autowired
    private IDriverService driverService;
    @Autowired
    private IPayService payService;
    // 司机端充值余额 微信回调
@ResponseBody
@@ -126,4 +130,19 @@
            e.printStackTrace();
        }
    }
    /**
     * 司机端充值余额 微信回调(app)
     */
    @ResponseBody
    @PostMapping("/wechat/notify")
    public Map<String,Object> wechatNotify(HttpServletRequest request) {
        try {
            log.info("司机微信充值-收到回调请求");
            return payService.wechatNotify(request);
        } catch (Exception e) {
            log.error("司机微信充值-回调处理报错异常:", e);
        }
        return null;
    }
}
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/api/PayController.java
@@ -59,18 +59,4 @@
        log.info("司机【{}】-微信充值-开下结束,返回前端数据为:{}", uid, JSON.toJSONString(wxPayAppVO));
        return ResultUtil.success(wxPayAppVO);
    }
    /**
     * 司机端充值余额 微信回调
     */
    @ResponseBody
    @PostMapping("/wechat/notify")
    public Map<String,Object> wechatNotify(HttpServletRequest request) {
        try {
            return payService.wechatNotify(request);
        } catch (Exception e) {
            log.error("司机余额充值-微信回调异常:", e);
        }
        return null;
    }
}
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/PayServiceImpl.java
@@ -1,5 +1,6 @@
package com.stylefeng.guns.modular.system.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.config.properties.WxPayProperties;
import com.stylefeng.guns.modular.system.model.Driver;
@@ -23,6 +24,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -58,18 +60,23 @@
    private final RedisUtil redisUtil;
    @Value("${callbackPath}")
    private String callbackPath;  //支付回调网关地址
    /**
     * 微信充值
     *
     * @param userId 充值用户
     * @param money 充值金额
     * @param money 充值金额,单位为元
     */
    @Transactional
    public WxPayAppVO wechatRecharge(Integer userId, BigDecimal money) throws Exception {
        PrepayRequest prepayRequest = buildPrepayRequest(money);
        log.info("司机充值-微信支付下单-请求数据为:{}", JSON.toJSONString(prepayRequest));
        // 1. 调用微信API下单
        PrepayResponse response = appService.prepay(prepayRequest);
        log.info("司机充值-微信支付下单-返回数据为:{}", JSON.toJSONString(response));
        String prepayId = response.getPrepayId();
        if(StringUtils.isEmpty(prepayId)) {
            return null;
@@ -103,7 +110,7 @@
                        .timestamp(request.getHeader("Wechatpay-Timestamp"))
                        .body(body)
                        .build();
        log.info("司机充值-回调业务处理开始-回调参数为:{}, body为:{}", requestParam, body);
        log.info("司机充值-收到回调请求-回调业务处理开始-回调参数为:{}, body为:{}", requestParam, body);
        NotificationConfig notificationConfig = (NotificationConfig) wxPayConfig;
        NotificationParser notificationParser = new NotificationParser(notificationConfig);
        Transaction transaction = notificationParser.parse(requestParam, Transaction.class);
@@ -133,7 +140,7 @@
            }
        }
        redisUtil.remove(outTradeNo); // 处理完逻辑删除
        log.info("司机充值-回调业务处理结束");
        log.info("司机充值-回调业务-处理结束");
        return notifyResult();
    }
@@ -197,7 +204,7 @@
        prepayRequest.setMchid(properties.getMchId());
        prepayRequest.setDescription("用户充值");          // 商品描述,不超过127个字符
        prepayRequest.setOutTradeNo(outTradeNo);          // 商户订单号
        prepayRequest.setNotifyUrl(properties.getNotifyUrl());            // 回调地址
        prepayRequest.setNotifyUrl(callbackPath+properties.getNotifyUrl());            // 回调地址
        Amount amount = new Amount();
        amount.setTotal(payAmount); // 金额:单位分(整数)
        amount.setCurrency("CNY"); // 币种
DriverOKTravel/guns-admin/src/main/resources/application.yml
@@ -122,4 +122,4 @@
  apiV3Key: GE519nBbA7XkjfND9dhaxztHF1SKndzW # APIv3密钥
  privateKeyPath: classpath:apiclient_key.pem # 私钥文件路径
  merchantSerialNumber: 74EEE9D1118435740621F2C7C5B4861F32968422 # 商户证书序列号
  notifyUrl: http://221.182.45.100:7777/driver/api/pay/wechat/notify  # 支付回调地址
  notifyUrl: /base/wechat/notify  # 支付回调地址
ManagementOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/PushEvaluationImpl.java
@@ -44,20 +44,26 @@
        // 获取出租车需要推送的数据
        List<Map<String, Object>>  mapList = this.baseMapper.getTaxiOrderList(startTime, endTime);
        List<PushEvaluationRequestDTO> taxiRequestList = PushEvaluationRequestDTO.convert(mapList);
        if(CollectionUtils.isNotEmpty(taxiRequestList)) {
            PushEvaluationLog evaluationLog = requestEvaluation(JSON.toJSONString(taxiRequestList));
            evaluationLog.setSerialNumber("taxi_"+startTime+"_"+endTime);
            // 记录日志
            this.insert(evaluationLog);
        }
        // 获取专车需要推送的数据
        List<Map<String, Object>>  mapPrivateList = this.baseMapper.getPrivateCarOrderList(startTime, endTime);
        List<PushEvaluationRequestDTO> privateRequestList = PushEvaluationRequestDTO.convert(mapPrivateList);
        // 数据推送
        List<PushEvaluationLog> logList = new ArrayList<>();
        taxiRequestList.forEach( taxi -> logList.add(requestEvaluation(taxi)));
        privateRequestList.forEach( privateCar -> logList.add(requestEvaluation(privateCar)));
        // 记录日志
        if(CollectionUtils.isNotEmpty(logList)) {
            this.insertBatch(logList);
        if(CollectionUtils.isNotEmpty(privateRequestList)) {
            PushEvaluationLog evaluationLog = requestEvaluation(JSON.toJSONString(privateRequestList));
            evaluationLog.setSerialNumber("privateCar_"+startTime+"_"+endTime);
            // 记录日志
            this.insert(evaluationLog);
        }
    }
    public PushEvaluationLog requestEvaluation(PushEvaluationRequestDTO request) {
    public PushEvaluationLog requestEvaluation(String requestData) {
        // 构建请求头
        HttpHeaders headers = buildHeaders();
        String url = properties.getUrl() + API_PATH;
@@ -66,19 +72,19 @@
        String errorMessage = "";
        try {
            // 发送请求
            HttpEntity<PushEvaluationRequestDTO> entity = new HttpEntity<>(request, headers);
            HttpEntity<String> entity = new HttpEntity<>(requestData, headers);
            log.info("上报数据到考评系统-请求地址为:{},请求参数为 : {}", url, JSON.toJSONString(entity));
            ResponseEntity<PushEvaluationResponseDTO> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, PushEvaluationResponseDTO.class);
            // 返回数据
            response = responseEntity.getBody();
            if (Objects.isNull(response)) {
                status = 0;
                log.error("上报数据到考评系统-调用接口失败,返回为空");
                log.error("上报数据到考评系统-调用接口-失败,返回为空");
            } else if(response.getCode() != 200 ) {
                status = 0;
                log.error("上报数据到考评系统-调用接口失败,返回失败数据为:{}", JSON.toJSONString(response));
                log.error("上报数据到考评系统-调用接口-失败,返回失败数据为:{}", JSON.toJSONString(response));
            }
            log.info("上报数据到考评系统-推送完成-请求URL:{},返回数据为:{}", url, JSON.toJSONString(response));
            log.info("上报数据到考评系统-调用接口-成功-请求URL:{},返回数据为:{}", url, JSON.toJSONString(response));
        } catch (Exception e) {
            status = 0;
            errorMessage = e.getLocalizedMessage();
@@ -86,10 +92,9 @@
        }
        PushEvaluationLog evaluationLog = new PushEvaluationLog();
        evaluationLog.setSerialNumber(request.getOrderNo()); // 订单号
        evaluationLog.setRequestUrl(url);
        evaluationLog.setRequestHeader(JSON.toJSONString(headers));
        evaluationLog.setRequestBody(JSON.toJSONString(request));
        evaluationLog.setRequestBody(requestData);
        Optional.ofNullable(response).map(JSON::toJSONString).ifPresent( evaluationLog::setResponseBody);
        evaluationLog.setStatus(status);
        evaluationLog.setRetryCount(0);
@@ -104,7 +109,7 @@
     */
    public HttpHeaders buildHeaders() {
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000); // 时间戳
        String nonce = RandomStringUtils.randomAlphanumeric(6); // 6位随机数
        String nonce = RandomStringUtils.randomNumeric(6); // 6位随机数
        String sign = SignUtil.generateSign("POST", API_PATH, properties.getAccessKey(), properties.getAccessSecret(), timestamp, nonce);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
@@ -114,5 +119,4 @@
        headers.set("sign", sign);
        return headers;
    }
}