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