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