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