This repository was archived by the owner on Nov 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomer.go
More file actions
219 lines (210 loc) · 6 KB
/
customer.go
File metadata and controls
219 lines (210 loc) · 6 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package stripe
import (
"encoding/json"
"net/url"
"strconv"
)
// Customer represents a customer, according to the Stripe API
type Customer struct {
Description string `json:"description"`
Object string `json:"object"` // Should always be "customer"
LiveMode bool `json:"livemode"`
ActiveCard *Card `json:"active_card"`
Created int64 `json:"created"`
ID string `json:"id"`
Balance int64 `json:"account_balance"`
Error *RawError `json:"error"`
Delinquent bool `json:"delinquent"` // Whether the latest charge failed
Discount *Discount `json:"discount"`
Email string `json:"email"`
Subscription *Subscription `json:"subscription"`
}
// ChargeValues sets *customer's non-empty properties to their appropriate key in *values
// This is useful for constructing HTTP requests from Customer objects
// This also satisfies the Chargeable interface, allowing Customers to be charged
func (customer *Customer) ChargeValues(values *url.Values) error {
if customer == nil {
// TODO: Throw an error
}
if customer.ActiveCard == nil {
// TODO: Throw an error
}
if customer.ID != "" {
values.Set("customer", customer.ID)
} else {
// TODO: throw an error
}
return nil
}
func (customer *Customer) Values(values *url.Values) error {
if customer == nil {
// TODO: Throw an error
}
if customer.Description != "" {
values.Set("description", customer.Description)
}
if customer.Email != "" {
values.Set("email", customer.Email)
}
return nil
}
// CreateCustomer creates a new customer on Stripe.
//
// All arguments except the Customer object are optional.
//
// If a Chargeable is provided, it is associated with that customer and automatically validated. Pass nil to omit the Chargeable.
//
// If plan is non-empty, it is used as the identifier of a Plan to subscribe customer to.
//
// If coupon is non-empty, it is used as a Coupon that will be applied to all of customer's recurring charges.
//
// If trial_end is not -1, it will be used as the UTC integer timestamp representing the end of the trial period for customer.
//
// trial_end overrides the plan's default trial period, if not -1.
func (stripe *Stripe) CreateCustomer(customer *Customer, chargeable Chargeable, plan, coupon string, trial_end int64) (resp *Customer, err error) {
values := make(url.Values)
err = customer.Values(&values)
if err != nil {
return nil, err
}
if chargeable != nil {
err = chargeable.ChargeValues(&values)
if err != nil {
return nil, err
}
}
if plan != "" {
values.Set("plan", plan)
}
if trial_end != -1 {
values.Set("trial_end", strconv.FormatInt(trial_end, 10))
}
if coupon != "" {
values.Set("coupon", coupon)
}
data := values.Encode()
r, err := stripe.request("POST", "customers", data)
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
// TODO: Throw an error
}
return
}
// UpdateCustomer updates the information Stripe has on *customer.
//
// All arguments except the Customer object are optional.
//
// If a Chargeable is provided, it is attached to *customer and automatically validated. Pass nil to omit the Chargeable.
//
// If couponID is non-empty, it is used as a Coupon that will be applied to all of *customer's recurring charges.
func (stripe *Stripe) UpdateCustomer(customer *Customer, chargeable Chargeable, couponID string) (resp *Customer, err error) {
if customer.ID == "" {
// TODO: throw an error
}
values := make(url.Values)
err = customer.Values(&values)
if err != nil {
return nil, err
}
if chargeable != nil {
err = chargeable.ChargeValues(&values)
if err != nil {
return nil, err
}
}
if couponID != "" {
values.Set("coupon", couponID)
}
data := values.Encode()
r, err := stripe.request("POST", "customers/"+customer.ID, data)
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
// TODO: Throw an error
}
return
}
// GetCustomer retrieves information on the Customer with ID of id.
func (stripe *Stripe) GetCustomer(id string) (resp *Customer, err error) {
r, err := stripe.request("GET", "customers/"+id, "")
if err != nil {
return nil, err
}
err = json.Unmarshal(r, &resp)
if err != nil {
return nil, err
}
if resp.Error != nil {
err = resp.Error
}
return resp, err
}
// DeleteCustomer permanently deletes the Customer with ID of id from Stripe. It cannot be undone.
func (stripe *Stripe) DeleteCustomer(id string) (success bool, err error) {
r, err := stripe.request("DELETE", "customers/"+id, "")
if err != nil {
return false, err
}
var raw struct {
Success bool "deleted"
ID string "id"
Error *RawError "error"
}
err = json.Unmarshal(r, &raw)
if err != nil {
return false, err
}
if raw.Error != nil {
// TODO: throw an error
}
return raw.Success, err
}
// ListCustomers queries the server for information about all your Customers. Results are returned sorted by creation date, with the most recently created Customers appearing first.
//
// Both the arguments are optional.
//
// Pass -1 to count to use the Stripe default (10). Count determines the number of customers to return. The maximum is 100.
//
// Pass -1 to offset to use the Stripe default (0). Offset determines the number of recent customers to skip.
func (stripe *Stripe) ListCustomers(count, offset int) (resp []*Customer, err error) {
values := make(url.Values)
if count >= 0 {
values.Set("count", strconv.Itoa(count))
}
if offset >= 0 {
values.Set("offset", strconv.Itoa(offset))
}
params := values.Encode()
if params != "" {
params = "?" + params
}
r, err := stripe.request("GET", "customers"+params, "")
if err != nil {
return nil, err
}
var raw struct {
Count int "count"
Data []*Customer "data"
Error *RawError "error"
}
err = json.Unmarshal(r, &raw)
if err != nil {
return nil, err
}
if raw.Error != nil {
// TODO: throw an error
}
resp = raw.Data
return
}