---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015941524-go.md"
title: "获取邀请开通的商户信息-请求示例-go"
doc_id: 4015941524
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"
)

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 := &ListSpInviteMchInfoRequest{
		QueryTimeStart:  wxpay_utility.String("2020-07-01T12:00:00+08:00"),
		QueryTimeEnd:    wxpay_utility.String("2020-07-02T12:00:00+08:00"),
		Offset:          wxpay_utility.Int64(10),
		Limit:           wxpay_utility.Int64(1),
		InviteCode:      wxpay_utility.String("code_20200101_123"),
		MchInviteStatus: MCHINVITESTATUS_MCH_INVITE_SUCC.Ptr(),
	}

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

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

func ListSpInviteMchInfo(config *wxpay_utility.MchConfig, request *ListSpInviteMchInfoRequest) (response *ListSpInviteMchInfoResp, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/new-tax-control-fapiao/fapiaomerchant/listspinvitemchinfo"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	query := reqUrl.Query()
	if request.QueryTimeStart != nil {
		query.Add("query_time_start", *request.QueryTimeStart)
	}
	if request.QueryTimeEnd != nil {
		query.Add("query_time_end", *request.QueryTimeEnd)
	}
	if request.Offset != nil {
		query.Add("offset", fmt.Sprintf("%v", *request.Offset))
	}
	if request.Limit != nil {
		query.Add("limit", fmt.Sprintf("%v", *request.Limit))
	}
	if request.InviteCode != nil {
		query.Add("invite_code", *request.InviteCode)
	}
	if request.MchInviteStatus != nil {
		query.Add("mch_invite_status", fmt.Sprintf("%v", *request.MchInviteStatus))
	}
	reqUrl.RawQuery = query.Encode()
	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 := &ListSpInviteMchInfoResp{}
		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 ListSpInviteMchInfoRequest struct {
	QueryTimeStart  *string          `json:"query_time_start,omitempty"`
	QueryTimeEnd    *string          `json:"query_time_end,omitempty"`
	Offset          *int64           `json:"offset,omitempty"`
	Limit           *int64           `json:"limit,omitempty"`
	InviteCode      *string          `json:"invite_code,omitempty"`
	MchInviteStatus *MchInviteStatus `json:"mch_invite_status,omitempty"`
}

func (o *ListSpInviteMchInfoRequest) MarshalJSON() ([]byte, error) {
	type Alias ListSpInviteMchInfoRequest
	a := &struct {
		QueryTimeStart  *string          `json:"query_time_start,omitempty"`
		QueryTimeEnd    *string          `json:"query_time_end,omitempty"`
		Offset          *int64           `json:"offset,omitempty"`
		Limit           *int64           `json:"limit,omitempty"`
		InviteCode      *string          `json:"invite_code,omitempty"`
		MchInviteStatus *MchInviteStatus `json:"mch_invite_status,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		QueryTimeStart:  nil,
		QueryTimeEnd:    nil,
		Offset:          nil,
		Limit:           nil,
		InviteCode:      nil,
		MchInviteStatus: nil,
		Alias:           (*Alias)(o),
	}
	return json.Marshal(a)
}

type ListSpInviteMchInfoResp struct {
	TotalCount          *int64                `json:"total_count,omitempty"`
	Offset              *int64                `json:"offset,omitempty"`
	Limit               *int64                `json:"limit,omitempty"`
	Links               *GetResponseLinks     `json:"links,omitempty"`
	MchInviteResultList []MchInviteResultInfo `json:"mch_invite_result_list,omitempty"`
}

type MchInviteStatus string

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

const (
	MCHINVITESTATUS_MCH_INVITE_SUCC   MchInviteStatus = "MCH_INVITE_SUCC"
	MCHINVITESTATUS_MCH_INVITE_FAILED MchInviteStatus = "MCH_INVITE_FAILED"
)

type GetResponseLinks struct {
	Next *string `json:"next,omitempty"`
	Prev *string `json:"prev,omitempty"`
	Self *string `json:"self,omitempty"`
}

type MchInviteResultInfo struct {
	SubMchid           *string                       `json:"sub_mchid,omitempty"`
	MchInviteStatus    *MchInviteStatus              `json:"mch_invite_status,omitempty"`
	EpName             *string                       `json:"ep_name,omitempty"`
	TaxId              *string                       `json:"tax_id,omitempty"`
	InviteCode         *string                       `json:"invite_code,omitempty"`
	OperateTime        *string                       `json:"operate_time,omitempty"`
	InviteFailedCode   *DigitalTaxInviteFailedReason `json:"invite_failed_code,omitempty"`
	InviteFailedReason *string                       `json:"invite_failed_reason,omitempty"`
}

type DigitalTaxInviteFailedReason string

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

const (
	DIGITALTAXINVITEFAILEDREASON_UNKNOWN_TAX_INFO           DigitalTaxInviteFailedReason = "UNKNOWN_TAX_INFO"
	DIGITALTAXINVITEFAILEDREASON_INVALID_COMPANY_INFO       DigitalTaxInviteFailedReason = "INVALID_COMPANY_INFO"
	DIGITALTAXINVITEFAILEDREASON_NOT_MEET_ACCESS_CONDITION  DigitalTaxInviteFailedReason = "NOT_MEET_ACCESS_CONDITION"
	DIGITALTAXINVITEFAILEDREASON_NOT_IN_PILOT_SCOPE         DigitalTaxInviteFailedReason = "NOT_IN_PILOT_SCOPE"
	DIGITALTAXINVITEFAILEDREASON_MISSING_TAX_AUTHORITY_CODE DigitalTaxInviteFailedReason = "MISSING_TAX_AUTHORITY_CODE"
	DIGITALTAXINVITEFAILEDREASON_MERCHANT_INFO_NOT_SET      DigitalTaxInviteFailedReason = "MERCHANT_INFO_NOT_SET"
	DIGITALTAXINVITEFAILEDREASON_UNKNOWN_ERROR              DigitalTaxInviteFailedReason = "UNKNOWN_ERROR"
)

```
