forked from stripe/stripe-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
152 lines (118 loc) · 4.13 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
// Package bankaccount provides the /bank_accounts APIs
package bankaccount
import (
"fmt"
"net/url"
"strconv"
stripe "github.com/stripe/stripe-go"
)
// Client is used to invoke /bank_accounts APIs.
type Client struct {
B stripe.Backend
Key string
}
const (
NewAccount stripe.BankAccountStatus = "new"
VerifiedAccount stripe.BankAccountStatus = "verified"
ValidatedAccount stripe.BankAccountStatus = "validated"
ErroredAccount stripe.BankAccountStatus = "errored"
)
// New POSTs a new bank account.
func New(params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
return getC().New(params)
}
func (c Client) New(params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
body := &url.Values{}
// Use token (if exists) or a dictionary containing a user’s bank account details.
if len(params.Token) > 0 {
body.Add("external_account", params.Token)
} else {
body.Add("external_account[object]", "bank_account")
body.Add("external_account[country]", params.Country)
body.Add("external_account[account_number]", params.Account)
body.Add("external_account[routing_number]", params.Routing)
body.Add("external_account[currency]", params.Currency)
if params.Default {
body.Add("external_account[default_for_currency]", strconv.FormatBool(params.Default))
}
}
params.AppendTo(body)
ba := &stripe.BankAccount{}
err := c.B.Call("POST", fmt.Sprintf("/accounts/%v/bank_accounts", params.AccountID), c.Key, body, ¶ms.Params, ba)
return ba, err
}
// Get returns the details of a bank account.
func Get(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
return getC().Get(id, params)
}
func (c Client) Get(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
var body *url.Values
var commonParams *stripe.Params
if params != nil {
commonParams = ¶ms.Params
body = &url.Values{}
params.AppendTo(body)
}
ba := &stripe.BankAccount{}
err := c.B.Call("GET", fmt.Sprintf("/accounts/%v/bank_accounts/%v", params.AccountID, id), c.Key, body, commonParams, ba)
return ba, err
}
// Update updates a bank account.
func Update(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
return getC().Update(id, params)
}
func (c Client) Update(id string, params *stripe.BankAccountParams) (*stripe.BankAccount, error) {
var body *url.Values
var commonParams *stripe.Params
if params != nil {
commonParams = ¶ms.Params
body = &url.Values{}
if params.Default {
body.Add("default_for_currency", strconv.FormatBool(params.Default))
}
params.AppendTo(body)
}
ba := &stripe.BankAccount{}
err := c.B.Call("POST", fmt.Sprintf("/accounts/%v/bank_accounts/%v", params.AccountID, id), c.Key, body, commonParams, ba)
return ba, err
}
// Del removes a bank account.
func Del(id string, params *stripe.BankAccountParams) error {
return getC().Del(id, params)
}
func (c Client) Del(id string, params *stripe.BankAccountParams) error {
return c.B.Call("DELETE", fmt.Sprintf("/accounts/%v/bank_accounts/%v", params.AccountID, id), c.Key, nil, nil, nil)
}
// List returns a list of bank accounts.
func List(params *stripe.BankAccountListParams) *Iter {
return getC().List(params)
}
func (c Client) List(params *stripe.BankAccountListParams) *Iter {
body := &url.Values{}
var lp *stripe.ListParams
params.AppendTo(body)
lp = ¶ms.ListParams
return &Iter{stripe.GetIter(lp, body, func(b url.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.BankAccountList{}
err := c.B.Call("GET", fmt.Sprintf("/accounts/%v/bank_accounts", params.AccountID), c.Key, &b, nil, list)
ret := make([]interface{}, len(list.Values))
for i, v := range list.Values {
ret[i] = v
}
return ret, list.ListMeta, err
})}
}
// Iter is an iterator for lists of BankAccount.
// The embedded Iter carries methods with it;
// see its documentation for details.
type Iter struct {
*stripe.Iter
}
// BankAccount returns the most recent BankAccount
// visited by a call to Next.
func (i *Iter) BankAccount() *stripe.BankAccount {
return i.Current().(*stripe.BankAccount)
}
func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}