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