---
url: "https://pay.weixin.qq.com/doc/v3/partner/4015336976-go.md"
title: "查询会员卡模板列表-请求示例-go"
doc_id: 4015336976
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"
	"time"
)

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 := &ListCardRequest{
		BrandId: wxpay_utility.String("1004"),
		State:   CARDSTATE_CARD_EFFECTIVE.Ptr(),
		Offset:  wxpay_utility.Int64(0),
		Limit:   wxpay_utility.Int64(20),
	}

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

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

func ListCard(config *wxpay_utility.MchConfig, request *ListCardRequest) (response *ListCardResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/brand/partner/card-member/cards"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	query := reqUrl.Query()
	if request.BrandId != nil {
		query.Add("brand_id", *request.BrandId)
	}
	if request.State != nil {
		query.Add("state", fmt.Sprintf("%v", *request.State))
	}
	if request.Offset != nil {
		query.Add("offset", fmt.Sprintf("%v", *request.Offset))
	}
	if request.Limit != nil {
		query.Add("limit", fmt.Sprintf("%v", *request.Limit))
	}
	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 := &ListCardResponse{}
		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 ListCardRequest struct {
	BrandId *string    `json:"brand_id,omitempty"`
	Offset  *int64     `json:"offset,omitempty"`
	Limit   *int64     `json:"limit,omitempty"`
	State   *CardState `json:"state,omitempty"`
}

func (o *ListCardRequest) MarshalJSON() ([]byte, error) {
	type Alias ListCardRequest
	a := &struct {
		BrandId *string    `json:"brand_id,omitempty"`
		Offset  *int64     `json:"offset,omitempty"`
		Limit   *int64     `json:"limit,omitempty"`
		State   *CardState `json:"state,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		BrandId: nil,
		Offset:  nil,
		Limit:   nil,
		State:   nil,
		Alias:   (*Alias)(o),
	}
	return json.Marshal(a)
}

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

type CardState string

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

const (
	CARDSTATE_CARD_EFFECTIVE CardState = "CARD_EFFECTIVE"
	CARDSTATE_CARD_INVALID   CardState = "CARD_INVALID"
)

type Card struct {
	OutRequestNo         *string       `json:"out_request_no,omitempty"`
	CardId               *string       `json:"card_id,omitempty"`
	BrandId              *string       `json:"brand_id,omitempty"`
	Appid                *string       `json:"appid,omitempty"`
	CardType             *CardType     `json:"card_type,omitempty"`
	CardTitle            *string       `json:"card_title,omitempty"`
	CardColor            *string       `json:"card_color,omitempty"`
	CardPictureUrl       *string       `json:"card_picture_url,omitempty"`
	CodeMode             *CodeMode     `json:"code_mode,omitempty"`
	CodeType             *CodeType     `json:"code_type,omitempty"`
	CodeJumpInformation  *CodeJumpInfo `json:"code_jump_information,omitempty"`
	Benefits             *string       `json:"benefits,omitempty"`
	NotifyUrl            *string       `json:"notify_url,omitempty"`
	NeedPinned           *bool         `json:"need_pinned,omitempty"`
	NeedDisplayLevel     *bool         `json:"need_display_level,omitempty"`
	InitLevel            *string       `json:"init_level,omitempty"`
	ServicePhone         *string       `json:"service_phone,omitempty"`
	LegalAgreement       *string       `json:"legal_agreement,omitempty"`
	ValidDateInformation *DateInfo     `json:"valid_date_information,omitempty"`
	MemberInformation    *MemberInfo   `json:"member_information,omitempty"`
	PointsInformation    *PointsInfo   `json:"points_information,omitempty"`
	BalanceInformation   *BalanceInfo  `json:"balance_information,omitempty"`
	PurchaseInformation  *PurchaseInfo `json:"purchase_information,omitempty"`
	UserInformation      *UserInfoForm `json:"user_information,omitempty"`
	State                *CardState    `json:"state,omitempty"`
	CreateTime           *time.Time    `json:"create_time,omitempty"`
	ModifyTime           *time.Time    `json:"modify_time,omitempty"`
}

type CardType string

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

const (
	CARDTYPE_PURCHASE CardType = "PURCHASE"
	CARDTYPE_NORMAL   CardType = "NORMAL"
	CARDTYPE_BALANCE  CardType = "BALANCE"
)

type CodeMode string

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

const (
	CODEMODE_SYSTEM_ALLOCATE   CodeMode = "SYSTEM_ALLOCATE"
	CODEMODE_MERCHANT_ALLOCATE CodeMode = "MERCHANT_ALLOCATE"
)

type CodeType string

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

const (
	CODETYPE_NONE_CODE            CodeType = "NONE_CODE"
	CODETYPE_BAR_CODE             CodeType = "BAR_CODE"
	CODETYPE_QR_CODE              CodeType = "QR_CODE"
	CODETYPE_BAR_CODE_AND_QR_CODE CodeType = "BAR_CODE_AND_QR_CODE"
	CODETYPE_JUMP_MINI_PROGRAM    CodeType = "JUMP_MINI_PROGRAM"
)

type CodeJumpInfo struct {
	JumpAppid *string `json:"jump_appid,omitempty"`
	JumpPath  *string `json:"jump_path,omitempty"`
}

type DateInfo struct {
	Type                     *DateType  `json:"type,omitempty"`
	AvailableBeginTime       *time.Time `json:"available_begin_time,omitempty"`
	AvailableEndTime         *time.Time `json:"available_end_time,omitempty"`
	AvailableDayAfterReceive *int64     `json:"available_day_after_receive,omitempty"`
}

type MemberInfo struct {
	JumpAppid *string `json:"jump_appid,omitempty"`
	JumpPath  *string `json:"jump_path,omitempty"`
}

type PointsInfo struct {
	JumpAppid *string `json:"jump_appid,omitempty"`
	JumpPath  *string `json:"jump_path,omitempty"`
}

type BalanceInfo struct {
	JumpAppid *string `json:"jump_appid,omitempty"`
	JumpPath  *string `json:"jump_path,omitempty"`
}

type PurchaseInfo struct {
	Price     *int64  `json:"price,omitempty"`
	JumpAppid *string `json:"jump_appid,omitempty"`
	JumpPath  *string `json:"jump_path,omitempty"`
}

type UserInfoForm struct {
	CommonFieldList []CommonFieldFlag     `json:"common_field_list,omitempty"`
	CustomFieldList []UserCustomFieldForm `json:"custom_field_list,omitempty"`
}

type DateType string

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

const (
	DATETYPE_FIX_TIME_RANGE DateType = "FIX_TIME_RANGE"
	DATETYPE_FIX_TERM       DateType = "FIX_TERM"
	DATETYPE_PERMANENT      DateType = "PERMANENT"
)

type CommonFieldFlag string

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

const (
	COMMONFIELDFLAG_USER_FORM_FLAG_SEX      CommonFieldFlag = "USER_FORM_FLAG_SEX"
	COMMONFIELDFLAG_USER_FORM_FLAG_NAME     CommonFieldFlag = "USER_FORM_FLAG_NAME"
	COMMONFIELDFLAG_USER_FORM_FLAG_BIRTHDAY CommonFieldFlag = "USER_FORM_FLAG_BIRTHDAY"
	COMMONFIELDFLAG_USER_FORM_FLAG_ADDRESS  CommonFieldFlag = "USER_FORM_FLAG_ADDRESS"
	COMMONFIELDFLAG_USER_FORM_FLAG_EMAIL    CommonFieldFlag = "USER_FORM_FLAG_EMAIL"
	COMMONFIELDFLAG_USER_FORM_FLAG_CITY     CommonFieldFlag = "USER_FORM_FLAG_CITY"
)

type UserCustomFieldForm struct {
	Type   *CustomFieldType `json:"type,omitempty"`
	Name   *string          `json:"name,omitempty"`
	Values []string         `json:"values,omitempty"`
}

type CustomFieldType string

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

const (
	CUSTOMFIELDTYPE_CHECK_BOX CustomFieldType = "CHECK_BOX"
	CUSTOMFIELDTYPE_RADIO     CustomFieldType = "RADIO"
)

```
