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