
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 USERPRODUCTCOUPONSENDCHANNEL_MEMBER UserProductCouponSendChannel = "MEMBER"
187 USERPRODUCTCOUPONSENDCHANNEL_STICKER_GIFT UserProductCouponSendChannel = "STICKER_GIFT"
188)
189
190type CouponUsageDetail struct {
191 UseRequestNo *string `json:"use_request_no,omitempty"`
192 UseTime *time.Time `json:"use_time,omitempty"`
193 ReturnRequestNo *string `json:"return_request_no,omitempty"`
194 ReturnTime *time.Time `json:"return_time,omitempty"`
195 AssociatedOrderInfo *UserProductCouponAssociatedOrderInfo `json:"associated_order_info,omitempty"`
196 AssociatedPayScoreOrderInfo *UserProductCouponAssociatedPayScoreOrderInfo `json:"associated_pay_score_order_info,omitempty"`
197 SavedAmount *int64 `json:"saved_amount,omitempty"`
198 UseProductNoList []string `json:"use_product_no_list,omitempty"`
199}
200
201type ProductCouponEntity struct {
202 ProductCouponId *string `json:"product_coupon_id,omitempty"`
203 Scope *ProductCouponScope `json:"scope,omitempty"`
204 Type *ProductCouponType `json:"type,omitempty"`
205 UsageMode *UsageMode `json:"usage_mode,omitempty"`
206 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"`
207 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"`
208 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"`
209 OutProductNo *string `json:"out_product_no,omitempty"`
210 State *ProductCouponState `json:"state,omitempty"`
211 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
212 DeactivateTime *string `json:"deactivate_time,omitempty"`
213 DeactivateReason *string `json:"deactivate_reason,omitempty"`
214 ShowAsExchange *bool `json:"show_as_exchange,omitempty"`
215 AvailableProductNoList []string `json:"available_product_no_list,omitempty"`
216 BusinessMode *ProductCouponBusinessMode `json:"business_mode,omitempty"`
217}
218
219type StockEntity struct {
220 ProductCouponId *string `json:"product_coupon_id,omitempty"`
221 StockId *string `json:"stock_id,omitempty"`
222 Remark *string `json:"remark,omitempty"`
223 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"`
224 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"`
225 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"`
226 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"`
227 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"`
228 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"`
229 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
230 StoreScope *StockStoreScope `json:"store_scope,omitempty"`
231 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"`
232 State *StockState `json:"state,omitempty"`
233 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
234 DeactivateTime *time.Time `json:"deactivate_time,omitempty"`
235 DeactivateReason *string `json:"deactivate_reason,omitempty"`
236}
237
238type CouponTagInfo struct {
239 CouponTagList []UserProductCouponTag `json:"coupon_tag_list,omitempty"`
240 MemberTagInfo *MemberTagInfo `json:"member_tag_info,omitempty"`
241}
242
243type UserProductCouponAssociatedOrderInfo struct {
244 TransactionId *string `json:"transaction_id,omitempty"`
245 OutTradeNo *string `json:"out_trade_no,omitempty"`
246 Mchid *string `json:"mchid,omitempty"`
247 SubMchid *string `json:"sub_mchid,omitempty"`
248}
249
250type UserProductCouponAssociatedPayScoreOrderInfo struct {
251 OrderId *string `json:"order_id,omitempty"`
252 OutOrderNo *string `json:"out_order_no,omitempty"`
253 Mchid *string `json:"mchid,omitempty"`
254 SubMchid *string `json:"sub_mchid,omitempty"`
255}
256
257type ProductCouponScope string
258
259func (e ProductCouponScope) Ptr() *ProductCouponScope {
260 return &e
261}
262
263const (
264 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL"
265 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE"
266 PRODUCTCOUPONSCOPE_CATEGORY ProductCouponScope = "CATEGORY"
267)
268
269type ProductCouponType string
270
271func (e ProductCouponType) Ptr() *ProductCouponType {
272 return &e
273}
274
275const (
276 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
277 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
278 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
279)
280
281type UsageMode string
282
283func (e UsageMode) Ptr() *UsageMode {
284 return &e
285}
286
287const (
288 USAGEMODE_SINGLE UsageMode = "SINGLE"
289 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
290)
291
292type SingleUsageInfo struct {
293 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
294 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
295}
296
297type ProgressiveBundleUsageInfo struct {
298 Count *int64 `json:"count,omitempty"`
299 IntervalDays *int64 `json:"interval_days,omitempty"`
300}
301
302type ProductCouponDisplayInfo struct {
303 Name *string `json:"name,omitempty"`
304 ImageUrl *string `json:"image_url,omitempty"`
305 BackgroundUrl *string `json:"background_url,omitempty"`
306 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
307 OriginalPrice *int64 `json:"original_price,omitempty"`
308 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
309}
310
311type ProductCouponState string
312
313func (e ProductCouponState) Ptr() *ProductCouponState {
314 return &e
315}
316
317const (
318 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
319 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
320 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
321)
322
323type ProductCouponBusinessMode string
324
325func (e ProductCouponBusinessMode) Ptr() *ProductCouponBusinessMode {
326 return &e
327}
328
329const (
330 PRODUCTCOUPONBUSINESSMODE_FREE ProductCouponBusinessMode = "FREE"
331)
332
333type CouponCodeMode string
334
335func (e CouponCodeMode) Ptr() *CouponCodeMode {
336 return &e
337}
338
339const (
340 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
341 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
342 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN"
343)
344
345type CouponCodeCountInfo struct {
346 TotalCount *int64 `json:"total_count,omitempty"`
347 AvailableCount *int64 `json:"available_count,omitempty"`
348}
349
350type StockSendRule struct {
351 MaxCount *int64 `json:"max_count,omitempty"`
352 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
353 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
354}
355
356type SingleUsageRule struct {
357 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
358 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
359 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
360 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
361}
362
363type UsageRuleDisplayInfo struct {
364 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
365 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
366 MiniProgramPath *string `json:"mini_program_path,omitempty"`
367 AppPath *string `json:"app_path,omitempty"`
368 UsageDescription *string `json:"usage_description,omitempty"`
369 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
370 AppJumpType *AppJumpType `json:"app_jump_type,omitempty"`
371 PasscodeLink *string `json:"passcode_link,omitempty"`
372}
373
374type CouponDisplayInfo struct {
375 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
376 BackgroundColor *string `json:"background_color,omitempty"`
377 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
378 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
379}
380
381type NotifyConfig struct {
382 NotifyAppid *string `json:"notify_appid,omitempty"`
383}
384
385type StockStoreScope string
386
387func (e StockStoreScope) Ptr() *StockStoreScope {
388 return &e
389}
390
391const (
392 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
393 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
394 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
395)
396
397type StockSentCountInfo struct {
398 TotalCount *int64 `json:"total_count,omitempty"`
399 TodayCount *int64 `json:"today_count,omitempty"`
400}
401
402type StockState string
403
404func (e StockState) Ptr() *StockState {
405 return &e
406}
407
408const (
409 STOCKSTATE_AUDITING StockState = "AUDITING"
410 STOCKSTATE_SENDING StockState = "SENDING"
411 STOCKSTATE_PAUSED StockState = "PAUSED"
412 STOCKSTATE_STOPPED StockState = "STOPPED"
413 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
414)
415
416type UserProductCouponTag string
417
418func (e UserProductCouponTag) Ptr() *UserProductCouponTag {
419 return &e
420}
421
422const (
423 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER"
424 USERPRODUCTCOUPONTAG_MEMBER_ACTIVITY UserProductCouponTag = "MEMBER_ACTIVITY"
425)
426
427type MemberTagInfo struct {
428 MemberCardId *string `json:"member_card_id,omitempty"`
429}
430
431type NormalCouponUsageRule struct {
432 Threshold *int64 `json:"threshold,omitempty"`
433 DiscountAmount *int64 `json:"discount_amount,omitempty"`
434}
435
436type DiscountCouponUsageRule struct {
437 Threshold *int64 `json:"threshold,omitempty"`
438 PercentOff *int64 `json:"percent_off,omitempty"`
439}
440
441type ComboPackage struct {
442 Name *string `json:"name,omitempty"`
443 PickCount *int64 `json:"pick_count,omitempty"`
444 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
445}
446
447type CouponAvailablePeriod struct {
448 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
449 AvailableEndTime *string `json:"available_end_time,omitempty"`
450 AvailableDays *int64 `json:"available_days,omitempty"`
451 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
452 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
453 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
454 AvailableSeconds *int64 `json:"available_seconds,omitempty"`
455}
456
457type ExchangeCouponUsageRule struct {
458 Threshold *int64 `json:"threshold,omitempty"`
459 ExchangePrice *int64 `json:"exchange_price,omitempty"`
460}
461
462type CouponUsageMethod string
463
464func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
465 return &e
466}
467
468const (
469 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
470 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
471 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
472 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
473)
474
475type CouponAvailableStoreInfo struct {
476 Description *string `json:"description,omitempty"`
477 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
478 MiniProgramPath *string `json:"mini_program_path,omitempty"`
479}
480
481type AppJumpType string
482
483func (e AppJumpType) Ptr() *AppJumpType {
484 return &e
485}
486
487const (
488 APPJUMPTYPE_H5 AppJumpType = "H5"
489 APPJUMPTYPE_PASSCODE_LINK AppJumpType = "PASSCODE_LINK"
490 APPJUMPTYPE_USAGE_GUIDE AppJumpType = "USAGE_GUIDE"
491)
492
493type CouponCodeDisplayMode string
494
495func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
496 return &e
497}
498
499const (
500 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
501 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
502 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
503)
504
505type EntranceMiniProgram struct {
506 Appid *string `json:"appid,omitempty"`
507 Path *string `json:"path,omitempty"`
508 EntranceWording *string `json:"entrance_wording,omitempty"`
509 GuidanceWording *string `json:"guidance_wording,omitempty"`
510}
511
512type EntranceOfficialAccount struct {
513 Appid *string `json:"appid,omitempty"`
514}
515
516type ComboPackageChoice struct {
517 Name *string `json:"name,omitempty"`
518 Price *int64 `json:"price,omitempty"`
519 Count *int64 `json:"count,omitempty"`
520 ImageUrl *string `json:"image_url,omitempty"`
521 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
522 MiniProgramPath *string `json:"mini_program_path,omitempty"`
523}
524
525type FixedWeekPeriod struct {
526 DayList []WeekEnum `json:"day_list,omitempty"`
527 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
528}
529
530type TimePeriod struct {
531 BeginTime *string `json:"begin_time,omitempty"`
532 EndTime *string `json:"end_time,omitempty"`
533}
534
535type WeekEnum string
536
537func (e WeekEnum) Ptr() *WeekEnum {
538 return &e
539}
540
541const (
542 WEEKENUM_MONDAY WeekEnum = "MONDAY"
543 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
544 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
545 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
546 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
547 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
548 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
549)
550
551type PeriodOfTheDay struct {
552 BeginTime *int64 `json:"begin_time,omitempty"`
553 EndTime *int64 `json:"end_time,omitempty"`
554}
555