
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 := &ModifyStockInfoRequest{
28 StockId: wxpay_utility.String("101156451224"),
29 CustomEntrance: &ModifyCustomEntrance{
30 MiniProgramsInfo: &ModifyMiniAppInfo{
31 MiniProgramsAppid: wxpay_utility.String("wx234545656765876"),
32 MiniProgramsPath: wxpay_utility.String("/path/index/index"),
33 EntranceWords: wxpay_utility.String("欢迎选购"),
34 GuidingWords: wxpay_utility.String("获取更多优惠"),
35 },
36 Appid: wxpay_utility.String("wx324345hgfhfghfg"),
37 HallId: wxpay_utility.String("233455656"),
38 CodeDisplayMode: CODEDISPLAYMODE_BARCODE.Ptr(),
39 },
40 StockName: wxpay_utility.String("8月1日活动券"),
41 Comment: wxpay_utility.String("活动使用"),
42 GoodsName: wxpay_utility.String("xxx商品使用"),
43 OutRequestNo: wxpay_utility.String("6122352020010133287985742"),
44 DisplayPatternInfo: &DisplayPatternInfo{
45 Description: wxpay_utility.String("xxx门店可用"),
46 MerchantLogoUrl: wxpay_utility.String("https://xxx"),
47 MerchantName: wxpay_utility.String("微信支付"),
48 BackgroundColor: wxpay_utility.String("xxxxx"),
49 CouponImageUrl: wxpay_utility.String("图片cdn地址"),
50 FinderInfo: &FinderInfo{
51 FinderId: wxpay_utility.String("sph6Rngt2T4RlUf"),
52 FinderVideoId: wxpay_utility.String("export/UzFfAgtgekIEAQAAAAAAb4MgnPInmAAAAAstQy6ubaLX4KHWvLEZgBPEwIEgVnk9HIP-zNPgMJofG6tpdGPJNg_ojtEjoT94"),
53 FinderVideoCoverImageUrl: wxpay_utility.String("https://wxpaylogo.qpic.cn/xxx"),
54 },
55 },
56 CouponUseRule: &ModifyCouponUseRule{
57 UseMethod: COUPONUSEMETHOD_OFF_LINE.Ptr(),
58 MiniProgramsAppid: wxpay_utility.String("wx23232232323"),
59 MiniProgramsPath: wxpay_utility.String("/path/index/index"),
60 },
61 StockSendRule: &ModifyStockSendRule{
62 NaturalPersonLimit: wxpay_utility.Bool(false),
63 PreventApiAbuse: wxpay_utility.Bool(false),
64 },
65 NotifyConfig: &NotifyConfig{
66 NotifyAppid: wxpay_utility.String("wx23232232323"),
67 },
68 Subsidy: wxpay_utility.Bool(true),
69 }
70
71 err = ModifyStockInfo(config, request)
72 if err != nil {
73 fmt.Printf("请求失败: %+v\n", err)
74
75 return
76 }
77
78
79 fmt.Println("请求成功")
80}
81
82func ModifyStockInfo(config *wxpay_utility.MchConfig, request *ModifyStockInfoRequest) (err error) {
83 const (
84 host = "https://api.mch.weixin.qq.com"
85 method = "PATCH"
86 path = "/v3/marketing/busifavor/stocks/{stock_id}"
87 )
88
89 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
90 if err != nil {
91 return err
92 }
93 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_id}", url.PathEscape(*request.StockId), -1)
94 reqBody, err := json.Marshal(request)
95 if err != nil {
96 return err
97 }
98 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
99 if err != nil {
100 return err
101 }
102 httpRequest.Header.Set("Accept", "application/json")
103 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
104 httpRequest.Header.Set("Content-Type", "application/json")
105 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
106 if err != nil {
107 return err
108 }
109 httpRequest.Header.Set("Authorization", authorization)
110
111 client := &http.Client{}
112 httpResponse, err := client.Do(httpRequest)
113 if err != nil {
114 return err
115 }
116 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
117 if err != nil {
118 return err
119 }
120 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
121
122 err = wxpay_utility.ValidateResponse(
123 config.WechatPayPublicKeyId(),
124 config.WechatPayPublicKey(),
125 &httpResponse.Header,
126 respBody,
127 )
128 if err != nil {
129 return err
130 }
131 return nil
132 } else {
133 return wxpay_utility.NewApiException(
134 httpResponse.StatusCode,
135 httpResponse.Header,
136 respBody,
137 )
138 }
139}
140
141type ModifyStockInfoRequest struct {
142 StockId *string `json:"stock_id,omitempty"`
143 CustomEntrance *ModifyCustomEntrance `json:"custom_entrance,omitempty"`
144 StockName *string `json:"stock_name,omitempty"`
145 Comment *string `json:"comment,omitempty"`
146 GoodsName *string `json:"goods_name,omitempty"`
147 OutRequestNo *string `json:"out_request_no,omitempty"`
148 DisplayPatternInfo *DisplayPatternInfo `json:"display_pattern_info,omitempty"`
149 CouponUseRule *ModifyCouponUseRule `json:"coupon_use_rule,omitempty"`
150 StockSendRule *ModifyStockSendRule `json:"stock_send_rule,omitempty"`
151 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
152 Subsidy *bool `json:"subsidy,omitempty"`
153}
154
155func (o *ModifyStockInfoRequest) MarshalJSON() ([]byte, error) {
156 type Alias ModifyStockInfoRequest
157 a := &struct {
158 StockId *string `json:"stock_id,omitempty"`
159 *Alias
160 }{
161
162 StockId: nil,
163 Alias: (*Alias)(o),
164 }
165 return json.Marshal(a)
166}
167
168type ModifyCustomEntrance struct {
169 MiniProgramsInfo *ModifyMiniAppInfo `json:"mini_programs_info,omitempty"`
170 Appid *string `json:"appid,omitempty"`
171 HallId *string `json:"hall_id,omitempty"`
172 CodeDisplayMode *CodeDisplayMode `json:"code_display_mode,omitempty"`
173}
174
175type DisplayPatternInfo struct {
176 Description *string `json:"description,omitempty"`
177 MerchantLogoUrl *string `json:"merchant_logo_url,omitempty"`
178 MerchantName *string `json:"merchant_name,omitempty"`
179 BackgroundColor *string `json:"background_color,omitempty"`
180 CouponImageUrl *string `json:"coupon_image_url,omitempty"`
181 FinderInfo *FinderInfo `json:"finder_info,omitempty"`
182}
183
184type ModifyCouponUseRule struct {
185 UseMethod *CouponUseMethod `json:"use_method,omitempty"`
186 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
187 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
188}
189
190type ModifyStockSendRule struct {
191 NaturalPersonLimit *bool `json:"natural_person_limit,omitempty"`
192 PreventApiAbuse *bool `json:"prevent_api_abuse,omitempty"`
193}
194
195type NotifyConfig struct {
196 NotifyAppid *string `json:"notify_appid,omitempty"`
197}
198
199type ModifyMiniAppInfo struct {
200 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
201 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
202 EntranceWords *string `json:"entrance_words,omitempty"`
203 GuidingWords *string `json:"guiding_words,omitempty"`
204}
205
206type CodeDisplayMode string
207
208func (e CodeDisplayMode) Ptr() *CodeDisplayMode {
209 return &e
210}
211
212const (
213 CODEDISPLAYMODE_NOT_SHOW CodeDisplayMode = "NOT_SHOW"
214 CODEDISPLAYMODE_BARCODE CodeDisplayMode = "BARCODE"
215 CODEDISPLAYMODE_QRCODE CodeDisplayMode = "QRCODE"
216)
217
218type FinderInfo struct {
219 FinderId *string `json:"finder_id,omitempty"`
220 FinderVideoId *string `json:"finder_video_id,omitempty"`
221 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
222}
223
224type CouponUseMethod string
225
226func (e CouponUseMethod) Ptr() *CouponUseMethod {
227 return &e
228}
229
230const (
231 COUPONUSEMETHOD_OFF_LINE CouponUseMethod = "OFF_LINE"
232 COUPONUSEMETHOD_MINI_PROGRAMS CouponUseMethod = "MINI_PROGRAMS"
233 COUPONUSEMETHOD_SELF_CONSUME CouponUseMethod = "SELF_CONSUME"
234 COUPONUSEMETHOD_PAYMENT_CODE CouponUseMethod = "PAYMENT_CODE"
235)
236