---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015770776-go.md"
title: "获取用户抬头填写链接-请求示例-go"
doc_id: 4015770776
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 := &AcquireFapiaoTitleUrlRequest{
		SubMchid:       wxpay_utility.String("1900000109"),
		FapiaoApplyId:  wxpay_utility.String("fapiao_20200701_123456"),
		Source:         SOURCE_WEB.Ptr(),
		Appid:          wxpay_utility.String("wxb1170446a4c0a5a2"),
		Openid:         wxpay_utility.String("plN5twRbHym_j-QcqCzstl0HmwEs"),
		TotalAmount:    wxpay_utility.Int64(382895),
		SellerName:     wxpay_utility.String("深圳市南山区测试商户"),
		ShowPhoneCell:  wxpay_utility.Bool(true),
		MustInputPhone: wxpay_utility.Bool(true),
		ShowEmailCell:  wxpay_utility.Bool(true),
		MustInputEmail: wxpay_utility.Bool(true),
	}

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

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

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

	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.FapiaoApplyId != nil {
		query.Add("fapiao_apply_id", *request.FapiaoApplyId)
	}
	if request.Source != nil {
		query.Add("source", fmt.Sprintf("%v", *request.Source))
	}
	if request.Appid != nil {
		query.Add("appid", *request.Appid)
	}
	if request.Openid != nil {
		query.Add("openid", *request.Openid)
	}
	if request.TotalAmount != nil {
		query.Add("total_amount", fmt.Sprintf("%v", *request.TotalAmount))
	}
	if request.SellerName != nil {
		query.Add("seller_name", *request.SellerName)
	}
	if request.ShowPhoneCell != nil {
		query.Add("show_phone_cell", fmt.Sprintf("%v", *request.ShowPhoneCell))
	}
	if request.MustInputPhone != nil {
		query.Add("must_input_phone", fmt.Sprintf("%v", *request.MustInputPhone))
	}
	if request.ShowEmailCell != nil {
		query.Add("show_email_cell", fmt.Sprintf("%v", *request.ShowEmailCell))
	}
	if request.MustInputEmail != nil {
		query.Add("must_input_email", fmt.Sprintf("%v", *request.MustInputEmail))
	}
	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 := &AcquireFapiaoTitleUrlResponse{}
		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 AcquireFapiaoTitleUrlRequest struct {
	SubMchid       *string `json:"sub_mchid,omitempty"`
	FapiaoApplyId  *string `json:"fapiao_apply_id,omitempty"`
	Appid          *string `json:"appid,omitempty"`
	Openid         *string `json:"openid,omitempty"`
	TotalAmount    *int64  `json:"total_amount,omitempty"`
	SellerName     *string `json:"seller_name,omitempty"`
	ShowPhoneCell  *bool   `json:"show_phone_cell,omitempty"`
	MustInputPhone *bool   `json:"must_input_phone,omitempty"`
	ShowEmailCell  *bool   `json:"show_email_cell,omitempty"`
	MustInputEmail *bool   `json:"must_input_email,omitempty"`
	Source         *Source `json:"source,omitempty"`
}

func (o *AcquireFapiaoTitleUrlRequest) MarshalJSON() ([]byte, error) {
	type Alias AcquireFapiaoTitleUrlRequest
	a := &struct {
		SubMchid       *string `json:"sub_mchid,omitempty"`
		FapiaoApplyId  *string `json:"fapiao_apply_id,omitempty"`
		Appid          *string `json:"appid,omitempty"`
		Openid         *string `json:"openid,omitempty"`
		TotalAmount    *int64  `json:"total_amount,omitempty"`
		SellerName     *string `json:"seller_name,omitempty"`
		ShowPhoneCell  *bool   `json:"show_phone_cell,omitempty"`
		MustInputPhone *bool   `json:"must_input_phone,omitempty"`
		ShowEmailCell  *bool   `json:"show_email_cell,omitempty"`
		MustInputEmail *bool   `json:"must_input_email,omitempty"`
		Source         *Source `json:"source,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		SubMchid:       nil,
		FapiaoApplyId:  nil,
		Appid:          nil,
		Openid:         nil,
		TotalAmount:    nil,
		SellerName:     nil,
		ShowPhoneCell:  nil,
		MustInputPhone: nil,
		ShowEmailCell:  nil,
		MustInputEmail: nil,
		Source:         nil,
		Alias:          (*Alias)(o),
	}
	return json.Marshal(a)
}

type AcquireFapiaoTitleUrlResponse struct {
	MiniprogramAppid    *string `json:"miniprogram_appid,omitempty"`
	MiniprogramPath     *string `json:"miniprogram_path,omitempty"`
	MiniprogramUserName *string `json:"miniprogram_user_name,omitempty"`
}

type Source string

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

const (
	SOURCE_WEB         Source = "WEB"
	SOURCE_MINIPROGRAM Source = "MINIPROGRAM"
)

```
