forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
208 lines (172 loc) · 6.59 KB
/
client.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
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
// Package invoice provides the /invoices APIs
package invoice
import (
"net/http"
stripe "github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/form"
)
// Client is the client used to invoke /invoices APIs.
type Client struct {
B stripe.Backend
Key string
}
// New creates a new invoice.
func New(params *stripe.InvoiceParams) (*stripe.Invoice, error) {
return getC().New(params)
}
// New creates a new invoice.
func (c Client) New(params *stripe.InvoiceParams) (*stripe.Invoice, error) {
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, "/v1/invoices", c.Key, params, invoice)
return invoice, err
}
// Del deletes an invoice.
func Del(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) {
return getC().Del(id, params)
}
// Del deletes an invoice.
func (c Client) Del(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodDelete, path, c.Key, params, invoice)
return invoice, err
}
// Get returns the details of an invoice.
func Get(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) {
return getC().Get(id, params)
}
// Get returns the details of an invoice.
func (c Client) Get(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodGet, path, c.Key, params, invoice)
return invoice, err
}
// Pay pays an invoice.
func Pay(id string, params *stripe.InvoicePayParams) (*stripe.Invoice, error) {
return getC().Pay(id, params)
}
// Pay pays an invoice.
func (c Client) Pay(id string, params *stripe.InvoicePayParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s/pay", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, path, c.Key, params, invoice)
return invoice, err
}
// Update updates an invoice.
func Update(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) {
return getC().Update(id, params)
}
// Update updates an invoice.
func (c Client) Update(id string, params *stripe.InvoiceParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, path, c.Key, params, invoice)
return invoice, err
}
// GetNext returns an upcoming invoice.
func GetNext(params *stripe.InvoiceParams) (*stripe.Invoice, error) {
return getC().GetNext(params)
}
// GetNext returns an upcoming invoice.
func (c Client) GetNext(params *stripe.InvoiceParams) (*stripe.Invoice, error) {
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodGet, "/v1/invoices/upcoming", c.Key, params, invoice)
return invoice, err
}
// List returns a list of invoices.
func List(params *stripe.InvoiceListParams) *Iter {
return getC().List(params)
}
// List returns a list of invoices.
func (c Client) List(listParams *stripe.InvoiceListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.InvoiceList{}
err := c.B.CallRaw(http.MethodGet, "/v1/invoices", c.Key, b, p, list)
ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
ret[i] = v
}
return ret, list.ListMeta, err
})}
}
// ListLines returns a list of line items on an invoice.
func ListLines(params *stripe.InvoiceLineListParams) *LineIter {
return getC().ListLines(params)
}
// ListLines returns a list of line items on an invoice.
func (c Client) ListLines(listParams *stripe.InvoiceLineListParams) *LineIter {
path := stripe.FormatURLPath("/v1/invoices/%s/lines", stripe.StringValue(listParams.ID))
return &LineIter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.InvoiceLineList{}
err := c.B.CallRaw(http.MethodGet, path, c.Key, b, p, list)
ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
ret[i] = v
}
return ret, list.ListMeta, err
})}
}
// FinalizeInvoice finalizes an invoice.
func FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error) {
return getC().FinalizeInvoice(id, params)
}
// FinalizeInvoice finalizes an invoice.
func (c Client) FinalizeInvoice(id string, params *stripe.InvoiceFinalizeParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s/finalize", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, path, c.Key, params, invoice)
return invoice, err
}
// MarkUncollectible marks an invoice as uncollectible.
func MarkUncollectible(id string, params *stripe.InvoiceMarkUncollectibleParams) (*stripe.Invoice, error) {
return getC().MarkUncollectible(id, params)
}
// MarkUncollectible marks an invoice as uncollectible.
func (c Client) MarkUncollectible(id string, params *stripe.InvoiceMarkUncollectibleParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s/mark_uncollectible", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, path, c.Key, params, invoice)
return invoice, err
}
// SendInvoice sends an invoice.
func SendInvoice(id string, params *stripe.InvoiceSendParams) (*stripe.Invoice, error) {
return getC().SendInvoice(id, params)
}
// SendInvoice sends an invoice.
func (c Client) SendInvoice(id string, params *stripe.InvoiceSendParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s/send", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, path, c.Key, params, invoice)
return invoice, err
}
// VoidInvoice voids an invoice.
func VoidInvoice(id string, params *stripe.InvoiceVoidParams) (*stripe.Invoice, error) {
return getC().VoidInvoice(id, params)
}
// VoidInvoice voids an invoice.
func (c Client) VoidInvoice(id string, params *stripe.InvoiceVoidParams) (*stripe.Invoice, error) {
path := stripe.FormatURLPath("/v1/invoices/%s/void", id)
invoice := &stripe.Invoice{}
err := c.B.Call(http.MethodPost, path, c.Key, params, invoice)
return invoice, err
}
// Iter is an iterator for invoices.
type Iter struct {
*stripe.Iter
}
// Invoice returns the invoice which the iterator is currently pointing to.
func (i *Iter) Invoice() *stripe.Invoice {
return i.Current().(*stripe.Invoice)
}
// LineIter is an iterator for line items on an invoice.
type LineIter struct {
*stripe.Iter
}
// InvoiceLine returns the line item which the iterator is currently pointing to.
func (i *LineIter) InvoiceLine() *stripe.InvoiceLine {
return i.Current().(*stripe.InvoiceLine)
}
func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}