forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
143 lines (117 loc) · 4.17 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
// 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, "/invoices", 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("/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("/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("/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, "/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, "/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("/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
})}
}
// 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}
}