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