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