
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_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_brand_utility.CreateBrandConfig(
17 "xxxxxxxx",
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 := &DeactivateUserProductCouponRequest{
29 ProductCouponId: wxpay_brand_utility.String("1000000013"),
30 StockId: wxpay_brand_utility.String("1000000013001"),
31 CouponCode: wxpay_brand_utility.String("Code_123456"),
32 Appid: wxpay_brand_utility.String("wx233544546545989"),
33 Openid: wxpay_brand_utility.String("oh-394z-6CGkNoJrsDLTTUKiAnp4"),
34 OutRequestNo: wxpay_brand_utility.String("MCHDEACTIVATE202003101234"),
35 DeactivateReason: wxpay_brand_utility.String("商品已下线,使用户商品券失效"),
36 }
37
38 response, err := DeactivateUserProductCoupon(config, request)
39 if err != nil {
40 fmt.Printf("请求失败: %+v\n", err)
41
42 return
43 }
44
45
46 fmt.Printf("请求成功: %+v\n", response)
47}
48
49func DeactivateUserProductCoupon(config *wxpay_brand_utility.BrandConfig, request *DeactivateUserProductCouponRequest) (response *UserProductCouponEntity, err error) {
50 const (
51 host = "https://api.mch.weixin.qq.com"
52 method = "POST"
53 path = "/brand/marketing/product-coupon/users/{openid}/coupons/{coupon_code}/deactivate"
54 )
55
56 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
57 if err != nil {
58 return nil, err
59 }
60 reqUrl.Path = strings.Replace(reqUrl.Path, "{coupon_code}", url.PathEscape(*request.CouponCode), -1)
61 reqUrl.Path = strings.Replace(reqUrl.Path, "{openid}", url.PathEscape(*request.Openid), -1)
62 reqBody, err := json.Marshal(request)
63 if err != nil {
64 return nil, err
65 }
66 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
67 if err != nil {
68 return nil, err
69 }
70 httpRequest.Header.Set("Accept", "application/json")
71 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
72 httpRequest.Header.Set("Content-Type", "application/json")
73 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
74 if err != nil {
75 return nil, err
76 }
77 httpRequest.Header.Set("Authorization", authorization)
78
79 client := &http.Client{}
80 httpResponse, err := client.Do(httpRequest)
81 if err != nil {
82 return nil, err
83 }
84 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
85 if err != nil {
86 return nil, err
87 }
88 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
89
90 err = wxpay_brand_utility.ValidateResponse(
91 config.WechatPayPublicKeyId(),
92 config.WechatPayPublicKey(),
93 &httpResponse.Header,
94 respBody,
95 )
96 if err != nil {
97 return nil, err
98 }
99 response := &UserProductCouponEntity{}
100 if err := json.Unmarshal(respBody, response); err != nil {
101 return nil, err
102 }
103
104 return response, nil
105 } else {
106 return nil, wxpay_brand_utility.NewApiException(
107 httpResponse.StatusCode,
108 httpResponse.Header,
109 respBody,
110 )
111 }
112}
113
114type DeactivateUserProductCouponRequest struct {
115 ProductCouponId *string `json:"product_coupon_id,omitempty"`
116 StockId *string `json:"stock_id,omitempty"`
117 CouponCode *string `json:"coupon_code,omitempty"`
118 Appid *string `json:"appid,omitempty"`
119 Openid *string `json:"openid,omitempty"`
120 OutRequestNo *string `json:"out_request_no,omitempty"`
121 DeactivateReason *string `json:"deactivate_reason,omitempty"`
122}
123
124func (o *DeactivateUserProductCouponRequest) MarshalJSON() ([]byte, error) {
125 type Alias DeactivateUserProductCouponRequest
126 a := &struct {
127 CouponCode *string `json:"coupon_code,omitempty"`
128 Openid *string `json:"openid,omitempty"`
129 *Alias
130 }{
131
132 CouponCode: nil,
133 Openid: nil,
134 Alias: (*Alias)(o),
135 }
136 return json.Marshal(a)
137}
138
139type UserProductCouponEntity struct {
140 CouponCode *string `json:"coupon_code,omitempty"`
141 CouponState *UserProductCouponState `json:"coupon_state,omitempty"`
142 ValidBeginTime *time.Time `json:"valid_begin_time,omitempty"`
143 ValidEndTime *time.Time `json:"valid_end_time,omitempty"`
144 ReceiveTime *string `json:"receive_time,omitempty"`
145 SendRequestNo *string `json:"send_request_no,omitempty"`
146 SendChannel *UserProductCouponSendChannel `json:"send_channel,omitempty"`
147 ConfirmRequestNo *string `json:"confirm_request_no,omitempty"`
148 ConfirmTime *time.Time `json:"confirm_time,omitempty"`
149 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
150 DeactivateTime *string `json:"deactivate_time,omitempty"`
151 DeactivateReason *string `json:"deactivate_reason,omitempty"`
152 SingleUsageDetail *CouponUsageDetail `json:"single_usage_detail,omitempty"`
153 ProductCoupon *ProductCouponEntity `json:"product_coupon,omitempty"`
154 Stock *StockEntity `json:"stock,omitempty"`
155 Attach *string `json:"attach,omitempty"`
156 ChannelCustomInfo *string `json:"channel_custom_info,omitempty"`
157 CouponTagInfo *CouponTagInfo `json:"coupon_tag_info,omitempty"`
158}
159
160type UserProductCouponState string
161
162func (e UserProductCouponState) Ptr() *UserProductCouponState {
163 return &e
164}
165
166const (
167 USERPRODUCTCOUPONSTATE_CONFIRMING UserProductCouponState = "CONFIRMING"
168 USERPRODUCTCOUPONSTATE_PENDING UserProductCouponState = "PENDING"
169 USERPRODUCTCOUPONSTATE_EFFECTIVE UserProductCouponState = "EFFECTIVE"
170 USERPRODUCTCOUPONSTATE_USED UserProductCouponState = "USED"
171 USERPRODUCTCOUPONSTATE_EXPIRED UserProductCouponState = "EXPIRED"
172 USERPRODUCTCOUPONSTATE_DELETED UserProductCouponState = "DELETED"
173 USERPRODUCTCOUPONSTATE_DEACTIVATED UserProductCouponState = "DEACTIVATED"
174)
175
176type UserProductCouponSendChannel string
177
178func (e UserProductCouponSendChannel) Ptr() *UserProductCouponSendChannel {
179 return &e
180}
181
182const (
183 USERPRODUCTCOUPONSENDCHANNEL_BRAND_MANAGE UserProductCouponSendChannel = "BRAND_MANAGE"
184 USERPRODUCTCOUPONSENDCHANNEL_API UserProductCouponSendChannel = "API"
185 USERPRODUCTCOUPONSENDCHANNEL_RECEIVE_COMPONENT UserProductCouponSendChannel = "RECEIVE_COMPONENT"
186)
187
188type CouponUsageDetail struct {
189 UseRequestNo *string `json:"use_request_no,omitempty"`
190 UseTime *time.Time `json:"use_time,omitempty"`
191 ReturnRequestNo *string `json:"return_request_no,omitempty"`
192 ReturnTime *time.Time `json:"return_time,omitempty"`
193 AssociatedOrderInfo *UserProductCouponAssociatedOrderInfo `json:"associated_order_info,omitempty"`
194 AssociatedPayScoreOrderInfo *UserProductCouponAssociatedPayScoreOrderInfo `json:"associated_pay_score_order_info,omitempty"`
195 SavedAmount *int64 `json:"saved_amount,omitempty"`
196}
197
198type ProductCouponEntity struct {
199 ProductCouponId *string `json:"product_coupon_id,omitempty"`
200 Scope *ProductCouponScope `json:"scope,omitempty"`
201 Type *ProductCouponType `json:"type,omitempty"`
202 UsageMode *UsageMode `json:"usage_mode,omitempty"`
203 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"`
204 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"`
205 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"`
206 OutProductNo *string `json:"out_product_no,omitempty"`
207 State *ProductCouponState `json:"state,omitempty"`
208 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
209 DeactivateTime *string `json:"deactivate_time,omitempty"`
210 DeactivateReason *string `json:"deactivate_reason,omitempty"`
211}
212
213type StockEntity struct {
214 ProductCouponId *string `json:"product_coupon_id,omitempty"`
215 StockId *string `json:"stock_id,omitempty"`
216 Remark *string `json:"remark,omitempty"`
217 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"`
218 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"`
219 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"`
220 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"`
221 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"`
222 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"`
223 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
224 StoreScope *StockStoreScope `json:"store_scope,omitempty"`
225 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"`
226 State *StockState `json:"state,omitempty"`
227 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
228 DeactivateTime *time.Time `json:"deactivate_time,omitempty"`
229 DeactivateReason *string `json:"deactivate_reason,omitempty"`
230}
231
232type CouponTagInfo struct {
233 CouponTagList []UserProductCouponTag `json:"coupon_tag_list,omitempty"`
234 MemberTagInfo *MemberTagInfo `json:"member_tag_info,omitempty"`
235}
236
237type UserProductCouponAssociatedOrderInfo struct {
238 TransactionId *string `json:"transaction_id,omitempty"`
239 OutTradeNo *string `json:"out_trade_no,omitempty"`
240 Mchid *string `json:"mchid,omitempty"`
241 SubMchid *string `json:"sub_mchid,omitempty"`
242}
243
244type UserProductCouponAssociatedPayScoreOrderInfo struct {
245 OrderId *string `json:"order_id,omitempty"`
246 OutOrderNo *string `json:"out_order_no,omitempty"`
247 Mchid *string `json:"mchid,omitempty"`
248 SubMchid *string `json:"sub_mchid,omitempty"`
249}
250
251type ProductCouponScope string
252
253func (e ProductCouponScope) Ptr() *ProductCouponScope {
254 return &e
255}
256
257const (
258 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL"
259 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE"
260)
261
262type ProductCouponType string
263
264func (e ProductCouponType) Ptr() *ProductCouponType {
265 return &e
266}
267
268const (
269 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
270 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
271 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
272)
273
274type UsageMode string
275
276func (e UsageMode) Ptr() *UsageMode {
277 return &e
278}
279
280const (
281 USAGEMODE_SINGLE UsageMode = "SINGLE"
282 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
283)
284
285type SingleUsageInfo struct {
286 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
287 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
288}
289
290type ProgressiveBundleUsageInfo struct {
291 Count *int64 `json:"count,omitempty"`
292 IntervalDays *int64 `json:"interval_days,omitempty"`
293}
294
295type ProductCouponDisplayInfo struct {
296 Name *string `json:"name,omitempty"`
297 ImageUrl *string `json:"image_url,omitempty"`
298 BackgroundUrl *string `json:"background_url,omitempty"`
299 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
300 OriginalPrice *int64 `json:"original_price,omitempty"`
301 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
302}
303
304type ProductCouponState string
305
306func (e ProductCouponState) Ptr() *ProductCouponState {
307 return &e
308}
309
310const (
311 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
312 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
313 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
314)
315
316type CouponCodeMode string
317
318func (e CouponCodeMode) Ptr() *CouponCodeMode {
319 return &e
320}
321
322const (
323 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
324 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
325 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN"
326)
327
328type CouponCodeCountInfo struct {
329 TotalCount *int64 `json:"total_count,omitempty"`
330 AvailableCount *int64 `json:"available_count,omitempty"`
331}
332
333type StockSendRule struct {
334 MaxCount *int64 `json:"max_count,omitempty"`
335 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
336 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
337}
338
339type SingleUsageRule struct {
340 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
341 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
342 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
343 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
344}
345
346type UsageRuleDisplayInfo struct {
347 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
348 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
349 MiniProgramPath *string `json:"mini_program_path,omitempty"`
350 AppPath *string `json:"app_path,omitempty"`
351 UsageDescription *string `json:"usage_description,omitempty"`
352 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
353}
354
355type CouponDisplayInfo struct {
356 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
357 BackgroundColor *string `json:"background_color,omitempty"`
358 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
359 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
360 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
361}
362
363type NotifyConfig struct {
364 NotifyAppid *string `json:"notify_appid,omitempty"`
365}
366
367type StockStoreScope string
368
369func (e StockStoreScope) Ptr() *StockStoreScope {
370 return &e
371}
372
373const (
374 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
375 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
376 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
377)
378
379type StockSentCountInfo struct {
380 TotalCount *int64 `json:"total_count,omitempty"`
381 TodayCount *int64 `json:"today_count,omitempty"`
382}
383
384type StockState string
385
386func (e StockState) Ptr() *StockState {
387 return &e
388}
389
390const (
391 STOCKSTATE_AUDITING StockState = "AUDITING"
392 STOCKSTATE_SENDING StockState = "SENDING"
393 STOCKSTATE_PAUSED StockState = "PAUSED"
394 STOCKSTATE_STOPPED StockState = "STOPPED"
395 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
396)
397
398type UserProductCouponTag string
399
400func (e UserProductCouponTag) Ptr() *UserProductCouponTag {
401 return &e
402}
403
404const (
405 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER"
406)
407
408type MemberTagInfo struct {
409 MemberCardId *string `json:"member_card_id,omitempty"`
410}
411
412type NormalCouponUsageRule struct {
413 Threshold *int64 `json:"threshold,omitempty"`
414 DiscountAmount *int64 `json:"discount_amount,omitempty"`
415}
416
417type DiscountCouponUsageRule struct {
418 Threshold *int64 `json:"threshold,omitempty"`
419 PercentOff *int64 `json:"percent_off,omitempty"`
420}
421
422type ComboPackage struct {
423 Name *string `json:"name,omitempty"`
424 PickCount *int64 `json:"pick_count,omitempty"`
425 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
426}
427
428type CouponAvailablePeriod struct {
429 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
430 AvailableEndTime *string `json:"available_end_time,omitempty"`
431 AvailableDays *int64 `json:"available_days,omitempty"`
432 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
433 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
434 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
435}
436
437type ExchangeCouponUsageRule struct {
438 Threshold *int64 `json:"threshold,omitempty"`
439 ExchangePrice *int64 `json:"exchange_price,omitempty"`
440}
441
442type CouponUsageMethod string
443
444func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
445 return &e
446}
447
448const (
449 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
450 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
451 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
452 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
453)
454
455type CouponAvailableStoreInfo struct {
456 Description *string `json:"description,omitempty"`
457 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
458 MiniProgramPath *string `json:"mini_program_path,omitempty"`
459}
460
461type CouponCodeDisplayMode string
462
463func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
464 return &e
465}
466
467const (
468 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
469 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
470 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
471)
472
473type EntranceMiniProgram struct {
474 Appid *string `json:"appid,omitempty"`
475 Path *string `json:"path,omitempty"`
476 EntranceWording *string `json:"entrance_wording,omitempty"`
477 GuidanceWording *string `json:"guidance_wording,omitempty"`
478}
479
480type EntranceOfficialAccount struct {
481 Appid *string `json:"appid,omitempty"`
482}
483
484type EntranceFinder struct {
485 FinderId *string `json:"finder_id,omitempty"`
486 FinderVideoId *string `json:"finder_video_id,omitempty"`
487 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
488}
489
490type ComboPackageChoice struct {
491 Name *string `json:"name,omitempty"`
492 Price *int64 `json:"price,omitempty"`
493 Count *int64 `json:"count,omitempty"`
494 ImageUrl *string `json:"image_url,omitempty"`
495 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
496 MiniProgramPath *string `json:"mini_program_path,omitempty"`
497}
498
499type FixedWeekPeriod struct {
500 DayList []WeekEnum `json:"day_list,omitempty"`
501 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
502}
503
504type TimePeriod struct {
505 BeginTime *string `json:"begin_time,omitempty"`
506 EndTime *string `json:"end_time,omitempty"`
507}
508
509type WeekEnum string
510
511func (e WeekEnum) Ptr() *WeekEnum {
512 return &e
513}
514
515const (
516 WEEKENUM_MONDAY WeekEnum = "MONDAY"
517 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
518 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
519 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
520 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
521 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
522 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
523)
524
525type PeriodOfTheDay struct {
526 BeginTime *int64 `json:"begin_time,omitempty"`
527 EndTime *int64 `json:"end_time,omitempty"`
528}
529