
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)
284
285type ProductCouponType string
286
287func (e ProductCouponType) Ptr() *ProductCouponType {
288 return &e
289}
290
291const (
292 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
293 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
294 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
295)
296
297type UsageMode string
298
299func (e UsageMode) Ptr() *UsageMode {
300 return &e
301}
302
303const (
304 USAGEMODE_SINGLE UsageMode = "SINGLE"
305 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
306)
307
308type SingleUsageInfo struct {
309 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
310 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
311}
312
313type ProgressiveBundleUsageInfo struct {
314 Count *int64 `json:"count,omitempty"`
315 IntervalDays *int64 `json:"interval_days,omitempty"`
316}
317
318type ProductCouponDisplayInfo struct {
319 Name *string `json:"name,omitempty"`
320 ImageUrl *string `json:"image_url,omitempty"`
321 BackgroundUrl *string `json:"background_url,omitempty"`
322 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
323 OriginalPrice *int64 `json:"original_price,omitempty"`
324 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
325}
326
327type ProductCouponState string
328
329func (e ProductCouponState) Ptr() *ProductCouponState {
330 return &e
331}
332
333const (
334 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
335 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
336 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
337)
338
339type CouponCodeMode string
340
341func (e CouponCodeMode) Ptr() *CouponCodeMode {
342 return &e
343}
344
345const (
346 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
347 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
348)
349
350type CouponCodeCountInfo struct {
351 TotalCount *int64 `json:"total_count,omitempty"`
352 AvailableCount *int64 `json:"available_count,omitempty"`
353}
354
355type StockSendRule struct {
356 MaxCount *int64 `json:"max_count,omitempty"`
357 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
358 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
359}
360
361type StockUsageRule struct {
362 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
363 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
364 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
365 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
366}
367
368type StockBundleInfo struct {
369 StockBundleId *string `json:"stock_bundle_id,omitempty"`
370 StockBundleIndex *int64 `json:"stock_bundle_index,omitempty"`
371}
372
373type UsageRuleDisplayInfo struct {
374 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
375 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
376 MiniProgramPath *string `json:"mini_program_path,omitempty"`
377 AppPath *string `json:"app_path,omitempty"`
378 UsageDescription *string `json:"usage_description,omitempty"`
379 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
380}
381
382type CouponDisplayInfo struct {
383 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
384 BackgroundColor *string `json:"background_color,omitempty"`
385 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
386 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
387 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
388}
389
390type NotifyConfig struct {
391 NotifyAppid *string `json:"notify_appid,omitempty"`
392}
393
394type StockStoreScope string
395
396func (e StockStoreScope) Ptr() *StockStoreScope {
397 return &e
398}
399
400const (
401 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
402 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
403 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
404)
405
406type StockSentCountInfo struct {
407 TotalCount *int64 `json:"total_count,omitempty"`
408 TodayCount *int64 `json:"today_count,omitempty"`
409}
410
411type StockState string
412
413func (e StockState) Ptr() *StockState {
414 return &e
415}
416
417const (
418 STOCKSTATE_AUDITING StockState = "AUDITING"
419 STOCKSTATE_SENDING StockState = "SENDING"
420 STOCKSTATE_PAUSED StockState = "PAUSED"
421 STOCKSTATE_STOPPED StockState = "STOPPED"
422 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
423)
424
425type NormalCouponUsageRule struct {
426 Threshold *int64 `json:"threshold,omitempty"`
427 DiscountAmount *int64 `json:"discount_amount,omitempty"`
428}
429
430type DiscountCouponUsageRule struct {
431 Threshold *int64 `json:"threshold,omitempty"`
432 PercentOff *int64 `json:"percent_off,omitempty"`
433}
434
435type ComboPackage struct {
436 Name *string `json:"name,omitempty"`
437 PickCount *int64 `json:"pick_count,omitempty"`
438 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
439}
440
441type CouponAvailablePeriod struct {
442 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
443 AvailableEndTime *string `json:"available_end_time,omitempty"`
444 AvailableDays *int64 `json:"available_days,omitempty"`
445 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
446 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
447 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
448}
449
450type ExchangeCouponUsageRule struct {
451 Threshold *int64 `json:"threshold,omitempty"`
452 ExchangePrice *int64 `json:"exchange_price,omitempty"`
453}
454
455type CouponUsageMethod string
456
457func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
458 return &e
459}
460
461const (
462 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
463 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
464 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
465 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
466)
467
468type CouponAvailableStoreInfo struct {
469 Description *string `json:"description,omitempty"`
470 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
471 MiniProgramPath *string `json:"mini_program_path,omitempty"`
472}
473
474type CouponCodeDisplayMode string
475
476func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
477 return &e
478}
479
480const (
481 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
482 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
483 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
484)
485
486type EntranceMiniProgram struct {
487 Appid *string `json:"appid,omitempty"`
488 Path *string `json:"path,omitempty"`
489 EntranceWording *string `json:"entrance_wording,omitempty"`
490 GuidanceWording *string `json:"guidance_wording,omitempty"`
491}
492
493type EntranceOfficialAccount struct {
494 Appid *string `json:"appid,omitempty"`
495}
496
497type EntranceFinder struct {
498 FinderId *string `json:"finder_id,omitempty"`
499 FinderVideoId *string `json:"finder_video_id,omitempty"`
500 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
501}
502
503type ComboPackageChoice struct {
504 Name *string `json:"name,omitempty"`
505 Price *int64 `json:"price,omitempty"`
506 Count *int64 `json:"count,omitempty"`
507 ImageUrl *string `json:"image_url,omitempty"`
508 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
509 MiniProgramPath *string `json:"mini_program_path,omitempty"`
510}
511
512type FixedWeekPeriod struct {
513 DayList []WeekEnum `json:"day_list,omitempty"`
514 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
515}
516
517type TimePeriod struct {
518 BeginTime *string `json:"begin_time,omitempty"`
519 EndTime *string `json:"end_time,omitempty"`
520}
521
522type WeekEnum string
523
524func (e WeekEnum) Ptr() *WeekEnum {
525 return &e
526}
527
528const (
529 WEEKENUM_MONDAY WeekEnum = "MONDAY"
530 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
531 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
532 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
533 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
534 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
535 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
536)
537
538type PeriodOfTheDay struct {
539 BeginTime *int64 `json:"begin_time,omitempty"`
540 EndTime *int64 `json:"end_time,omitempty"`
541}
542