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