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