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