diff --git a/src/AtolOnlineClient/AtolOnline.php b/src/AtolOnlineClient/AtolOnline.php index 1c72133..1191f29 100644 --- a/src/AtolOnlineClient/AtolOnline.php +++ b/src/AtolOnlineClient/AtolOnline.php @@ -51,7 +51,7 @@ class AtolOnline DeserializationContext::create()->setGroups(['post']) ); } catch (RuntimeException $exception) { - throw new InvalidResponseException($exception->getMessage()); + throw $this->createInvalidResponseException($response, $exception); } } @@ -69,7 +69,7 @@ class AtolOnline DeserializationContext::create()->setGroups(['get']) ); } catch (RuntimeException $exception) { - throw new InvalidResponseException($exception->getMessage()); + throw $this->createInvalidResponseException($response, $exception); } } @@ -107,4 +107,26 @@ class AtolOnline { return $this->api; } + + /** + * @param string $response + * @param RuntimeException $previous + * @return InvalidResponseException + */ + private function createInvalidResponseException(string $response, RuntimeException $previous): InvalidResponseException + { + $exception = new InvalidResponseException($previous->getMessage()); + + preg_match('/(\d+) ([\w ]+)<\/title><\/head>/m', $response, $matches); + + if (count($matches) === 3) { + [, $code, $message] = $matches; + + $exception + ->setCodeError($code) + ->setMessageError($message); + } + + return $exception; + } } diff --git a/src/AtolOnlineClient/AtolOnlineApi.php b/src/AtolOnlineClient/AtolOnlineApi.php index 5f5224a..10c2325 100644 --- a/src/AtolOnlineClient/AtolOnlineApi.php +++ b/src/AtolOnlineClient/AtolOnlineApi.php @@ -94,7 +94,7 @@ class AtolOnlineApi */ public function getVersion(): string { - return $this->connection->version; + return $this->connection->version ?: self::API_VERSION_V3; } /** @@ -103,7 +103,7 @@ class AtolOnlineApi * @param $paymentReceiptRequest * @return string */ - public function sell($paymentReceiptRequest): ?string + public function sell($paymentReceiptRequest) { return $this->sendOperationRequest('sell', $paymentReceiptRequest); } @@ -114,7 +114,7 @@ class AtolOnlineApi * @param $paymentReceiptRequest * @return string */ - public function sellRefund($paymentReceiptRequest): ?string + public function sellRefund($paymentReceiptRequest) { return $this->sendOperationRequest('sell_refund', $paymentReceiptRequest); } diff --git a/src/AtolOnlineClient/Exception/InvalidResponseException.php b/src/AtolOnlineClient/Exception/InvalidResponseException.php index 0c612ce..5365754 100644 --- a/src/AtolOnlineClient/Exception/InvalidResponseException.php +++ b/src/AtolOnlineClient/Exception/InvalidResponseException.php @@ -2,6 +2,58 @@ namespace AtolOnlineClient\Exception; +/** + * Class InvalidResponseException + * + * @package AtolOnlineClient\Exception + */ class InvalidResponseException extends AtolException { + /** + * @var int|null + */ + protected $codeError; + + /** + * @var string|null + */ + protected $messageError; + + /** + * @return int|null + */ + public function getCodeError(): ?int + { + return $this->codeError; + } + + /** + * @param int|null $codeError + * @return InvalidResponseException + */ + public function setCodeError(?int $codeError): InvalidResponseException + { + $this->codeError = $codeError; + + return $this; + } + + /** + * @return string|null + */ + public function getMessageError(): ?string + { + return $this->messageError; + } + + /** + * @param string|null $messageError + * @return InvalidResponseException + */ + public function setMessageError(?string $messageError): InvalidResponseException + { + $this->messageError = $messageError; + + return $this; + } } diff --git a/tests/AtolOnlineClient/AtolOnlineTest.php b/tests/AtolOnlineClient/AtolOnlineTest.php index 8e5d75a..bbdc577 100644 --- a/tests/AtolOnlineClient/AtolOnlineTest.php +++ b/tests/AtolOnlineClient/AtolOnlineTest.php @@ -10,6 +10,7 @@ use AtolOnlineClient\Request\V4\PaymentReceiptRequest; use AtolOnlineClient\Request\V4\ReceiptRequest; use AtolOnlineClient\Request\V4\ServiceRequest; use GuzzleHttp\Client; +use JMS\Serializer\Exception\RuntimeException; use JMS\Serializer\SerializerInterface; use PHPUnit\Framework\TestCase; @@ -121,6 +122,26 @@ class AtolOnlineTest extends TestCase $this->atol->deserializeCheckStatusResponse('<html></html>'); } + /** + * @param int|null $code + * @param string|null $message + * @param string $html + * @covers \AtolOnlineClient\AtolOnline::createInvalidResponseException + * @dataProvider dataInvalidResponse + */ + public function testCreateInvalidResponseException(?int $code, ?string $message, string $html): void + { + /** @var InvalidResponseException $exception */ + $exception = $this->callMethod($this->atol, 'createInvalidResponseException', [ + 'response' => $html, + 'previous' => new RuntimeException() + ]); + + $this->assertInstanceOf(InvalidResponseException::class, $exception); + $this->assertEquals($code, $exception->getCodeError()); + $this->assertEquals($message, $exception->getMessageError()); + } + /** * @covers \AtolOnlineClient\AtolOnline::serializeOperationRequest */ @@ -197,4 +218,48 @@ class AtolOnlineTest extends TestCase ['error_response_v4.json'] ]; } + + /** + * @return array + */ + public function dataInvalidResponse(): array + { + return [ + [ + 'code' => 404, + 'message' => 'Not Found', + 'html' => '<html><head><title>404 Not Found

404 Not Found


openresty/1.15.8.1rc1
', + ], + [ + 'code' => 502, + 'message' => 'Bad Gateway', + 'html' => '502 Bad Gateway

502 Bad Gateway


nginx/1.15.8
', + ], + [ + 'code' => null, + 'message' => null, + 'html' => '', + ], + [ + 'code' => null, + 'message' => null, + 'html' => '', + ], + ]; + } + + /** + * @param mixed $object + * @param string $name + * @param array $args + * @return mixed + */ + private function callMethod($object, string $name, array $args = []) + { + $reflection = new \ReflectionClass($object); + $method = $reflection->getMethod($name); + $method->setAccessible(true); + + return $method->invokeArgs($object, $args); + } }