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