Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #110 from heidelpay/change/PHPLIB-224/add-errorId-…
Browse files Browse the repository at this point in the history
…to-api-exception

Merge remote-tracking branch 'remotes/github/develop' into change/PHPLIB-224/add-errorId-to-api-exception
  • Loading branch information
Simon Gabriel authored Aug 28, 2019
2 parents afbd59d + ce22fa8 commit 15de4db
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion src/Exceptions/HeidelpayApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -64,4 +69,12 @@ public function getMerchantMessage(): string
{
return $this->getMessage();
}

/**
* @return string
*/
public function getErrorId(): string
{
return $this->errorId;
}
}
11 changes: 9 additions & 2 deletions src/Services/HttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions test/integration/BasketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public function basketItemWithInvalidUrlWillThrowAnError($expectException, $imag
} catch (HeidelpayApiException $e) {
$this->assertTrue($expectException);
$this->assertEquals($exceptionCode, $e->getCode());
$this->assertNotNull($e->getErrorId());
}
}

Expand Down
1 change: 1 addition & 0 deletions test/integration/CustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/integration/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 15de4db

Please sign in to comment.