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