-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBankAccountsClient.php
More file actions
332 lines (321 loc) · 12.5 KB
/
BankAccountsClient.php
File metadata and controls
332 lines (321 loc) · 12.5 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
namespace Square\BankAccounts;
use Psr\Http\Client\ClientInterface;
use Square\Core\Client\RawClient;
use Square\BankAccounts\Requests\ListBankAccountsRequest;
use Square\Core\Pagination\Pager;
use Square\Types\BankAccount;
use Square\Core\Pagination\CursorPager;
use Square\Types\ListBankAccountsResponse;
use Square\BankAccounts\Requests\CreateBankAccountRequest;
use Square\Types\CreateBankAccountResponse;
use Square\Exceptions\SquareException;
use Square\Exceptions\SquareApiException;
use Square\Core\Json\JsonApiRequest;
use Square\Environments;
use Square\Core\Client\HttpMethod;
use JsonException;
use Psr\Http\Client\ClientExceptionInterface;
use Square\BankAccounts\Requests\GetByV1IdBankAccountsRequest;
use Square\Types\GetBankAccountByV1IdResponse;
use Square\BankAccounts\Requests\GetBankAccountsRequest;
use Square\Types\GetBankAccountResponse;
use Square\BankAccounts\Requests\DisableBankAccountRequest;
use Square\Types\DisableBankAccountResponse;
class BankAccountsClient
{
/**
* @var array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options @phpstan-ignore-next-line Property is used in endpoint methods via HttpEndpointGenerator
*/
private array $options;
/**
* @var RawClient $client
*/
private RawClient $client;
/**
* @param RawClient $client
* @param ?array{
* baseUrl?: string,
* client?: ClientInterface,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* } $options
*/
public function __construct(
RawClient $client,
?array $options = null,
) {
$this->client = $client;
$this->options = $options ?? [];
}
/**
* Returns a list of [BankAccount](entity:BankAccount) objects linked to a Square account.
*
* @param ListBankAccountsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return Pager<BankAccount>
*/
public function list(ListBankAccountsRequest $request = new ListBankAccountsRequest(), ?array $options = null): Pager
{
return new CursorPager(
request: $request,
getNextPage: fn (ListBankAccountsRequest $request) => $this->_list($request, $options),
setCursor: function (ListBankAccountsRequest $request, ?string $cursor) {
$request->setCursor($cursor);
},
/* @phpstan-ignore-next-line */
getNextCursor: fn (ListBankAccountsResponse $response) => $response?->getCursor() ?? null,
/* @phpstan-ignore-next-line */
getItems: fn (ListBankAccountsResponse $response) => $response?->getBankAccounts() ?? [],
);
}
/**
* Store a bank account on file for a square account
*
* @param CreateBankAccountRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return CreateBankAccountResponse
* @throws SquareException
* @throws SquareApiException
*/
public function createBankAccount(CreateBankAccountRequest $request, ?array $options = null): CreateBankAccountResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/bank-accounts",
method: HttpMethod::POST,
body: $request,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return CreateBankAccountResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
/**
* Returns details of a [BankAccount](entity:BankAccount) identified by V1 bank account ID.
*
* @param GetByV1IdBankAccountsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return GetBankAccountByV1IdResponse
* @throws SquareException
* @throws SquareApiException
*/
public function getByV1Id(GetByV1IdBankAccountsRequest $request, ?array $options = null): GetBankAccountByV1IdResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/bank-accounts/by-v1-id/{$request->getV1BankAccountId()}",
method: HttpMethod::GET,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return GetBankAccountByV1IdResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
/**
* Retrieve details of a [BankAccount](entity:BankAccount) bank account linked to a Square account.
*
* @param GetBankAccountsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return GetBankAccountResponse
* @throws SquareException
* @throws SquareApiException
*/
public function get(GetBankAccountsRequest $request, ?array $options = null): GetBankAccountResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/bank-accounts/{$request->getBankAccountId()}",
method: HttpMethod::GET,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return GetBankAccountResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
/**
* Disable a bank account.
*
* @param DisableBankAccountRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return DisableBankAccountResponse
* @throws SquareException
* @throws SquareApiException
*/
public function disableBankAccount(DisableBankAccountRequest $request, ?array $options = null): DisableBankAccountResponse
{
$options = array_merge($this->options, $options ?? []);
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/bank-accounts/{$request->getBankAccountId()}/disable",
method: HttpMethod::POST,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return DisableBankAccountResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
/**
* Returns a list of [BankAccount](entity:BankAccount) objects linked to a Square account.
*
* @param ListBankAccountsRequest $request
* @param ?array{
* baseUrl?: string,
* maxRetries?: int,
* timeout?: float,
* headers?: array<string, string>,
* queryParameters?: array<string, mixed>,
* bodyProperties?: array<string, mixed>,
* } $options
* @return ListBankAccountsResponse
* @throws SquareException
* @throws SquareApiException
*/
private function _list(ListBankAccountsRequest $request = new ListBankAccountsRequest(), ?array $options = null): ListBankAccountsResponse
{
$options = array_merge($this->options, $options ?? []);
$query = [];
if ($request->getCursor() != null) {
$query['cursor'] = $request->getCursor();
}
if ($request->getLimit() != null) {
$query['limit'] = $request->getLimit();
}
if ($request->getLocationId() != null) {
$query['location_id'] = $request->getLocationId();
}
if ($request->getCustomerId() != null) {
$query['customer_id'] = $request->getCustomerId();
}
try {
$response = $this->client->sendRequest(
new JsonApiRequest(
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
path: "v2/bank-accounts",
method: HttpMethod::GET,
query: $query,
),
$options,
);
$statusCode = $response->getStatusCode();
if ($statusCode >= 200 && $statusCode < 400) {
$json = $response->getBody()->getContents();
return ListBankAccountsResponse::fromJson($json);
}
} catch (JsonException $e) {
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
} catch (ClientExceptionInterface $e) {
throw new SquareException(message: $e->getMessage(), previous: $e);
}
throw new SquareApiException(
message: 'API request failed',
statusCode: $statusCode,
body: $response->getBody()->getContents(),
);
}
}