---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015941495-go.md"
title: "获取开通服务商电子发票能力邀请链接-请求示例-go"
doc_id: 4015941495
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 := &GetSpInviteUrlRequest{
		OperationType:         INVITEOPERATIONTYPE_AUTH_BINDING.Ptr(),
		FapiaoMode:            []InvoiceMode{INVOICEMODE_THIRD_OR_SELF_FAPIAO},
		FapiaoAbilityTypeList: []FapiaoAbilityType{FAPIAOABILITYTYPE_BASE_ABILITY},
		InviteChannel:         wxpay_utility.String("miniprogram"),
		OperateUser:           wxpay_utility.String("mI7HGEJ4Q2B91IGjHZu/Gthm87Szv0MK2AoC0/3ZMDgltMtdoY6O0qZ4F1iXiwCuqkkBe+9M4ggvdzRVVVB9s+zLEQ8nv74vsgl77MZx14nd5obtCcfAvPfDJob3oG7FqlThmYKJqjiOwBvvQse7p9R8onj/POzSrbM8re8ZYGp4LcehXopTLdk2ZVWRv8bnJgKZeY4vpMmq4xuRTYk6xNXvowBBKLu4oaNFNdBO3fip1a1rFW0vRw=="),
		InviteCode:            wxpay_utility.String("code_20200101_123"),
		SubMchid:              wxpay_utility.String("19998278783"),
		TaxpayerId:            wxpay_utility.String("202003261233701778"),
	}

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

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

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

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	query := reqUrl.Query()
	if request.OperationType != nil {
		query.Add("operation_type", fmt.Sprintf("%v", *request.OperationType))
	}
	if request.FapiaoMode != nil {
		query.Add("fapiao_mode", fmt.Sprintf("%v", *request.FapiaoMode))
	}
	if request.FapiaoAbilityTypeList != nil {
		query.Add("fapiao_ability_type_list", fmt.Sprintf("%v", *request.FapiaoAbilityTypeList))
	}
	if request.InviteChannel != nil {
		query.Add("invite_channel", *request.InviteChannel)
	}
	if request.OperateUser != nil {
		query.Add("operate_user", *request.OperateUser)
	}
	if request.InviteCode != nil {
		query.Add("invite_code", *request.InviteCode)
	}
	if request.SubMchid != nil {
		query.Add("sub_mchid", *request.SubMchid)
	}
	if request.TaxpayerId != nil {
		query.Add("taxpayer_id", *request.TaxpayerId)
	}
	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 := &GetSpInviteUrlResp{}
		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 GetSpInviteUrlRequest struct {
	FapiaoMode            []InvoiceMode        `json:"fapiao_mode,omitempty"`
	FapiaoAbilityTypeList []FapiaoAbilityType  `json:"fapiao_ability_type_list,omitempty"`
	InviteChannel         *string              `json:"invite_channel,omitempty"`
	OperateUser           *string              `json:"operate_user,omitempty"`
	InviteCode            *string              `json:"invite_code,omitempty"`
	SubMchid              *string              `json:"sub_mchid,omitempty"`
	TaxpayerId            *string              `json:"taxpayer_id,omitempty"`
	OperationType         *InviteOperationType `json:"operation_type,omitempty"`
}

func (o *GetSpInviteUrlRequest) MarshalJSON() ([]byte, error) {
	type Alias GetSpInviteUrlRequest
	a := &struct {
		FapiaoMode            []InvoiceMode        `json:"fapiao_mode,omitempty"`
		FapiaoAbilityTypeList []FapiaoAbilityType  `json:"fapiao_ability_type_list,omitempty"`
		InviteChannel         *string              `json:"invite_channel,omitempty"`
		OperateUser           *string              `json:"operate_user,omitempty"`
		InviteCode            *string              `json:"invite_code,omitempty"`
		SubMchid              *string              `json:"sub_mchid,omitempty"`
		TaxpayerId            *string              `json:"taxpayer_id,omitempty"`
		OperationType         *InviteOperationType `json:"operation_type,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		FapiaoMode:            nil,
		FapiaoAbilityTypeList: nil,
		InviteChannel:         nil,
		OperateUser:           nil,
		InviteCode:            nil,
		SubMchid:              nil,
		TaxpayerId:            nil,
		OperationType:         nil,
		Alias:                 (*Alias)(o),
	}
	return json.Marshal(a)
}

type GetSpInviteUrlResp struct {
	InviteUrl        *string `json:"invite_url,omitempty"`
	MiniprogramAppid *string `json:"miniprogram_appid,omitempty"`
	MiniprogramPath  *string `json:"miniprogram_path,omitempty"`
}

type InvoiceMode string

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

const (
	INVOICEMODE_THIRD_OR_SELF_FAPIAO InvoiceMode = "THIRD_OR_SELF_FAPIAO"
	INVOICEMODE_TENCENT_DIGITAL_TAX  InvoiceMode = "TENCENT_DIGITAL_TAX"
)

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 InviteOperationType string

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

const (
	INVITEOPERATIONTYPE_AUTH_BINDING InviteOperationType = "AUTH_BINDING"
)

```
