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