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