
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)
289
290type ProductCouponType string
291
292func (e ProductCouponType) Ptr() *ProductCouponType {
293 return &e
294}
295
296const (
297 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
298 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
299 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
300)
301
302type UsageMode string
303
304func (e UsageMode) Ptr() *UsageMode {
305 return &e
306}
307
308const (
309 USAGEMODE_SINGLE UsageMode = "SINGLE"
310 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
311)
312
313type SingleUsageInfo struct {
314 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
315 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
316}
317
318type ProgressiveBundleUsageInfo struct {
319 Count *int64 `json:"count,omitempty"`
320 IntervalDays *int64 `json:"interval_days,omitempty"`
321}
322
323type ProductCouponDisplayInfo struct {
324 Name *string `json:"name,omitempty"`
325 ImageUrl *string `json:"image_url,omitempty"`
326 BackgroundUrl *string `json:"background_url,omitempty"`
327 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"`
328 OriginalPrice *int64 `json:"original_price,omitempty"`
329 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"`
330}
331
332type ProductCouponState string
333
334func (e ProductCouponState) Ptr() *ProductCouponState {
335 return &e
336}
337
338const (
339 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING"
340 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE"
341 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED"
342)
343
344type CouponCodeMode string
345
346func (e CouponCodeMode) Ptr() *CouponCodeMode {
347 return &e
348}
349
350const (
351 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY"
352 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD"
353)
354
355type CouponCodeCountInfo struct {
356 TotalCount *int64 `json:"total_count,omitempty"`
357 AvailableCount *int64 `json:"available_count,omitempty"`
358}
359
360type StockSendRule struct {
361 MaxCount *int64 `json:"max_count,omitempty"`
362 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"`
363 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"`
364}
365
366type StockUsageRule struct {
367 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"`
368 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
369 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
370 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
371}
372
373type StockBundleInfo struct {
374 StockBundleId *string `json:"stock_bundle_id,omitempty"`
375 StockBundleIndex *int64 `json:"stock_bundle_index,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 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"`
395}
396
397type NotifyConfig struct {
398 NotifyAppid *string `json:"notify_appid,omitempty"`
399}
400
401type StockStoreScope string
402
403func (e StockStoreScope) Ptr() *StockStoreScope {
404 return &e
405}
406
407const (
408 STOCKSTORESCOPE_NONE StockStoreScope = "NONE"
409 STOCKSTORESCOPE_ALL StockStoreScope = "ALL"
410 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC"
411)
412
413type StockSentCountInfo struct {
414 TotalCount *int64 `json:"total_count,omitempty"`
415 TodayCount *int64 `json:"today_count,omitempty"`
416}
417
418type StockState string
419
420func (e StockState) Ptr() *StockState {
421 return &e
422}
423
424const (
425 STOCKSTATE_AUDITING StockState = "AUDITING"
426 STOCKSTATE_SENDING StockState = "SENDING"
427 STOCKSTATE_PAUSED StockState = "PAUSED"
428 STOCKSTATE_STOPPED StockState = "STOPPED"
429 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED"
430)
431
432type NormalCouponUsageRule struct {
433 Threshold *int64 `json:"threshold,omitempty"`
434 DiscountAmount *int64 `json:"discount_amount,omitempty"`
435}
436
437type DiscountCouponUsageRule struct {
438 Threshold *int64 `json:"threshold,omitempty"`
439 PercentOff *int64 `json:"percent_off,omitempty"`
440}
441
442type ComboPackage struct {
443 Name *string `json:"name,omitempty"`
444 PickCount *int64 `json:"pick_count,omitempty"`
445 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"`
446}
447
448type CouponAvailablePeriod struct {
449 AvailableBeginTime *string `json:"available_begin_time,omitempty"`
450 AvailableEndTime *string `json:"available_end_time,omitempty"`
451 AvailableDays *int64 `json:"available_days,omitempty"`
452 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"`
453 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"`
454 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,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)
491
492type CouponCodeDisplayMode string
493
494func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode {
495 return &e
496}
497
498const (
499 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE"
500 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE"
501 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE"
502)
503
504type EntranceMiniProgram struct {
505 Appid *string `json:"appid,omitempty"`
506 Path *string `json:"path,omitempty"`
507 EntranceWording *string `json:"entrance_wording,omitempty"`
508 GuidanceWording *string `json:"guidance_wording,omitempty"`
509}
510
511type EntranceOfficialAccount struct {
512 Appid *string `json:"appid,omitempty"`
513}
514
515type EntranceFinder struct {
516 FinderId *string `json:"finder_id,omitempty"`
517 FinderVideoId *string `json:"finder_video_id,omitempty"`
518 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"`
519}
520
521type ComboPackageChoice struct {
522 Name *string `json:"name,omitempty"`
523 Price *int64 `json:"price,omitempty"`
524 Count *int64 `json:"count,omitempty"`
525 ImageUrl *string `json:"image_url,omitempty"`
526 MiniProgramAppid *string `json:"mini_program_appid,omitempty"`
527 MiniProgramPath *string `json:"mini_program_path,omitempty"`
528}
529
530type FixedWeekPeriod struct {
531 DayList []WeekEnum `json:"day_list,omitempty"`
532 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"`
533}
534
535type TimePeriod struct {
536 BeginTime *string `json:"begin_time,omitempty"`
537 EndTime *string `json:"end_time,omitempty"`
538}
539
540type WeekEnum string
541
542func (e WeekEnum) Ptr() *WeekEnum {
543 return &e
544}
545
546const (
547 WEEKENUM_MONDAY WeekEnum = "MONDAY"
548 WEEKENUM_TUESDAY WeekEnum = "TUESDAY"
549 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY"
550 WEEKENUM_THURSDAY WeekEnum = "THURSDAY"
551 WEEKENUM_FRIDAY WeekEnum = "FRIDAY"
552 WEEKENUM_SATURDAY WeekEnum = "SATURDAY"
553 WEEKENUM_SUNDAY WeekEnum = "SUNDAY"
554)
555
556type PeriodOfTheDay struct {
557 BeginTime *int64 `json:"begin_time,omitempty"`
558 EndTime *int64 `json:"end_time,omitempty"`
559}
560