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