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