From 144d587d84fc6614165572f0cbdaa9a9cce3d5eb Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Thu, 22 Jun 2023 15:27:31 +0100 Subject: [PATCH 01/36] create BaseException --- src/API/Exceptions/BaseException.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/API/Exceptions/BaseException.php diff --git a/src/API/Exceptions/BaseException.php b/src/API/Exceptions/BaseException.php new file mode 100644 index 0000000..fbfe480 --- /dev/null +++ b/src/API/Exceptions/BaseException.php @@ -0,0 +1,7 @@ + Date: Thu, 22 Jun 2023 15:48:15 +0100 Subject: [PATCH 02/36] create RequestException --- src/API/Exceptions/RequestException.php | 42 +++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/API/Exceptions/RequestException.php diff --git a/src/API/Exceptions/RequestException.php b/src/API/Exceptions/RequestException.php new file mode 100644 index 0000000..09b1d8e --- /dev/null +++ b/src/API/Exceptions/RequestException.php @@ -0,0 +1,42 @@ +request = $request; + $this->response = $response; + } + + public function getRequest() + { + return $this->request; + } + + public function getResponse() + { + return $this->response; + } + + public function hasResponse(): bool + { + return $this->response !== null; + } +} From 7dc5e33c7d701d74c0a2c4022121fa782ab676a9 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Thu, 22 Jun 2023 15:57:56 +0100 Subject: [PATCH 03/36] create BadResponseException --- src/API/Exceptions/BadResponseException.php | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/API/Exceptions/BadResponseException.php diff --git a/src/API/Exceptions/BadResponseException.php b/src/API/Exceptions/BadResponseException.php new file mode 100644 index 0000000..22d8d92 --- /dev/null +++ b/src/API/Exceptions/BadResponseException.php @@ -0,0 +1,28 @@ + Date: Thu, 22 Jun 2023 16:03:30 +0100 Subject: [PATCH 04/36] create ClientException --- src/API/Exceptions/ClientException.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/API/Exceptions/ClientException.php diff --git a/src/API/Exceptions/ClientException.php b/src/API/Exceptions/ClientException.php new file mode 100644 index 0000000..510cc63 --- /dev/null +++ b/src/API/Exceptions/ClientException.php @@ -0,0 +1,10 @@ + Date: Thu, 22 Jun 2023 16:05:15 +0100 Subject: [PATCH 05/36] create ServerException --- src/API/Exceptions/ServerException.php | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/API/Exceptions/ServerException.php diff --git a/src/API/Exceptions/ServerException.php b/src/API/Exceptions/ServerException.php new file mode 100644 index 0000000..932e8a6 --- /dev/null +++ b/src/API/Exceptions/ServerException.php @@ -0,0 +1,10 @@ + Date: Thu, 22 Jun 2023 16:17:51 +0100 Subject: [PATCH 06/36] create UnsetPublicKeyException --- src/API/Exceptions/Keys/UnsetPublicKeyException.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/API/Exceptions/Keys/UnsetPublicKeyException.php diff --git a/src/API/Exceptions/Keys/UnsetPublicKeyException.php b/src/API/Exceptions/Keys/UnsetPublicKeyException.php new file mode 100644 index 0000000..347d418 --- /dev/null +++ b/src/API/Exceptions/Keys/UnsetPublicKeyException.php @@ -0,0 +1,12 @@ + Date: Thu, 22 Jun 2023 16:39:50 +0100 Subject: [PATCH 07/36] create UnsetPrivateKeyException --- src/API/Exceptions/Keys/UnsetPrivateKeyException.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/API/Exceptions/Keys/UnsetPrivateKeyException.php diff --git a/src/API/Exceptions/Keys/UnsetPrivateKeyException.php b/src/API/Exceptions/Keys/UnsetPrivateKeyException.php new file mode 100644 index 0000000..d7135de --- /dev/null +++ b/src/API/Exceptions/Keys/UnsetPrivateKeyException.php @@ -0,0 +1,12 @@ + Date: Thu, 22 Jun 2023 17:32:16 +0100 Subject: [PATCH 08/36] change exception to UnsetPrivateKeyException in assertPrivateKeyIsSet method --- src/API/Endpoints/Endpoint.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/API/Endpoints/Endpoint.php b/src/API/Endpoints/Endpoint.php index 5754031..563ee00 100644 --- a/src/API/Endpoints/Endpoint.php +++ b/src/API/Endpoints/Endpoint.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use YouCan\Pay\API\APIServiceInterface; +use YouCan\Pay\API\Exceptions\Keys\UnsetPrivateKeyException; abstract class Endpoint { @@ -35,7 +36,7 @@ protected function createEndpoint(): string protected function assertPrivateKeyIsSet(): void { if ($this->apiService->getPrivateKey() === null) { - throw new InvalidArgumentException("private key not set"); + throw new UnsetPrivateKeyException(); } } From 010d2aff693cb7998c2d3f479677efd07725b5e5 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Thu, 22 Jun 2023 17:42:56 +0100 Subject: [PATCH 09/36] change exception to UnsetPublicKeyException in assertPublicKeyIsSet method --- src/API/Endpoints/Endpoint.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/API/Endpoints/Endpoint.php b/src/API/Endpoints/Endpoint.php index 563ee00..d348a44 100644 --- a/src/API/Endpoints/Endpoint.php +++ b/src/API/Endpoints/Endpoint.php @@ -5,6 +5,7 @@ use InvalidArgumentException; use YouCan\Pay\API\APIServiceInterface; use YouCan\Pay\API\Exceptions\Keys\UnsetPrivateKeyException; +use YouCan\Pay\API\Exceptions\Keys\UnsetPublicKeyException; abstract class Endpoint { @@ -43,7 +44,7 @@ protected function assertPrivateKeyIsSet(): void protected function assertPublicKeyIsSet(): void { if ($this->apiService->getPublicKey() === null) { - throw new InvalidArgumentException("public key not set"); + throw new UnsetPublicKeyException(); } } } From 7f11d9483da4939677bac7b01a76a9ac61fd99f7 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 11:35:07 +0100 Subject: [PATCH 10/36] remove the $request property from the RequestException --- src/API/Exceptions/RequestException.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/API/Exceptions/RequestException.php b/src/API/Exceptions/RequestException.php index 09b1d8e..f5ce27f 100644 --- a/src/API/Exceptions/RequestException.php +++ b/src/API/Exceptions/RequestException.php @@ -6,30 +6,20 @@ class RequestException extends BaseException { - /** @var string */ - protected $request; - /** @var string|null */ protected $response; public function __construct( string $message, - string $request, string $response = null, int $code = 0, Throwable $previous = null ) { parent::__construct($message, $code, $previous); - $this->request = $request; $this->response = $response; } - public function getRequest() - { - return $this->request; - } - public function getResponse() { return $this->response; From 62707ca92d45e5f200238138339b13781b1ac3ae Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 11:35:56 +0100 Subject: [PATCH 11/36] remove the $request property from the BadResponseException --- src/API/Exceptions/BadResponseException.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/API/Exceptions/BadResponseException.php b/src/API/Exceptions/BadResponseException.php index 22d8d92..37f843c 100644 --- a/src/API/Exceptions/BadResponseException.php +++ b/src/API/Exceptions/BadResponseException.php @@ -6,14 +6,14 @@ class BadResponseException extends RequestException { + public function __construct( string $message, - string $request, - string $response, - $code = 0, + string $response = null, + int $code = 0, Throwable $previous = null ) { - parent::__construct($message, $request, $response, $code, $previous); + parent::__construct($message, $response, $code, $previous); } public function hasResponse(): bool From 6ae515f43a79a9178a26b77a66251624b2bd912b Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 12:05:45 +0100 Subject: [PATCH 12/36] replace usages of InvalidResponseException with ServerException --- src/API/Endpoints/TokenEndpoint.php | 7 ++++--- tests/API/Endpoints/TokenEndpointTest.php | 5 ++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 5427d48..c4ba47b 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -4,6 +4,7 @@ use InvalidArgumentException; use YouCan\Pay\API\Exceptions\InvalidResponseException; +use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Exceptions\ValidationException; use YouCan\Pay\API\Response; use YouCan\Pay\Models\Token; @@ -86,10 +87,10 @@ private function assertResponse(Response $response): void } 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/tests/API/Endpoints/TokenEndpointTest.php b/tests/API/Endpoints/TokenEndpointTest.php index db0b3b6..d5803ac 100644 --- a/tests/API/Endpoints/TokenEndpointTest.php +++ b/tests/API/Endpoints/TokenEndpointTest.php @@ -5,8 +5,7 @@ use Tests\API\FakeAPIService; use Tests\BaseTestCase; use YouCan\Pay\API\Endpoints\TokenEndpoint; -use YouCan\Pay\API\Endpoints\TransactionEndpoint; -use YouCan\Pay\API\Exceptions\InvalidResponseException; +use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Exceptions\ValidationException; use YouCan\Pay\API\Response; @@ -87,7 +86,7 @@ public function test_validation_exception() public function test_internal_error() { - $this->expectException(InvalidResponseException::class); + $this->expectException(ServerException::class); $response = new Response( 500, From a6e43ac6f5565a5e7d6c942106e0689224caae62 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 12:16:10 +0100 Subject: [PATCH 13/36] replace usages of ValidationException with ClientException --- src/API/Endpoints/TokenEndpoint.php | 13 +++++++++++-- tests/API/Endpoints/TokenEndpointTest.php | 7 ++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index c4ba47b..ea08adb 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -3,6 +3,7 @@ namespace YouCan\Pay\API\Endpoints; use InvalidArgumentException; +use YouCan\Pay\API\Exceptions\ClientException; use YouCan\Pay\API\Exceptions\InvalidResponseException; use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Exceptions\ValidationException; @@ -70,13 +71,21 @@ 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 ClientException( + (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 ClientException( + (string)$response->get('message'), + json_encode($response->getResponse()), + $response->getStatusCode(), + ); } throw new InvalidResponseException( diff --git a/tests/API/Endpoints/TokenEndpointTest.php b/tests/API/Endpoints/TokenEndpointTest.php index d5803ac..3d1d42b 100644 --- a/tests/API/Endpoints/TokenEndpointTest.php +++ b/tests/API/Endpoints/TokenEndpointTest.php @@ -5,6 +5,7 @@ use Tests\API\FakeAPIService; use Tests\BaseTestCase; use YouCan\Pay\API\Endpoints\TokenEndpoint; +use YouCan\Pay\API\Exceptions\ClientException; use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Exceptions\ValidationException; use YouCan\Pay\API\Response; @@ -34,7 +35,7 @@ public function test_generate_token_successfully() public function test_validation_exception_amount_is_less_than_minimum() { - $this->expectException(ValidationException::class); + $this->expectException(ClientException::class); $response = new Response( 422, @@ -51,7 +52,7 @@ public function test_validation_exception_amount_is_less_than_minimum() public function test_validation_exception_amount_is_greater_than_maximum() { - $this->expectException(ValidationException::class); + $this->expectException(ClientException::class); $response = new Response( 422, @@ -69,7 +70,7 @@ public function test_validation_exception_amount_is_greater_than_maximum() public function test_validation_exception() { - $this->expectException(ValidationException::class); + $this->expectException(ClientException::class); $response = new Response( 422, From 3cf48c1663c6f8fd6feff2c114c900fa211ea357 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 14:32:16 +0100 Subject: [PATCH 14/36] create UnexpectedResultException --- src/API/Exceptions/UnexpectedResultException.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/API/Exceptions/UnexpectedResultException.php diff --git a/src/API/Exceptions/UnexpectedResultException.php b/src/API/Exceptions/UnexpectedResultException.php new file mode 100644 index 0000000..31d44c4 --- /dev/null +++ b/src/API/Exceptions/UnexpectedResultException.php @@ -0,0 +1,7 @@ + Date: Mon, 26 Jun 2023 14:41:25 +0100 Subject: [PATCH 15/36] replace usages of InvalidResponseException with UnexpectedResultException --- src/API/Endpoints/TokenEndpoint.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index ea08adb..85f810d 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -6,6 +6,7 @@ use YouCan\Pay\API\Exceptions\ClientException; use YouCan\Pay\API\Exceptions\InvalidResponseException; use YouCan\Pay\API\Exceptions\ServerException; +use YouCan\Pay\API\Exceptions\UnexpectedResultException; use YouCan\Pay\API\Exceptions\ValidationException; use YouCan\Pay\API\Response; use YouCan\Pay\Models\Token; @@ -88,10 +89,10 @@ private function assertResponse(Response $response): void ); } - 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() ); } From a6048047dc961627a3d41fa95dace4c59b48405f Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 14:49:59 +0100 Subject: [PATCH 16/36] create UnsupportedResponseException --- src/API/Exceptions/UnsupportedResponseException.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/API/Exceptions/UnsupportedResponseException.php diff --git a/src/API/Exceptions/UnsupportedResponseException.php b/src/API/Exceptions/UnsupportedResponseException.php new file mode 100644 index 0000000..855f767 --- /dev/null +++ b/src/API/Exceptions/UnsupportedResponseException.php @@ -0,0 +1,7 @@ + Date: Mon, 26 Jun 2023 15:00:40 +0100 Subject: [PATCH 17/36] replace InvalidResponseException with UnsupportedResponseException --- src/API/Endpoints/TokenEndpoint.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 85f810d..0bf41f5 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -7,6 +7,7 @@ use YouCan\Pay\API\Exceptions\InvalidResponseException; use YouCan\Pay\API\Exceptions\ServerException; 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; @@ -60,10 +61,10 @@ 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 UnsupportedResponseException( + '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(), ); } From 7e53431e1fa04e8e76cb851f8689030eecc449e7 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 15:41:09 +0100 Subject: [PATCH 18/36] replace InvalidResponseException with UnsupportedResponseException --- src/API/Endpoints/TokenEndpoint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 0bf41f5..54a25e3 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -105,10 +105,10 @@ private function assertResponse(Response $response): void ); } - throw new InvalidResponseException( + throw new UnsupportedResponseException( + 'not supported status code from the server.', $response->getStatusCode(), json_encode($response->getResponse()), - 'not supported status code from the server.' ); } } From 6bea64f3d7c0ae35b501ecd1f763cf97d9ec9e7f Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 15:48:54 +0100 Subject: [PATCH 19/36] create MissingTokenException --- src/API/Exceptions/Token/MissingTokenException.php | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/API/Exceptions/Token/MissingTokenException.php diff --git a/src/API/Exceptions/Token/MissingTokenException.php b/src/API/Exceptions/Token/MissingTokenException.php new file mode 100644 index 0000000..d5e4999 --- /dev/null +++ b/src/API/Exceptions/Token/MissingTokenException.php @@ -0,0 +1,7 @@ + Date: Mon, 26 Jun 2023 15:49:00 +0100 Subject: [PATCH 20/36] replace UnsupportedResponseException with MissingTokenException --- src/API/Endpoints/TokenEndpoint.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 54a25e3..66b5b8e 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -5,6 +5,7 @@ use InvalidArgumentException; use YouCan\Pay\API\Exceptions\ClientException; use YouCan\Pay\API\Exceptions\InvalidResponseException; +use YouCan\Pay\API\Exceptions\MissingTokenException; use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Exceptions\UnexpectedResultException; use YouCan\Pay\API\Exceptions\UnsupportedResponseException; @@ -61,7 +62,7 @@ private function assertResponse(Response $response): void { if ($response->getStatusCode() === 200) { if (!is_array($response->get('token')) || !is_string($response->get('token')['id'])) { - throw new UnsupportedResponseException( + throw new MissingTokenException( 'missing token in response. Please try again or contact support', json_encode($response->getResponse()), $response->getStatusCode(), From 2e2097807fc3cbc0f637b61ce0aa82ece5ba8a9b Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 15:54:18 +0100 Subject: [PATCH 21/36] update MissingTokenException --- src/API/Endpoints/TokenEndpoint.php | 3 +-- src/API/Exceptions/Token/MissingTokenException.php | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 66b5b8e..ead8b44 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -4,9 +4,8 @@ use InvalidArgumentException; use YouCan\Pay\API\Exceptions\ClientException; -use YouCan\Pay\API\Exceptions\InvalidResponseException; -use YouCan\Pay\API\Exceptions\MissingTokenException; 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; diff --git a/src/API/Exceptions/Token/MissingTokenException.php b/src/API/Exceptions/Token/MissingTokenException.php index d5e4999..3aa8ddc 100644 --- a/src/API/Exceptions/Token/MissingTokenException.php +++ b/src/API/Exceptions/Token/MissingTokenException.php @@ -1,6 +1,8 @@ Date: Mon, 26 Jun 2023 16:14:59 +0100 Subject: [PATCH 22/36] update BadResponseException --- src/API/Exceptions/BadResponseException.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/API/Exceptions/BadResponseException.php b/src/API/Exceptions/BadResponseException.php index 37f843c..c45f751 100644 --- a/src/API/Exceptions/BadResponseException.php +++ b/src/API/Exceptions/BadResponseException.php @@ -6,7 +6,6 @@ class BadResponseException extends RequestException { - public function __construct( string $message, string $response = null, From e9a23d528843e360136c046041dc4fb266b7dcf8 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 16:18:26 +0100 Subject: [PATCH 23/36] update TokenEndpoint --- src/API/Endpoints/TokenEndpoint.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index ead8b44..15fde68 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -107,8 +107,8 @@ private function assertResponse(Response $response): void throw new UnsupportedResponseException( 'not supported status code from the server.', - $response->getStatusCode(), json_encode($response->getResponse()), + $response->getStatusCode(), ); } } From 2194096d687a43379b10cf8034b6d6a3578536ba Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 16:27:22 +0100 Subject: [PATCH 24/36] replace usages of InvalidResponseException with ServerException --- src/API/Endpoints/KeysEndpoint.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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(), ); } From cbe11bc35bdc45a73be5dcc8db3ecf0981ca5539 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Mon, 26 Jun 2023 16:29:15 +0100 Subject: [PATCH 25/36] add exceptions types to method signature --- src/API/Endpoints/TokenEndpoint.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index 15fde68..a27ca34 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -2,13 +2,11 @@ namespace YouCan\Pay\API\Endpoints; -use InvalidArgumentException; use YouCan\Pay\API\Exceptions\ClientException; 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; @@ -55,7 +53,7 @@ protected function endpoint(): string /** * @param Response $response * - * @throws ValidationException|InvalidArgumentException + * @throws MissingTokenException|ClientException|UnexpectedResultException|ServerException|UnsupportedResponseException */ private function assertResponse(Response $response): void { From be51e643e3ef287e54a74cc7b3f3a9c4ebb8ac38 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:16:00 +0100 Subject: [PATCH 26/36] ClientException inherit from the BaseException --- src/API/Exceptions/ClientException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/Exceptions/ClientException.php b/src/API/Exceptions/ClientException.php index 510cc63..30d79af 100644 --- a/src/API/Exceptions/ClientException.php +++ b/src/API/Exceptions/ClientException.php @@ -5,6 +5,6 @@ /** * Exception when a client error is encountered (4xx codes) */ -class ClientException extends BadResponseException +class ClientException extends BaseException { } From 83caff779a81c6571aaec8fbf4a918c84dd8b3c2 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:16:20 +0100 Subject: [PATCH 27/36] ServerException inherit from the BaseException --- src/API/Exceptions/ServerException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/Exceptions/ServerException.php b/src/API/Exceptions/ServerException.php index 932e8a6..a4acde4 100644 --- a/src/API/Exceptions/ServerException.php +++ b/src/API/Exceptions/ServerException.php @@ -5,6 +5,6 @@ /** * Exception when a server error is encountered (5xx codes) */ -class ServerException extends BadResponseException +class ServerException extends ClientException { } From 4d6b1ff27a55a25dc6cbdaeed0f797f51f8cac03 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:20:20 +0100 Subject: [PATCH 28/36] update BaseException --- src/API/Exceptions/BaseException.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/API/Exceptions/BaseException.php b/src/API/Exceptions/BaseException.php index fbfe480..3362af8 100644 --- a/src/API/Exceptions/BaseException.php +++ b/src/API/Exceptions/BaseException.php @@ -2,6 +2,31 @@ namespace YouCan\Pay\API\Exceptions; +use Throwable; + class BaseException extends \RuntimeException { + /** @var string|null */ + protected $response; + + public function __construct( + string $message, + string $response = null, + int $code = 0, + Throwable $previous = null + ) { + parent::__construct($message, $code, $previous); + + $this->response = $response; + } + + public function getResponse() + { + return $this->response; + } + + public function hasResponse(): bool + { + return $this->response !== null; + } } From 201ab25079802029c9d1047ad0e1492b85d6ad19 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:20:39 +0100 Subject: [PATCH 29/36] ValidationException inherit from the ClientException --- src/API/Exceptions/ValidationException.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/API/Exceptions/ValidationException.php b/src/API/Exceptions/ValidationException.php index 1f49c5f..f84b981 100644 --- a/src/API/Exceptions/ValidationException.php +++ b/src/API/Exceptions/ValidationException.php @@ -2,12 +2,6 @@ namespace YouCan\Pay\API\Exceptions; -use Exception; - -class ValidationException extends Exception +class ValidationException extends ClientException { - public function __construct(string $message) - { - parent::__construct($message); - } } From 4126d5acc0692eb6e3f1038fba47fa2e80c59bd6 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:33:21 +0100 Subject: [PATCH 30/36] inherit from BaseException --- src/API/Exceptions/Token/MissingTokenException.php | 4 ++-- src/API/Exceptions/UnexpectedResultException.php | 2 +- src/API/Exceptions/UnsupportedResponseException.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/API/Exceptions/Token/MissingTokenException.php b/src/API/Exceptions/Token/MissingTokenException.php index 3aa8ddc..918061f 100644 --- a/src/API/Exceptions/Token/MissingTokenException.php +++ b/src/API/Exceptions/Token/MissingTokenException.php @@ -2,8 +2,8 @@ namespace YouCan\Pay\API\Exceptions\Token; -use YouCan\Pay\API\Exceptions\BadResponseException; +use YouCan\Pay\API\Exceptions\BaseException; -class MissingTokenException extends BadResponseException +class MissingTokenException extends BaseException { } diff --git a/src/API/Exceptions/UnexpectedResultException.php b/src/API/Exceptions/UnexpectedResultException.php index 31d44c4..9f1dfda 100644 --- a/src/API/Exceptions/UnexpectedResultException.php +++ b/src/API/Exceptions/UnexpectedResultException.php @@ -2,6 +2,6 @@ namespace YouCan\Pay\API\Exceptions; -class UnexpectedResultException extends BadResponseException +class UnexpectedResultException extends BaseException { } diff --git a/src/API/Exceptions/UnsupportedResponseException.php b/src/API/Exceptions/UnsupportedResponseException.php index 855f767..ce50e7e 100644 --- a/src/API/Exceptions/UnsupportedResponseException.php +++ b/src/API/Exceptions/UnsupportedResponseException.php @@ -2,6 +2,6 @@ namespace YouCan\Pay\API\Exceptions; -class UnsupportedResponseException extends BadResponseException +class UnsupportedResponseException extends BaseException { } From 79f7826be4a91cca26a668c89186f3eabd281ab5 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:36:10 +0100 Subject: [PATCH 31/36] delete BadResponseException --- src/API/Exceptions/BadResponseException.php | 27 --------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/API/Exceptions/BadResponseException.php diff --git a/src/API/Exceptions/BadResponseException.php b/src/API/Exceptions/BadResponseException.php deleted file mode 100644 index c45f751..0000000 --- a/src/API/Exceptions/BadResponseException.php +++ /dev/null @@ -1,27 +0,0 @@ - Date: Tue, 27 Jun 2023 10:39:48 +0100 Subject: [PATCH 32/36] ServerException inherit from the BaseException --- src/API/Exceptions/ServerException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/Exceptions/ServerException.php b/src/API/Exceptions/ServerException.php index a4acde4..6e51c89 100644 --- a/src/API/Exceptions/ServerException.php +++ b/src/API/Exceptions/ServerException.php @@ -5,6 +5,6 @@ /** * Exception when a server error is encountered (5xx codes) */ -class ServerException extends ClientException +class ServerException extends BaseException { } From 3bbb1f551a3a3bb651722564c5a9bc6326e99d77 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:42:18 +0100 Subject: [PATCH 33/36] replace usages ClientException of withValidationException --- src/API/Endpoints/TokenEndpoint.php | 8 ++++---- tests/API/Endpoints/TokenEndpointTest.php | 7 +++---- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/API/Endpoints/TokenEndpoint.php b/src/API/Endpoints/TokenEndpoint.php index a27ca34..38356c7 100644 --- a/src/API/Endpoints/TokenEndpoint.php +++ b/src/API/Endpoints/TokenEndpoint.php @@ -2,11 +2,11 @@ namespace YouCan\Pay\API\Endpoints; -use YouCan\Pay\API\Exceptions\ClientException; 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; @@ -53,7 +53,7 @@ protected function endpoint(): string /** * @param Response $response * - * @throws MissingTokenException|ClientException|UnexpectedResultException|ServerException|UnsupportedResponseException + * @throws MissingTokenException|ValidationException|UnexpectedResultException|ServerException|UnsupportedResponseException */ private function assertResponse(Response $response): void { @@ -71,7 +71,7 @@ private function assertResponse(Response $response): void if ($response->getStatusCode() === 404) { if ($response->get('success') === false && is_string($response->get('message'))) { - throw new ClientException( + throw new ValidationException( (string)$response->get('message'), json_encode($response->getResponse()), $response->getStatusCode(), @@ -81,7 +81,7 @@ private function assertResponse(Response $response): void if ($response->getStatusCode() === 422) { if ($response->get('success') === false && is_string($response->get('message'))) { - throw new ClientException( + throw new ValidationException( (string)$response->get('message'), json_encode($response->getResponse()), $response->getStatusCode(), diff --git a/tests/API/Endpoints/TokenEndpointTest.php b/tests/API/Endpoints/TokenEndpointTest.php index 3d1d42b..d5803ac 100644 --- a/tests/API/Endpoints/TokenEndpointTest.php +++ b/tests/API/Endpoints/TokenEndpointTest.php @@ -5,7 +5,6 @@ use Tests\API\FakeAPIService; use Tests\BaseTestCase; use YouCan\Pay\API\Endpoints\TokenEndpoint; -use YouCan\Pay\API\Exceptions\ClientException; use YouCan\Pay\API\Exceptions\ServerException; use YouCan\Pay\API\Exceptions\ValidationException; use YouCan\Pay\API\Response; @@ -35,7 +34,7 @@ public function test_generate_token_successfully() public function test_validation_exception_amount_is_less_than_minimum() { - $this->expectException(ClientException::class); + $this->expectException(ValidationException::class); $response = new Response( 422, @@ -52,7 +51,7 @@ public function test_validation_exception_amount_is_less_than_minimum() public function test_validation_exception_amount_is_greater_than_maximum() { - $this->expectException(ClientException::class); + $this->expectException(ValidationException::class); $response = new Response( 422, @@ -70,7 +69,7 @@ public function test_validation_exception_amount_is_greater_than_maximum() public function test_validation_exception() { - $this->expectException(ClientException::class); + $this->expectException(ValidationException::class); $response = new Response( 422, From ed0bd12ee9b30c56959535768a04c45923585da8 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:47:48 +0100 Subject: [PATCH 34/36] UnsetPublicKeyException and UnsetPrivateKeyException inherit from ValidationException --- src/API/Exceptions/Keys/UnsetPrivateKeyException.php | 4 ++-- src/API/Exceptions/Keys/UnsetPublicKeyException.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/API/Exceptions/Keys/UnsetPrivateKeyException.php b/src/API/Exceptions/Keys/UnsetPrivateKeyException.php index d7135de..ce9f5aa 100644 --- a/src/API/Exceptions/Keys/UnsetPrivateKeyException.php +++ b/src/API/Exceptions/Keys/UnsetPrivateKeyException.php @@ -2,9 +2,9 @@ namespace YouCan\Pay\API\Exceptions\Keys; -use YouCan\Pay\API\Exceptions\BaseException; +use YouCan\Pay\API\Exceptions\ValidationException; -class UnsetPrivateKeyException extends BaseException +class UnsetPrivateKeyException extends ValidationException { public function __construct() { parent::__construct("private key not set"); diff --git a/src/API/Exceptions/Keys/UnsetPublicKeyException.php b/src/API/Exceptions/Keys/UnsetPublicKeyException.php index 347d418..d11a174 100644 --- a/src/API/Exceptions/Keys/UnsetPublicKeyException.php +++ b/src/API/Exceptions/Keys/UnsetPublicKeyException.php @@ -2,9 +2,9 @@ namespace YouCan\Pay\API\Exceptions\Keys; -use YouCan\Pay\API\Exceptions\BaseException; +use YouCan\Pay\API\Exceptions\ValidationException; -class UnsetPublicKeyException extends BaseException +class UnsetPublicKeyException extends ValidationException { public function __construct() { parent::__construct("public key not set"); From 98dbc85c23d4612174e6c1855c245410cf50549c Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:53:22 +0100 Subject: [PATCH 35/36] delete RequestException --- src/API/Exceptions/RequestException.php | 32 ------------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/API/Exceptions/RequestException.php diff --git a/src/API/Exceptions/RequestException.php b/src/API/Exceptions/RequestException.php deleted file mode 100644 index f5ce27f..0000000 --- a/src/API/Exceptions/RequestException.php +++ /dev/null @@ -1,32 +0,0 @@ -response = $response; - } - - public function getResponse() - { - return $this->response; - } - - public function hasResponse(): bool - { - return $this->response !== null; - } -} From 40836c272e273c7727ff380ef13926187bb43e30 Mon Sep 17 00:00:00 2001 From: y-abderrazik Date: Tue, 27 Jun 2023 10:55:45 +0100 Subject: [PATCH 36/36] update UnsetPrivateKeyException and UnsetPublicKeyException --- src/API/Endpoints/Endpoint.php | 4 ++-- src/API/Exceptions/Keys/UnsetPrivateKeyException.php | 3 --- src/API/Exceptions/Keys/UnsetPublicKeyException.php | 3 --- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/API/Endpoints/Endpoint.php b/src/API/Endpoints/Endpoint.php index d348a44..5d4d002 100644 --- a/src/API/Endpoints/Endpoint.php +++ b/src/API/Endpoints/Endpoint.php @@ -37,14 +37,14 @@ protected function createEndpoint(): string protected function assertPrivateKeyIsSet(): void { if ($this->apiService->getPrivateKey() === null) { - throw new UnsetPrivateKeyException(); + throw new UnsetPrivateKeyException("private key not set"); } } protected function assertPublicKeyIsSet(): void { if ($this->apiService->getPublicKey() === null) { - throw new UnsetPublicKeyException(); + throw new UnsetPublicKeyException("public key not set"); } } } diff --git a/src/API/Exceptions/Keys/UnsetPrivateKeyException.php b/src/API/Exceptions/Keys/UnsetPrivateKeyException.php index ce9f5aa..c23efe6 100644 --- a/src/API/Exceptions/Keys/UnsetPrivateKeyException.php +++ b/src/API/Exceptions/Keys/UnsetPrivateKeyException.php @@ -6,7 +6,4 @@ class UnsetPrivateKeyException extends ValidationException { - public function __construct() { - parent::__construct("private key not set"); - } } diff --git a/src/API/Exceptions/Keys/UnsetPublicKeyException.php b/src/API/Exceptions/Keys/UnsetPublicKeyException.php index d11a174..213e16e 100644 --- a/src/API/Exceptions/Keys/UnsetPublicKeyException.php +++ b/src/API/Exceptions/Keys/UnsetPublicKeyException.php @@ -6,7 +6,4 @@ class UnsetPublicKeyException extends ValidationException { - public function __construct() { - parent::__construct("public key not set"); - } }