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