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