
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9)
10
11func main() {
12
13 config, err := wxpay_utility.CreateMchConfig(
14 "19xxxxxxxx",
15 "1DDE55AD98Exxxxxxxxxx",
16 "/path/to/apiclient_key.pem",
17 "PUB_KEY_ID_xxxxxxxxxxxxx",
18 "/path/to/wxp_pub.pem",
19 )
20 if err != nil {
21 fmt.Println(err)
22 return
23 }
24
25 request := &ListBrandStoresRequest{
26 BrandId: wxpay_utility.String("123456789"),
27 StoreState: STORESTATE_OPEN.Ptr(),
28 Offset: wxpay_utility.Int64(100),
29 Limit: wxpay_utility.Int64(20),
30 }
31
32 response, err := ListBrandStores(config, request)
33 if err != nil {
34 fmt.Printf("请求失败: %+v\n", err)
35
36 return
37 }
38
39
40 fmt.Printf("请求成功: %+v\n", response)
41}
42
43func ListBrandStores(config *wxpay_utility.MchConfig, request *ListBrandStoresRequest) (response *BrandStoresListResponse, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "GET"
47 path = "/v3/brand/partner/store/brandstores"
48 )
49
50 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
51 if err != nil {
52 return nil, err
53 }
54 query := reqUrl.Query()
55 if request.BrandId != nil {
56 query.Add("brand_id", *request.BrandId)
57 }
58 if request.StoreState != nil {
59 query.Add("store_state", fmt.Sprintf("%v", *request.StoreState))
60 }
61 if request.Offset != nil {
62 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
63 }
64 if request.Limit != nil {
65 query.Add("limit", fmt.Sprintf("%v", *request.Limit))
66 }
67 reqUrl.RawQuery = query.Encode()
68 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Accept", "application/json")
73 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
74 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
75 if err != nil {
76 return nil, err
77 }
78 httpRequest.Header.Set("Authorization", authorization)
79
80 client := &http.Client{}
81 httpResponse, err := client.Do(httpRequest)
82 if err != nil {
83 return nil, err
84 }
85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
86 if err != nil {
87 return nil, err
88 }
89 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
90
91 err = wxpay_utility.ValidateResponse(
92 config.WechatPayPublicKeyId(),
93 config.WechatPayPublicKey(),
94 &httpResponse.Header,
95 respBody,
96 )
97 if err != nil {
98 return nil, err
99 }
100 response := &BrandStoresListResponse{}
101 if err := json.Unmarshal(respBody, response); err != nil {
102 return nil, err
103 }
104
105 return response, nil
106 } else {
107 return nil, wxpay_utility.NewApiException(
108 httpResponse.StatusCode,
109 httpResponse.Header,
110 respBody,
111 )
112 }
113}
114
115type ListBrandStoresRequest struct {
116 BrandId *string `json:"brand_id,omitempty"`
117 Offset *int64 `json:"offset,omitempty"`
118 Limit *int64 `json:"limit,omitempty"`
119 StoreState *StoreState `json:"store_state,omitempty"`
120}
121
122func (o *ListBrandStoresRequest) MarshalJSON() ([]byte, error) {
123 type Alias ListBrandStoresRequest
124 a := &struct {
125 BrandId *string `json:"brand_id,omitempty"`
126 Offset *int64 `json:"offset,omitempty"`
127 Limit *int64 `json:"limit,omitempty"`
128 StoreState *StoreState `json:"store_state,omitempty"`
129 *Alias
130 }{
131
132 BrandId: nil,
133 Offset: nil,
134 Limit: nil,
135 StoreState: nil,
136 Alias: (*Alias)(o),
137 }
138 return json.Marshal(a)
139}
140
141type BrandStoresListResponse struct {
142 BrandId *string `json:"brand_id,omitempty"`
143 Data []BrandStoresEntity `json:"data,omitempty"`
144 Offset *int64 `json:"offset,omitempty"`
145 Limit *int64 `json:"limit,omitempty"`
146 TotalCount *int64 `json:"total_count,omitempty"`
147}
148
149type StoreState string
150
151func (e StoreState) Ptr() *StoreState {
152 return &e
153}
154
155const (
156 STORESTATE_OPEN StoreState = "OPEN"
157 STORESTATE_CREATING StoreState = "CREATING"
158 STORESTATE_CLOSED StoreState = "CLOSED"
159)
160
161type BrandStoresEntity struct {
162 BrandId *string `json:"brand_id,omitempty"`
163 StoreId *string `json:"store_id,omitempty"`
164 StoreState *StoreState `json:"store_state,omitempty"`
165 AuditState *AuditState `json:"audit_state,omitempty"`
166 ReviewRejectReason *string `json:"review_reject_reason,omitempty"`
167 StoreBasics *StoreBase `json:"store_basics,omitempty"`
168 StoreAddress *StoreLocation `json:"store_address,omitempty"`
169 StoreBusiness *StoreBusiness `json:"store_business,omitempty"`
170 StoreRecipient []StoreRecipient `json:"store_recipient,omitempty"`
171}
172
173type AuditState string
174
175func (e AuditState) Ptr() *AuditState {
176 return &e
177}
178
179const (
180 AUDITSTATE_SUCCESS AuditState = "SUCCESS"
181 AUDITSTATE_PROCESSING AuditState = "PROCESSING"
182 AUDITSTATE_REJECTED AuditState = "REJECTED"
183)
184
185type StoreBase struct {
186 StoreReferenceId *string `json:"store_reference_id,omitempty"`
187 BranchName *string `json:"branch_name,omitempty"`
188}
189
190type StoreLocation struct {
191 AddressCode *string `json:"address_code,omitempty"`
192 AddressDetail *string `json:"address_detail,omitempty"`
193 AddressComplements *string `json:"address_complements,omitempty"`
194 Longitude *string `json:"longitude,omitempty"`
195 Latitude *string `json:"latitude,omitempty"`
196}
197
198type StoreBusiness struct {
199 ServicePhone *string `json:"service_phone,omitempty"`
200 BusinessHours *string `json:"business_hours,omitempty"`
201}
202
203type StoreRecipient struct {
204 Mchid *string `json:"mchid,omitempty"`
205 CompanyName *string `json:"company_name,omitempty"`
206 RecipientState *RecipientState `json:"recipient_state,omitempty"`
207}
208
209type RecipientState string
210
211func (e RecipientState) Ptr() *RecipientState {
212 return &e
213}
214
215const (
216 RECIPIENTSTATE_CONFIRMED RecipientState = "CONFIRMED"
217 RECIPIENTSTATE_ADMIN_REJECTED RecipientState = "ADMIN_REJECTED"
218 RECIPIENTSTATE_CONFIRMING RecipientState = "CONFIRMING"
219 RECIPIENTSTATE_TIMEOUT_REJECTED RecipientState = "TIMEOUT_REJECTED"
220)
221