---
url: "https://pay.weixin.qq.com/doc/v3/partner/4019557725-java.md"
title: "同步车辆离场通知-请求示例-java"
doc_id: 4019557725
example_lang: "java"
---

## Java


需配合微信支付工具库 WXPayUtility 使用，请参考 [Java](https://pay.weixin.qq.com/doc/v3/partner/4014985777.md)

```
package com.java.demo;

import com.java.utils.WXPayUtility; // 引用微信支付工具库，参考：https://pay.weixin.qq.com/doc/v3/partner/4014985777

import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Expose;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 同步车辆离场通知
 */
public class SyncParkingExit {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "POST";
  private static String PATH = "/v3/parking/reminders/exit";

  public static void main(String[] args) {
    // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
    SyncParkingExit client = new SyncParkingExit(
      "19xxxxxxxx",                    // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340
      "1DDE55AD98Exxxxxxxxxx",         // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924
      "/path/to/apiclient_key.pem",     // 商户API证书私钥文件路径，本地文件路径
      "PUB_KEY_ID_xxxxxxxxxxxxx",      // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589
      "/path/to/wxp_pub.pem"           // 微信支付公钥文件路径，本地文件路径
    );

    SyncParkingExitRequest request = new SyncParkingExitRequest();
    request.outSerialNumber = "PARK20260423001";
    request.exitTimestamp = 1719128975L;
    request.plateNumber = "粤B12345";
    request.plateColor = PlateColor.PLATE_COLOR_UNKNOWN;
    request.parkingId = "21232735744117624001123604298240";
    request.payState = PayStatus.PAY_STATUS_UNKNOWN;
    request.payType = PayType.PAY_TYPE_UNKNOWN;
    request.outTradeNo = "4200001234567890123456789012";
    request.totalAmount = 1L;
    request.paidAmount = 1L;
    request.openid = "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o";
    request.payChannel = PayChannel.PAY_CHANNEL_UNKNOWN;
    request.payTime = 1719128975L;
    request.token = "msg_pay_token_example";
    request.wxTradeNo = "4200001234567890123456789012";
    try {
      SyncParkingExitResponse response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
        System.out.println(response);
    } catch (WXPayUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public SyncParkingExitResponse run(SyncParkingExitRequest request) {
    String uri = PATH;
    String reqBody = WXPayUtility.toJson(request);

    Request.Builder reqBuilder = new Request.Builder().url(HOST + uri);
    reqBuilder.addHeader("Accept", "application/json");
    reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId);
    reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody));
    reqBuilder.addHeader("Content-Type", "application/json");
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody);
    reqBuilder.method(METHOD, requestBody);
    Request httpRequest = reqBuilder.build();

    // 发送HTTP请求
    OkHttpClient client = new OkHttpClient.Builder().build();
    try (Response httpResponse = client.newCall(httpRequest).execute()) {
      String respBody = WXPayUtility.extractBody(httpResponse);
      if (httpResponse.code() >= 200 && httpResponse.code() < 300) {
        // 2XX 成功，验证应答签名
        WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey,
            httpResponse.headers(), respBody);

        // 从HTTP应答报文构建返回数据
        return WXPayUtility.fromJson(respBody, SyncParkingExitResponse.class);
      } else {
        throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers());
      }
    } catch (IOException e) {
      throw new UncheckedIOException("Sending request to " + uri + " failed.", e);
    }
  }

  private final String mchid;
  private final String certificateSerialNo;
  private final PrivateKey privateKey;
  private final String wechatPayPublicKeyId;
  private final PublicKey wechatPayPublicKey;

  public SyncParkingExit(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) {
    this.mchid = mchid;
    this.certificateSerialNo = certificateSerialNo;
    this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath);
    this.wechatPayPublicKeyId = wechatPayPublicKeyId;
    this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath);
  }

  public static class SyncParkingExitRequest {
    @SerializedName("out_serial_number")
    public String outSerialNumber;

    @SerializedName("exit_timestamp")
    public Long exitTimestamp;

    @SerializedName("plate_number")
    public String plateNumber;

    @SerializedName("plate_color")
    public PlateColor plateColor;

    @SerializedName("parking_id")
    public String parkingId;

    @SerializedName("pay_state")
    public PayStatus payState;

    @SerializedName("pay_type")
    public PayType payType;

    @SerializedName("out_trade_no")
    public String outTradeNo;

    @SerializedName("total_amount")
    public Long totalAmount;

    @SerializedName("paid_amount")
    public Long paidAmount;

    @SerializedName("openid")
    public String openid;

    @SerializedName("pay_channel")
    public PayChannel payChannel;

    @SerializedName("pay_time")
    public Long payTime;

    @SerializedName("token")
    public String token;

    @SerializedName("wx_trade_no")
    public String wxTradeNo;
  }

  public static class SyncParkingExitResponse {
    @SerializedName("code")
    public String code;

    @SerializedName("message")
    public String message;
  }

  public enum PlateColor {
    @SerializedName("PLATE_COLOR_UNKNOWN")
    PLATE_COLOR_UNKNOWN,
    @SerializedName("PLATE_COLOR_BLUE")
    PLATE_COLOR_BLUE,
    @SerializedName("PLATE_COLOR_YELLOW")
    PLATE_COLOR_YELLOW,
    @SerializedName("PLATE_COLOR_BLACK")
    PLATE_COLOR_BLACK,
    @SerializedName("PLATE_COLOR_WHITE")
    PLATE_COLOR_WHITE,
    @SerializedName("PLATE_COLOR_GREEN")
    PLATE_COLOR_GREEN,
    @SerializedName("PLATE_COLOR_YELLOW_GREEN")
    PLATE_COLOR_YELLOW_GREEN,
    @SerializedName("PLATE_COLOR_AGRICULTURAL_YELLOW")
    PLATE_COLOR_AGRICULTURAL_YELLOW,
    @SerializedName("PLATE_COLOR_OTHER")
    PLATE_COLOR_OTHER
  }

  public enum PayStatus {
    @SerializedName("PAY_STATUS_UNKNOWN")
    PAY_STATUS_UNKNOWN,
    @SerializedName("PAY_STATUS_UNPAID")
    PAY_STATUS_UNPAID,
    @SerializedName("PAY_STATUS_PAID")
    PAY_STATUS_PAID
  }

  public enum PayType {
    @SerializedName("PAY_TYPE_UNKNOWN")
    PAY_TYPE_UNKNOWN,
    @SerializedName("PAY_TYPE_WXPAY")
    PAY_TYPE_WXPAY,
    @SerializedName("PAY_TYPE_OTHERS")
    PAY_TYPE_OTHERS
  }

  public enum PayChannel {
    @SerializedName("PAY_CHANNEL_UNKNOWN")
    PAY_CHANNEL_UNKNOWN,
    @SerializedName("PAY_CHANNEL_WECHAT_PAY_NOTIF")
    PAY_CHANNEL_WECHAT_PAY_NOTIF,
    @SerializedName("PAY_CHANNEL_SCAN_QRCODE")
    PAY_CHANNEL_SCAN_QRCODE,
    @SerializedName("PAY_CHANNEL_OFFICIAL_ACCOUNT")
    PAY_CHANNEL_OFFICIAL_ACCOUNT,
    @SerializedName("PAY_CHANNEL_MCH_MINI_PROG")
    PAY_CHANNEL_MCH_MINI_PROG,
    @SerializedName("PAY_CHANNEL_OTHERS")
    PAY_CHANNEL_OTHERS
  }

}

```
