diff --git a/src/API/Endpoints/Endpoint.php b/src/API/Endpoints/Endpoint.php index 5754031..5d4d002 100644 --- a/src/API/Endpoints/Endpoint.php +++ b/src/API/Endpoints/Endpoint.php @@ -4,6 +4,8 @@ use InvalidArgumentException; use YouCan\Pay\API\APIServiceInterface; +use YouCan\Pay\API\Exceptions\Keys\UnsetPrivateKeyException; +use YouCan\Pay\API\Exceptions\Keys\UnsetPublicKeyException; abstract class Endpoint { @@ -35,14 +37,14 @@ protected function createEndpoint(): string protected function assertPrivateKeyIsSet(): void { if ($this->apiService->getPrivateKey() === null) { - throw new InvalidArgumentException("private key not set"); + throw new UnsetPrivateKeyException("private key not set"); } } protected function assertPublicKeyIsSet(): void { if ($this->apiService->getPublicKey() === null) { - throw new InvalidArgumentException("public key not set"); + throw new UnsetPublicKeyException("public key not set"); } } } diff --git a/src/API/Endpoints/KeysEndpoint.php b/src/API/Endpoints/KeysEndpoint.php index a1cc167..5e5fa2b 100644 --- a/src/API/Endpoints/KeysEndpoint.php +++ b/src/API/Endpoints/KeysEndpoint.php @@ -3,6 +3,7 @@ namespace YouCan\Pay\API\Endpoints; use YouCan\Pay\API\Exceptions\InvalidResponseException; +use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Response; class KeysEndpoint extends Endpoint @@ -13,7 +14,7 @@ class KeysEndpoint extends Endpoint * @param string|null $privateKey * @param string|null $publicKey * @return bool - * @throws InvalidResponseException + * @throws ServerException */ public function check(?string $privateKey = null, ?string $publicKey = null): bool { @@ -40,7 +41,7 @@ protected function endpoint(): string /** * @param Response $response * @return bool - * @throws InvalidResponseException + * @throws ServerException */ private function assertResponse(Response $response): bool { @@ -49,10 +50,10 @@ private function assertResponse(Response $response): bool } if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) { - throw new InvalidResponseException( - $response->getStatusCode(), + throw new ServerException( + 'internal error from server. Support has been notified. Please try again!', json_encode($response->getResponse()), - 'internal error from server. Support has been notified. Please try again!' + $response->getStatusCode(), ); } diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 5427d48..38356c7 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -2,8 +2,10 @@ namespace YouCan\Pay\API\Endpoints; -use InvalidArgumentException; -use YouCan\Pay\API\Exceptions\InvalidResponseException; +use YouCan\Pay\API\Exceptions\ServerException; +use YouCan\Pay\API\Exceptions\Token\MissingTokenException; +use YouCan\Pay\API\Exceptions\UnexpectedResultException; +use YouCan\Pay\API\Exceptions\UnsupportedResponseException; use YouCan\Pay\API\Exceptions\ValidationException; use YouCan\Pay\API\Response; use YouCan\Pay\Models\Token; @@ -51,16 +53,16 @@ protected function endpoint(): string /** * @param Response $response * - * @throws ValidationException|InvalidArgumentException + * @throws MissingTokenException|ValidationException|UnexpectedResultException|ServerException|UnsupportedResponseException */ private function assertResponse(Response $response): void { if ($response->getStatusCode() === 200) { if (!is_array($response->get('token')) || !is_string($response->get('token')['id'])) { - throw new InvalidResponseException( - $response->getStatusCode(), + throw new MissingTokenException( + 'missing token in response. Please try again or contact support', json_encode($response->getResponse()), - 'missing token in response. Please try again or contact support' + $response->getStatusCode(), ); } @@ -69,34 +71,42 @@ private function assertResponse(Response $response): void if ($response->getStatusCode() === 404) { if ($response->get('success') === false && is_string($response->get('message'))) { - throw new ValidationException((string)$response->get('message')); + throw new ValidationException( + (string)$response->get('message'), + json_encode($response->getResponse()), + $response->getStatusCode(), + ); } } if ($response->getStatusCode() === 422) { if ($response->get('success') === false && is_string($response->get('message'))) { - throw new ValidationException((string)$response->get('message')); + throw new ValidationException( + (string)$response->get('message'), + json_encode($response->getResponse()), + $response->getStatusCode(), + ); } - throw new InvalidResponseException( - $response->getStatusCode(), + throw new UnexpectedResultException( + 'got unexpected result from server. Validation response with wrong payload', json_encode($response->getResponse()), - 'got unexpected result from server. Validation response with wrong payload' + $response->getStatusCode() ); } if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) { - throw new InvalidResponseException( - $response->getStatusCode(), + throw new ServerException( + 'internal error from server. Support has been notified. Please try again!', json_encode($response->getResponse()), - 'internal error from server. Support has been notified. Please try again!' + $response->getStatusCode(), ); } - throw new InvalidResponseException( - $response->getStatusCode(), + throw new UnsupportedResponseException( + 'not supported status code from the server.', json_encode($response->getResponse()), - 'not supported status code from the server.' + $response->getStatusCode(), ); } } diff --git a/src/API/Exceptions/BaseException.php b/src/API/Exceptions/BaseException.php new file mode 100644 index 0000000..3362af8 --- /dev/null +++ b/src/API/Exceptions/BaseException.php @@ -0,0 +1,32 @@ +response = $response; + } + + public function getResponse() + { + return $this->response; + } + + public function hasResponse(): bool + { + return $this->response !== null; + } +} diff --git a/src/API/Exceptions/ClientException.php b/src/API/Exceptions/ClientException.php new file mode 100644 index 0000000..30d79af --- /dev/null +++ b/src/API/Exceptions/ClientException.php @@ -0,0 +1,10 @@ +expectException(InvalidResponseException::class); + $this->expectException(ServerException::class); $response = new Response( 500,