
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}
237
238type CouponDisplayInfo struct {
239 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
240 BackgroundColor *string `json:"background_color,omitempty"`
241 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
242 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
243 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
244}
245
246type NotifyConfig struct {
247 NotifyAppid *string `json:"notify_appid,omitempty"`
248}
249
250type StockStoreScope string
251
252func (e StockStoreScope) Ptr() *StockStoreScope {
253 return &e
254}
255
256const (
257 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
258 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
259 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
260)
261
262type StockSentCountInfo struct {
263 TotalCount *int64 `json:"total_count,omitempty"`
264 TodayCount *int64 `json:"today_count,omitempty"`
265}
266
267type CouponAvailablePeriod struct {
268 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
269 AvailableEndTime *string `json:"available_end_time,omitempty"`
270 AvailableDays *int64 `json:"available_days,omitempty"`
271 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
272 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
273 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
274}
275
276type NormalCouponUsageRule struct {
277 Threshold *int64 `json:"threshold,omitempty"`
278 DiscountAmount *int64 `json:"discount_amount,omitempty"`
279}
280
281type DiscountCouponUsageRule struct {
282 Threshold *int64 `json:"threshold,omitempty"`
283 PercentOff *int64 `json:"percent_off,omitempty"`
284}
285
286type ExchangeCouponUsageRule struct {
287 Threshold *int64 `json:"threshold,omitempty"`
288 ExchangePrice *int64 `json:"exchange_price,omitempty"`
289}
290
291type CouponUsageMethod string
292
293func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
294 return &e
295}
296
297const (
298 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
299 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
300 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
301 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
302)
303
304type CouponAvailableStoreInfo struct {
305 Description *string `json:"description,omitempty"`
306 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
307 MiniProgramPath *string `json:"mini_program_path,omitempty"`
308}
309
310type CouponCodeDisplayMode string
311
312func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
313 return &e
314}
315
316const (
317 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
318 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
319 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
320)
321
322type EntranceMiniProgram struct {
323 Appid *string `json:"appid,omitempty"`
324 Path *string `json:"path,omitempty"`
325 EntranceWording *string `json:"entrance_wording,omitempty"`
326 GuidanceWording *string `json:"guidance_wording,omitempty"`
327}
328
329type EntranceOfficialAccount struct {
330 Appid *string `json:"appid,omitempty"`
331}
332
333type EntranceFinder struct {
334 FinderId *string `json:"finder_id,omitempty"`
335 FinderVideoId *string `json:"finder_video_id,omitempty"`
336 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
337}
338
339type FixedWeekPeriod struct {
340 DayList []WeekEnum `json:"day_list,omitempty"`
341 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
342}
343
344type TimePeriod struct {
345 BeginTime *string `json:"begin_time,omitempty"`
346 EndTime *string `json:"end_time,omitempty"`
347}
348
349type WeekEnum string
350
351func (e WeekEnum) Ptr() *WeekEnum {
352 return &e
353}
354
355const (
356 WEEKENUM_MONDAY WeekEnum = "MONDAY"
357 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
358 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
359 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
360 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
361 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
362 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
363)
364
365type PeriodOfTheDay struct {
366 BeginTime *int64 `json:"begin_time,omitempty"`
367 EndTime *int64 `json:"end_time,omitempty"`
368}
369