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