---
url: "https://pay.weixin.qq.com/doc/v3/merchant/4012380536-go.md"
title: "查询合作关系列表-请求示例-go"
doc_id: 4012380536
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"
)

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 := &ListPartnershipsRequest{
		Limit:  wxpay_utility.Int64(5),
		Offset: wxpay_utility.Int64(10),
		AuthorizedData: &AuthorizedDataFilter{
			BusinessType: AUTHBUSINESSTYPE_FAVOR_STOCK.Ptr(),
			StockId:      wxpay_utility.String("2433405"),
		},
		Partner: &PartnerInfo{
			Type:       PARTNERTYPE_APPID.Ptr(),
			Appid:      wxpay_utility.String("wx4e1916a585d1f4e9"),
			MerchantId: wxpay_utility.String("2480029552"),
		},
	}

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

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

func ListPartnerships(config *wxpay_utility.MchConfig, request *ListPartnershipsRequest) (response *ListPartnershipsResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/marketing/partnerships"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	query := reqUrl.Query()
	if request.Limit != nil {
		query.Add("limit", fmt.Sprintf("%v", *request.Limit))
	}
	if request.Offset != nil {
		query.Add("offset", fmt.Sprintf("%v", *request.Offset))
	}
	if request.Partner != nil {
		jsonStr, err := json.Marshal(*request.Partner)
		if err != nil {
			return nil, fmt.Errorf("failed to marshal partner: %v", err)
		}
		query.Add("partner", string(jsonStr))
	}
	if request.AuthorizedData != nil {
		jsonStr, err := json.Marshal(*request.AuthorizedData)
		if err != nil {
			return nil, fmt.Errorf("failed to marshal authorized_data: %v", err)
		}
		query.Add("authorized_data", string(jsonStr))
	}
	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 := &ListPartnershipsResponse{}
		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 ListPartnershipsRequest struct {
	Limit          *int64                `json:"limit,omitempty"`
	Offset         *int64                `json:"offset,omitempty"`
	Partner        *PartnerInfo          `json:"partner,omitempty"`
	AuthorizedData *AuthorizedDataFilter `json:"authorized_data,omitempty"`
}

func (o *ListPartnershipsRequest) MarshalJSON() ([]byte, error) {
	type Alias ListPartnershipsRequest
	a := &struct {
		Limit          *int64                `json:"limit,omitempty"`
		Offset         *int64                `json:"offset,omitempty"`
		Partner        *PartnerInfo          `json:"partner,omitempty"`
		AuthorizedData *AuthorizedDataFilter `json:"authorized_data,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		Limit:          nil,
		Offset:         nil,
		Partner:        nil,
		AuthorizedData: nil,
		Alias:          (*Alias)(o),
	}
	return json.Marshal(a)
}

type ListPartnershipsResponse struct {
	Data       []PartnershipsEntity `json:"data,omitempty"`
	Offset     *int64               `json:"offset,omitempty"`
	Limit      *int64               `json:"limit,omitempty"`
	TotalCount *int64               `json:"total_count,omitempty"`
}

type PartnerInfo struct {
	Type       *PartnerType `json:"type,omitempty"`
	Appid      *string      `json:"appid,omitempty"`
	MerchantId *string      `json:"merchant_id,omitempty"`
}

type AuthorizedDataFilter struct {
	BusinessType *AuthBusinessType `json:"business_type,omitempty"`
	StockId      *string           `json:"stock_id,omitempty"`
}

type PartnershipsEntity struct {
	Partner        *PartnerInfo    `json:"partner,omitempty"`
	AuthorizedData *AuthorizedData `json:"authorized_data,omitempty"`
	BuildTime      *string         `json:"build_time,omitempty"`
	TerminateTime  *string         `json:"terminate_time,omitempty"`
	CreateTime     *string         `json:"create_time,omitempty"`
	UpdateTime     *string         `json:"update_time,omitempty"`
}

type PartnerType string

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

const (
	PARTNERTYPE_APPID    PartnerType = "APPID"
	PARTNERTYPE_MERCHANT PartnerType = "MERCHANT"
)

type AuthBusinessType string

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

const (
	AUTHBUSINESSTYPE_FAVOR_STOCK     AuthBusinessType = "FAVOR_STOCK"
	AUTHBUSINESSTYPE_BUSIFAVOR_STOCK AuthBusinessType = "BUSIFAVOR_STOCK"
)

type AuthorizedData struct {
	BusinessType *AuthBusinessType `json:"business_type,omitempty"`
	StockId      *string           `json:"stock_id,omitempty"`
}

```
