Skip to content

Commit

Permalink
Merge pull request #2822 from Adyen/ECP-9509
Browse files Browse the repository at this point in the history
[ECP-9509] Fix the refund response handler to handle exceptions thrown by Checkout API
  • Loading branch information
khushboo-singhvi authored Dec 17, 2024
2 parents ea6e8fc + 40b9805 commit 9f5fade
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gateway/Http/Client/TransactionRefund.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public function placeRequest(TransferInterface $transferObject): array
$this->adyenHelper->logResponse($responseData);
} catch (AdyenException $e) {
$this->adyenHelper->logAdyenException($e);
$responseData['error'] = $e->getMessage();
$responseData['errorCode'] = $e->getAdyenErrorCode();
}
$responses[] = $responseData;
}
Expand Down
43 changes: 43 additions & 0 deletions Test/Unit/Gateway/Http/Client/TransactionRefundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Adyen\Service\Checkout\ModificationsApi;
use Magento\Payment\Gateway\Http\TransferInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Adyen\AdyenException;

class TransactionRefundTest extends AbstractAdyenTestCase
{
Expand Down Expand Up @@ -102,4 +103,46 @@ public function testPlaceRequestIncludesHeadersInRequest()
$this->assertCount(1, $responses);
$this->assertArrayHasKey('pspReference', $responses[0]);
}

public function testPlaceRequestHandlesException()
{
$requestBody = [
'amount' => ['value' => 1000, 'currency' => 'EUR'],
'paymentPspReference' => '123456789'
];

$headers = ['idempotencyExtraData' => ['order_id' => '1001']];

$transferObjectMock = $this->createConfiguredMock(TransferInterface::class, [
'getBody' => [$requestBody],
'getHeaders' => $headers,
'getClientConfig' => []
]);

$serviceMock = $this->createMock(ModificationsApi::class);
$adyenClientMock = $this->createMock(Client::class);

$this->adyenHelperMock->method('initializeAdyenClientWithClientConfig')->willReturn($adyenClientMock);
$this->adyenHelperMock->method('initializeModificationsApi')->willReturn($serviceMock);
$this->adyenHelperMock->method('buildRequestHeaders')->willReturn(['custom-header' => 'value']);

$this->idempotencyHelperMock->expects($this->once())
->method('generateIdempotencyKey')
->with($requestBody, $headers['idempotencyExtraData'])
->willReturn('generated_idempotency_key');

$serviceMock->expects($this->once())
->method('refundCapturedPayment')
->willThrowException(new AdyenException());

$this->adyenHelperMock->expects($this->once())
->method('logAdyenException')
->with($this->isInstanceOf(AdyenException::class));

$responses = $this->transactionRefund->placeRequest($transferObjectMock);
$this->assertIsArray($responses);
$this->assertCount(1, $responses);
$this->assertArrayHasKey('error', $responses[0]);
$this->assertArrayHasKey('errorCode', $responses[0]);
}
}

0 comments on commit 9f5fade

Please sign in to comment.