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