---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015792561-java.md"
title: "检查子商户开票功能状态-请求示例-java"
doc_id: 4015792561
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 CheckSubMchStatus {
  private static String HOST = "https://api.mch.weixin.qq.com";
  private static String METHOD = "GET";
  private static String PATH = "/v3/new-tax-control-fapiao/merchant/{sub_mchid}/check-status";

  public static void main(String[] args) {
    // TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
    CheckSubMchStatus client = new CheckSubMchStatus(
      "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"           // 微信支付公钥文件路径，本地文件路径
    );

    CheckSubMchStatusRequest request = new CheckSubMchStatusRequest();
    request.subMchid = "1900000109";
    try {
      CheckSubMchStatusResponse response = client.run(request);
        // TODO: 请求成功，继续业务逻辑
        System.out.println(response);
    } catch (WXPayUtility.ApiException e) {
        // TODO: 请求失败，根据状态码执行不同的逻辑
        e.printStackTrace();
    }
  }

  public CheckSubMchStatusResponse run(CheckSubMchStatusRequest request) {
    String uri = PATH;
    uri = uri.replace("{sub_mchid}", WXPayUtility.urlEncode(request.subMchid));

    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, null));
    reqBuilder.method(METHOD, null);
    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, CheckSubMchStatusResponse.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 CheckSubMchStatus(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 CheckSubMchStatusRequest {
    @SerializedName("sub_mchid")
    @Expose(serialize = false)
    public String subMchid;
  }

  public static class CheckSubMchStatusResponse {
    @SerializedName("sub_mchid")
    public String subMchid;

    @SerializedName("third_mode")
    public ThirdMode thirdMode;

    @SerializedName("digital_tax_mode")
    public DigitalTaxMode digitalTaxMode;
  }

  public static class ThirdMode {
    @SerializedName("status")
    public ThirdModeStatus status;
  }

  public static class DigitalTaxMode {
    @SerializedName("status")
    public DigitalTaxApplyStatus status;

    @SerializedName("billing_person_info")
    public List<DigitalTaxBillingPersonInfo> billingPersonInfo;

    @SerializedName("access_time")
    public String accessTime;

    @SerializedName("expired_time")
    public String expiredTime;

    @SerializedName("access_fail_reason")
    public String accessFailReason;

    @SerializedName("ability_info")
    public List<DigitalTaxAbilityInfo> abilityInfo;
  }

  public enum ThirdModeStatus {
    @SerializedName("DISABLED")
    DISABLED,
    @SerializedName("ENABLED")
    ENABLED
  }

  public enum DigitalTaxApplyStatus {
    @SerializedName("APPROVAL_PENDING")
    APPROVAL_PENDING,
    @SerializedName("ACCESS_CONFIRMED_PENDING")
    ACCESS_CONFIRMED_PENDING,
    @SerializedName("ABILITY_CONFIRMED_PENDING")
    ABILITY_CONFIRMED_PENDING,
    @SerializedName("BILLING_PERSON_REGISTER_PENDING")
    BILLING_PERSON_REGISTER_PENDING,
    @SerializedName("BILLING_PERSON_CONFIRMED_PENDING")
    BILLING_PERSON_CONFIRMED_PENDING,
    @SerializedName("SECURITY_SETTING_PENDING")
    SECURITY_SETTING_PENDING,
    @SerializedName("APPLY_FAILED")
    APPLY_FAILED,
    @SerializedName("DISABLED")
    DISABLED,
    @SerializedName("ENABLED")
    ENABLED,
    @SerializedName("RESOURCE_EXPIRED")
    RESOURCE_EXPIRED
  }

  public static class DigitalTaxBillingPersonInfo {
    @SerializedName("id")
    public String id;

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

  public static class DigitalTaxAbilityInfo {
    @SerializedName("type")
    public FapiaoAbilityType type;

    @SerializedName("status")
    public DigitalTaxAbilityAuthorizeStatus status;

    @SerializedName("authorize_fail_reason")
    public String authorizeFailReason;
  }

  public enum FapiaoAbilityType {
    @SerializedName("BASE_ABILITY")
    BASE_ABILITY,
    @SerializedName("REAL_ESTATE_ABILITY")
    REAL_ESTATE_ABILITY,
    @SerializedName("REFINED_OIL_ABILITY")
    REFINED_OIL_ABILITY
  }

  public enum DigitalTaxAbilityAuthorizeStatus {
    @SerializedName("AUTHORIZED")
    AUTHORIZED,
    @SerializedName("AUTHORIZE_PENDING")
    AUTHORIZE_PENDING,
    @SerializedName("DEAUTHORIZED")
    DEAUTHORIZED,
    @SerializedName("AUTHORIZE_FAILED")
    AUTHORIZE_FAILED
  }

}

```
