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