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 pathinvoice.go
More file actions
264 lines (251 loc) · 6.81 KB
/
invoice.go
File metadata and controls
264 lines (251 loc) · 6.81 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package stripe
import (
"encoding/json"
"net/url"
"strconv"
)
type Invoice struct {
ID string `json:"id"`
LiveMode bool `json:"livemode"`
AmountDue int `json:"amount_due"`
AttemptCount int `json:"attempt_count"`
Attempted bool `json:"attempted"`
Closed bool `json:"closed"`
CustomerID string `json:"customer"`
Date int64 `json:"date"`
Paid bool `json:"paid"`
PeriodEnd int64 `json:"period_end"`
PeriodStart int64 `json:"period_start"`
StartingBalance int `json:"starting_balance"`
Subtotal int `json:"subtotal"`
Total int `json:"total"`
ChargeID *string `json:"charge"`
Discount *Discount `json:"discount"`
EndingBalance *int `json:"ending_balance"`
NextPaymentAttempt *int `json:"next_payment_attempt"`
Lines struct {
InvoiceItems []*InvoiceItem `json:"invoiceitems"`
Subscriptions []*SubscriptionItem `json:"subscriptions"`
Prorated []*InvoiceItem `json:"prorations"`
}
Object string `json:"object"`
Error *RawError `json:"error"`
}
type SubscriptionItem struct {
Amount int `json:"amount"`
Period struct {
Start int64 `json:"start"`
End int64 `json:"end"`
} `json:"period"`
Plan *Plan `json:"plan"`
}
func (stripe *Stripe) GetInvoice(id string) (resp *Invoice, err error) {
r, err := stripe.request("GET", "invoices/"+id, "")
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
}
func (stripe *Stripe) GetNextInvoice(customer string) (resp *Invoice, err error) {
values := make(url.Values)
values.Set("customer", customer)
params := values.Encode()
r, err := stripe.request("GET", "invoices/upcoming"+params, "")
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
}
func (stripe *Stripe) ListInvoices(count, offset int, customer string) (resp []*Invoice, err error) {
values := make(url.Values)
if count >= 0 {
values.Set("count", strconv.Itoa(count))
}
if offset >= 0 {
values.Set("offset", strconv.Itoa(offset))
}
if customer != "" {
values.Set("customer", customer)
}
params := values.Encode()
if params != "" {
params = "?" + params
}
r, err := stripe.request("GET", "invoices"+params, "")
if err != nil {
return nil, err
}
var raw struct {
Count int "count"
Data []*Invoice "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
}
type InvoiceItem struct {
ID string `json:"id"`
LiveMode bool `json:"livemode"`
Date int64 `json:"date"`
Description *string `json:"description"`
Currency string `json:"currency"`
Amount int `json:"amount"`
CustomerID string `json:"customer"`
InvoiceID *string `json:"invoice"`
Object int `json:"object"`
Error *RawError `json:"error"`
}
func (item *InvoiceItem) Values(values *url.Values) error {
if item == nil {
//TODO: Throw error
}
if item.CustomerID == "" {
// TODO: Throw error
}
if item.Amount == 0 {
// TODO: Throw error
}
if item.Currency != "USD" {
// TODO: Throw error
}
if item.InvoiceID != nil {
values.Set("invoice", *item.InvoiceID)
}
if item.Description != nil {
values.Set("desription", *item.Description)
}
values.Set("customer", item.CustomerID)
values.Set("amount", strconv.Itoa(item.Amount))
values.Set("currency", item.Currency)
return nil
}
func (stripe *Stripe) CreateInvoiceItem(item *InvoiceItem) (resp *InvoiceItem, err error) {
values := make(url.Values)
err = item.Values(&values)
if err != nil {
return nil, err
}
data := values.Encode()
r, err := stripe.request("POST", "invoiceitems", 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
}
func (stripe *Stripe) GetInvoiceItem(id string) (resp *InvoiceItem, err error) {
r, err := stripe.request("GET", "invoiceitems/"+id, "")
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
}
func (stripe *Stripe) UpdateInvoiceItem(id string, amount int, description string) (resp *InvoiceItem, err error) {
values := make(url.Values)
if amount >= 0 {
values.Set("amount", strconv.Itoa(amount))
}
if description != "" {
values.Set("description", description)
}
data := values.Encode()
r, err := stripe.request("POST", "invoiceitems/"+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
}
func (stripe *Stripe) DeleteInvoiceItem(id string) (success bool, err error) {
r, err := stripe.request("DELETE", "invoiceitems/"+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
}
func (stripe *Stripe) ListInvoiceItems(count, offset int, customer string) (resp []*InvoiceItem, err error) {
values := make(url.Values)
if count >= 0 {
values.Set("count", strconv.Itoa(count))
}
if offset >= 0 {
values.Set("offset", strconv.Itoa(offset))
}
if customer != "" {
values.Set("customer", customer)
}
params := values.Encode()
if params != "" {
params = "?" + params
}
r, err := stripe.request("GET", "invoiceitems"+params, "")
if err != nil {
return nil, err
}
var raw struct {
Count int `json:"count"`
Data []*InvoiceItem `json:"data"`
Error *RawError `json:"error"`
}
err = json.Unmarshal(r, &raw)
if err != nil {
return nil, err
}
if raw.Error != nil {
// TODO: throw an error
}
resp = raw.Data
return
}