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