| 1 |
<?php |
| 2 |
|
| 3 |
namespace Square\Terminal; |
| 4 |
|
| 5 |
use Square\Terminal\Actions\ActionsClient; |
| 6 |
use Square\Terminal\Checkouts\CheckoutsClient; |
| 7 |
use Square\Terminal\Refunds\RefundsClient; |
| 8 |
use GuzzleHttp\ClientInterface; |
| 9 |
use Square\Core\Client\RawClient; |
| 10 |
use Square\Terminal\Requests\DismissTerminalActionRequest; |
| 11 |
use Square\Types\DismissTerminalActionResponse; |
| 12 |
use Square\Exceptions\SquareException; |
| 13 |
use Square\Exceptions\SquareApiException; |
| 14 |
use Square\Core\Json\JsonApiRequest; |
| 15 |
use Square\Environments; |
| 16 |
use Square\Core\Client\HttpMethod; |
| 17 |
use JsonException; |
| 18 |
use GuzzleHttp\Exception\RequestException; |
| 19 |
use Psr\Http\Client\ClientExceptionInterface; |
| 20 |
use Square\Terminal\Requests\DismissTerminalCheckoutRequest; |
| 21 |
use Square\Types\DismissTerminalCheckoutResponse; |
| 22 |
use Square\Terminal\Requests\DismissTerminalRefundRequest; |
| 23 |
use Square\Types\DismissTerminalRefundResponse; |
| 24 |
|
| 25 |
class TerminalClient |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var ActionsClient $actions |
| 29 |
*/ |
| 30 |
public ActionsClient $actions; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var CheckoutsClient $checkouts |
| 34 |
*/ |
| 35 |
public CheckoutsClient $checkouts; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var RefundsClient $refunds |
| 39 |
*/ |
| 40 |
public RefundsClient $refunds; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var array{ |
| 44 |
* baseUrl?: string, |
| 45 |
* client?: ClientInterface, |
| 46 |
* maxRetries?: int, |
| 47 |
* timeout?: float, |
| 48 |
* headers?: array<string, string>, |
| 49 |
* } $options |
| 50 |
*/ |
| 51 |
private array $options; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var RawClient $client |
| 55 |
*/ |
| 56 |
private RawClient $client; |
| 57 |
|
| 58 |
/** |
| 59 |
* @param RawClient $client |
| 60 |
* @param ?array{ |
| 61 |
* baseUrl?: string, |
| 62 |
* client?: ClientInterface, |
| 63 |
* maxRetries?: int, |
| 64 |
* timeout?: float, |
| 65 |
* headers?: array<string, string>, |
| 66 |
* } $options |
| 67 |
*/ |
| 68 |
public function __construct( |
| 69 |
RawClient $client, |
| 70 |
?array $options = null, |
| 71 |
) { |
| 72 |
$this->client = $client; |
| 73 |
$this->options = $options ?? []; |
| 74 |
$this->actions = new ActionsClient($this->client, $this->options); |
| 75 |
$this->checkouts = new CheckoutsClient($this->client, $this->options); |
| 76 |
$this->refunds = new RefundsClient($this->client, $this->options); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Dismisses a Terminal action request if the status and type of the request permits it. |
| 81 |
* |
| 82 |
* See [Link and Dismiss Actions](https://developer.squareup.com/docs/terminal-api/advanced-features/custom-workflows/link-and-dismiss-actions) for more details. |
| 83 |
* |
| 84 |
* @param DismissTerminalActionRequest $request |
| 85 |
* @param ?array{ |
| 86 |
* baseUrl?: string, |
| 87 |
* maxRetries?: int, |
| 88 |
* timeout?: float, |
| 89 |
* headers?: array<string, string>, |
| 90 |
* queryParameters?: array<string, mixed>, |
| 91 |
* bodyProperties?: array<string, mixed>, |
| 92 |
* } $options |
| 93 |
* @return DismissTerminalActionResponse |
| 94 |
* @throws SquareException |
| 95 |
* @throws SquareApiException |
| 96 |
*/ |
| 97 |
public function dismissTerminalAction(DismissTerminalActionRequest $request, ?array $options = null): DismissTerminalActionResponse |
| 98 |
{ |
| 99 |
$options = array_merge($this->options, $options ?? []); |
| 100 |
try { |
| 101 |
$response = $this->client->sendRequest( |
| 102 |
new JsonApiRequest( |
| 103 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 104 |
path: "v2/terminals/actions/{$request->getActionId()}/dismiss", |
| 105 |
method: HttpMethod::POST, |
| 106 |
), |
| 107 |
$options, |
| 108 |
); |
| 109 |
$statusCode = $response->getStatusCode(); |
| 110 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 111 |
$json = $response->getBody()->getContents(); |
| 112 |
return DismissTerminalActionResponse::fromJson($json); |
| 113 |
} |
| 114 |
} catch (JsonException $e) { |
| 115 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 116 |
} catch (RequestException $e) { |
| 117 |
$response = $e->getResponse(); |
| 118 |
if ($response === null) { |
| 119 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 120 |
} |
| 121 |
throw new SquareApiException( |
| 122 |
message: "API request failed", |
| 123 |
statusCode: $response->getStatusCode(), |
| 124 |
body: $response->getBody()->getContents(), |
| 125 |
); |
| 126 |
} catch (ClientExceptionInterface $e) { |
| 127 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 128 |
} |
| 129 |
throw new SquareApiException( |
| 130 |
message: 'API request failed', |
| 131 |
statusCode: $statusCode, |
| 132 |
body: $response->getBody()->getContents(), |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Dismisses a Terminal checkout request if the status and type of the request permits it. |
| 138 |
* |
| 139 |
* @param DismissTerminalCheckoutRequest $request |
| 140 |
* @param ?array{ |
| 141 |
* baseUrl?: string, |
| 142 |
* maxRetries?: int, |
| 143 |
* timeout?: float, |
| 144 |
* headers?: array<string, string>, |
| 145 |
* queryParameters?: array<string, mixed>, |
| 146 |
* bodyProperties?: array<string, mixed>, |
| 147 |
* } $options |
| 148 |
* @return DismissTerminalCheckoutResponse |
| 149 |
* @throws SquareException |
| 150 |
* @throws SquareApiException |
| 151 |
*/ |
| 152 |
public function dismissTerminalCheckout(DismissTerminalCheckoutRequest $request, ?array $options = null): DismissTerminalCheckoutResponse |
| 153 |
{ |
| 154 |
$options = array_merge($this->options, $options ?? []); |
| 155 |
try { |
| 156 |
$response = $this->client->sendRequest( |
| 157 |
new JsonApiRequest( |
| 158 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 159 |
path: "v2/terminals/checkouts/{$request->getCheckoutId()}/dismiss", |
| 160 |
method: HttpMethod::POST, |
| 161 |
), |
| 162 |
$options, |
| 163 |
); |
| 164 |
$statusCode = $response->getStatusCode(); |
| 165 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 166 |
$json = $response->getBody()->getContents(); |
| 167 |
return DismissTerminalCheckoutResponse::fromJson($json); |
| 168 |
} |
| 169 |
} catch (JsonException $e) { |
| 170 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 171 |
} catch (RequestException $e) { |
| 172 |
$response = $e->getResponse(); |
| 173 |
if ($response === null) { |
| 174 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 175 |
} |
| 176 |
throw new SquareApiException( |
| 177 |
message: "API request failed", |
| 178 |
statusCode: $response->getStatusCode(), |
| 179 |
body: $response->getBody()->getContents(), |
| 180 |
); |
| 181 |
} catch (ClientExceptionInterface $e) { |
| 182 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 183 |
} |
| 184 |
throw new SquareApiException( |
| 185 |
message: 'API request failed', |
| 186 |
statusCode: $statusCode, |
| 187 |
body: $response->getBody()->getContents(), |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Dismisses a Terminal refund request if the status and type of the request permits it. |
| 193 |
* |
| 194 |
* @param DismissTerminalRefundRequest $request |
| 195 |
* @param ?array{ |
| 196 |
* baseUrl?: string, |
| 197 |
* maxRetries?: int, |
| 198 |
* timeout?: float, |
| 199 |
* headers?: array<string, string>, |
| 200 |
* queryParameters?: array<string, mixed>, |
| 201 |
* bodyProperties?: array<string, mixed>, |
| 202 |
* } $options |
| 203 |
* @return DismissTerminalRefundResponse |
| 204 |
* @throws SquareException |
| 205 |
* @throws SquareApiException |
| 206 |
*/ |
| 207 |
public function dismissTerminalRefund(DismissTerminalRefundRequest $request, ?array $options = null): DismissTerminalRefundResponse |
| 208 |
{ |
| 209 |
$options = array_merge($this->options, $options ?? []); |
| 210 |
try { |
| 211 |
$response = $this->client->sendRequest( |
| 212 |
new JsonApiRequest( |
| 213 |
baseUrl: $options['baseUrl'] ?? $this->client->options['baseUrl'] ?? Environments::Production->value, |
| 214 |
path: "v2/terminals/refunds/{$request->getTerminalRefundId()}/dismiss", |
| 215 |
method: HttpMethod::POST, |
| 216 |
), |
| 217 |
$options, |
| 218 |
); |
| 219 |
$statusCode = $response->getStatusCode(); |
| 220 |
if ($statusCode >= 200 && $statusCode < 400) { |
| 221 |
$json = $response->getBody()->getContents(); |
| 222 |
return DismissTerminalRefundResponse::fromJson($json); |
| 223 |
} |
| 224 |
} catch (JsonException $e) { |
| 225 |
throw new SquareException(message: "Failed to deserialize response: {$e->getMessage()}", previous: $e); |
| 226 |
} catch (RequestException $e) { |
| 227 |
$response = $e->getResponse(); |
| 228 |
if ($response === null) { |
| 229 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 230 |
} |
| 231 |
throw new SquareApiException( |
| 232 |
message: "API request failed", |
| 233 |
statusCode: $response->getStatusCode(), |
| 234 |
body: $response->getBody()->getContents(), |
| 235 |
); |
| 236 |
} catch (ClientExceptionInterface $e) { |
| 237 |
throw new SquareException(message: $e->getMessage(), previous: $e); |
| 238 |
} |
| 239 |
throw new SquareApiException( |
| 240 |
message: 'API request failed', |
| 241 |
statusCode: $statusCode, |
| 242 |
body: $response->getBody()->getContents(), |
| 243 |
); |
| 244 |
} |
| 245 |
} |
| 246 |
|