---
url: "https://pay.weixin.qq.com/doc/v3/merchant/4012716457-go.md"
title: "微信单号查询转账单-请求示例-go"
doc_id: 4012716457
example_lang: "go"
---

## Go


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

```
package main

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

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/merchant/4013070756
	config, err := wxpay_utility.CreateMchConfig(
		"19xxxxxxxx",                 // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756
		"1DDE55AD98Exxxxxxxxxx",      // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053
		"/path/to/apiclient_key.pem", // 商户API证书私钥文件路径，本地文件路径
		"PUB_KEY_ID_xxxxxxxxxxxxx",   // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816
		"/path/to/wxp_pub.pem",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &GetTransferBillByNoRequest{
		TransferBillNo: wxpay_utility.String("1330000071100999991182020050700019480001"),
	}

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

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

func GetTransferBillByNo(config *wxpay_utility.MchConfig, request *GetTransferBillByNoRequest) (response *TransferBillEntity, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/fund-app/mch-transfer/transfer-bills/transfer-bill-no/{transfer_bill_no}"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqUrl.Path = strings.Replace(reqUrl.Path, "{transfer_bill_no}", url.PathEscape(*request.TransferBillNo), -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 := &TransferBillEntity{}
		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 GetTransferBillByNoRequest struct {
	TransferBillNo *string `json:"transfer_bill_no,omitempty"`
}

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

type TransferBillEntity struct {
	MchId          *string             `json:"mch_id,omitempty"`
	OutBillNo      *string             `json:"out_bill_no,omitempty"`
	TransferBillNo *string             `json:"transfer_bill_no,omitempty"`
	Appid          *string             `json:"appid,omitempty"`
	State          *TransferBillStatus `json:"state,omitempty"`
	TransferAmount *int64              `json:"transfer_amount,omitempty"`
	TransferRemark *string             `json:"transfer_remark,omitempty"`
	FailReason     *string             `json:"fail_reason,omitempty"`
	Openid         *string             `json:"openid,omitempty"`
	UserName       *string             `json:"user_name,omitempty"`
	CreateTime     *time.Time          `json:"create_time,omitempty"`
	UpdateTime     *string             `json:"update_time,omitempty"`
}

type TransferBillStatus string

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

const (
	TRANSFERBILLSTATUS_ACCEPTED          TransferBillStatus = "ACCEPTED"
	TRANSFERBILLSTATUS_PROCESSING        TransferBillStatus = "PROCESSING"
	TRANSFERBILLSTATUS_WAIT_USER_CONFIRM TransferBillStatus = "WAIT_USER_CONFIRM"
	TRANSFERBILLSTATUS_TRANSFERING       TransferBillStatus = "TRANSFERING"
	TRANSFERBILLSTATUS_SUCCESS           TransferBillStatus = "SUCCESS"
	TRANSFERBILLSTATUS_FAIL              TransferBillStatus = "FAIL"
	TRANSFERBILLSTATUS_CANCELING         TransferBillStatus = "CANCELING"
	TRANSFERBILLSTATUS_CANCELLED         TransferBillStatus = "CANCELLED"
)

```
