
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
11)
12
13func main() {
14
15 config, err := wxpay_utility.CreateMchConfig(
16 "19xxxxxxxx",
17 "1DDE55AD98Exxxxxxxxxx",
18 "/path/to/apiclient_key.pem",
19 "PUB_KEY_ID_xxxxxxxxxxxxx",
20 "/path/to/wxp_pub.pem",
21 )
22 if err != nil {
23 fmt.Println(err)
24 return
25 }
26
27 request := &DeactivateProductCouponRequest{
28 OutRequestNo: wxpay_utility.String("34657_20250101_123456"),
29 ProductCouponId: wxpay_utility.String("1000000013"),
30 DeactivateReason: wxpay_utility.String("商品券信息有误,重新创建"),
31 BrandId: wxpay_utility.String("120344"),
32 }
33
34 response, err := DeactivateProductCoupon(config, request)
35 if err != nil {
36 fmt.Printf("请求失败: %+v\n", err)
37
38 return
39 }
40
41
42 fmt.Printf("请求成功: %+v\n", response)
43}
44
45func DeactivateProductCoupon(config *wxpay_utility.MchConfig, request *DeactivateProductCouponRequest) (response *ProductCouponEntity, err error) {
46 const (
47 host = "https://api.mch.weixin.qq.com"
48 method = "POST"
49 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/deactivate"
50 )
51
52 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
53 if err != nil {
54 return nil, err
55 }
56 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1)
57 reqBody, err := json.Marshal(request)
58 if err != nil {
59 return nil, err
60 }
61 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
62 if err != nil {
63 return nil, err
64 }
65 httpRequest.Header.Set("Accept", "application/json")
66 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
67 httpRequest.Header.Set("Content-Type", "application/json")
68 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Authorization", authorization)
73
74 client := &http.Client{}
75 httpResponse, err := client.Do(httpRequest)
76 if err != nil {
77 return nil, err
78 }
79 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
80 if err != nil {
81 return nil, err
82 }
83 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
84
85 err = wxpay_utility.ValidateResponse(
86 config.WechatPayPublicKeyId(),
87 config.WechatPayPublicKey(),
88 &httpResponse.Header,
89 respBody,
90 )
91 if err != nil {
92 return nil, err
93 }
94 response := &ProductCouponEntity{}
95 if err := json.Unmarshal(respBody, response); err != nil {
96 return nil, err
97 }
98
99 return response, nil
100 } else {
101 return nil, wxpay_utility.NewApiException(
102 httpResponse.StatusCode,
103 httpResponse.Header,
104 respBody,
105 )
106 }
107}
108
109type DeactivateProductCouponRequest struct {
110 OutRequestNo *string `json:"out_request_no,omitempty"`
111 ProductCouponId *string `json:"product_coupon_id,omitempty"`
112 DeactivateReason *string `json:"deactivate_reason,omitempty"`
113 BrandId *string `json:"brand_id,omitempty"`
114}
115
116func (o *DeactivateProductCouponRequest) MarshalJSON() ([]byte, error) {
117 type Alias DeactivateProductCouponRequest
118 a := &struct {
119 ProductCouponId *string `json:"product_coupon_id,omitempty"`
120 *Alias
121 }{
122
123 ProductCouponId: nil,
124 Alias: (*Alias)(o),
125 }
126 return json.Marshal(a)
127}
128
129type ProductCouponEntity struct {
130 ProductCouponId *string `json:"product_coupon_id,omitempty"`
131 Scope *ProductCouponScope `json:"scope,omitempty"`
132 Type *ProductCouponType `json:"type,omitempty"`
133 UsageMode *UsageMode `json:"usage_mode,omitempty"`
134 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"`
135 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"`
136 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"`
137 OutProductNo *string `json:"out_product_no,omitempty"`
138 State *ProductCouponState `json:"state,omitempty"`
139 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
140 DeactivateTime *string `json:"deactivate_time,omitempty"`
141 DeactivateReason *string `json:"deactivate_reason,omitempty"`
142 BrandId *string `json:"brand_id,omitempty"`
143}
144
145type ProductCouponScope string
146
147func (e ProductCouponScope) Ptr() *ProductCouponScope {
148 return &e
149}
150
151const (
152 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL"
153 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE"
154)
155
156type ProductCouponType string
157
158func (e ProductCouponType) Ptr() *ProductCouponType {
159 return &e
160}
161
162const (
163 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
164 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
165 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
166)
167
168type UsageMode string
169
170func (e UsageMode) Ptr() *UsageMode {
171 return &e
172}
173
174const (
175 USAGEMODE_SINGLE UsageMode = "SINGLE"
176 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
177)
178
179type SingleUsageInfo struct {
180 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
181 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
182}
183
184type ProgressiveBundleUsageInfo struct {
185 Count *int64 `json:"count,omitempty"`
186 IntervalDays *int64 `json:"interval_days,omitempty"`
187}
188
189type ProductCouponDisplayInfo struct {
190 Name *string `json:"name,omitempty"`
191 ImageUrl *string `json:"image_url,omitempty"`
192 BackgroundUrl *string `json:"background_url,omitempty"`
193 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
194 OriginalPrice *int64 `json:"original_price,omitempty"`
195 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
196}
197
198type ProductCouponState string
199
200func (e ProductCouponState) Ptr() *ProductCouponState {
201 return &e
202}
203
204const (
205 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
206 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
207 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
208)
209
210type NormalCouponUsageRule struct {
211 Threshold *int64 `json:"threshold,omitempty"`
212 DiscountAmount *int64 `json:"discount_amount,omitempty"`
213}
214
215type DiscountCouponUsageRule struct {
216 Threshold *int64 `json:"threshold,omitempty"`
217 PercentOff *int64 `json:"percent_off,omitempty"`
218}
219
220type ComboPackage struct {
221 Name *string `json:"name,omitempty"`
222 PickCount *int64 `json:"pick_count,omitempty"`
223 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
224}
225
226type ComboPackageChoice struct {
227 Name *string `json:"name,omitempty"`
228 Price *int64 `json:"price,omitempty"`
229 Count *int64 `json:"count,omitempty"`
230 ImageUrl *string `json:"image_url,omitempty"`
231 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
232 MiniProgramPath *string `json:"mini_program_path,omitempty"`
233}
234