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 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 All 212 releases
charitable / vendor / square / square / src / Disputes / Evidence / EvidenceClient.php

EvidenceClient.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.13, at vendor/square/square/src/Disputes/Evidence/EvidenceClient.php

263 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Square\Disputes\Evidence;
4
5 use GuzzleHttp\ClientInterface;
6 use Square\Core\Client\RawClient;
7 use Square\Disputes\Evidence\Requests\ListEvidenceRequest;
8 use Square\Core\Pagination\Pager;
9 use Square\Types\DisputeEvidence;
10 use Square\Core\Pagination\CursorPager;
11 use Square\Types\ListDisputeEvidenceResponse;
12 use Square\Disputes\Evidence\Requests\GetEvidenceRequest;
13 use Square\Types\GetDisputeEvidenceResponse;
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\Disputes\Evidence\Requests\DeleteEvidenceRequest;
23 use Square\Types\DeleteDisputeEvidenceResponse;
24
25 class EvidenceClient
26 {
27 /**
28 * @var array{
29 * baseUrl?: string,
30 * client?: ClientInterface,
31 * maxRetries?: int,
32 * timeout?: float,
33 * headers?: array<string, string>,
34 * } $options
35 */
36 private array $options;
37
38 /**
39 * @var RawClient $client
40 */
41 private RawClient $client;
42
43 /**
44 * @param RawClient $client
45 * @param ?array{
46 * baseUrl?: string,
47 * client?: ClientInterface,
48 * maxRetries?: int,
49 * timeout?: float,
50 * headers?: array<string, string>,
51 * } $options
52 */
53 public function __construct(
54 RawClient $client,
55 ?array $options = null,
56 ) {
57 $this->client = $client;
58 $this->options = $options ?? [];
59 }
60
61 /**
62 * Returns a list of evidence associated with a dispute.
63 *
64 * @param ListEvidenceRequest $request
65 * @param ?array{
66 * baseUrl?: string,
67 * maxRetries?: int,
68 * timeout?: float,
69 * headers?: array<string, string>,
70 * queryParameters?: array<string, mixed>,
71 * bodyProperties?: array<string, mixed>,
72 * } $options
73 * @return Pager<DisputeEvidence>
74 */
75 public function list(ListEvidenceRequest $request, ?array $options = null): Pager
76 {
77 return new CursorPager(
78 request: $request,
79 getNextPage: fn (ListEvidenceRequest $request) => $this->_list($request, $options),
80 setCursor: function (ListEvidenceRequest $request, ?string $cursor) {
81 $request->setCursor($cursor);
82 },
83 /* @phpstan-ignore-next-line */
84 getNextCursor: fn (ListDisputeEvidenceResponse $response) => $response?->getCursor() ?? null,
85 /* @phpstan-ignore-next-line */
86 getItems: fn (ListDisputeEvidenceResponse $response) => $response?->getEvidence() ?? [],
87 );
88 }
89
90 /**
91 * Returns the metadata for the evidence specified in the request URL path.
92 *
93 * You must maintain a copy of any evidence uploaded if you want to reference it later. Evidence cannot be downloaded after you upload it.
94 *
95 * @param GetEvidenceRequest $request
96 * @param ?array{
97 * baseUrl?: string,
98 * maxRetries?: int,
99 * timeout?: float,
100 * headers?: array<string, string>,
101 * queryParameters?: array<string, mixed>,
102 * bodyProperties?: array<string, mixed>,
103 * } $options
104 * @return GetDisputeEvidenceResponse
105 * @throws SquareException
106 * @throws SquareApiException
107 */
108 public function get(GetEvidenceRequest $request, ?array $options = null): GetDisputeEvidenceResponse
109 {
110 $options = array_merge($this->options, $options ?? []);
111 try {
112 $response = $this->client->sendRequest(
113 new JsonApiRequest(
114 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
115 path: "v2/disputes/{$request->getDisputeId()}/evidence/{$request->getEvidenceId()}",
116 method: HttpMethod::GET,
117 ),
118 $options,
119 );
120 $statusCode = $response->getStatusCode();
121 if ($statusCode >= 200 && $statusCode < 400) {
122 $json = $response->getBody()->getContents();
123 return GetDisputeEvidenceResponse::fromJson($json);
124 }
125 } catch (JsonException $e) {
126 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
127 } catch (RequestException $e) {
128 $response = $e->getResponse();
129 if ($response === null) {
130 throw new SquareException(message: $e->getMessage(), previous: $e);
131 }
132 throw new SquareApiException(
133 message: "API request failed",
134 statusCode: $response->getStatusCode(),
135 body: $response->getBody()->getContents(),
136 );
137 } catch (ClientExceptionInterface $e) {
138 throw new SquareException(message: $e->getMessage(), previous: $e);
139 }
140 throw new SquareApiException(
141 message: 'API request failed',
142 statusCode: $statusCode,
143 body: $response->getBody()->getContents(),
144 );
145 }
146
147 /**
148 * Removes specified evidence from a dispute.
149 * Square does not send the bank any evidence that is removed.
150 *
151 * @param DeleteEvidenceRequest $request
152 * @param ?array{
153 * baseUrl?: string,
154 * maxRetries?: int,
155 * timeout?: float,
156 * headers?: array<string, string>,
157 * queryParameters?: array<string, mixed>,
158 * bodyProperties?: array<string, mixed>,
159 * } $options
160 * @return DeleteDisputeEvidenceResponse
161 * @throws SquareException
162 * @throws SquareApiException
163 */
164 public function delete(DeleteEvidenceRequest $request, ?array $options = null): DeleteDisputeEvidenceResponse
165 {
166 $options = array_merge($this->options, $options ?? []);
167 try {
168 $response = $this->client->sendRequest(
169 new JsonApiRequest(
170 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
171 path: "v2/disputes/{$request->getDisputeId()}/evidence/{$request->getEvidenceId()}",
172 method: HttpMethod::DELETE,
173 ),
174 $options,
175 );
176 $statusCode = $response->getStatusCode();
177 if ($statusCode >= 200 && $statusCode < 400) {
178 $json = $response->getBody()->getContents();
179 return DeleteDisputeEvidenceResponse::fromJson($json);
180 }
181 } catch (JsonException $e) {
182 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
183 } catch (RequestException $e) {
184 $response = $e->getResponse();
185 if ($response === null) {
186 throw new SquareException(message: $e->getMessage(), previous: $e);
187 }
188 throw new SquareApiException(
189 message: "API request failed",
190 statusCode: $response->getStatusCode(),
191 body: $response->getBody()->getContents(),
192 );
193 } catch (ClientExceptionInterface $e) {
194 throw new SquareException(message: $e->getMessage(), previous: $e);
195 }
196 throw new SquareApiException(
197 message: 'API request failed',
198 statusCode: $statusCode,
199 body: $response->getBody()->getContents(),
200 );
201 }
202
203 /**
204 * Returns a list of evidence associated with a dispute.
205 *
206 * @param ListEvidenceRequest $request
207 * @param ?array{
208 * baseUrl?: string,
209 * maxRetries?: int,
210 * timeout?: float,
211 * headers?: array<string, string>,
212 * queryParameters?: array<string, mixed>,
213 * bodyProperties?: array<string, mixed>,
214 * } $options
215 * @return ListDisputeEvidenceResponse
216 * @throws SquareException
217 * @throws SquareApiException
218 */
219 private function _list(ListEvidenceRequest $request, ?array $options = null): ListDisputeEvidenceResponse
220 {
221 $options = array_merge($this->options, $options ?? []);
222 $query = [];
223 if ($request->getCursor() != null) {
224 $query['cursor'] = $request->getCursor();
225 }
226 try {
227 $response = $this->client->sendRequest(
228 new JsonApiRequest(
229 baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value,
230 path: "v2/disputes/{$request->getDisputeId()}/evidence",
231 method: HttpMethod::GET,
232 query: $query,
233 ),
234 $options,
235 );
236 $statusCode = $response->getStatusCode();
237 if ($statusCode >= 200 && $statusCode < 400) {
238 $json = $response->getBody()->getContents();
239 return ListDisputeEvidenceResponse::fromJson($json);
240 }
241 } catch (JsonException $e) {
242 throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e);
243 } catch (RequestException $e) {
244 $response = $e->getResponse();
245 if ($response === null) {
246 throw new SquareException(message: $e->getMessage(), previous: $e);
247 }
248 throw new SquareApiException(
249 message: "API request failed",
250 statusCode: $response->getStatusCode(),
251 body: $response->getBody()->getContents(),
252 );
253 } catch (ClientExceptionInterface $e) {
254 throw new SquareException(message: $e->getMessage(), previous: $e);
255 }
256 throw new SquareApiException(
257 message: 'API request failed',
258 statusCode: $statusCode,
259 body: $response->getBody()->getContents(),
260 );
261 }
262 }
263