PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.13
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.13
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / vendor / square / square / src / Webhooks / Subscriptions / SubscriptionsClient.php

SubscriptionsClient.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at vendor/square/square/src/Webhooks/Subscriptions/SubscriptionsClient.php

501 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Square\Webhooks\Subscriptions;
4
5 use GuzzleHttp\ClientInterface;
6 use Square\Core\Client\RawClient;
7 use Square\Webhooks\Subscriptions\Requests\ListSubscriptionsRequest;
8 use Square\Core\Pagination\Pager;
9 use Square\Types\WebhookSubscription;
10 use Square\Core\Pagination\CursorPager;
11 use Square\Types\ListWebhookSubscriptionsResponse;
12 use Square\Webhooks\Subscriptions\Requests\CreateWebhookSubscriptionRequest;
13 use Square\Types\CreateWebhookSubscriptionResponse;
14 use Square\Exceptions\SquareException;
15 use Square\Exceptions\SquareApiException;
16 use Square\Core\Json\JsonApiRequest;
17 use Square\Environments;
18 use Square\Core\Client\HttpMethod;
19 use JsonException;
20 use GuzzleHttp\Exception\RequestException;
21 use Psr\Http\Client\ClientExceptionInterface;
22 use Square\Webhooks\Subscriptions\Requests\GetSubscriptionsRequest;
23 use Square\Types\GetWebhookSubscriptionResponse;
24 use Square\Webhooks\Subscriptions\Requests\UpdateWebhookSubscriptionRequest;
25 use Square\Types\UpdateWebhookSubscriptionResponse;
26 use Square\Webhooks\Subscriptions\Requests\DeleteSubscriptionsRequest;
27 use Square\Types\DeleteWebhookSubscriptionResponse;
28 use Square\Webhooks\Subscriptions\Requests\UpdateWebhookSubscriptionSignatureKeyRequest;
29 use Square\Types\UpdateWebhookSubscriptionSignatureKeyResponse;
30 use Square\Webhooks\Subscriptions\Requests\TestWebhookSubscriptionRequest;
31 use Square\Types\TestWebhookSubscriptionResponse;
32
33 class SubscriptionsClient
34 {
35 /**
36 * @var array{
37 * baseUrl?: string,
38 * client?: ClientInterface,
39 * maxRetries?: int,
40 * timeout?: float,
41 * headers?: array<string, string>,
42 * } $options
43 */
44 private array $options;
45
46 /**
47 * @var RawClient $client
48 */
49 private RawClient $client;
50
51 /**
52 * @param RawClient $client
53 * @param ?array{
54 * baseUrl?: string,
55 * client?: ClientInterface,
56 * maxRetries?: int,
57 * timeout?: float,
58 * headers?: array<string, string>,
59 * } $options
60 */
61 public function __construct(
62 RawClient $client,
63 ?array $options = null,
64 ) {
65 $this->client = $client;
66 $this->options = $options ?? [];
67 }
68
69 /**
70 * Lists all webhook subscriptions owned by your application.
71 *
72 * @param ListSubscriptionsRequest $request
73 * @param ?array{
74 * baseUrl?: string,
75 * maxRetries?: int,
76 * timeout?: float,
77 * headers?: array<string, string>,
78 * queryParameters?: array<string, mixed>,
79 * bodyProperties?: array<string, mixed>,
80 * } $options
81 * @return Pager<WebhookSubscription>
82 */
83 public function list(ListSubscriptionsRequest $request = new ListSubscriptionsRequest(), ?array $options = null): Pager
84 {
85 return new CursorPager(
86 request: $request,
87 getNextPage: fn (ListSubscriptionsRequest $request) => $this->_list($request, $options),
88 setCursor: function (ListSubscriptionsRequest $request, ?string $cursor) {
89 $request->setCursor($cursor);
90 },
91 /* @phpstan-ignore-next-line */
92 getNextCursor: fn (ListWebhookSubscriptionsResponse $response) => $response?->getCursor() ?? null,
93 /* @phpstan-ignore-next-line */
94 getItems: fn (ListWebhookSubscriptionsResponse $response) => $response?->getSubscriptions() ?? [],
95 );
96 }
97
98 /**
99 * Creates a webhook subscription.
100 *
101 * @param CreateWebhookSubscriptionRequest $request
102 * @param ?array{
103 * baseUrl?: string,
104 * maxRetries?: int,
105 * timeout?: float,
106 * headers?: array<string, string>,
107 * queryParameters?: array<string, mixed>,
108 * bodyProperties?: array<string, mixed>,
109 * } $options
110 * @return CreateWebhookSubscriptionResponse
111 * @throws SquareException
112 * @throws SquareApiException
113 */
114 public function create(CreateWebhookSubscriptionRequest $request, ?array $options = null): CreateWebhookSubscriptionResponse
115 {
116 $options = array_merge($this->options, $options ?? []);
117 try {
118 $response = $this->client->sendRequest(
119 new JsonApiRequest(
120 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
121 path: "v2/webhooks/subscriptions",
122 method: HttpMethod::POST,
123 body: $request,
124 ),
125 $options,
126 );
127 $statusCode = $response->getStatusCode();
128 if ($statusCode >= 200 && $statusCode < 400) {
129 $json = $response->getBody()->getContents();
130 return CreateWebhookSubscriptionResponse::fromJson($json);
131 }
132 } catch (JsonException $e) {
133 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
134 } catch (RequestException $e) {
135 $response = $e->getResponse();
136 if ($response === null) {
137 throw new SquareException(message: $e->getMessage(), previous: $e);
138 }
139 throw new SquareApiException(
140 message: "API request failed",
141 statusCode: $response->getStatusCode(),
142 body: $response->getBody()->getContents(),
143 );
144 } catch (ClientExceptionInterface $e) {
145 throw new SquareException(message: $e->getMessage(), previous: $e);
146 }
147 throw new SquareApiException(
148 message: 'API request failed',
149 statusCode: $statusCode,
150 body: $response->getBody()->getContents(),
151 );
152 }
153
154 /**
155 * Retrieves a webhook subscription identified by its ID.
156 *
157 * @param GetSubscriptionsRequest $request
158 * @param ?array{
159 * baseUrl?: string,
160 * maxRetries?: int,
161 * timeout?: float,
162 * headers?: array<string, string>,
163 * queryParameters?: array<string, mixed>,
164 * bodyProperties?: array<string, mixed>,
165 * } $options
166 * @return GetWebhookSubscriptionResponse
167 * @throws SquareException
168 * @throws SquareApiException
169 */
170 public function get(GetSubscriptionsRequest $request, ?array $options = null): GetWebhookSubscriptionResponse
171 {
172 $options = array_merge($this->options, $options ?? []);
173 try {
174 $response = $this->client->sendRequest(
175 new JsonApiRequest(
176 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
177 path: "v2/webhooks/subscriptions/{$request->getSubscriptionId()}",
178 method: HttpMethod::GET,
179 ),
180 $options,
181 );
182 $statusCode = $response->getStatusCode();
183 if ($statusCode >= 200 && $statusCode < 400) {
184 $json = $response->getBody()->getContents();
185 return GetWebhookSubscriptionResponse::fromJson($json);
186 }
187 } catch (JsonException $e) {
188 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
189 } catch (RequestException $e) {
190 $response = $e->getResponse();
191 if ($response === null) {
192 throw new SquareException(message: $e->getMessage(), previous: $e);
193 }
194 throw new SquareApiException(
195 message: "API request failed",
196 statusCode: $response->getStatusCode(),
197 body: $response->getBody()->getContents(),
198 );
199 } catch (ClientExceptionInterface $e) {
200 throw new SquareException(message: $e->getMessage(), previous: $e);
201 }
202 throw new SquareApiException(
203 message: 'API request failed',
204 statusCode: $statusCode,
205 body: $response->getBody()->getContents(),
206 );
207 }
208
209 /**
210 * Updates a webhook subscription.
211 *
212 * @param UpdateWebhookSubscriptionRequest $request
213 * @param ?array{
214 * baseUrl?: string,
215 * maxRetries?: int,
216 * timeout?: float,
217 * headers?: array<string, string>,
218 * queryParameters?: array<string, mixed>,
219 * bodyProperties?: array<string, mixed>,
220 * } $options
221 * @return UpdateWebhookSubscriptionResponse
222 * @throws SquareException
223 * @throws SquareApiException
224 */
225 public function update(UpdateWebhookSubscriptionRequest $request, ?array $options = null): UpdateWebhookSubscriptionResponse
226 {
227 $options = array_merge($this->options, $options ?? []);
228 try {
229 $response = $this->client->sendRequest(
230 new JsonApiRequest(
231 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
232 path: "v2/webhooks/subscriptions/{$request->getSubscriptionId()}",
233 method: HttpMethod::PUT,
234 body: $request,
235 ),
236 $options,
237 );
238 $statusCode = $response->getStatusCode();
239 if ($statusCode >= 200 && $statusCode < 400) {
240 $json = $response->getBody()->getContents();
241 return UpdateWebhookSubscriptionResponse::fromJson($json);
242 }
243 } catch (JsonException $e) {
244 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
245 } catch (RequestException $e) {
246 $response = $e->getResponse();
247 if ($response === null) {
248 throw new SquareException(message: $e->getMessage(), previous: $e);
249 }
250 throw new SquareApiException(
251 message: "API request failed",
252 statusCode: $response->getStatusCode(),
253 body: $response->getBody()->getContents(),
254 );
255 } catch (ClientExceptionInterface $e) {
256 throw new SquareException(message: $e->getMessage(), previous: $e);
257 }
258 throw new SquareApiException(
259 message: 'API request failed',
260 statusCode: $statusCode,
261 body: $response->getBody()->getContents(),
262 );
263 }
264
265 /**
266 * Deletes a webhook subscription.
267 *
268 * @param DeleteSubscriptionsRequest $request
269 * @param ?array{
270 * baseUrl?: string,
271 * maxRetries?: int,
272 * timeout?: float,
273 * headers?: array<string, string>,
274 * queryParameters?: array<string, mixed>,
275 * bodyProperties?: array<string, mixed>,
276 * } $options
277 * @return DeleteWebhookSubscriptionResponse
278 * @throws SquareException
279 * @throws SquareApiException
280 */
281 public function delete(DeleteSubscriptionsRequest $request, ?array $options = null): DeleteWebhookSubscriptionResponse
282 {
283 $options = array_merge($this->options, $options ?? []);
284 try {
285 $response = $this->client->sendRequest(
286 new JsonApiRequest(
287 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
288 path: "v2/webhooks/subscriptions/{$request->getSubscriptionId()}",
289 method: HttpMethod::DELETE,
290 ),
291 $options,
292 );
293 $statusCode = $response->getStatusCode();
294 if ($statusCode >= 200 && $statusCode < 400) {
295 $json = $response->getBody()->getContents();
296 return DeleteWebhookSubscriptionResponse::fromJson($json);
297 }
298 } catch (JsonException $e) {
299 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
300 } catch (RequestException $e) {
301 $response = $e->getResponse();
302 if ($response === null) {
303 throw new SquareException(message: $e->getMessage(), previous: $e);
304 }
305 throw new SquareApiException(
306 message: "API request failed",
307 statusCode: $response->getStatusCode(),
308 body: $response->getBody()->getContents(),
309 );
310 } catch (ClientExceptionInterface $e) {
311 throw new SquareException(message: $e->getMessage(), previous: $e);
312 }
313 throw new SquareApiException(
314 message: 'API request failed',
315 statusCode: $statusCode,
316 body: $response->getBody()->getContents(),
317 );
318 }
319
320 /**
321 * Updates a webhook subscription by replacing the existing signature key with a new one.
322 *
323 * @param UpdateWebhookSubscriptionSignatureKeyRequest $request
324 * @param ?array{
325 * baseUrl?: string,
326 * maxRetries?: int,
327 * timeout?: float,
328 * headers?: array<string, string>,
329 * queryParameters?: array<string, mixed>,
330 * bodyProperties?: array<string, mixed>,
331 * } $options
332 * @return UpdateWebhookSubscriptionSignatureKeyResponse
333 * @throws SquareException
334 * @throws SquareApiException
335 */
336 public function updateSignatureKey(UpdateWebhookSubscriptionSignatureKeyRequest $request, ?array $options = null): UpdateWebhookSubscriptionSignatureKeyResponse
337 {
338 $options = array_merge($this->options, $options ?? []);
339 try {
340 $response = $this->client->sendRequest(
341 new JsonApiRequest(
342 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
343 path: "v2/webhooks/subscriptions/{$request->getSubscriptionId()}/signature-key",
344 method: HttpMethod::POST,
345 body: $request,
346 ),
347 $options,
348 );
349 $statusCode = $response->getStatusCode();
350 if ($statusCode >= 200 && $statusCode < 400) {
351 $json = $response->getBody()->getContents();
352 return UpdateWebhookSubscriptionSignatureKeyResponse::fromJson($json);
353 }
354 } catch (JsonException $e) {
355 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
356 } catch (RequestException $e) {
357 $response = $e->getResponse();
358 if ($response === null) {
359 throw new SquareException(message: $e->getMessage(), previous: $e);
360 }
361 throw new SquareApiException(
362 message: "API request failed",
363 statusCode: $response->getStatusCode(),
364 body: $response->getBody()->getContents(),
365 );
366 } catch (ClientExceptionInterface $e) {
367 throw new SquareException(message: $e->getMessage(), previous: $e);
368 }
369 throw new SquareApiException(
370 message: 'API request failed',
371 statusCode: $statusCode,
372 body: $response->getBody()->getContents(),
373 );
374 }
375
376 /**
377 * Tests a webhook subscription by sending a test event to the notification URL.
378 *
379 * @param TestWebhookSubscriptionRequest $request
380 * @param ?array{
381 * baseUrl?: string,
382 * maxRetries?: int,
383 * timeout?: float,
384 * headers?: array<string, string>,
385 * queryParameters?: array<string, mixed>,
386 * bodyProperties?: array<string, mixed>,
387 * } $options
388 * @return TestWebhookSubscriptionResponse
389 * @throws SquareException
390 * @throws SquareApiException
391 */
392 public function test(TestWebhookSubscriptionRequest $request, ?array $options = null): TestWebhookSubscriptionResponse
393 {
394 $options = array_merge($this->options, $options ?? []);
395 try {
396 $response = $this->client->sendRequest(
397 new JsonApiRequest(
398 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
399 path: "v2/webhooks/subscriptions/{$request->getSubscriptionId()}/test",
400 method: HttpMethod::POST,
401 body: $request,
402 ),
403 $options,
404 );
405 $statusCode = $response->getStatusCode();
406 if ($statusCode >= 200 && $statusCode < 400) {
407 $json = $response->getBody()->getContents();
408 return TestWebhookSubscriptionResponse::fromJson($json);
409 }
410 } catch (JsonException $e) {
411 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
412 } catch (RequestException $e) {
413 $response = $e->getResponse();
414 if ($response === null) {
415 throw new SquareException(message: $e->getMessage(), previous: $e);
416 }
417 throw new SquareApiException(
418 message: "API request failed",
419 statusCode: $response->getStatusCode(),
420 body: $response->getBody()->getContents(),
421 );
422 } catch (ClientExceptionInterface $e) {
423 throw new SquareException(message: $e->getMessage(), previous: $e);
424 }
425 throw new SquareApiException(
426 message: 'API request failed',
427 statusCode: $statusCode,
428 body: $response->getBody()->getContents(),
429 );
430 }
431
432 /**
433 * Lists all webhook subscriptions owned by your application.
434 *
435 * @param ListSubscriptionsRequest $request
436 * @param ?array{
437 * baseUrl?: string,
438 * maxRetries?: int,
439 * timeout?: float,
440 * headers?: array<string, string>,
441 * queryParameters?: array<string, mixed>,
442 * bodyProperties?: array<string, mixed>,
443 * } $options
444 * @return ListWebhookSubscriptionsResponse
445 * @throws SquareException
446 * @throws SquareApiException
447 */
448 private function _list(ListSubscriptionsRequest $request = new ListSubscriptionsRequest(), ?array $options = null): ListWebhookSubscriptionsResponse
449 {
450 $options = array_merge($this->options, $options ?? []);
451 $query = [];
452 if ($request->getCursor() != null) {
453 $query['cursor'] = $request->getCursor();
454 }
455 if ($request->getIncludeDisabled() != null) {
456 $query['include_disabled'] = $request->getIncludeDisabled();
457 }
458 if ($request->getSortOrder() != null) {
459 $query['sort_order'] = $request->getSortOrder();
460 }
461 if ($request->getLimit() != null) {
462 $query['limit'] = $request->getLimit();
463 }
464 try {
465 $response = $this->client->sendRequest(
466 new JsonApiRequest(
467 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
468 path: "v2/webhooks/subscriptions",
469 method: HttpMethod::GET,
470 query: $query,
471 ),
472 $options,
473 );
474 $statusCode = $response->getStatusCode();
475 if ($statusCode >= 200 && $statusCode < 400) {
476 $json = $response->getBody()->getContents();
477 return ListWebhookSubscriptionsResponse::fromJson($json);
478 }
479 } catch (JsonException $e) {
480 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
481 } catch (RequestException $e) {
482 $response = $e->getResponse();
483 if ($response === null) {
484 throw new SquareException(message: $e->getMessage(), previous: $e);
485 }
486 throw new SquareApiException(
487 message: "API request failed",
488 statusCode: $response->getStatusCode(),
489 body: $response->getBody()->getContents(),
490 );
491 } catch (ClientExceptionInterface $e) {
492 throw new SquareException(message: $e->getMessage(), previous: $e);
493 }
494 throw new SquareApiException(
495 message: 'API request failed',
496 statusCode: $statusCode,
497 body: $response->getBody()->getContents(),
498 );
499 }
500 }
501