This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcoupon.go
144 lines (123 loc) · 4.17 KB
/
coupon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package stripe
import (
"net/url"
"strconv"
)
// Coupon Durations
const (
DurationForever = "forever"
DurationOnce = "once"
DurationRepeating = "repeating"
)
// Coupon represents percent-off discount you might want to apply to a customer.
//
// see https://stripe.com/docs/api#coupon_object
type Coupon struct {
Id string `json:"id"`
Duration string `json:"duration"`
PercentOff int `json:"percent_off"`
DurationInMonths Int `json:"duration_in_months,omitempty"`
MaxRedemptions Int `json:"max_redemptions,omitempty"`
RedeemBy Int64 `json:"redeem_by,omitempty"`
TimesRedeemed int `json:"times_redeemed,omitempty"`
Livemode bool `json:"livemode"`
}
// CouponClient encapsulates operations for creating, updating, deleting and
// querying coupons using the Stripe REST API.
type CouponClient struct{}
// CouponParams encapsulates options for creating a new Coupon.
type CouponParams struct {
// (Optional) Unique string of your choice that will be used to identify
// this coupon when applying it a customer.
Id string
// A positive integer between 1 and 100 that represents the discount the
// coupon will apply.
PercentOff int
// Specifies how long the discount will be in effect. Can be forever, once,
// or repeating.
Duration string
// (Optional) If duration is repeating, a positive integer that specifies
// the number of months the discount will be in effect.
DurationInMonths int
// (Optional) A positive integer specifying the number of times the coupon
// can be redeemed before it's no longer valid. For example, you might have
// a 50% off coupon that the first 20 readers of your blog can use.
MaxRedemptions int
// (Optional) UTC timestamp specifying the last time at which the coupon can
// be redeemed. After the redeem_by date, the coupon can no longer be
// applied to new customers.
RedeemBy int64
}
// Creates a new Coupon.
//
// see https://stripe.com/docs/api#create_coupon
func (self *CouponClient) Create(params *CouponParams) (*Coupon, error) {
coupon := Coupon{}
values := url.Values{
"duration": {params.Duration},
"percent_off": {strconv.Itoa(params.PercentOff)},
}
// coupon id is optional, add if specified
if len(params.Id) != 0 {
values.Add("id", params.Id)
}
// duration in months is optional, add if specified
if params.DurationInMonths != 0 {
values.Add("duration_in_months", strconv.Itoa(params.DurationInMonths))
}
// max_redemptions is optional, add if specified
if params.MaxRedemptions != 0 {
values.Add("max_redemptions", strconv.Itoa(params.MaxRedemptions))
}
// redeem_by is optional, add if specified
if params.RedeemBy != 0 {
values.Add("redeem_by", strconv.FormatInt(params.RedeemBy, 10))
}
err := query("POST", "/v1/coupons", values, &coupon)
return &coupon, err
}
// Retrieves the coupon with the given ID.
//
// see https://stripe.com/docs/api#retrieve_coupon
func (self *CouponClient) Retrieve(id string) (*Coupon, error) {
coupon := Coupon{}
path := "/v1/coupons/" + url.QueryEscape(id)
err := query("GET", path, nil, &coupon)
return &coupon, err
}
// Deletes the coupon with the given ID.
//
// see https://stripe.com/docs/api#delete_coupon
func (self *CouponClient) Delete(id string) (bool, error) {
resp := DeleteResp{}
path := "/v1/coupons/" + url.QueryEscape(id)
if err := query("DELETE", path, nil, &resp); err != nil {
return false, err
}
return resp.Deleted, nil
}
// Returns a list of your coupons.
//
// see https://stripe.com/docs/api#list_coupons
func (self *CouponClient) List() ([]*Coupon, error) {
return self.ListN(10, 0)
}
// Returns a list of your coupons at the specified range.
//
// see https://stripe.com/docs/api#list_coupons
func (self *CouponClient) ListN(count int, offset int) ([]*Coupon, error) {
// define a wrapper function for the Coupon List, so that we can
// cleanly parse the JSON
type listCouponResp struct{ Data []*Coupon }
resp := listCouponResp{}
// add the count and offset to the list of url values
values := url.Values{
"count": {strconv.Itoa(count)},
"offset": {strconv.Itoa(offset)},
}
err := query("GET", "/v1/coupons", values, &resp)
if err != nil {
return nil, err
}
return resp.Data, nil
}