Skip to content

Commit

Permalink
[ECP-9506] Fix /payments response handling if an exception is thrown (#…
Browse files Browse the repository at this point in the history
…2763)

* [ECP-9506] Fix response handling of non-200 http status codes

* [ECP-9506] Update unit tests

---------

Co-authored-by: Can Demiralp <[email protected]>
  • Loading branch information
candemiralp and Can Demiralp authored Oct 4, 2024
1 parent c3be66f commit 1935d6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Gateway/Http/Client/TransactionPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public function placeRequest(TransferInterface $transferObject): array
$this->adyenHelper->logResponse($responseData);
} catch (AdyenException $e) {
$this->adyenHelper->logAdyenException($e);

$responseObj['error'] = $e->getMessage();
$responseObj['errorCode'] = $e->getAdyenErrorCode();

$responseCollection[] = $responseObj;
}

return $responseCollection;
Expand Down
9 changes: 6 additions & 3 deletions Gateway/Validator/CheckoutResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ public function validate(array $validationSubject): ResultInterface
// Extract all the payment responses
$responseCollection = $validationSubject['response'];
unset($validationSubject['response']);

// hasOnlyGiftCards is needed later but cannot be processed as a response
unset($responseCollection['hasOnlyGiftCards']);

// Assign the remaining items to $commandSubject
$commandSubject = $validationSubject;

if (empty($responseCollection)) {
throw new ValidatorException(__("No responses were provided"));
}

// hasOnlyGiftCards is needed later but cannot be processed as a response
unset($responseCollection['hasOnlyGiftCards']);
foreach ($responseCollection as $thisResponse) {
$responseSubject = array_merge($commandSubject, ['response' => $thisResponse]);
$this->validateResponse($responseSubject);
Expand All @@ -90,11 +92,12 @@ private function validateResponse($responseSubject): void

$payment->setAdditionalInformation('3dActive', false);

// validate result
// Handle empty result for unexpected cases
if (empty($response['resultCode'])) {
$this->handleEmptyResultCode($response);
}

// Handle the `/payments` response
$this->validateResult($response, $payment);
}

Expand Down
18 changes: 18 additions & 0 deletions Test/Unit/Gateway/Http/Client/TransactionPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Adyen\Payment\Test\Unit\Gateway\Http\Client;

use Adyen\AdyenException;
use Adyen\Model\Checkout\ApplicationInfo;
use Adyen\Model\Checkout\PaymentRequest;
use Adyen\Model\Checkout\PaymentResponse as CheckoutPaymentResponse;
Expand Down Expand Up @@ -87,6 +88,23 @@ public function testPlaceRequestWithResultCode()
$this->assertEquals($requestBody, $result);
}

public function testPlaceRequestWithoutResultCode()
{
$transferObjectMock = $this->createMock(TransferInterface::class);

$requestBody = ['amount' => ['value' => 1000]];
$transferObjectMock->method('getBody')->willReturn($requestBody);
$transferObjectMock->method('getClientConfig')->willReturn(['storeId' => 1]);

$clientMock = $this->createMock(PaymentsApi::class);
$clientMock->method('payments')->willThrowException(new AdyenException());

$this->adyenHelperMock->method('initializePaymentsApi')->willReturn($clientMock);

$response = $this->transactionPayment->placeRequest($transferObjectMock);
$this->assertArrayHasKey('errorCode', $response[0]);
}

public function testPlaceRequestGeneratesIdempotencyKey()
{
$requestBody = ['reference' => 'ABC12345', 'amount' => ['value' => 100], 'applicationInfo' => $this->applicationInfoMock];
Expand Down

0 comments on commit 1935d6f

Please sign in to comment.