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