| 1 |
<?php |
| 2 |
|
| 3 |
namespace Square\Locations\Transactions; |
| 4 |
|
| 5 |
use GuzzleHttp\ClientInterface; |
| 6 |
use Square\Core\Client\RawClient; |
| 7 |
use Square\Locations\Transactions\Requests\ListTransactionsRequest; |
| 8 |
use Square\Types\ListTransactionsResponse; |
| 9 |
use Square\Exceptions\SquareException; |
| 10 |
use Square\Exceptions\SquareApiException; |
| 11 |
use Square\Core\Json\JsonApiRequest; |
| 12 |
use Square\Environments; |
| 13 |
use Square\Core\Client\HttpMethod; |
| 14 |
use JsonException; |
| 15 |
use GuzzleHttp\Exception\RequestException; |
| 16 |
use Psr\Http\Client\ClientExceptionInterface; |
| 17 |
use Square\Locations\Transactions\Requests\GetTransactionsRequest; |
| 18 |
use Square\Types\GetTransactionResponse; |
| 19 |
use Square\Locations\Transactions\Requests\CaptureTransactionsRequest; |
| 20 |
use Square\Types\CaptureTransactionResponse; |
| 21 |
use Square\Locations\Transactions\Requests\VoidTransactionsRequest; |
| 22 |
use Square\Types\VoidTransactionResponse; |
| 23 |
|
| 24 |
class TransactionsClient |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var array{ |
| 28 |
* baseUrl?: string, |
| 29 |
* client?: ClientInterface, |
| 30 |
* maxRetries?: int, |
| 31 |
* timeout?: float, |
| 32 |
* headers?: array<string, string>, |
| 33 |
* } $options |
| 34 |
*/ |
| 35 |
private array $options; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var RawClient $client |
| 39 |
*/ |
| 40 |
private RawClient $client; |
| 41 |
|
| 42 |
/** |
| 43 |
* @param RawClient $client |
| 44 |
* @param ?array{ |
| 45 |
* baseUrl?: string, |
| 46 |
* client?: ClientInterface, |
| 47 |
* maxRetries?: int, |
| 48 |
* timeout?: float, |
| 49 |
* headers?: array<string, string>, |
| 50 |
* } $options |
| 51 |
*/ |
| 52 |
public function __construct( |
| 53 |
RawClient $client, |
| 54 |
?array $options = null, |
| 55 |
) { |
| 56 |
$this->client = $client; |
| 57 |
$this->options = $options ?? []; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Lists transactions for a particular location. |
| 62 |
* |
| 63 |
* Transactions include payment information from sales and exchanges and refund |
| 64 |
* information from returns and exchanges. |
| 65 |
* |
| 66 |
* Max results per [page](https://developer.squareup.com/docs/working-with-apis/pagination): 50 |
| 67 |
* |
| 68 |
* @param ListTransactionsRequest $request |
| 69 |
* @param ?array{ |
| 70 |
* baseUrl?: string, |
| 71 |
* maxRetries?: int, |
| 72 |
* timeout?: float, |
| 73 |
* headers?: array<string, string>, |
| 74 |
* queryParameters?: array<string, mixed>, |
| 75 |
* bodyProperties?: array<string, mixed>, |
| 76 |
* } $options |
| 77 |
* @return ListTransactionsResponse |
| 78 |
* @throws SquareException |
| 79 |
* @throws SquareApiException |
| 80 |
*/ |
| 81 |
public function list(ListTransactionsRequest $request, ?array $options = null): ListTransactionsResponse |
| 82 |
{ |
| 83 |
$options = array_merge($this->options, $options ?? []); |
| 84 |
$query = []; |
| 85 |
if ($request->getBeginTime() != null) { |
| 86 |
$query['begin_time'] = $request->getBeginTime(); |
| 87 |
} |
| 88 |
if ($request->getEndTime() != null) { |
| 89 |
$query['end_time'] = $request->getEndTime(); |
| 90 |
} |
| 91 |
if ($request->getSortOrder() != null) { |
| 92 |
$query['sort_order'] = $request->getSortOrder(); |
| 93 |
} |
| 94 |
if ($request->getCursor() != null) { |
| 95 |
$query['cursor'] = $request->getCursor(); |
| 96 |
} |
| 97 |
try { |
| 98 |
$response = $this->client->sendRequest( |
| 99 |
new JsonApiRequest( |
| 100 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 101 |
path: "v2/locations/{$request->getLocationId()}/transactions", |
| 102 |
method: HttpMethod::GET, |
| 103 |
query: $query, |
| 104 |
), |
| 105 |
$options, |
| 106 |
); |
| 107 |
$statusCode = $response->getStatusCode(); |
| 108 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 109 |
$json = $response->getBody()->getContents(); |
| 110 |
return ListTransactionsResponse::fromJson($json); |
| 111 |
} |
| 112 |
} catch (JsonException $e) { |
| 113 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 114 |
} catch (RequestException $e) { |
| 115 |
$response = $e->getResponse(); |
| 116 |
if ($response === null) { |
| 117 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 118 |
} |
| 119 |
throw new SquareApiException( |
| 120 |
message: "API request failed", |
| 121 |
statusCode: $response->getStatusCode(), |
| 122 |
body: $response->getBody()->getContents(), |
| 123 |
); |
| 124 |
} catch (ClientExceptionInterface $e) { |
| 125 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 126 |
} |
| 127 |
throw new SquareApiException( |
| 128 |
message: 'API request failed', |
| 129 |
statusCode: $statusCode, |
| 130 |
body: $response->getBody()->getContents(), |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Retrieves details for a single transaction. |
| 136 |
* |
| 137 |
* @param GetTransactionsRequest $request |
| 138 |
* @param ?array{ |
| 139 |
* baseUrl?: string, |
| 140 |
* maxRetries?: int, |
| 141 |
* timeout?: float, |
| 142 |
* headers?: array<string, string>, |
| 143 |
* queryParameters?: array<string, mixed>, |
| 144 |
* bodyProperties?: array<string, mixed>, |
| 145 |
* } $options |
| 146 |
* @return GetTransactionResponse |
| 147 |
* @throws SquareException |
| 148 |
* @throws SquareApiException |
| 149 |
*/ |
| 150 |
public function get(GetTransactionsRequest $request, ?array $options = null): GetTransactionResponse |
| 151 |
{ |
| 152 |
$options = array_merge($this->options, $options ?? []); |
| 153 |
try { |
| 154 |
$response = $this->client->sendRequest( |
| 155 |
new JsonApiRequest( |
| 156 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 157 |
path: "v2/locations/{$request->getLocationId()}/transactions/{$request->getTransactionId()}", |
| 158 |
method: HttpMethod::GET, |
| 159 |
), |
| 160 |
$options, |
| 161 |
); |
| 162 |
$statusCode = $response->getStatusCode(); |
| 163 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 164 |
$json = $response->getBody()->getContents(); |
| 165 |
return GetTransactionResponse::fromJson($json); |
| 166 |
} |
| 167 |
} catch (JsonException $e) { |
| 168 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 169 |
} catch (RequestException $e) { |
| 170 |
$response = $e->getResponse(); |
| 171 |
if ($response === null) { |
| 172 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 173 |
} |
| 174 |
throw new SquareApiException( |
| 175 |
message: "API request failed", |
| 176 |
statusCode: $response->getStatusCode(), |
| 177 |
body: $response->getBody()->getContents(), |
| 178 |
); |
| 179 |
} catch (ClientExceptionInterface $e) { |
| 180 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 181 |
} |
| 182 |
throw new SquareApiException( |
| 183 |
message: 'API request failed', |
| 184 |
statusCode: $statusCode, |
| 185 |
body: $response->getBody()->getContents(), |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Captures a transaction that was created with the [Charge](api-endpoint:Transactions-Charge) |
| 191 |
* endpoint with a `delay_capture` value of `true`. |
| 192 |
* |
| 193 |
* |
| 194 |
* See [Delayed capture transactions](https://developer.squareup.com/docs/payments/transactions/overview#delayed-capture) |
| 195 |
* for more information. |
| 196 |
* |
| 197 |
* @param CaptureTransactionsRequest $request |
| 198 |
* @param ?array{ |
| 199 |
* baseUrl?: string, |
| 200 |
* maxRetries?: int, |
| 201 |
* timeout?: float, |
| 202 |
* headers?: array<string, string>, |
| 203 |
* queryParameters?: array<string, mixed>, |
| 204 |
* bodyProperties?: array<string, mixed>, |
| 205 |
* } $options |
| 206 |
* @return CaptureTransactionResponse |
| 207 |
* @throws SquareException |
| 208 |
* @throws SquareApiException |
| 209 |
*/ |
| 210 |
public function capture(CaptureTransactionsRequest $request, ?array $options = null): CaptureTransactionResponse |
| 211 |
{ |
| 212 |
$options = array_merge($this->options, $options ?? []); |
| 213 |
try { |
| 214 |
$response = $this->client->sendRequest( |
| 215 |
new JsonApiRequest( |
| 216 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 217 |
path: "v2/locations/{$request->getLocationId()}/transactions/{$request->getTransactionId()}/capture", |
| 218 |
method: HttpMethod::POST, |
| 219 |
), |
| 220 |
$options, |
| 221 |
); |
| 222 |
$statusCode = $response->getStatusCode(); |
| 223 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 224 |
$json = $response->getBody()->getContents(); |
| 225 |
return CaptureTransactionResponse::fromJson($json); |
| 226 |
} |
| 227 |
} catch (JsonException $e) { |
| 228 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 229 |
} catch (RequestException $e) { |
| 230 |
$response = $e->getResponse(); |
| 231 |
if ($response === null) { |
| 232 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 233 |
} |
| 234 |
throw new SquareApiException( |
| 235 |
message: "API request failed", |
| 236 |
statusCode: $response->getStatusCode(), |
| 237 |
body: $response->getBody()->getContents(), |
| 238 |
); |
| 239 |
} catch (ClientExceptionInterface $e) { |
| 240 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 241 |
} |
| 242 |
throw new SquareApiException( |
| 243 |
message: 'API request failed', |
| 244 |
statusCode: $statusCode, |
| 245 |
body: $response->getBody()->getContents(), |
| 246 |
); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Cancels a transaction that was created with the [Charge](api-endpoint:Transactions-Charge) |
| 251 |
* endpoint with a `delay_capture` value of `true`. |
| 252 |
* |
| 253 |
* |
| 254 |
* See [Delayed capture transactions](https://developer.squareup.com/docs/payments/transactions/overview#delayed-capture) |
| 255 |
* for more information. |
| 256 |
* |
| 257 |
* @param VoidTransactionsRequest $request |
| 258 |
* @param ?array{ |
| 259 |
* baseUrl?: string, |
| 260 |
* maxRetries?: int, |
| 261 |
* timeout?: float, |
| 262 |
* headers?: array<string, string>, |
| 263 |
* queryParameters?: array<string, mixed>, |
| 264 |
* bodyProperties?: array<string, mixed>, |
| 265 |
* } $options |
| 266 |
* @return VoidTransactionResponse |
| 267 |
* @throws SquareException |
| 268 |
* @throws SquareApiException |
| 269 |
*/ |
| 270 |
public function void(VoidTransactionsRequest $request, ?array $options = null): VoidTransactionResponse |
| 271 |
{ |
| 272 |
$options = array_merge($this->options, $options ?? []); |
| 273 |
try { |
| 274 |
$response = $this->client->sendRequest( |
| 275 |
new JsonApiRequest( |
| 276 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 277 |
path: "v2/locations/{$request->getLocationId()}/transactions/{$request->getTransactionId()}/void", |
| 278 |
method: HttpMethod::POST, |
| 279 |
), |
| 280 |
$options, |
| 281 |
); |
| 282 |
$statusCode = $response->getStatusCode(); |
| 283 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 284 |
$json = $response->getBody()->getContents(); |
| 285 |
return VoidTransactionResponse::fromJson($json); |
| 286 |
} |
| 287 |
} catch (JsonException $e) { |
| 288 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 289 |
} catch (RequestException $e) { |
| 290 |
$response = $e->getResponse(); |
| 291 |
if ($response === null) { |
| 292 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 293 |
} |
| 294 |
throw new SquareApiException( |
| 295 |
message: "API request failed", |
| 296 |
statusCode: $response->getStatusCode(), |
| 297 |
body: $response->getBody()->getContents(), |
| 298 |
); |
| 299 |
} catch (ClientExceptionInterface $e) { |
| 300 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 301 |
} |
| 302 |
throw new SquareApiException( |
| 303 |
message: 'API request failed', |
| 304 |
statusCode: $statusCode, |
| 305 |
body: $response->getBody()->getContents(), |
| 306 |
); |
| 307 |
} |
| 308 |
} |
| 309 |
|