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