diff --git a/Gateway/Http/Client/TransactionRefund.php b/Gateway/Http/Client/TransactionRefund.php index 3484c030d..238f18fb7 100644 --- a/Gateway/Http/Client/TransactionRefund.php +++ b/Gateway/Http/Client/TransactionRefund.php @@ -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; } diff --git a/Test/Unit/Gateway/Http/Client/TransactionRefundTest.php b/Test/Unit/Gateway/Http/Client/TransactionRefundTest.php index 3d9882599..21b273045 100644 --- a/Test/Unit/Gateway/Http/Client/TransactionRefundTest.php +++ b/Test/Unit/Gateway/Http/Client/TransactionRefundTest.php @@ -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 { @@ -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]); + } }