
1package main
2
3import (
4 "demo/wxpay_brand_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10 "time"
11)
12
13func main() {
14
15 config, err := wxpay_brand_utility.CreateBrandConfig(
16 "xxxxxxxx",
17 "1DDE55AD98Exxxxxxxxxx",
18 "/path/to/apiclient_key.pem",
19 "PUB_KEY_ID_xxxxxxxxxxxxx",
20 "/path/to/wxp_pub.pem",
21 )
22 if err != nil {
23 fmt.Println(err)
24 return
25 }
26
27 request := &ListStocksRequest{
28 ProductCouponId: wxpay_brand_utility.String("1000000013"),
29 PageSize: wxpay_brand_utility.Int64(10),
30 State: STOCKSTATE_DEACTIVATED.Ptr(),
31 }
32
33 response, err := ListStocks(config, request)
34 if err != nil {
35 fmt.Printf("请求失败: %+v\n", err)
36
37 return
38 }
39
40
41 fmt.Printf("请求成功: %+v\n", response)
42}
43
44func ListStocks(config *wxpay_brand_utility.BrandConfig, request *ListStocksRequest) (response *ListStocksResponse, err error) {
45 const (
46 host = "https://api.mch.weixin.qq.com"
47 method = "GET"
48 path = "/brand/marketing/product-coupon/product-coupons/{product_coupon_id}/stocks"
49 )
50
51 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
52 if err != nil {
53 return nil, err
54 }
55 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1)
56 query := reqUrl.Query()
57 if request.State != nil {
58 query.Add("state", fmt.Sprintf("%v", *request.State))
59 }
60 if request.PageSize != nil {
61 query.Add("page_size", fmt.Sprintf("%v", *request.PageSize))
62 }
63 if request.PageToken != nil {
64 query.Add("page_token", *request.PageToken)
65 }
66 if request.StockBundleId != nil {
67 query.Add("stock_bundle_id", *request.StockBundleId)
68 }
69 reqUrl.RawQuery = query.Encode()
70 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
71 if err != nil {
72 return nil, err
73 }
74 httpRequest.Header.Set("Accept", "application/json")
75 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
76 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
77 if err != nil {
78 return nil, err
79 }
80 httpRequest.Header.Set("Authorization", authorization)
81
82 client := &http.Client{}
83 httpResponse, err := client.Do(httpRequest)
84 if err != nil {
85 return nil, err
86 }
87 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
88 if err != nil {
89 return nil, err
90 }
91 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
92
93 err = wxpay_brand_utility.ValidateResponse(
94 config.WechatPayPublicKeyId(),
95 config.WechatPayPublicKey(),
96 &httpResponse.Header,
97 respBody,
98 )
99 if err != nil {
100 return nil, err
101 }
102 response := &ListStocksResponse{}
103 if err := json.Unmarshal(respBody, response); err != nil {
104 return nil, err
105 }
106
107 return response, nil
108 } else {
109 return nil, wxpay_brand_utility.NewApiException(
110 httpResponse.StatusCode,
111 httpResponse.Header,
112 respBody,
113 )
114 }
115}
116
117type ListStocksRequest struct {
118 ProductCouponId *string `json:"product_coupon_id,omitempty"`
119 PageSize *int64 `json:"page_size,omitempty"`
120 PageToken *string `json:"page_token,omitempty"`
121 StockBundleId *string `json:"stock_bundle_id,omitempty"`
122 State *StockState `json:"state,omitempty"`
123}
124
125func (o *ListStocksRequest) MarshalJSON() ([]byte, error) {
126 type Alias ListStocksRequest
127 a := &struct {
128 ProductCouponId *string `json:"product_coupon_id,omitempty"`
129 PageSize *int64 `json:"page_size,omitempty"`
130 PageToken *string `json:"page_token,omitempty"`
131 StockBundleId *string `json:"stock_bundle_id,omitempty"`
132 State *StockState `json:"state,omitempty"`
133 *Alias
134 }{
135
136 ProductCouponId: nil,
137 PageSize: nil,
138 PageToken: nil,
139 StockBundleId: nil,
140 State: nil,
141 Alias: (*Alias)(o),
142 }
143 return json.Marshal(a)
144}
145
146type ListStocksResponse struct {
147 TotalCount *int64 `json:"total_count,omitempty"`
148 StockList []StockEntity `json:"stock_list,omitempty"`
149 NextPageToken *string `json:"next_page_token,omitempty"`
150}
151
152type StockState string
153
154func (e StockState) Ptr() *StockState {
155 return &e
156}
157
158const (
159 STOCKSTATE_AUDITING StockState = "AUDITING"
160 STOCKSTATE_SENDING StockState = "SENDING"
161 STOCKSTATE_PAUSED StockState = "PAUSED"
162 STOCKSTATE_STOPPED StockState = "STOPPED"
163 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
164)
165
166type StockEntity struct {
167 ProductCouponId *string `json:"product_coupon_id,omitempty"`
168 StockId *string `json:"stock_id,omitempty"`
169 Remark *string `json:"remark,omitempty"`
170 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"`
171 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"`
172 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"`
173 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"`
174 ProgressiveBundleUsageRule *StockUsageRule `json:"progressive_bundle_usage_rule,omitempty"`
175 StockBundleInfo *StockBundleInfo `json:"stock_bundle_info,omitempty"`
176 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"`
177 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"`
178 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
179 StoreScope *StockStoreScope `json:"store_scope,omitempty"`
180 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"`
181 State *StockState `json:"state,omitempty"`
182 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
183 DeactivateTime *time.Time `json:"deactivate_time,omitempty"`
184 DeactivateReason *string `json:"deactivate_reason,omitempty"`
185}
186
187type CouponCodeMode string
188
189func (e CouponCodeMode) Ptr() *CouponCodeMode {
190 return &e
191}
192
193const (
194 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
195 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
196 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN"
197)
198
199type CouponCodeCountInfo struct {
200 TotalCount *int64 `json:"total_count,omitempty"`
201 AvailableCount *int64 `json:"available_count,omitempty"`
202}
203
204type StockSendRule struct {
205 MaxCount *int64 `json:"max_count,omitempty"`
206 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
207 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
208}
209
210type SingleUsageRule struct {
211 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
212 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
213 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
214 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
215}
216
217type StockUsageRule struct {
218 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
219 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
220 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
221 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
222}
223
224type StockBundleInfo struct {
225 StockBundleId *string `json:"stock_bundle_id,omitempty"`
226 StockBundleIndex *int64 `json:"stock_bundle_index,omitempty"`
227}
228
229type UsageRuleDisplayInfo struct {
230 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
231 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
232 MiniProgramPath *string `json:"mini_program_path,omitempty"`
233 AppPath *string `json:"app_path,omitempty"`
234 UsageDescription *string `json:"usage_description,omitempty"`
235 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
236 AppJumpType *AppJumpType `json:"app_jump_type,omitempty"`
237 PasscodeLink *string `json:"passcode_link,omitempty"`
238}
239
240type CouponDisplayInfo struct {
241 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
242 BackgroundColor *string `json:"background_color,omitempty"`
243 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
244 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
245 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
246}
247
248type NotifyConfig struct {
249 NotifyAppid *string `json:"notify_appid,omitempty"`
250}
251
252type StockStoreScope string
253
254func (e StockStoreScope) Ptr() *StockStoreScope {
255 return &e
256}
257
258const (
259 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
260 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
261 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
262)
263
264type StockSentCountInfo struct {
265 TotalCount *int64 `json:"total_count,omitempty"`
266 TodayCount *int64 `json:"today_count,omitempty"`
267}
268
269type CouponAvailablePeriod struct {
270 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
271 AvailableEndTime *string `json:"available_end_time,omitempty"`
272 AvailableDays *int64 `json:"available_days,omitempty"`
273 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
274 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
275 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
276}
277
278type NormalCouponUsageRule struct {
279 Threshold *int64 `json:"threshold,omitempty"`
280 DiscountAmount *int64 `json:"discount_amount,omitempty"`
281}
282
283type DiscountCouponUsageRule struct {
284 Threshold *int64 `json:"threshold,omitempty"`
285 PercentOff *int64 `json:"percent_off,omitempty"`
286}
287
288type ExchangeCouponUsageRule struct {
289 Threshold *int64 `json:"threshold,omitempty"`
290 ExchangePrice *int64 `json:"exchange_price,omitempty"`
291}
292
293type CouponUsageMethod string
294
295func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
296 return &e
297}
298
299const (
300 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
301 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
302 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
303 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
304)
305
306type CouponAvailableStoreInfo struct {
307 Description *string `json:"description,omitempty"`
308 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
309 MiniProgramPath *string `json:"mini_program_path,omitempty"`
310}
311
312type AppJumpType string
313
314func (e AppJumpType) Ptr() *AppJumpType {
315 return &e
316}
317
318const (
319 APPJUMPTYPE_H5 AppJumpType = "H5"
320 APPJUMPTYPE_PASSCODE_LINK AppJumpType = "PASSCODE_LINK"
321)
322
323type CouponCodeDisplayMode string
324
325func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
326 return &e
327}
328
329const (
330 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
331 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
332 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
333)
334
335type EntranceMiniProgram struct {
336 Appid *string `json:"appid,omitempty"`
337 Path *string `json:"path,omitempty"`
338 EntranceWording *string `json:"entrance_wording,omitempty"`
339 GuidanceWording *string `json:"guidance_wording,omitempty"`
340}
341
342type EntranceOfficialAccount struct {
343 Appid *string `json:"appid,omitempty"`
344}
345
346type EntranceFinder struct {
347 FinderId *string `json:"finder_id,omitempty"`
348 FinderVideoId *string `json:"finder_video_id,omitempty"`
349 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
350}
351
352type FixedWeekPeriod struct {
353 DayList []WeekEnum `json:"day_list,omitempty"`
354 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
355}
356
357type TimePeriod struct {
358 BeginTime *string `json:"begin_time,omitempty"`
359 EndTime *string `json:"end_time,omitempty"`
360}
361
362type WeekEnum string
363
364func (e WeekEnum) Ptr() *WeekEnum {
365 return &e
366}
367
368const (
369 WEEKENUM_MONDAY WeekEnum = "MONDAY"
370 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
371 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
372 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
373 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
374 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
375 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
376)
377
378type PeriodOfTheDay struct {
379 BeginTime *int64 `json:"begin_time,omitempty"`
380 EndTime *int64 `json:"end_time,omitempty"`
381}
382