diff --git a/CHANGELOG.md b/CHANGELOG.md index 950a3383..e2a3410b 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a * The SDK now supports the webhook event `payout`. * Example for Flexipay direct. * Email parameter to `Paypal` payment type. +* Error id to `HeidelpayApiException`. ### Changed * The webhook tests now cover all supported events. diff --git a/src/Exceptions/HeidelpayApiException.php b/src/Exceptions/HeidelpayApiException.php index 23c4a0d7..38f6e07b 100755 --- a/src/Exceptions/HeidelpayApiException.php +++ b/src/Exceptions/HeidelpayApiException.php @@ -34,19 +34,24 @@ class HeidelpayApiException extends Exception /** @var string $clientMessage */ protected $clientMessage; + /** @var string */ + private $errorId; + /** * HeidelpayApiException constructor. * * @param string $merchantMessage * @param string $clientMessage * @param string $code + * @param string $errorId */ - public function __construct($merchantMessage = '', $clientMessage = '', $code = 'No error code provided') + public function __construct($merchantMessage = '', $clientMessage = '', $code = 'No error code provided', $errorId = 'No error id provided') { $merchantMessage = empty($merchantMessage) ? static::MESSAGE : $merchantMessage; $this->clientMessage = empty($clientMessage) ? static::CLIENT_MESSAGE : $clientMessage; parent::__construct($merchantMessage); $this->code = $code; + $this->errorId = $errorId; } /** @@ -64,4 +69,12 @@ public function getMerchantMessage(): string { return $this->getMessage(); } + + /** + * @return string + */ + public function getErrorId(): string + { + return $this->errorId; + } } diff --git a/src/Services/HttpService.php b/src/Services/HttpService.php index 42178d7c..6186dc66 100755 --- a/src/Services/HttpService.php +++ b/src/Services/HttpService.php @@ -185,7 +185,8 @@ private function handleErrors($responseCode, $response) $responseObject = json_decode($response, false); if ($responseCode >= 400 || isset($responseObject->errors)) { - $code = null; + $code = null; + $errorId = null; $customerMessage = $code; $merchantMessage = $customerMessage; if (isset($responseObject->errors[0])) { @@ -194,8 +195,14 @@ private function handleErrors($responseCode, $response) $customerMessage = $errors->customerMessage ?? ''; $code = $errors->code ?? ''; } + if (isset($responseObject->id)) { + $errorId = $responseObject->id; + if (IdService::getResourceTypeFromIdString($errorId) !== 'err') { + $errorId = null; + } + } - throw new HeidelpayApiException($merchantMessage, $customerMessage, $code); + throw new HeidelpayApiException($merchantMessage, $customerMessage, $code, $errorId); } } diff --git a/test/integration/BasketTest.php b/test/integration/BasketTest.php index 7656ffeb..ac64c22d 100755 --- a/test/integration/BasketTest.php +++ b/test/integration/BasketTest.php @@ -129,6 +129,7 @@ public function basketItemWithInvalidUrlWillThrowAnError($expectException, $imag } catch (HeidelpayApiException $e) { $this->assertTrue($expectException); $this->assertEquals($exceptionCode, $e->getCode()); + $this->assertNotNull($e->getErrorId()); } } diff --git a/test/integration/CustomerTest.php b/test/integration/CustomerTest.php index a474da9c..590fb2eb 100755 --- a/test/integration/CustomerTest.php +++ b/test/integration/CustomerTest.php @@ -340,6 +340,7 @@ public function customerShouldBeFetchedByCustomerIdAndUpdatedIfItAlreadyExists() $this->assertTrue(false, 'Exception should be thrown here.'); } catch (HeidelpayApiException $e) { $this->assertEquals($e->getCode(), ApiResponseCodes::API_ERROR_CUSTOMER_CAN_NOT_BE_FOUND); + $this->assertNotNull($e->getErrorId()); } // create customer with api diff --git a/test/integration/ExceptionTest.php b/test/integration/ExceptionTest.php index 28e6dc95..b143533f 100755 --- a/test/integration/ExceptionTest.php +++ b/test/integration/ExceptionTest.php @@ -52,6 +52,7 @@ public function apiExceptionShouldHoldClientMessage() } catch (HeidelpayApiException $e) { $this->assertInstanceOf(HeidelpayApiException::class, $e); $this->assertEquals(ApiResponseCodes::API_ERROR_TRANSACTION_AUTHORIZE_NOT_ALLOWED, $e->getCode()); + $this->assertNotNull($e->getErrorId()); $firstClientMessage = $e->getClientMessage(); $this->assertNotEmpty($firstClientMessage); $this->assertNotEquals($e->getMerchantMessage(), $firstClientMessage); @@ -63,6 +64,7 @@ public function apiExceptionShouldHoldClientMessage() } catch (HeidelpayApiException $e) { $this->assertInstanceOf(HeidelpayApiException::class, $e); $this->assertEquals(ApiResponseCodes::API_ERROR_TRANSACTION_AUTHORIZE_NOT_ALLOWED, $e->getCode()); + $this->assertNotNull($e->getErrorId()); $secondClientMessage = $e->getClientMessage(); $this->assertNotEmpty($secondClientMessage); $this->assertNotEquals($e->getMerchantMessage(), $secondClientMessage);