
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 PRODUCTCOUPONSCOPE_CATEGORY ProductCouponScope = "CATEGORY"
261)
262
263type ProductCouponType string
264
265func (e ProductCouponType) Ptr() *ProductCouponType {
266 return &e
267}
268
269const (
270 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
271 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
272 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
273)
274
275type UsageMode string
276
277func (e UsageMode) Ptr() *UsageMode {
278 return &e
279}
280
281const (
282 USAGEMODE_SINGLE UsageMode = "SINGLE"
283 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
284)
285
286type SingleUsageInfo struct {
287 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
288 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
289}
290
291type ProgressiveBundleUsageInfo struct {
292 Count *int64 `json:"count,omitempty"`
293 IntervalDays *int64 `json:"interval_days,omitempty"`
294}
295
296type ProductCouponDisplayInfo struct {
297 Name *string `json:"name,omitempty"`
298 ImageUrl *string `json:"image_url,omitempty"`
299 BackgroundUrl *string `json:"background_url,omitempty"`
300 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
301 OriginalPrice *int64 `json:"original_price,omitempty"`
302 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
303}
304
305type ProductCouponState string
306
307func (e ProductCouponState) Ptr() *ProductCouponState {
308 return &e
309}
310
311const (
312 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
313 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
314 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
315)
316
317type CouponCodeMode string
318
319func (e CouponCodeMode) Ptr() *CouponCodeMode {
320 return &e
321}
322
323const (
324 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
325 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
326 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN"
327)
328
329type CouponCodeCountInfo struct {
330 TotalCount *int64 `json:"total_count,omitempty"`
331 AvailableCount *int64 `json:"available_count,omitempty"`
332}
333
334type StockSendRule struct {
335 MaxCount *int64 `json:"max_count,omitempty"`
336 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
337 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
338}
339
340type SingleUsageRule struct {
341 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
342 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
343 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
344 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
345}
346
347type UsageRuleDisplayInfo struct {
348 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
349 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
350 MiniProgramPath *string `json:"mini_program_path,omitempty"`
351 AppPath *string `json:"app_path,omitempty"`
352 UsageDescription *string `json:"usage_description,omitempty"`
353 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
354 AppJumpType *AppJumpType `json:"app_jump_type,omitempty"`
355 PasscodeLink *string `json:"passcode_link,omitempty"`
356}
357
358type CouponDisplayInfo struct {
359 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
360 BackgroundColor *string `json:"background_color,omitempty"`
361 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
362 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
363 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
364}
365
366type NotifyConfig struct {
367 NotifyAppid *string `json:"notify_appid,omitempty"`
368}
369
370type StockStoreScope string
371
372func (e StockStoreScope) Ptr() *StockStoreScope {
373 return &e
374}
375
376const (
377 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
378 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
379 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
380)
381
382type StockSentCountInfo struct {
383 TotalCount *int64 `json:"total_count,omitempty"`
384 TodayCount *int64 `json:"today_count,omitempty"`
385}
386
387type StockState string
388
389func (e StockState) Ptr() *StockState {
390 return &e
391}
392
393const (
394 STOCKSTATE_AUDITING StockState = "AUDITING"
395 STOCKSTATE_SENDING StockState = "SENDING"
396 STOCKSTATE_PAUSED StockState = "PAUSED"
397 STOCKSTATE_STOPPED StockState = "STOPPED"
398 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
399)
400
401type UserProductCouponTag string
402
403func (e UserProductCouponTag) Ptr() *UserProductCouponTag {
404 return &e
405}
406
407const (
408 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER"
409)
410
411type MemberTagInfo struct {
412 MemberCardId *string `json:"member_card_id,omitempty"`
413}
414
415type NormalCouponUsageRule struct {
416 Threshold *int64 `json:"threshold,omitempty"`
417 DiscountAmount *int64 `json:"discount_amount,omitempty"`
418}
419
420type DiscountCouponUsageRule struct {
421 Threshold *int64 `json:"threshold,omitempty"`
422 PercentOff *int64 `json:"percent_off,omitempty"`
423}
424
425type ComboPackage struct {
426 Name *string `json:"name,omitempty"`
427 PickCount *int64 `json:"pick_count,omitempty"`
428 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
429}
430
431type CouponAvailablePeriod struct {
432 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
433 AvailableEndTime *string `json:"available_end_time,omitempty"`
434 AvailableDays *int64 `json:"available_days,omitempty"`
435 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
436 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
437 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
438 AvailableSeconds *int64 `json:"available_seconds,omitempty"`
439}
440
441type ExchangeCouponUsageRule struct {
442 Threshold *int64 `json:"threshold,omitempty"`
443 ExchangePrice *int64 `json:"exchange_price,omitempty"`
444}
445
446type CouponUsageMethod string
447
448func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
449 return &e
450}
451
452const (
453 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
454 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
455 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
456 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
457)
458
459type CouponAvailableStoreInfo struct {
460 Description *string `json:"description,omitempty"`
461 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
462 MiniProgramPath *string `json:"mini_program_path,omitempty"`
463}
464
465type AppJumpType string
466
467func (e AppJumpType) Ptr() *AppJumpType {
468 return &e
469}
470
471const (
472 APPJUMPTYPE_H5 AppJumpType = "H5"
473 APPJUMPTYPE_PASSCODE_LINK AppJumpType = "PASSCODE_LINK"
474 APPJUMPTYPE_USAGE_GUIDE AppJumpType = "USAGE_GUIDE"
475)
476
477type CouponCodeDisplayMode string
478
479func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
480 return &e
481}
482
483const (
484 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
485 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
486 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
487)
488
489type EntranceMiniProgram struct {
490 Appid *string `json:"appid,omitempty"`
491 Path *string `json:"path,omitempty"`
492 EntranceWording *string `json:"entrance_wording,omitempty"`
493 GuidanceWording *string `json:"guidance_wording,omitempty"`
494}
495
496type EntranceOfficialAccount struct {
497 Appid *string `json:"appid,omitempty"`
498}
499
500type EntranceFinder struct {
501 FinderId *string `json:"finder_id,omitempty"`
502 FinderVideoId *string `json:"finder_video_id,omitempty"`
503 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
504}
505
506type ComboPackageChoice struct {
507 Name *string `json:"name,omitempty"`
508 Price *int64 `json:"price,omitempty"`
509 Count *int64 `json:"count,omitempty"`
510 ImageUrl *string `json:"image_url,omitempty"`
511 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
512 MiniProgramPath *string `json:"mini_program_path,omitempty"`
513}
514
515type FixedWeekPeriod struct {
516 DayList []WeekEnum `json:"day_list,omitempty"`
517 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
518}
519
520type TimePeriod struct {
521 BeginTime *string `json:"begin_time,omitempty"`
522 EndTime *string `json:"end_time,omitempty"`
523}
524
525type WeekEnum string
526
527func (e WeekEnum) Ptr() *WeekEnum {
528 return &e
529}
530
531const (
532 WEEKENUM_MONDAY WeekEnum = "MONDAY"
533 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
534 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
535 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
536 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
537 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
538 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
539)
540
541type PeriodOfTheDay struct {
542 BeginTime *int64 `json:"begin_time,omitempty"`
543 EndTime *int64 `json:"end_time,omitempty"`
544}
545