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