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