diff --git a/src/Exception/BadRequestApiException.php b/src/Exception/BadRequestApiException.php new file mode 100644 index 0000000..a89fe28 --- /dev/null +++ b/src/Exception/BadRequestApiException.php @@ -0,0 +1,9 @@ +getErrorResponseMessage($response->getBody()->getContents()); switch (true) { + case $responseCode == 400: + return new BadRequestApiException($message, $responseCode); + case $responseCode == 401: + return new UnauthorizedApiException($message, $responseCode); + case $responseCode == 403: + return new ForbiddenApiException($message, $responseCode); case $responseCode == 404: return new NotFoundApiException($message, $responseCode); + case $responseCode == 409: + return new ConflictApiException($message, $responseCode); + case $responseCode == 422: + return new UnprocessableContentApiException($message, $responseCode); + case $responseCode == 429: + return new TooManyRequestsApiException($message, $responseCode); case $responseCode >= 500: case $responseCode == 0: // network communication failure return new ServiceUnavailableApiException($message, $responseCode); diff --git a/src/Service/ApiServiceInterface.php b/src/Service/ApiServiceInterface.php index 9bb2b2f..b69a03a 100644 --- a/src/Service/ApiServiceInterface.php +++ b/src/Service/ApiServiceInterface.php @@ -4,7 +4,9 @@ use Psr\Http\Message\StreamInterface; use ThePay\ApiClient\Exception\ApiExceptionInterface; +use ThePay\ApiClient\Exception\ConflictApiException; use ThePay\ApiClient\Exception\NotFoundApiException; +use ThePay\ApiClient\Exception\UnprocessableContentApiException; use ThePay\ApiClient\Filter\PaymentsFilter; use ThePay\ApiClient\Filter\TransactionFilter; use ThePay\ApiClient\Model\AccountBalance; @@ -30,6 +32,9 @@ use ThePay\ApiClient\ValueObject\LanguageCode; use ThePay\ApiClient\ValueObject\StringValue; +/** + * @note List only exceptions meant for flow control in `@throws`, all other errors should be covered by {@see ApiExceptionInterface}. + */ interface ApiServiceInterface { /** @@ -171,7 +176,7 @@ public function getAccountStatementGPC(TransactionFilter $filter, ?GPCPaymentIde * * @param non-empty-string|null $methodCode * - * @throws ApiExceptionInterface + * @throws ConflictApiException|ApiExceptionInterface */ public function createPayment(CreatePaymentParams $createPaymentParams, ?string $methodCode = null): CreatePaymentResponse; @@ -180,7 +185,7 @@ public function createPayment(CreatePaymentParams $createPaymentParams, ?string * * @see https://docs.thepay.eu/#tag/Preauthorized-Payments/paths/~1v2~1projects~1%7Bproject_id%7D~1payments~1%7Bpayment_uid%7D~1preauthorized/post * - * @throws NotFoundApiException|ApiExceptionInterface + * @throws NotFoundApiException|UnprocessableContentApiException|ApiExceptionInterface */ public function realizePreauthorizedPayment(RealizePreauthorizedPaymentParams $params): RealizePreauthorizedPaymentResult; @@ -189,7 +194,7 @@ public function realizePreauthorizedPayment(RealizePreauthorizedPaymentParams $p * * @see https://docs.thepay.eu/#tag/Preauthorized-Payments/paths/~1v1~1projects~1%7Bproject_id%7D~1payments~1%7Bpayment_uid%7D~1preauthorized/delete * - * @throws NotFoundApiException|ApiExceptionInterface + * @throws NotFoundApiException|UnprocessableContentApiException|ApiExceptionInterface */ public function cancelPreauthorizedPayment(Identifier $uid): void; diff --git a/src/TheClient.php b/src/TheClient.php index 5283360..4edca2f 100644 --- a/src/TheClient.php +++ b/src/TheClient.php @@ -5,7 +5,9 @@ use InvalidArgumentException; use Psr\Http\Message\StreamInterface; use ThePay\ApiClient\Exception\ApiExceptionInterface; +use ThePay\ApiClient\Exception\ConflictApiException; use ThePay\ApiClient\Exception\NotFoundApiException; +use ThePay\ApiClient\Exception\UnprocessableContentApiException; use ThePay\ApiClient\Filter\PaymentMethodFilter; use ThePay\ApiClient\Filter\PaymentsFilter; use ThePay\ApiClient\Filter\TransactionFilter; @@ -295,7 +297,7 @@ public function getPaymentButtonsForPayment($uid, $languageCode = null, $useInli /** * @param non-empty-string|null $methodCode * - * @throws ApiExceptionInterface + * @throws ConflictApiException|ApiExceptionInterface */ public function createPayment(CreatePaymentParams $params, ?string $methodCode = null): CreatePaymentResponse { @@ -327,7 +329,7 @@ public function changePaymentMethod(string $paymentUid, string $methodCode): voi * * @return RealizePreauthorizedPaymentResult * - * @throws InvalidArgumentException|NotFoundApiException|ApiExceptionInterface + * @throws InvalidArgumentException|NotFoundApiException|UnprocessableContentApiException|ApiExceptionInterface */ public function realizePreauthorizedPayment(RealizePreauthorizedPaymentParams $params): RealizePreauthorizedPaymentResult { @@ -340,7 +342,7 @@ public function realizePreauthorizedPayment(RealizePreauthorizedPaymentParams $p /** * @param non-empty-string $paymentUid * - * @throws InvalidArgumentException|NotFoundApiException|ApiExceptionInterface + * @throws InvalidArgumentException|NotFoundApiException|UnprocessableContentApiException|ApiExceptionInterface */ public function cancelPreauthorizedPayment(string $paymentUid): void { @@ -392,7 +394,7 @@ public function createPaymentRefund($paymentUid, $amount, $reason) * * @return string with binary content of PDF file * - * @throws NotFoundApiException|ApiExceptionInterface if payment is not paid yet + * @throws InvalidArgumentException|NotFoundApiException|ApiExceptionInterface if payment is not paid yet */ public function generatePaymentConfirmationPdf(string $paymentUid, ?string $languageCode = null): string {