This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
forked from plaid/plaid-go
/
payment.go
286 lines (243 loc) · 7.27 KB
/
payment.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package plaid
import (
"encoding/json"
"fmt"
"time"
)
type PaymentRecipientAddress struct {
// Street is an array with length in range [1, 4].
Street []string `json:"street"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
// Country is an uppercase ISO 3166-1 alpha-2 country code.
Country string `json:"country"`
}
type createPaymentRecipientRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
Name string `json:"name"`
IBAN *string `json:"iban",omitempty`
Address *PaymentRecipientAddress `json:"address",omitempty`
BACS *PaymentRecipientBacs `json:"bacs",omitempty`
}
type OptionalRecipientCreateParams struct {
Address *PaymentRecipientAddress
BACS *PaymentRecipientBacs
IBAN *string
}
type PaymentRecipientBacs struct {
Account string `json:"account"`
SortCode string `json:"sort_code"`
}
type CreatePaymentRecipientResponse struct {
APIResponse
RecipientID string `json:"recipient_id"`
}
func (c *Client) CreatePaymentRecipient(
name string,
params OptionalRecipientCreateParams,
) (resp CreatePaymentRecipientResponse, err error) {
jsonBody, err := json.Marshal(createPaymentRecipientRequest{
ClientID: c.clientID,
Secret: c.secret,
Name: name,
Address: params.Address,
IBAN: params.IBAN,
BACS: params.BACS,
})
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/recipient/create", jsonBody, &resp)
return resp, err
}
type getPaymentRecipientRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
RecipientID string `json:"recipient_id"`
}
type Recipient struct {
RecipientID string `json:"recipient_id"`
Name string `json:"name"`
IBAN *string `json:"iban",omitempty`
Address *PaymentRecipientAddress `json:"address"`
BACS *PaymentRecipientBacs `json:"bacs",omitempty`
}
type GetPaymentRecipientResponse struct {
APIResponse
Recipient
}
func (c *Client) GetPaymentRecipient(recipientID string) (resp GetPaymentRecipientResponse, err error) {
jsonBody, err := json.Marshal(getPaymentRecipientRequest{
ClientID: c.clientID,
Secret: c.secret,
RecipientID: recipientID,
})
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/recipient/get", jsonBody, &resp)
return resp, err
}
type listPaymentRecipientsRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
}
type ListPaymentRecipientsResponse struct {
APIResponse
Recipients []Recipient `json:"recipients"`
}
func (c *Client) ListPaymentRecipients() (resp ListPaymentRecipientsResponse, err error) {
jsonBody, err := json.Marshal(listPaymentRecipientsRequest{
ClientID: c.clientID,
Secret: c.secret,
})
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/recipient/list", jsonBody, &resp)
return resp, err
}
type PaymentAmount struct {
Currency string `json:"currency"`
Value float64 `json:"value"`
}
type PaymentSchedule struct {
Interval string `json:"interval"`
IntervalExecutionDay int `json:"interval_execution_day"`
StartDate string `json:"start_date"`
}
type createPaymentRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
RecipientID string `json:"recipient_id"`
Reference string `json:"reference"`
Amount PaymentAmount `json:"amount"`
}
type createStandingOrderRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
RecipientID string `json:"recipient_id"`
Reference string `json:"reference"`
Amount PaymentAmount `json:"amount"`
Schedule *PaymentSchedule `json:"schedule"`
}
type CreatePaymentResponse struct {
APIResponse
PaymentID string `json:"payment_id"`
Status string `json:"status"`
}
func (c *Client) CreatePayment(
recipientID string,
reference string,
amount PaymentAmount,
schedule *PaymentSchedule,
) (resp CreatePaymentResponse, err error) {
var jsonBody []byte
if schedule == nil {
jsonBody, err = json.Marshal(createPaymentRequest{
ClientID: c.clientID,
Secret: c.secret,
RecipientID: recipientID,
Reference: reference,
Amount: amount,
})
} else {
jsonBody, err = json.Marshal(createStandingOrderRequest{
ClientID: c.clientID,
Secret: c.secret,
RecipientID: recipientID,
Reference: reference,
Amount: amount,
Schedule: schedule,
})
}
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/payment/create", jsonBody, &resp)
return resp, err
}
type createPaymentTokenRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
PaymentID string `json:"payment_id"`
}
type CreatePaymentTokenResponse struct {
APIResponse
PaymentToken string `json:"payment_token"`
PaymentTokenExpirationTime time.Time `json:"payment_token_expiration_time"`
}
func (c *Client) CreatePaymentToken(
paymentID string,
) (resp CreatePaymentTokenResponse, err error) {
fmt.Println("Warning: this method will be deprecated in a future version. To replace the payment_token, look into the link_token at https://plaid.com/docs/api/tokens/#linktokencreate.")
jsonBody, err := json.Marshal(createPaymentTokenRequest{
ClientID: c.clientID,
Secret: c.secret,
PaymentID: paymentID,
})
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/payment/token/create", jsonBody, &resp)
return resp, err
}
type getPaymentRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
PaymentID string `json:"payment_id"`
}
type Payment struct {
PaymentID string `json:"payment_id"`
Reference string `json:"reference"`
Amount PaymentAmount `json:"amount"`
Schedule *PaymentSchedule `json:"schedule"`
Status string `json:"status"`
LastStatusUpdate time.Time `json:"last_status_update"`
RecipientID string `json:"recipient_id"`
}
type GetPaymentResponse struct {
APIResponse
Payment
}
func (c *Client) GetPayment(paymentID string) (resp GetPaymentResponse, err error) {
jsonBody, err := json.Marshal(getPaymentRequest{
ClientID: c.clientID,
Secret: c.secret,
PaymentID: paymentID,
})
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/payment/get", jsonBody, &resp)
return resp, err
}
type listPaymentsRequest struct {
ClientID string `json:"client_id"`
Secret string `json:"secret"`
Count *int `json:"count"`
Cursor *string `json:"cursor"`
}
type ListPaymentsResponse struct {
APIResponse
Payments []Payment `json:"payments"`
NextCursor string `json:"next_cursor"`
}
type ListPaymentsOptions struct {
Count *int
Cursor *string
}
func (c *Client) ListPayments(options ListPaymentsOptions) (resp ListPaymentsResponse, err error) {
jsonBody, err := json.Marshal(listPaymentsRequest{
ClientID: c.clientID,
Secret: c.secret,
Count: options.Count,
Cursor: options.Cursor,
})
if err != nil {
return resp, err
}
err = c.Call("/payment_initiation/payment/list", jsonBody, &resp)
return resp, err
}