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