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