
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 PRODUCTCOUPONSCOPE_CATEGORY ProductCouponScope = "CATEGORY"
155)
156
157type ProductCouponType string
158
159func (e ProductCouponType) Ptr() *ProductCouponType {
160 return &e
161}
162
163const (
164 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
165 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
166 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
167)
168
169type UsageMode string
170
171func (e UsageMode) Ptr() *UsageMode {
172 return &e
173}
174
175const (
176 USAGEMODE_SINGLE UsageMode = "SINGLE"
177 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
178)
179
180type SingleUsageInfo struct {
181 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
182 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
183}
184
185type ProgressiveBundleUsageInfo struct {
186 Count *int64 `json:"count,omitempty"`
187 IntervalDays *int64 `json:"interval_days,omitempty"`
188}
189
190type ProductCouponDisplayInfo struct {
191 Name *string `json:"name,omitempty"`
192 ImageUrl *string `json:"image_url,omitempty"`
193 BackgroundUrl *string `json:"background_url,omitempty"`
194 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
195 OriginalPrice *int64 `json:"original_price,omitempty"`
196 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
197}
198
199type ProductCouponState string
200
201func (e ProductCouponState) Ptr() *ProductCouponState {
202 return &e
203}
204
205const (
206 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
207 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
208 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
209)
210
211type NormalCouponUsageRule struct {
212 Threshold *int64 `json:"threshold,omitempty"`
213 DiscountAmount *int64 `json:"discount_amount,omitempty"`
214}
215
216type DiscountCouponUsageRule struct {
217 Threshold *int64 `json:"threshold,omitempty"`
218 PercentOff *int64 `json:"percent_off,omitempty"`
219}
220
221type ComboPackage struct {
222 Name *string `json:"name,omitempty"`
223 PickCount *int64 `json:"pick_count,omitempty"`
224 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
225}
226
227type ComboPackageChoice struct {
228 Name *string `json:"name,omitempty"`
229 Price *int64 `json:"price,omitempty"`
230 Count *int64 `json:"count,omitempty"`
231 ImageUrl *string `json:"image_url,omitempty"`
232 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
233 MiniProgramPath *string `json:"mini_program_path,omitempty"`
234}
235