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