
1package main
2
3import (
4 "demo/wxpay_brand_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_brand_utility.CreateBrandConfig(
16 "xxxxxxxx",
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 := &GetUserProductCouponRequest{
28 ProductCouponId: wxpay_brand_utility.String("1000000013"),
29 StockId: wxpay_brand_utility.String("1000000013001"),
30 CouponCode: wxpay_brand_utility.String("Code_123456"),
31 Appid: wxpay_brand_utility.String("wx233544546545989"),
32 Openid: wxpay_brand_utility.String("oh-394z-6CGkNoJrsDLTTUKiAnp4"),
33 }
34
35 response, err := GetUserProductCoupon(config, request)
36 if err != nil {
37 fmt.Printf("请求失败: %+v\n", err)
38
39 return
40 }
41
42
43 fmt.Printf("请求成功: %+v\n", response)
44}
45
46func GetUserProductCoupon(config *wxpay_brand_utility.BrandConfig, request *GetUserProductCouponRequest) (response *UserProductCouponEntity, err error) {
47 const (
48 host = "https://api.mch.weixin.qq.com"
49 method = "GET"
50 path = "/brand/marketing/product-coupon/users/{openid}/coupons/{coupon_code}"
51 )
52
53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
54 if err != nil {
55 return nil, err
56 }
57 reqUrl.Path = strings.Replace(reqUrl.Path, "{coupon_code}", url.PathEscape(*request.CouponCode), -1)
58 reqUrl.Path = strings.Replace(reqUrl.Path, "{openid}", url.PathEscape(*request.Openid), -1)
59 query := reqUrl.Query()
60 if request.ProductCouponId != nil {
61 query.Add("product_coupon_id", *request.ProductCouponId)
62 }
63 if request.StockId != nil {
64 query.Add("stock_id", *request.StockId)
65 }
66 if request.Appid != nil {
67 query.Add("appid", *request.Appid)
68 }
69 reqUrl.RawQuery = query.Encode()
70 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
71 if err != nil {
72 return nil, err
73 }
74 httpRequest.Header.Set("Accept", "application/json")
75 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
76 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
77 if err != nil {
78 return nil, err
79 }
80 httpRequest.Header.Set("Authorization", authorization)
81
82 client := &http.Client{}
83 httpResponse, err := client.Do(httpRequest)
84 if err != nil {
85 return nil, err
86 }
87 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
88 if err != nil {
89 return nil, err
90 }
91 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
92
93 err = wxpay_brand_utility.ValidateResponse(
94 config.WechatPayPublicKeyId(),
95 config.WechatPayPublicKey(),
96 &httpResponse.Header,
97 respBody,
98 )
99 if err != nil {
100 return nil, err
101 }
102 response := &UserProductCouponEntity{}
103 if err := json.Unmarshal(respBody, response); err != nil {
104 return nil, err
105 }
106
107 return response, nil
108 } else {
109 return nil, wxpay_brand_utility.NewApiException(
110 httpResponse.StatusCode,
111 httpResponse.Header,
112 respBody,
113 )
114 }
115}
116
117type GetUserProductCouponRequest struct {
118 ProductCouponId *string `json:"product_coupon_id,omitempty"`
119 StockId *string `json:"stock_id,omitempty"`
120 CouponCode *string `json:"coupon_code,omitempty"`
121 Appid *string `json:"appid,omitempty"`
122 Openid *string `json:"openid,omitempty"`
123}
124
125func (o *GetUserProductCouponRequest) MarshalJSON() ([]byte, error) {
126 type Alias GetUserProductCouponRequest
127 a := &struct {
128 ProductCouponId *string `json:"product_coupon_id,omitempty"`
129 StockId *string `json:"stock_id,omitempty"`
130 CouponCode *string `json:"coupon_code,omitempty"`
131 Appid *string `json:"appid,omitempty"`
132 Openid *string `json:"openid,omitempty"`
133 *Alias
134 }{
135
136 ProductCouponId: nil,
137 StockId: nil,
138 CouponCode: nil,
139 Appid: nil,
140 Openid: nil,
141 Alias: (*Alias)(o),
142 }
143 return json.Marshal(a)
144}
145
146type UserProductCouponEntity struct {
147 CouponCode *string `json:"coupon_code,omitempty"`
148 CouponState *UserProductCouponState `json:"coupon_state,omitempty"`
149 ValidBeginTime *time.Time `json:"valid_begin_time,omitempty"`
150 ValidEndTime *time.Time `json:"valid_end_time,omitempty"`
151 ReceiveTime *string `json:"receive_time,omitempty"`
152 SendRequestNo *string `json:"send_request_no,omitempty"`
153 SendChannel *UserProductCouponSendChannel `json:"send_channel,omitempty"`
154 ConfirmRequestNo *string `json:"confirm_request_no,omitempty"`
155 ConfirmTime *time.Time `json:"confirm_time,omitempty"`
156 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
157 DeactivateTime *string `json:"deactivate_time,omitempty"`
158 DeactivateReason *string `json:"deactivate_reason,omitempty"`
159 SingleUsageDetail *CouponUsageDetail `json:"single_usage_detail,omitempty"`
160 ProgressiveBundleUsageDetail *CouponUsageDetail `json:"progressive_bundle_usage_detail,omitempty"`
161 UserProductCouponBundleInfo *UserProductCouponBundleInfo `json:"user_product_coupon_bundle_info,omitempty"`
162 ProductCoupon *ProductCouponEntity `json:"product_coupon,omitempty"`
163 Stock *StockEntity `json:"stock,omitempty"`
164 Attach *string `json:"attach,omitempty"`
165 ChannelCustomInfo *string `json:"channel_custom_info,omitempty"`
166 CouponTagInfo *CouponTagInfo `json:"coupon_tag_info,omitempty"`
167}
168
169type UserProductCouponState string
170
171func (e UserProductCouponState) Ptr() *UserProductCouponState {
172 return &e
173}
174
175const (
176 USERPRODUCTCOUPONSTATE_CONFIRMING UserProductCouponState = "CONFIRMING"
177 USERPRODUCTCOUPONSTATE_PENDING UserProductCouponState = "PENDING"
178 USERPRODUCTCOUPONSTATE_EFFECTIVE UserProductCouponState = "EFFECTIVE"
179 USERPRODUCTCOUPONSTATE_USED UserProductCouponState = "USED"
180 USERPRODUCTCOUPONSTATE_EXPIRED UserProductCouponState = "EXPIRED"
181 USERPRODUCTCOUPONSTATE_DELETED UserProductCouponState = "DELETED"
182 USERPRODUCTCOUPONSTATE_DEACTIVATED UserProductCouponState = "DEACTIVATED"
183)
184
185type UserProductCouponSendChannel string
186
187func (e UserProductCouponSendChannel) Ptr() *UserProductCouponSendChannel {
188 return &e
189}
190
191const (
192 USERPRODUCTCOUPONSENDCHANNEL_BRAND_MANAGE UserProductCouponSendChannel = "BRAND_MANAGE"
193 USERPRODUCTCOUPONSENDCHANNEL_API UserProductCouponSendChannel = "API"
194 USERPRODUCTCOUPONSENDCHANNEL_RECEIVE_COMPONENT UserProductCouponSendChannel = "RECEIVE_COMPONENT"
195)
196
197type CouponUsageDetail struct {
198 UseRequestNo *string `json:"use_request_no,omitempty"`
199 UseTime *time.Time `json:"use_time,omitempty"`
200 ReturnRequestNo *string `json:"return_request_no,omitempty"`
201 ReturnTime *time.Time `json:"return_time,omitempty"`
202 AssociatedOrderInfo *UserProductCouponAssociatedOrderInfo `json:"associated_order_info,omitempty"`
203 AssociatedPayScoreOrderInfo *UserProductCouponAssociatedPayScoreOrderInfo `json:"associated_pay_score_order_info,omitempty"`
204 SavedAmount *int64 `json:"saved_amount,omitempty"`
205}
206
207type UserProductCouponBundleInfo struct {
208 UserCouponBundleId *string `json:"user_coupon_bundle_id,omitempty"`
209 UserCouponBundleIndex *int64 `json:"user_coupon_bundle_index,omitempty"`
210 TotalCount *int64 `json:"total_count,omitempty"`
211 UsedCount *int64 `json:"used_count,omitempty"`
212}
213
214type ProductCouponEntity struct {
215 ProductCouponId *string `json:"product_coupon_id,omitempty"`
216 Scope *ProductCouponScope `json:"scope,omitempty"`
217 Type *ProductCouponType `json:"type,omitempty"`
218 UsageMode *UsageMode `json:"usage_mode,omitempty"`
219 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"`
220 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"`
221 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"`
222 OutProductNo *string `json:"out_product_no,omitempty"`
223 State *ProductCouponState `json:"state,omitempty"`
224 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
225 DeactivateTime *string `json:"deactivate_time,omitempty"`
226 DeactivateReason *string `json:"deactivate_reason,omitempty"`
227}
228
229type StockEntity struct {
230 ProductCouponId *string `json:"product_coupon_id,omitempty"`
231 StockId *string `json:"stock_id,omitempty"`
232 Remark *string `json:"remark,omitempty"`
233 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"`
234 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"`
235 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"`
236 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"`
237 ProgressiveBundleUsageRule *StockUsageRule `json:"progressive_bundle_usage_rule,omitempty"`
238 StockBundleInfo *StockBundleInfo `json:"stock_bundle_info,omitempty"`
239 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"`
240 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"`
241 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
242 StoreScope *StockStoreScope `json:"store_scope,omitempty"`
243 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"`
244 State *StockState `json:"state,omitempty"`
245 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
246 DeactivateTime *time.Time `json:"deactivate_time,omitempty"`
247 DeactivateReason *string `json:"deactivate_reason,omitempty"`
248}
249
250type CouponTagInfo struct {
251 CouponTagList []UserProductCouponTag `json:"coupon_tag_list,omitempty"`
252 MemberTagInfo *MemberTagInfo `json:"member_tag_info,omitempty"`
253}
254
255type UserProductCouponAssociatedOrderInfo struct {
256 TransactionId *string `json:"transaction_id,omitempty"`
257 OutTradeNo *string `json:"out_trade_no,omitempty"`
258 Mchid *string `json:"mchid,omitempty"`
259 SubMchid *string `json:"sub_mchid,omitempty"`
260}
261
262type UserProductCouponAssociatedPayScoreOrderInfo struct {
263 OrderId *string `json:"order_id,omitempty"`
264 OutOrderNo *string `json:"out_order_no,omitempty"`
265 Mchid *string `json:"mchid,omitempty"`
266 SubMchid *string `json:"sub_mchid,omitempty"`
267}
268
269type ProductCouponScope string
270
271func (e ProductCouponScope) Ptr() *ProductCouponScope {
272 return &e
273}
274
275const (
276 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL"
277 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE"
278)
279
280type ProductCouponType string
281
282func (e ProductCouponType) Ptr() *ProductCouponType {
283 return &e
284}
285
286const (
287 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
288 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
289 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
290)
291
292type UsageMode string
293
294func (e UsageMode) Ptr() *UsageMode {
295 return &e
296}
297
298const (
299 USAGEMODE_SINGLE UsageMode = "SINGLE"
300 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
301)
302
303type SingleUsageInfo struct {
304 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
305 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
306}
307
308type ProgressiveBundleUsageInfo struct {
309 Count *int64 `json:"count,omitempty"`
310 IntervalDays *int64 `json:"interval_days,omitempty"`
311}
312
313type ProductCouponDisplayInfo struct {
314 Name *string `json:"name,omitempty"`
315 ImageUrl *string `json:"image_url,omitempty"`
316 BackgroundUrl *string `json:"background_url,omitempty"`
317 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
318 OriginalPrice *int64 `json:"original_price,omitempty"`
319 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
320}
321
322type ProductCouponState string
323
324func (e ProductCouponState) Ptr() *ProductCouponState {
325 return &e
326}
327
328const (
329 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
330 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
331 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
332)
333
334type CouponCodeMode string
335
336func (e CouponCodeMode) Ptr() *CouponCodeMode {
337 return &e
338}
339
340const (
341 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
342 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
343 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN"
344)
345
346type CouponCodeCountInfo struct {
347 TotalCount *int64 `json:"total_count,omitempty"`
348 AvailableCount *int64 `json:"available_count,omitempty"`
349}
350
351type StockSendRule struct {
352 MaxCount *int64 `json:"max_count,omitempty"`
353 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
354 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
355}
356
357type SingleUsageRule struct {
358 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
359 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
360 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
361 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
362}
363
364type StockUsageRule struct {
365 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
366 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
367 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
368 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
369}
370
371type StockBundleInfo struct {
372 StockBundleId *string `json:"stock_bundle_id,omitempty"`
373 StockBundleIndex *int64 `json:"stock_bundle_index,omitempty"`
374}
375
376type UsageRuleDisplayInfo struct {
377 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
378 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
379 MiniProgramPath *string `json:"mini_program_path,omitempty"`
380 AppPath *string `json:"app_path,omitempty"`
381 UsageDescription *string `json:"usage_description,omitempty"`
382 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
383}
384
385type CouponDisplayInfo struct {
386 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
387 BackgroundColor *string `json:"background_color,omitempty"`
388 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
389 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
390 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
391}
392
393type NotifyConfig struct {
394 NotifyAppid *string `json:"notify_appid,omitempty"`
395}
396
397type StockStoreScope string
398
399func (e StockStoreScope) Ptr() *StockStoreScope {
400 return &e
401}
402
403const (
404 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
405 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
406 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
407)
408
409type StockSentCountInfo struct {
410 TotalCount *int64 `json:"total_count,omitempty"`
411 TodayCount *int64 `json:"today_count,omitempty"`
412}
413
414type StockState string
415
416func (e StockState) Ptr() *StockState {
417 return &e
418}
419
420const (
421 STOCKSTATE_AUDITING StockState = "AUDITING"
422 STOCKSTATE_SENDING StockState = "SENDING"
423 STOCKSTATE_PAUSED StockState = "PAUSED"
424 STOCKSTATE_STOPPED StockState = "STOPPED"
425 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
426)
427
428type UserProductCouponTag string
429
430func (e UserProductCouponTag) Ptr() *UserProductCouponTag {
431 return &e
432}
433
434const (
435 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER"
436)
437
438type MemberTagInfo struct {
439 MemberCardId *string `json:"member_card_id,omitempty"`
440}
441
442type NormalCouponUsageRule struct {
443 Threshold *int64 `json:"threshold,omitempty"`
444 DiscountAmount *int64 `json:"discount_amount,omitempty"`
445}
446
447type DiscountCouponUsageRule struct {
448 Threshold *int64 `json:"threshold,omitempty"`
449 PercentOff *int64 `json:"percent_off,omitempty"`
450}
451
452type ComboPackage struct {
453 Name *string `json:"name,omitempty"`
454 PickCount *int64 `json:"pick_count,omitempty"`
455 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
456}
457
458type CouponAvailablePeriod struct {
459 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
460 AvailableEndTime *string `json:"available_end_time,omitempty"`
461 AvailableDays *int64 `json:"available_days,omitempty"`
462 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
463 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
464 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
465}
466
467type ExchangeCouponUsageRule struct {
468 Threshold *int64 `json:"threshold,omitempty"`
469 ExchangePrice *int64 `json:"exchange_price,omitempty"`
470}
471
472type CouponUsageMethod string
473
474func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
475 return &e
476}
477
478const (
479 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
480 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
481 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
482 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
483)
484
485type CouponAvailableStoreInfo struct {
486 Description *string `json:"description,omitempty"`
487 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
488 MiniProgramPath *string `json:"mini_program_path,omitempty"`
489}
490
491type CouponCodeDisplayMode string
492
493func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
494 return &e
495}
496
497const (
498 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
499 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
500 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
501)
502
503type EntranceMiniProgram struct {
504 Appid *string `json:"appid,omitempty"`
505 Path *string `json:"path,omitempty"`
506 EntranceWording *string `json:"entrance_wording,omitempty"`
507 GuidanceWording *string `json:"guidance_wording,omitempty"`
508}
509
510type EntranceOfficialAccount struct {
511 Appid *string `json:"appid,omitempty"`
512}
513
514type EntranceFinder struct {
515 FinderId *string `json:"finder_id,omitempty"`
516 FinderVideoId *string `json:"finder_video_id,omitempty"`
517 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
518}
519
520type ComboPackageChoice struct {
521 Name *string `json:"name,omitempty"`
522 Price *int64 `json:"price,omitempty"`
523 Count *int64 `json:"count,omitempty"`
524 ImageUrl *string `json:"image_url,omitempty"`
525 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
526 MiniProgramPath *string `json:"mini_program_path,omitempty"`
527}
528
529type FixedWeekPeriod struct {
530 DayList []WeekEnum `json:"day_list,omitempty"`
531 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
532}
533
534type TimePeriod struct {
535 BeginTime *string `json:"begin_time,omitempty"`
536 EndTime *string `json:"end_time,omitempty"`
537}
538
539type WeekEnum string
540
541func (e WeekEnum) Ptr() *WeekEnum {
542 return &e
543}
544
545const (
546 WEEKENUM_MONDAY WeekEnum = "MONDAY"
547 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
548 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
549 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
550 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
551 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
552 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
553)
554
555type PeriodOfTheDay struct {
556 BeginTime *int64 `json:"begin_time,omitempty"`
557 EndTime *int64 `json:"end_time,omitempty"`
558}
559