---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015792561-go.md"
title: "检查子商户开票功能状态-请求示例-go"
doc_id: 4015792561
example_lang: "go"
---

## Go


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

```
package main

import (
	"demo/wxpay_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"strings"
)

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
	config, err := wxpay_utility.CreateMchConfig(
		"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",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &CheckSubMchStatusRequest{
		SubMchid: wxpay_utility.String("1900000109"),
	}

	response, err := CheckSubMchStatus(config, request)
	if err != nil {
		fmt.Printf("请求失败: %+v\n", err)
		// TODO: 请求失败，根据状态码执行不同的处理
		return
	}

	// TODO: 请求成功，继续业务逻辑
	fmt.Printf("请求成功: %+v\n", response)
}

func CheckSubMchStatus(config *wxpay_utility.MchConfig, request *CheckSubMchStatusRequest) (response *CheckSubMchStatusResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/new-tax-control-fapiao/merchant/{sub_mchid}/check-status"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqUrl.Path = strings.Replace(reqUrl.Path, "{sub_mchid}", url.PathEscape(*request.SubMchid), -1)
	httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Authorization", authorization)

	client := &http.Client{}
	httpResponse, err := client.Do(httpRequest)
	if err != nil {
		return nil, err
	}
	respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return nil, err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return nil, err
		}
		response := &CheckSubMchStatusResponse{}
		if err := json.Unmarshal(respBody, response); err != nil {
			return nil, err
		}

		return response, nil
	} else {
		return nil, wxpay_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type CheckSubMchStatusRequest struct {
	SubMchid *string `json:"sub_mchid,omitempty"`
}

func (o *CheckSubMchStatusRequest) MarshalJSON() ([]byte, error) {
	type Alias CheckSubMchStatusRequest
	a := &struct {
		SubMchid *string `json:"sub_mchid,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		SubMchid: nil,
		Alias:    (*Alias)(o),
	}
	return json.Marshal(a)
}

type CheckSubMchStatusResponse struct {
	SubMchid       *string         `json:"sub_mchid,omitempty"`
	ThirdMode      *ThirdMode      `json:"third_mode,omitempty"`
	DigitalTaxMode *DigitalTaxMode `json:"digital_tax_mode,omitempty"`
}

type ThirdMode struct {
	Status *ThirdModeStatus `json:"status,omitempty"`
}

type DigitalTaxMode struct {
	Status            *DigitalTaxApplyStatus        `json:"status,omitempty"`
	BillingPersonInfo []DigitalTaxBillingPersonInfo `json:"billing_person_info,omitempty"`
	AccessTime        *string                       `json:"access_time,omitempty"`
	ExpiredTime       *string                       `json:"expired_time,omitempty"`
	AccessFailReason  *string                       `json:"access_fail_reason,omitempty"`
	AbilityInfo       []DigitalTaxAbilityInfo       `json:"ability_info,omitempty"`
}

type ThirdModeStatus string

func (e ThirdModeStatus) Ptr() *ThirdModeStatus {
	return &e
}

const (
	THIRDMODESTATUS_DISABLED ThirdModeStatus = "DISABLED"
	THIRDMODESTATUS_ENABLED  ThirdModeStatus = "ENABLED"
)

type DigitalTaxApplyStatus string

func (e DigitalTaxApplyStatus) Ptr() *DigitalTaxApplyStatus {
	return &e
}

const (
	DIGITALTAXAPPLYSTATUS_APPROVAL_PENDING                 DigitalTaxApplyStatus = "APPROVAL_PENDING"
	DIGITALTAXAPPLYSTATUS_ACCESS_CONFIRMED_PENDING         DigitalTaxApplyStatus = "ACCESS_CONFIRMED_PENDING"
	DIGITALTAXAPPLYSTATUS_ABILITY_CONFIRMED_PENDING        DigitalTaxApplyStatus = "ABILITY_CONFIRMED_PENDING"
	DIGITALTAXAPPLYSTATUS_BILLING_PERSON_REGISTER_PENDING  DigitalTaxApplyStatus = "BILLING_PERSON_REGISTER_PENDING"
	DIGITALTAXAPPLYSTATUS_BILLING_PERSON_CONFIRMED_PENDING DigitalTaxApplyStatus = "BILLING_PERSON_CONFIRMED_PENDING"
	DIGITALTAXAPPLYSTATUS_SECURITY_SETTING_PENDING         DigitalTaxApplyStatus = "SECURITY_SETTING_PENDING"
	DIGITALTAXAPPLYSTATUS_APPLY_FAILED                     DigitalTaxApplyStatus = "APPLY_FAILED"
	DIGITALTAXAPPLYSTATUS_DISABLED                         DigitalTaxApplyStatus = "DISABLED"
	DIGITALTAXAPPLYSTATUS_ENABLED                          DigitalTaxApplyStatus = "ENABLED"
	DIGITALTAXAPPLYSTATUS_RESOURCE_EXPIRED                 DigitalTaxApplyStatus = "RESOURCE_EXPIRED"
)

type DigitalTaxBillingPersonInfo struct {
	Id   *string `json:"id,omitempty"`
	Name *string `json:"name,omitempty"`
}

type DigitalTaxAbilityInfo struct {
	Type                *FapiaoAbilityType                `json:"type,omitempty"`
	Status              *DigitalTaxAbilityAuthorizeStatus `json:"status,omitempty"`
	AuthorizeFailReason *string                           `json:"authorize_fail_reason,omitempty"`
}

type FapiaoAbilityType string

func (e FapiaoAbilityType) Ptr() *FapiaoAbilityType {
	return &e
}

const (
	FAPIAOABILITYTYPE_BASE_ABILITY        FapiaoAbilityType = "BASE_ABILITY"
	FAPIAOABILITYTYPE_REAL_ESTATE_ABILITY FapiaoAbilityType = "REAL_ESTATE_ABILITY"
	FAPIAOABILITYTYPE_REFINED_OIL_ABILITY FapiaoAbilityType = "REFINED_OIL_ABILITY"
)

type DigitalTaxAbilityAuthorizeStatus string

func (e DigitalTaxAbilityAuthorizeStatus) Ptr() *DigitalTaxAbilityAuthorizeStatus {
	return &e
}

const (
	DIGITALTAXABILITYAUTHORIZESTATUS_AUTHORIZED        DigitalTaxAbilityAuthorizeStatus = "AUTHORIZED"
	DIGITALTAXABILITYAUTHORIZESTATUS_AUTHORIZE_PENDING DigitalTaxAbilityAuthorizeStatus = "AUTHORIZE_PENDING"
	DIGITALTAXABILITYAUTHORIZESTATUS_DEAUTHORIZED      DigitalTaxAbilityAuthorizeStatus = "DEAUTHORIZED"
	DIGITALTAXABILITYAUTHORIZESTATUS_AUTHORIZE_FAILED  DigitalTaxAbilityAuthorizeStatus = "AUTHORIZE_FAILED"
)

```
