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