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