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