
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 USERPRODUCTCOUPONSENDCHANNEL_MEMBER UserProductCouponSendChannel = "MEMBER"
201 USERPRODUCTCOUPONSENDCHANNEL_STICKER_GIFT UserProductCouponSendChannel = "STICKER_GIFT"
202)
203
204type CouponUsageDetail struct {
205 UseRequestNo *string `json:"use_request_no,omitempty"`
206 UseTime *time.Time `json:"use_time,omitempty"`
207 ReturnRequestNo *string `json:"return_request_no,omitempty"`
208 ReturnTime *time.Time `json:"return_time,omitempty"`
209 AssociatedOrderInfo *UserProductCouponAssociatedOrderInfo `json:"associated_order_info,omitempty"`
210 AssociatedPayScoreOrderInfo *UserProductCouponAssociatedPayScoreOrderInfo `json:"associated_pay_score_order_info,omitempty"`
211 SavedAmount *int64 `json:"saved_amount,omitempty"`
212 UseProductNoList []string `json:"use_product_no_list,omitempty"`
213}
214
215type ProductCouponEntity struct {
216 ProductCouponId *string `json:"product_coupon_id,omitempty"`
217 Scope *ProductCouponScope `json:"scope,omitempty"`
218 Type *ProductCouponType `json:"type,omitempty"`
219 UsageMode *UsageMode `json:"usage_mode,omitempty"`
220 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"`
221 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"`
222 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"`
223 OutProductNo *string `json:"out_product_no,omitempty"`
224 State *ProductCouponState `json:"state,omitempty"`
225 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
226 DeactivateTime *string `json:"deactivate_time,omitempty"`
227 DeactivateReason *string `json:"deactivate_reason,omitempty"`
228 BrandId *string `json:"brand_id,omitempty"`
229 ShowAsExchange *bool `json:"show_as_exchange,omitempty"`
230 AvailableProductNoList []string `json:"available_product_no_list,omitempty"`
231}
232
233type StockEntity struct {
234 ProductCouponId *string `json:"product_coupon_id,omitempty"`
235 StockId *string `json:"stock_id,omitempty"`
236 Remark *string `json:"remark,omitempty"`
237 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"`
238 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"`
239 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"`
240 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"`
241 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"`
242 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"`
243 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"`
244 StoreScope *StockStoreScope `json:"store_scope,omitempty"`
245 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"`
246 State *StockState `json:"state,omitempty"`
247 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"`
248 DeactivateTime *time.Time `json:"deactivate_time,omitempty"`
249 DeactivateReason *string `json:"deactivate_reason,omitempty"`
250 BrandId *string `json:"brand_id,omitempty"`
251}
252
253type UserProductCouponTag string
254
255func (e UserProductCouponTag) Ptr() *UserProductCouponTag {
256 return &e
257}
258
259const (
260 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER"
261 USERPRODUCTCOUPONTAG_MEMBER_ACTIVITY UserProductCouponTag = "MEMBER_ACTIVITY"
262)
263
264type MemberTagInfo struct {
265 MemberCardId *string `json:"member_card_id,omitempty"`
266}
267
268type UserProductCouponAssociatedOrderInfo struct {
269 TransactionId *string `json:"transaction_id,omitempty"`
270 OutTradeNo *string `json:"out_trade_no,omitempty"`
271 Mchid *string `json:"mchid,omitempty"`
272 SubMchid *string `json:"sub_mchid,omitempty"`
273}
274
275type UserProductCouponAssociatedPayScoreOrderInfo struct {
276 OrderId *string `json:"order_id,omitempty"`
277 OutOrderNo *string `json:"out_order_no,omitempty"`
278 Mchid *string `json:"mchid,omitempty"`
279 SubMchid *string `json:"sub_mchid,omitempty"`
280}
281
282type ProductCouponScope string
283
284func (e ProductCouponScope) Ptr() *ProductCouponScope {
285 return &e
286}
287
288const (
289 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL"
290 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE"
291 PRODUCTCOUPONSCOPE_CATEGORY ProductCouponScope = "CATEGORY"
292)
293
294type ProductCouponType string
295
296func (e ProductCouponType) Ptr() *ProductCouponType {
297 return &e
298}
299
300const (
301 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
302 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
303 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
304)
305
306type UsageMode string
307
308func (e UsageMode) Ptr() *UsageMode {
309 return &e
310}
311
312const (
313 USAGEMODE_SINGLE UsageMode = "SINGLE"
314 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
315)
316
317type SingleUsageInfo struct {
318 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
319 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
320}
321
322type ProgressiveBundleUsageInfo struct {
323 Count *int64 `json:"count,omitempty"`
324 IntervalDays *int64 `json:"interval_days,omitempty"`
325}
326
327type ProductCouponDisplayInfo struct {
328 Name *string `json:"name,omitempty"`
329 ImageUrl *string `json:"image_url,omitempty"`
330 BackgroundUrl *string `json:"background_url,omitempty"`
331 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
332 OriginalPrice *int64 `json:"original_price,omitempty"`
333 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
334}
335
336type ProductCouponState string
337
338func (e ProductCouponState) Ptr() *ProductCouponState {
339 return &e
340}
341
342const (
343 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
344 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
345 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
346)
347
348type CouponCodeMode string
349
350func (e CouponCodeMode) Ptr() *CouponCodeMode {
351 return &e
352}
353
354const (
355 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
356 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
357 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN"
358)
359
360type CouponCodeCountInfo struct {
361 TotalCount *int64 `json:"total_count,omitempty"`
362 AvailableCount *int64 `json:"available_count,omitempty"`
363}
364
365type StockSendRule struct {
366 MaxCount *int64 `json:"max_count,omitempty"`
367 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
368 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
369}
370
371type SingleUsageRule struct {
372 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
373 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
374 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
375 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
376}
377
378type UsageRuleDisplayInfo struct {
379 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"`
380 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
381 MiniProgramPath *string `json:"mini_program_path,omitempty"`
382 AppPath *string `json:"app_path,omitempty"`
383 UsageDescription *string `json:"usage_description,omitempty"`
384 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"`
385 AppJumpType *AppJumpType `json:"app_jump_type,omitempty"`
386 PasscodeLink *string `json:"passcode_link,omitempty"`
387}
388
389type CouponDisplayInfo struct {
390 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"`
391 BackgroundColor *string `json:"background_color,omitempty"`
392 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"`
393 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"`
394}
395
396type NotifyConfig struct {
397 NotifyAppid *string `json:"notify_appid,omitempty"`
398}
399
400type StockStoreScope string
401
402func (e StockStoreScope) Ptr() *StockStoreScope {
403 return &e
404}
405
406const (
407 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
408 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
409 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
410)
411
412type StockSentCountInfo struct {
413 TotalCount *int64 `json:"total_count,omitempty"`
414 TodayCount *int64 `json:"today_count,omitempty"`
415}
416
417type StockState string
418
419func (e StockState) Ptr() *StockState {
420 return &e
421}
422
423const (
424 STOCKSTATE_AUDITING StockState = "AUDITING"
425 STOCKSTATE_SENDING StockState = "SENDING"
426 STOCKSTATE_PAUSED StockState = "PAUSED"
427 STOCKSTATE_STOPPED StockState = "STOPPED"
428 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
429)
430
431type NormalCouponUsageRule struct {
432 Threshold *int64 `json:"threshold,omitempty"`
433 DiscountAmount *int64 `json:"discount_amount,omitempty"`
434}
435
436type DiscountCouponUsageRule struct {
437 Threshold *int64 `json:"threshold,omitempty"`
438 PercentOff *int64 `json:"percent_off,omitempty"`
439}
440
441type ComboPackage struct {
442 Name *string `json:"name,omitempty"`
443 PickCount *int64 `json:"pick_count,omitempty"`
444 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
445}
446
447type CouponAvailablePeriod struct {
448 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
449 AvailableEndTime *string `json:"available_end_time,omitempty"`
450 AvailableDays *int64 `json:"available_days,omitempty"`
451 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
452 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
453 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"`
454 AvailableSeconds *int64 `json:"available_seconds,omitempty"`
455}
456
457type ExchangeCouponUsageRule struct {
458 Threshold *int64 `json:"threshold,omitempty"`
459 ExchangePrice *int64 `json:"exchange_price,omitempty"`
460}
461
462type CouponUsageMethod string
463
464func (e CouponUsageMethod) Ptr() *CouponUsageMethod {
465 return &e
466}
467
468const (
469 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE"
470 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM"
471 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP"
472 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE"
473)
474
475type CouponAvailableStoreInfo struct {
476 Description *string `json:"description,omitempty"`
477 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
478 MiniProgramPath *string `json:"mini_program_path,omitempty"`
479}
480
481type AppJumpType string
482
483func (e AppJumpType) Ptr() *AppJumpType {
484 return &e
485}
486
487const (
488 APPJUMPTYPE_H5 AppJumpType = "H5"
489 APPJUMPTYPE_PASSCODE_LINK AppJumpType = "PASSCODE_LINK"
490 APPJUMPTYPE_USAGE_GUIDE AppJumpType = "USAGE_GUIDE"
491)
492
493type CouponCodeDisplayMode string
494
495func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
496 return &e
497}
498
499const (
500 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
501 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
502 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
503)
504
505type EntranceMiniProgram struct {
506 Appid *string `json:"appid,omitempty"`
507 Path *string `json:"path,omitempty"`
508 EntranceWording *string `json:"entrance_wording,omitempty"`
509 GuidanceWording *string `json:"guidance_wording,omitempty"`
510}
511
512type EntranceOfficialAccount struct {
513 Appid *string `json:"appid,omitempty"`
514}
515
516type ComboPackageChoice struct {
517 Name *string `json:"name,omitempty"`
518 Price *int64 `json:"price,omitempty"`
519 Count *int64 `json:"count,omitempty"`
520 ImageUrl *string `json:"image_url,omitempty"`
521 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
522 MiniProgramPath *string `json:"mini_program_path,omitempty"`
523}
524
525type FixedWeekPeriod struct {
526 DayList []WeekEnum `json:"day_list,omitempty"`
527 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
528}
529
530type TimePeriod struct {
531 BeginTime *string `json:"begin_time,omitempty"`
532 EndTime *string `json:"end_time,omitempty"`
533}
534
535type WeekEnum string
536
537func (e WeekEnum) Ptr() *WeekEnum {
538 return &e
539}
540
541const (
542 WEEKENUM_MONDAY WeekEnum = "MONDAY"
543 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
544 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
545 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
546 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
547 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
548 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
549)
550
551type PeriodOfTheDay struct {
552 BeginTime *int64 `json:"begin_time,omitempty"`
553 EndTime *int64 `json:"end_time,omitempty"`
554}
555