---
url: "https://pay.weixin.qq.com/doc/brand/4015782988-go.md"
title: "创建品牌门店-请求示例-go"
doc_id: 4015782988
example_lang: "go"
---

## Go


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

```
package main

import (
	"bytes"
	"demo/wxpay_brand_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/brand/4015826866
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

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

	request := &BrandStoresCreateRequest{
		StoreBasics: &StoreBase{
			StoreReferenceId: wxpay_brand_utility.String("MDL001"),
			BranchName:       wxpay_brand_utility.String("海岸城店"),
		},
		StoreAddress: &StoreLocation{
			AddressCode:        wxpay_brand_utility.String("440305"),
			AddressDetail:      wxpay_brand_utility.String("深南大道10000号腾讯大厦1楼"),
			AddressComplements: wxpay_brand_utility.String("地铁A口右侧100米"),
			Longitude:          wxpay_brand_utility.String("112.63484"),
			Latitude:           wxpay_brand_utility.String("37.75464"),
		},
		StoreBusiness: &StoreBusiness{
			ServicePhone:  wxpay_brand_utility.String("0755-86013388|0755-86013399"),
			BusinessHours: wxpay_brand_utility.String("周一至周五 09:00-20:00|周六至周日 10:00-次日22:00"),
		},
	}

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

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

func CreateBrandStore(config *wxpay_brand_utility.BrandConfig, request *BrandStoresCreateRequest) (response *BrandStoresEntity, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/brand/store/brandstores"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqBody, err := json.Marshal(request)
	if err != nil {
		return nil, err
	}
	httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	httpRequest.Header.Set("Content-Type", "application/json")
	authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
	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_brand_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return nil, err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_brand_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return nil, err
		}
		response := &BrandStoresEntity{}
		if err := json.Unmarshal(respBody, response); err != nil {
			return nil, err
		}

		return response, nil
	} else {
		return nil, wxpay_brand_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type BrandStoresCreateRequest struct {
	StoreBasics   *StoreBase     `json:"store_basics,omitempty"`
	StoreAddress  *StoreLocation `json:"store_address,omitempty"`
	StoreBusiness *StoreBusiness `json:"store_business,omitempty"`
}

type BrandStoresEntity struct {
	StoreId            *string          `json:"store_id,omitempty"`
	StoreState         *StoreState      `json:"store_state,omitempty"`
	AuditState         *AuditState      `json:"audit_state,omitempty"`
	ReviewRejectReason *string          `json:"review_reject_reason,omitempty"`
	StoreBasics        *StoreBase       `json:"store_basics,omitempty"`
	StoreAddress       *StoreLocation   `json:"store_address,omitempty"`
	StoreBusiness      *StoreBusiness   `json:"store_business,omitempty"`
	StoreRecipient     []StoreRecipient `json:"store_recipient,omitempty"`
}

type StoreBase struct {
	StoreReferenceId *string `json:"store_reference_id,omitempty"`
	BranchName       *string `json:"branch_name,omitempty"`
}

type StoreLocation struct {
	AddressCode        *string `json:"address_code,omitempty"`
	AddressDetail      *string `json:"address_detail,omitempty"`
	AddressComplements *string `json:"address_complements,omitempty"`
	Longitude          *string `json:"longitude,omitempty"`
	Latitude           *string `json:"latitude,omitempty"`
}

type StoreBusiness struct {
	ServicePhone  *string `json:"service_phone,omitempty"`
	BusinessHours *string `json:"business_hours,omitempty"`
}

type StoreState string

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

const (
	STORESTATE_OPEN     StoreState = "OPEN"
	STORESTATE_CREATING StoreState = "CREATING"
	STORESTATE_CLOSED   StoreState = "CLOSED"
)

type AuditState string

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

const (
	AUDITSTATE_SUCCESS    AuditState = "SUCCESS"
	AUDITSTATE_PROCESSING AuditState = "PROCESSING"
	AUDITSTATE_REJECTED   AuditState = "REJECTED"
)

type StoreRecipient struct {
	Mchid          *string         `json:"mchid,omitempty"`
	CompanyName    *string         `json:"company_name,omitempty"`
	RecipientState *RecipientState `json:"recipient_state,omitempty"`
}

type RecipientState string

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

const (
	RECIPIENTSTATE_CONFIRMED        RecipientState = "CONFIRMED"
	RECIPIENTSTATE_ADMIN_REJECTED   RecipientState = "ADMIN_REJECTED"
	RECIPIENTSTATE_CONFIRMING       RecipientState = "CONFIRMING"
	RECIPIENTSTATE_TIMEOUT_REJECTED RecipientState = "TIMEOUT_REJECTED"
)

```
