
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 := &ListCouponsByFilterRequest{
28 Openid: wxpay_utility.String("2323dfsdf342342"),
29 Appid: wxpay_utility.String("wx233544546545989"),
30 StockId: wxpay_utility.String("100088"),
31 CouponState: COUPONSTATUS_SENDED.Ptr(),
32 CreatorMerchant: wxpay_utility.String("1000000001"),
33 BelongMerchant: wxpay_utility.String("1000000002"),
34 SenderMerchant: wxpay_utility.String("1000000003"),
35 Offset: wxpay_utility.Int64(0),
36 Limit: wxpay_utility.Int64(20),
37 }
38
39 response, err := ListCouponsByFilter(config, request)
40 if err != nil {
41 fmt.Printf("请求失败: %+v\n", err)
42
43 return
44 }
45
46
47 fmt.Printf("请求成功: %+v\n", response)
48}
49
50func ListCouponsByFilter(config *wxpay_utility.MchConfig, request *ListCouponsByFilterRequest) (response *CouponListResponse, err error) {
51 const (
52 host = "https://api.mch.weixin.qq.com"
53 method = "GET"
54 path = "/v3/marketing/busifavor/users/{openid}/coupons"
55 )
56
57 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
58 if err != nil {
59 return nil, err
60 }
61 reqUrl.Path = strings.Replace(reqUrl.Path, "{openid}", url.PathEscape(*request.Openid), -1)
62 query := reqUrl.Query()
63 if request.Appid != nil {
64 query.Add("appid", *request.Appid)
65 }
66 if request.StockId != nil {
67 query.Add("stock_id", *request.StockId)
68 }
69 if request.CouponState != nil {
70 query.Add("coupon_state", fmt.Sprintf("%v", *request.CouponState))
71 }
72 if request.CreatorMerchant != nil {
73 query.Add("creator_merchant", *request.CreatorMerchant)
74 }
75 if request.BelongMerchant != nil {
76 query.Add("belong_merchant", *request.BelongMerchant)
77 }
78 if request.SenderMerchant != nil {
79 query.Add("sender_merchant", *request.SenderMerchant)
80 }
81 if request.Offset != nil {
82 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
83 }
84 if request.Limit != nil {
85 query.Add("limit", fmt.Sprintf("%v", *request.Limit))
86 }
87 reqUrl.RawQuery = query.Encode()
88 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
89 if err != nil {
90 return nil, err
91 }
92 httpRequest.Header.Set("Accept", "application/json")
93 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
94 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
95 if err != nil {
96 return nil, err
97 }
98 httpRequest.Header.Set("Authorization", authorization)
99
100 client := &http.Client{}
101 httpResponse, err := client.Do(httpRequest)
102 if err != nil {
103 return nil, err
104 }
105 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
106 if err != nil {
107 return nil, err
108 }
109 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
110
111 err = wxpay_utility.ValidateResponse(
112 config.WechatPayPublicKeyId(),
113 config.WechatPayPublicKey(),
114 &httpResponse.Header,
115 respBody,
116 )
117 if err != nil {
118 return nil, err
119 }
120 response := &CouponListResponse{}
121 if err := json.Unmarshal(respBody, response); err != nil {
122 return nil, err
123 }
124
125 return response, nil
126 } else {
127 return nil, wxpay_utility.NewApiException(
128 httpResponse.StatusCode,
129 httpResponse.Header,
130 respBody,
131 )
132 }
133}
134
135type ListCouponsByFilterRequest struct {
136 Openid *string `json:"openid,omitempty"`
137 Appid *string `json:"appid,omitempty"`
138 StockId *string `json:"stock_id,omitempty"`
139 CreatorMerchant *string `json:"creator_merchant,omitempty"`
140 BelongMerchant *string `json:"belong_merchant,omitempty"`
141 SenderMerchant *string `json:"sender_merchant,omitempty"`
142 Offset *int64 `json:"offset,omitempty"`
143 Limit *int64 `json:"limit,omitempty"`
144 CouponState *CouponStatus `json:"coupon_state,omitempty"`
145}
146
147func (o *ListCouponsByFilterRequest) MarshalJSON() ([]byte, error) {
148 type Alias ListCouponsByFilterRequest
149 a := &struct {
150 Openid *string `json:"openid,omitempty"`
151 Appid *string `json:"appid,omitempty"`
152 StockId *string `json:"stock_id,omitempty"`
153 CreatorMerchant *string `json:"creator_merchant,omitempty"`
154 BelongMerchant *string `json:"belong_merchant,omitempty"`
155 SenderMerchant *string `json:"sender_merchant,omitempty"`
156 Offset *int64 `json:"offset,omitempty"`
157 Limit *int64 `json:"limit,omitempty"`
158 CouponState *CouponStatus `json:"coupon_state,omitempty"`
159 *Alias
160 }{
161
162 Openid: nil,
163 Appid: nil,
164 StockId: nil,
165 CreatorMerchant: nil,
166 BelongMerchant: nil,
167 SenderMerchant: nil,
168 Offset: nil,
169 Limit: nil,
170 CouponState: nil,
171 Alias: (*Alias)(o),
172 }
173 return json.Marshal(a)
174}
175
176type CouponListResponse struct {
177 Data []CouponEntity `json:"data,omitempty"`
178 TotalCount *int64 `json:"total_count,omitempty"`
179 Limit *int64 `json:"limit,omitempty"`
180 Offset *int64 `json:"offset,omitempty"`
181}
182
183type CouponStatus string
184
185func (e CouponStatus) Ptr() *CouponStatus {
186 return &e
187}
188
189const (
190 COUPONSTATUS_SENDED CouponStatus = "SENDED"
191 COUPONSTATUS_USED CouponStatus = "USED"
192 COUPONSTATUS_EXPIRED CouponStatus = "EXPIRED"
193 COUPONSTATUS_DELETED CouponStatus = "DELETED"
194 COUPONSTATUS_DEACTIVATED CouponStatus = "DEACTIVATED"
195)
196
197type CouponEntity struct {
198 BelongMerchant *string `json:"belong_merchant,omitempty"`
199 StockName *string `json:"stock_name,omitempty"`
200 Comment *string `json:"comment,omitempty"`
201 GoodsName *string `json:"goods_name,omitempty"`
202 StockType *BusiFavorStockType `json:"stock_type,omitempty"`
203 Transferable *bool `json:"transferable,omitempty"`
204 Shareable *bool `json:"shareable,omitempty"`
205 CouponState *CouponStatus `json:"coupon_state,omitempty"`
206 DisplayPatternInfo *DisplayPatternInfo `json:"display_pattern_info,omitempty"`
207 CouponUseRule *CouponUseRule `json:"coupon_use_rule,omitempty"`
208 CustomEntrance *CustomEntrance `json:"custom_entrance,omitempty"`
209 CouponCode *string `json:"coupon_code,omitempty"`
210 StockId *string `json:"stock_id,omitempty"`
211 AvailableStartTime *time.Time `json:"available_start_time,omitempty"`
212 ExpireTime *time.Time `json:"expire_time,omitempty"`
213 ReceiveTime *time.Time `json:"receive_time,omitempty"`
214 SendRequestNo *string `json:"send_request_no,omitempty"`
215 UseRequestNo *string `json:"use_request_no,omitempty"`
216 UseTime *time.Time `json:"use_time,omitempty"`
217 AssociateOutTradeNo *time.Time `json:"associate_out_trade_no,omitempty"`
218 ReturnRequestNo *string `json:"return_request_no,omitempty"`
219 ReturnTime *time.Time `json:"return_time,omitempty"`
220 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
221 DeactivateTime *time.Time `json:"deactivate_time,omitempty"`
222 DeactivateReason *string `json:"deactivate_reason,omitempty"`
223}
224
225type BusiFavorStockType string
226
227func (e BusiFavorStockType) Ptr() *BusiFavorStockType {
228 return &e
229}
230
231const (
232 BUSIFAVORSTOCKTYPE_NORMAL BusiFavorStockType = "NORMAL"
233 BUSIFAVORSTOCKTYPE_DISCOUNT BusiFavorStockType = "DISCOUNT"
234 BUSIFAVORSTOCKTYPE_EXCHANGE BusiFavorStockType = "EXCHANGE"
235)
236
237type DisplayPatternInfo struct {
238 Description *string `json:"description,omitempty"`
239 MerchantLogoUrl *string `json:"merchant_logo_url,omitempty"`
240 MerchantName *string `json:"merchant_name,omitempty"`
241 BackgroundColor *string `json:"background_color,omitempty"`
242 CouponImageUrl *string `json:"coupon_image_url,omitempty"`
243 FinderInfo *FinderInfo `json:"finder_info,omitempty"`
244}
245
246type CouponUseRule struct {
247 CouponAvailableTime *FavorAvailableTime `json:"coupon_available_time,omitempty"`
248 FixedNormalCoupon *FixedValueStockMsg `json:"fixed_normal_coupon,omitempty"`
249 DiscountCoupon *DiscountMsg `json:"discount_coupon,omitempty"`
250 ExchangeCoupon *ExchangeMsg `json:"exchange_coupon,omitempty"`
251 UseMethod *CouponUseMethod `json:"use_method,omitempty"`
252 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
253 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
254}
255
256type CustomEntrance struct {
257 MiniProgramsInfo *MiniAppInfo `json:"mini_programs_info,omitempty"`
258 Appid *string `json:"appid,omitempty"`
259 HallId *string `json:"hall_id,omitempty"`
260 StoreId *string `json:"store_id,omitempty"`
261 CodeDisplayMode *CodeDisplayMode `json:"code_display_mode,omitempty"`
262}
263
264type FinderInfo struct {
265 FinderId *string `json:"finder_id,omitempty"`
266 FinderVideoId *string `json:"finder_video_id,omitempty"`
267 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
268}
269
270type FavorAvailableTime struct {
271 AvailableBeginTime *time.Time `json:"available_begin_time,omitempty"`
272 AvailableEndTime *time.Time `json:"available_end_time,omitempty"`
273 AvailableDayAfterReceive *int64 `json:"available_day_after_receive,omitempty"`
274 AvailableWeek *AvailableWeek `json:"available_week,omitempty"`
275 IrregularyAvaliableTime []IrregularAvailableTime `json:"irregulary_avaliable_time,omitempty"`
276 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
277}
278
279type FixedValueStockMsg struct {
280 DiscountAmount *int64 `json:"discount_amount,omitempty"`
281 TransactionMinimum *int64 `json:"transaction_minimum,omitempty"`
282}
283
284type DiscountMsg struct {
285 DiscountPercent *int64 `json:"discount_percent,omitempty"`
286 TransactionMinimum *int64 `json:"transaction_minimum,omitempty"`
287}
288
289type ExchangeMsg struct {
290 ExchangePrice *int64 `json:"exchange_price,omitempty"`
291 TransactionMinimum *int64 `json:"transaction_minimum,omitempty"`
292}
293
294type CouponUseMethod string
295
296func (e CouponUseMethod) Ptr() *CouponUseMethod {
297 return &e
298}
299
300const (
301 COUPONUSEMETHOD_OFF_LINE CouponUseMethod = "OFF_LINE"
302 COUPONUSEMETHOD_MINI_PROGRAMS CouponUseMethod = "MINI_PROGRAMS"
303 COUPONUSEMETHOD_SELF_CONSUME CouponUseMethod = "SELF_CONSUME"
304 COUPONUSEMETHOD_PAYMENT_CODE CouponUseMethod = "PAYMENT_CODE"
305)
306
307type MiniAppInfo struct {
308 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
309 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
310 EntranceWords *string `json:"entrance_words,omitempty"`
311 GuidingWords *string `json:"guiding_words,omitempty"`
312}
313
314type CodeDisplayMode string
315
316func (e CodeDisplayMode) Ptr() *CodeDisplayMode {
317 return &e
318}
319
320const (
321 CODEDISPLAYMODE_NOT_SHOW CodeDisplayMode = "NOT_SHOW"
322 CODEDISPLAYMODE_BARCODE CodeDisplayMode = "BARCODE"
323 CODEDISPLAYMODE_QRCODE CodeDisplayMode = "QRCODE"
324)
325
326type AvailableWeek struct {
327 WeekDay []int64 `json:"week_day,omitempty"`
328 AvailableDayTime []AvailableCurrentDayTime `json:"available_day_time,omitempty"`
329}
330
331type IrregularAvailableTime struct {
332 BeginTime *time.Time `json:"begin_time,omitempty"`
333 EndTime *time.Time `json:"end_time,omitempty"`
334}
335
336type AvailableCurrentDayTime struct {
337 BeginTime *int64 `json:"begin_time,omitempty"`
338 EndTime *int64 `json:"end_time,omitempty"`
339}
340