
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}
379
380type CouponDisplayInfo struct {
381 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
382 BackgroundColor *string `json:"background_color,omitempty"`
383 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
384 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
385 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
386}
387
388type NotifyConfig struct {
389 NotifyAppid *string `json:"notify_appid,omitempty"`
390}
391
392type StockStoreScope string
393
394func (e StockStoreScope) Ptr() *StockStoreScope {
395 return &e
396}
397
398const (
399 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
400 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
401 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
402)
403
404type StockSentCountInfo struct {
405 TotalCount *int64 `json:"total_count,omitempty"`
406 TodayCount *int64 `json:"today_count,omitempty"`
407}
408
409type StockState string
410
411func (e StockState) Ptr() *StockState {
412 return &e
413}
414
415const (
416 STOCKSTATE_AUDITING StockState = "AUDITING"
417 STOCKSTATE_SENDING StockState = "SENDING"
418 STOCKSTATE_PAUSED StockState = "PAUSED"
419 STOCKSTATE_STOPPED StockState = "STOPPED"
420 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
421)
422
423type NormalCouponUsageRule struct {
424 Threshold *int64 `json:"threshold,omitempty"`
425 DiscountAmount *int64 `json:"discount_amount,omitempty"`
426}
427
428type DiscountCouponUsageRule struct {
429 Threshold *int64 `json:"threshold,omitempty"`
430 PercentOff *int64 `json:"percent_off,omitempty"`
431}
432
433type ComboPackage struct {
434 Name *string `json:"name,omitempty"`
435 PickCount *int64 `json:"pick_count,omitempty"`
436 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
437}
438
439type CouponAvailablePeriod struct {
440 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
441 AvailableEndTime *string `json:"available_end_time,omitempty"`
442 AvailableDays *int64 `json:"available_days,omitempty"`
443 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
444 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
445 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
446}
447
448type ExchangeCouponUsageRule struct {
449 Threshold *int64 `json:"threshold,omitempty"`
450 ExchangePrice *int64 `json:"exchange_price,omitempty"`
451}
452
453type CouponUsageMethod string
454
455func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
456 return &e
457}
458
459const (
460 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
461 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
462 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
463 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
464)
465
466type CouponAvailableStoreInfo struct {
467 Description *string `json:"description,omitempty"`
468 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
469 MiniProgramPath *string `json:"mini_program_path,omitempty"`
470}
471
472type CouponCodeDisplayMode string
473
474func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
475 return &e
476}
477
478const (
479 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
480 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
481 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
482)
483
484type EntranceMiniProgram struct {
485 Appid *string `json:"appid,omitempty"`
486 Path *string `json:"path,omitempty"`
487 EntranceWording *string `json:"entrance_wording,omitempty"`
488 GuidanceWording *string `json:"guidance_wording,omitempty"`
489}
490
491type EntranceOfficialAccount struct {
492 Appid *string `json:"appid,omitempty"`
493}
494
495type EntranceFinder struct {
496 FinderId *string `json:"finder_id,omitempty"`
497 FinderVideoId *string `json:"finder_video_id,omitempty"`
498 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
499}
500
501type ComboPackageChoice struct {
502 Name *string `json:"name,omitempty"`
503 Price *int64 `json:"price,omitempty"`
504 Count *int64 `json:"count,omitempty"`
505 ImageUrl *string `json:"image_url,omitempty"`
506 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
507 MiniProgramPath *string `json:"mini_program_path,omitempty"`
508}
509
510type FixedWeekPeriod struct {
511 DayList []WeekEnum `json:"day_list,omitempty"`
512 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
513}
514
515type TimePeriod struct {
516 BeginTime *string `json:"begin_time,omitempty"`
517 EndTime *string `json:"end_time,omitempty"`
518}
519
520type WeekEnum string
521
522func (e WeekEnum) Ptr() *WeekEnum {
523 return &e
524}
525
526const (
527 WEEKENUM_MONDAY WeekEnum = "MONDAY"
528 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
529 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
530 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
531 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
532 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
533 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
534)
535
536type PeriodOfTheDay struct {
537 BeginTime *int64 `json:"begin_time,omitempty"`
538 EndTime *int64 `json:"end_time,omitempty"`
539}
540