
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 AppJumpType *AppJumpType `json:"app_jump_type,omitempty"`
359 PasscodeLink *string `json:"passcode_link,omitempty"`
360}
361
362type CouponDisplayInfo struct {
363 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
364 BackgroundColor *string `json:"background_color,omitempty"`
365 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
366 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
367 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
368}
369
370type NotifyConfig struct {
371 NotifyAppid *string `json:"notify_appid,omitempty"`
372}
373
374type StockStoreScope string
375
376func (e StockStoreScope) Ptr() *StockStoreScope {
377 return &e
378}
379
380const (
381 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
382 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
383 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
384)
385
386type StockSentCountInfo struct {
387 TotalCount *int64 `json:"total_count,omitempty"`
388 TodayCount *int64 `json:"today_count,omitempty"`
389}
390
391type StockState string
392
393func (e StockState) Ptr() *StockState {
394 return &e
395}
396
397const (
398 STOCKSTATE_AUDITING StockState = "AUDITING"
399 STOCKSTATE_SENDING StockState = "SENDING"
400 STOCKSTATE_PAUSED StockState = "PAUSED"
401 STOCKSTATE_STOPPED StockState = "STOPPED"
402 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
403)
404
405type UserProductCouponTag string
406
407func (e UserProductCouponTag) Ptr() *UserProductCouponTag {
408 return &e
409}
410
411const (
412 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER"
413)
414
415type MemberTagInfo struct {
416 MemberCardId *string `json:"member_card_id,omitempty"`
417}
418
419type NormalCouponUsageRule struct {
420 Threshold *int64 `json:"threshold,omitempty"`
421 DiscountAmount *int64 `json:"discount_amount,omitempty"`
422}
423
424type DiscountCouponUsageRule struct {
425 Threshold *int64 `json:"threshold,omitempty"`
426 PercentOff *int64 `json:"percent_off,omitempty"`
427}
428
429type ComboPackage struct {
430 Name *string `json:"name,omitempty"`
431 PickCount *int64 `json:"pick_count,omitempty"`
432 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
433}
434
435type CouponAvailablePeriod struct {
436 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
437 AvailableEndTime *string `json:"available_end_time,omitempty"`
438 AvailableDays *int64 `json:"available_days,omitempty"`
439 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
440 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
441 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
442}
443
444type ExchangeCouponUsageRule struct {
445 Threshold *int64 `json:"threshold,omitempty"`
446 ExchangePrice *int64 `json:"exchange_price,omitempty"`
447}
448
449type CouponUsageMethod string
450
451func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
452 return &e
453}
454
455const (
456 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
457 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
458 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
459 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
460)
461
462type CouponAvailableStoreInfo struct {
463 Description *string `json:"description,omitempty"`
464 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
465 MiniProgramPath *string `json:"mini_program_path,omitempty"`
466}
467
468type AppJumpType string
469
470func (e AppJumpType) Ptr() *AppJumpType {
471 return &e
472}
473
474const (
475 APPJUMPTYPE_H5 AppJumpType = "H5"
476 APPJUMPTYPE_PASSCODE_LINK AppJumpType = "PASSCODE_LINK"
477)
478
479type CouponCodeDisplayMode string
480
481func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
482 return &e
483}
484
485const (
486 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
487 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
488 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
489)
490
491type EntranceMiniProgram struct {
492 Appid *string `json:"appid,omitempty"`
493 Path *string `json:"path,omitempty"`
494 EntranceWording *string `json:"entrance_wording,omitempty"`
495 GuidanceWording *string `json:"guidance_wording,omitempty"`
496}
497
498type EntranceOfficialAccount struct {
499 Appid *string `json:"appid,omitempty"`
500}
501
502type EntranceFinder struct {
503 FinderId *string `json:"finder_id,omitempty"`
504 FinderVideoId *string `json:"finder_video_id,omitempty"`
505 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
506}
507
508type ComboPackageChoice struct {
509 Name *string `json:"name,omitempty"`
510 Price *int64 `json:"price,omitempty"`
511 Count *int64 `json:"count,omitempty"`
512 ImageUrl *string `json:"image_url,omitempty"`
513 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
514 MiniProgramPath *string `json:"mini_program_path,omitempty"`
515}
516
517type FixedWeekPeriod struct {
518 DayList []WeekEnum `json:"day_list,omitempty"`
519 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
520}
521
522type TimePeriod struct {
523 BeginTime *string `json:"begin_time,omitempty"`
524 EndTime *string `json:"end_time,omitempty"`
525}
526
527type WeekEnum string
528
529func (e WeekEnum) Ptr() *WeekEnum {
530 return &e
531}
532
533const (
534 WEEKENUM_MONDAY WeekEnum = "MONDAY"
535 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
536 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
537 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
538 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
539 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
540 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
541)
542
543type PeriodOfTheDay struct {
544 BeginTime *int64 `json:"begin_time,omitempty"`
545 EndTime *int64 `json:"end_time,omitempty"`
546}
547