---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015784260-go.md"
title: "获取用户填写抬头信息-请求示例-go"
doc_id: 4015784260
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 := &GetUserTitleRequest{
		SubMchid:      wxpay_utility.String("1900000109"),
		Scene:         SCENE_WITH_WECHATPAY.Ptr(),
		FapiaoApplyId: wxpay_utility.String("4200000444201910177461284488"),
	}

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

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

func GetUserTitle(config *wxpay_utility.MchConfig, request *GetUserTitleRequest) (response *UserTitleEntity, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/new-tax-control-fapiao/user-title"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	query := reqUrl.Query()
	if request.SubMchid != nil {
		query.Add("sub_mchid", *request.SubMchid)
	}
	if request.Scene != nil {
		query.Add("scene", fmt.Sprintf("%v", *request.Scene))
	}
	if request.FapiaoApplyId != nil {
		query.Add("fapiao_apply_id", *request.FapiaoApplyId)
	}
	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 := &UserTitleEntity{}
		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 GetUserTitleRequest struct {
	SubMchid      *string `json:"sub_mchid,omitempty"`
	FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"`
	Scene         *Scene  `json:"scene,omitempty"`
}

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

type UserTitleEntity struct {
	Type             *BuyerType      `json:"type,omitempty"`
	Name             *string         `json:"name,omitempty"`
	TaxpayerId       *string         `json:"taxpayer_id,omitempty"`
	Address          *string         `json:"address,omitempty"`
	Telephone        *string         `json:"telephone,omitempty"`
	BankName         *string         `json:"bank_name,omitempty"`
	BankAccount      *string         `json:"bank_account,omitempty"`
	Phone            *string         `json:"phone,omitempty"`
	Email            *string         `json:"email,omitempty"`
	Amount           *int64          `json:"amount,omitempty"`
	OutTradeNo       *string         `json:"out_trade_no,omitempty"`
	FapiaoBillType   *FapiaoBillType `json:"fapiao_bill_type,omitempty"`
	UserApplyMessage *string         `json:"user_apply_message,omitempty"`
}

type Scene string

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

const (
	SCENE_WITH_WECHATPAY    Scene = "WITH_WECHATPAY"
	SCENE_WITHOUT_WECHATPAY Scene = "WITHOUT_WECHATPAY"
)

type BuyerType string

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

const (
	BUYERTYPE_INDIVIDUAL   BuyerType = "INDIVIDUAL"
	BUYERTYPE_ORGANIZATION BuyerType = "ORGANIZATION"
)

type FapiaoBillType string

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

const (
	FAPIAOBILLTYPE_COMM_FAPIAO FapiaoBillType = "COMM_FAPIAO"
	FAPIAOBILLTYPE_VAT_FAPIAO  FapiaoBillType = "VAT_FAPIAO"
)

```
