Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECP-9506] Fix /payments response handling if an exception is thrown #2763

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading