
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10 "time"
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 := &QueryStockRequest{
28 StockId: wxpay_utility.String("100088"),
29 }
30
31 response, err := QueryStock(config, request)
32 if err != nil {
33 fmt.Printf("请求失败: %+v\n", err)
34
35 return
36 }
37
38
39 fmt.Printf("请求成功: %+v\n", response)
40}
41
42func QueryStock(config *wxpay_utility.MchConfig, request *QueryStockRequest) (response *StockGetResponse, err error) {
43 const (
44 host = "https://api.mch.weixin.qq.com"
45 method = "GET"
46 path = "/v3/marketing/busifavor/stocks/{stock_id}"
47 )
48
49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
50 if err != nil {
51 return nil, err
52 }
53 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_id}", url.PathEscape(*request.StockId), -1)
54 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
55 if err != nil {
56 return nil, err
57 }
58 httpRequest.Header.Set("Accept", "application/json")
59 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
60 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
61 if err != nil {
62 return nil, err
63 }
64 httpRequest.Header.Set("Authorization", authorization)
65
66 client := &http.Client{}
67 httpResponse, err := client.Do(httpRequest)
68 if err != nil {
69 return nil, err
70 }
71 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
72 if err != nil {
73 return nil, err
74 }
75 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
76
77 err = wxpay_utility.ValidateResponse(
78 config.WechatPayPublicKeyId(),
79 config.WechatPayPublicKey(),
80 &httpResponse.Header,
81 respBody,
82 )
83 if err != nil {
84 return nil, err
85 }
86 response := &StockGetResponse{}
87 if err := json.Unmarshal(respBody, response); err != nil {
88 return nil, err
89 }
90
91 return response, nil
92 } else {
93 return nil, wxpay_utility.NewApiException(
94 httpResponse.StatusCode,
95 httpResponse.Header,
96 respBody,
97 )
98 }
99}
100
101type QueryStockRequest struct {
102 StockId *string `json:"stock_id,omitempty"`
103}
104
105func (o *QueryStockRequest) MarshalJSON() ([]byte, error) {
106 type Alias QueryStockRequest
107 a := &struct {
108 StockId *string `json:"stock_id,omitempty"`
109 *Alias
110 }{
111
112 StockId: nil,
113 Alias: (*Alias)(o),
114 }
115 return json.Marshal(a)
116}
117
118type StockGetResponse struct {
119 StockName *string `json:"stock_name,omitempty"`
120 BelongMerchant *string `json:"belong_merchant,omitempty"`
121 Comment *string `json:"comment,omitempty"`
122 GoodsName *string `json:"goods_name,omitempty"`
123 StockType *BusiFavorStockType `json:"stock_type,omitempty"`
124 CouponUseRule *CouponUseRule `json:"coupon_use_rule,omitempty"`
125 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"`
126 CustomEntrance *CustomEntrance `json:"custom_entrance,omitempty"`
127 DisplayPatternInfo *DisplayPatternInfo `json:"display_pattern_info,omitempty"`
128 StockState *StockStatus `json:"stock_state,omitempty"`
129 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"`
130 StockId *string `json:"stock_id,omitempty"`
131 CouponCodeCount *CouponCodeCount `json:"coupon_code_count,omitempty"`
132 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
133 SendCountInformation *SendCount `json:"send_count_information,omitempty"`
134 Subsidy *bool `json:"subsidy,omitempty"`
135}
136
137type BusiFavorStockType string
138
139func (e BusiFavorStockType) Ptr() *BusiFavorStockType {
140 return &e
141}
142
143const (
144 BUSIFAVORSTOCKTYPE_NORMAL BusiFavorStockType = "NORMAL"
145 BUSIFAVORSTOCKTYPE_DISCOUNT BusiFavorStockType = "DISCOUNT"
146 BUSIFAVORSTOCKTYPE_EXCHANGE BusiFavorStockType = "EXCHANGE"
147)
148
149type CouponUseRule struct {
150 CouponAvailableTime *FavorAvailableTime `json:"coupon_available_time,omitempty"`
151 FixedNormalCoupon *FixedValueStockMsg `json:"fixed_normal_coupon,omitempty"`
152 DiscountCoupon *DiscountMsg `json:"discount_coupon,omitempty"`
153 ExchangeCoupon *ExchangeMsg `json:"exchange_coupon,omitempty"`
154 UseMethod *CouponUseMethod `json:"use_method,omitempty"`
155 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
156 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
157}
158
159type StockSendRule struct {
160 MaxAmount *int64 `json:"max_amount,omitempty"`
161 MaxCoupons *int64 `json:"max_coupons,omitempty"`
162 MaxCouponsPerUser *int64 `json:"max_coupons_per_user,omitempty"`
163 MaxAmountByDay *int64 `json:"max_amount_by_day,omitempty"`
164 MaxCouponsByDay *int64 `json:"max_coupons_by_day,omitempty"`
165 NaturalPersonLimit *bool `json:"natural_person_limit,omitempty"`
166 PreventApiAbuse *bool `json:"prevent_api_abuse,omitempty"`
167 Transferable *bool `json:"transferable,omitempty"`
168 Shareable *bool `json:"shareable,omitempty"`
169}
170
171type CustomEntrance struct {
172 MiniProgramsInfo *MiniAppInfo `json:"mini_programs_info,omitempty"`
173 Appid *string `json:"appid,omitempty"`
174 HallId *string `json:"hall_id,omitempty"`
175 StoreId *string `json:"store_id,omitempty"`
176 CodeDisplayMode *CodeDisplayMode `json:"code_display_mode,omitempty"`
177}
178
179type DisplayPatternInfo struct {
180 Description *string `json:"description,omitempty"`
181 MerchantLogoUrl *string `json:"merchant_logo_url,omitempty"`
182 MerchantName *string `json:"merchant_name,omitempty"`
183 BackgroundColor *string `json:"background_color,omitempty"`
184 CouponImageUrl *string `json:"coupon_image_url,omitempty"`
185 FinderInfo *FinderInfo `json:"finder_info,omitempty"`
186}
187
188type StockStatus string
189
190func (e StockStatus) Ptr() *StockStatus {
191 return &e
192}
193
194const (
195 STOCKSTATUS_UNAUDIT StockStatus = "UNAUDIT"
196 STOCKSTATUS_RUNNING StockStatus = "RUNNING"
197 STOCKSTATUS_STOPED StockStatus = "STOPED"
198 STOCKSTATUS_PAUSED StockStatus = "PAUSED"
199)
200
201type CouponCodeMode string
202
203func (e CouponCodeMode) Ptr() *CouponCodeMode {
204 return &e
205}
206
207const (
208 COUPONCODEMODE_WECHATPAY_MODE CouponCodeMode = "WECHATPAY_MODE"
209 COUPONCODEMODE_MERCHANT_API CouponCodeMode = "MERCHANT_API"
210 COUPONCODEMODE_MERCHANT_UPLOAD CouponCodeMode = "MERCHANT_UPLOAD"
211)
212
213type CouponCodeCount struct {
214 TotalCount *int64 `json:"total_count,omitempty"`
215 AvailableCount *int64 `json:"available_count,omitempty"`
216}
217
218type NotifyConfig struct {
219 NotifyAppid *string `json:"notify_appid,omitempty"`
220}
221
222type SendCount struct {
223 TotalSendNum *int64 `json:"total_send_num,omitempty"`
224 TotalSendAmount *int64 `json:"total_send_amount,omitempty"`
225 TodaySendNum *int64 `json:"today_send_num,omitempty"`
226 TodaySendAmount *int64 `json:"today_send_amount,omitempty"`
227}
228
229type FavorAvailableTime struct {
230 AvailableBeginTime *time.Time `json:"available_begin_time,omitempty"`
231 AvailableEndTime *time.Time `json:"available_end_time,omitempty"`
232 AvailableDayAfterReceive *int64 `json:"available_day_after_receive,omitempty"`
233 AvailableWeek *AvailableWeek `json:"available_week,omitempty"`
234 IrregularyAvaliableTime []IrregularAvailableTime `json:"irregulary_avaliable_time,omitempty"`
235 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
236}
237
238type FixedValueStockMsg struct {
239 DiscountAmount *int64 `json:"discount_amount,omitempty"`
240 TransactionMinimum *int64 `json:"transaction_minimum,omitempty"`
241}
242
243type DiscountMsg struct {
244 DiscountPercent *int64 `json:"discount_percent,omitempty"`
245 TransactionMinimum *int64 `json:"transaction_minimum,omitempty"`
246}
247
248type ExchangeMsg struct {
249 ExchangePrice *int64 `json:"exchange_price,omitempty"`
250 TransactionMinimum *int64 `json:"transaction_minimum,omitempty"`
251}
252
253type CouponUseMethod string
254
255func (e CouponUseMethod) Ptr() *CouponUseMethod {
256 return &e
257}
258
259const (
260 COUPONUSEMETHOD_OFF_LINE CouponUseMethod = "OFF_LINE"
261 COUPONUSEMETHOD_MINI_PROGRAMS CouponUseMethod = "MINI_PROGRAMS"
262 COUPONUSEMETHOD_SELF_CONSUME CouponUseMethod = "SELF_CONSUME"
263 COUPONUSEMETHOD_PAYMENT_CODE CouponUseMethod = "PAYMENT_CODE"
264)
265
266type MiniAppInfo struct {
267 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
268 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
269 EntranceWords *string `json:"entrance_words,omitempty"`
270 GuidingWords *string `json:"guiding_words,omitempty"`
271}
272
273type CodeDisplayMode string
274
275func (e CodeDisplayMode) Ptr() *CodeDisplayMode {
276 return &e
277}
278
279const (
280 CODEDISPLAYMODE_NOT_SHOW CodeDisplayMode = "NOT_SHOW"
281 CODEDISPLAYMODE_BARCODE CodeDisplayMode = "BARCODE"
282 CODEDISPLAYMODE_QRCODE CodeDisplayMode = "QRCODE"
283)
284
285type FinderInfo struct {
286 FinderId *string `json:"finder_id,omitempty"`
287 FinderVideoId *string `json:"finder_video_id,omitempty"`
288 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
289}
290
291type AvailableWeek struct {
292 WeekDay []int64 `json:"week_day,omitempty"`
293 AvailableDayTime []AvailableCurrentDayTime `json:"available_day_time,omitempty"`
294}
295
296type IrregularAvailableTime struct {
297 BeginTime *time.Time `json:"begin_time,omitempty"`
298 EndTime *time.Time `json:"end_time,omitempty"`
299}
300
301type AvailableCurrentDayTime struct {
302 BeginTime *int64 `json:"begin_time,omitempty"`
303 EndTime *int64 `json:"end_time,omitempty"`
304}
305