From 37b489b6cc4513bf1d0ac2c8c8e81d79450f2d4c Mon Sep 17 00:00:00 2001 From: Dimitri BOUTEILLE Date: Wed, 12 Feb 2025 08:20:15 +0100 Subject: [PATCH] Fix : Undefined array key "giftcard" in /Helper/GiftcardPayment.php --- Helper/GiftcardPayment.php | 13 ++++++++-- Test/Unit/Helper/GiftcardPaymentTest.php | 33 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/Helper/GiftcardPayment.php b/Helper/GiftcardPayment.php index 26abe13c1..abdea4776 100644 --- a/Helper/GiftcardPayment.php +++ b/Helper/GiftcardPayment.php @@ -108,12 +108,21 @@ public function getQuoteGiftcardTotalBalance(int $quoteId): int $totalBalance = 0; foreach ($stateDataArray as $stateData) { - $stateData = json_decode($stateData['state_data'], true); + $state = $stateData['state_data'] ?? null; + if (!is_string($state)) { + continue; + } + + $stateData = json_decode($state, true); + $giftCardValue = $stateData['giftcard']['balance']['value'] ?? null; + if (!is_numeric($giftCardValue)) { + continue; + } if (isset($stateData['paymentMethod']['type']) || isset($stateData['paymentMethod']['brand']) || $stateData['paymentMethod']['type'] === 'giftcard') { - $totalBalance += $stateData['giftcard']['balance']['value']; + $totalBalance += $giftCardValue; } } diff --git a/Test/Unit/Helper/GiftcardPaymentTest.php b/Test/Unit/Helper/GiftcardPaymentTest.php index 7e03aa1ee..a35c71e12 100644 --- a/Test/Unit/Helper/GiftcardPaymentTest.php +++ b/Test/Unit/Helper/GiftcardPaymentTest.php @@ -263,6 +263,39 @@ public function testGetQuoteGiftcardTotalBalance(): void $this->assertEquals($expectedGiftcardTotalBalance, $totalBalance); } + public function testGetQuoteGiftcardTotalBalanceWithInvalidState(): void + { + $quoteId = 1; + + $state = <<createConfiguredMock(StateDataCollection::class, [ + 'getStateDataRowsWithQuoteId' => $this->createConfiguredMock(StateDataCollection::class, [ + 'getData' => [ + [ + 'entity_id' => 1, + 'quote_id' => 1, + 'state_data' => $state + ] + ] + ]) + ]); + + $giftcardPaymentHelper = $this->createGiftcardPaymentHelper($adyenStateDataMock); + $totalBalance = $giftcardPaymentHelper->getQuoteGiftcardTotalBalance($quoteId); + + $this->assertEquals(0, $totalBalance, 'The total must be equal to 0 because the giftcard key is undefined.'); + } + private static function discountTestDataProvider(): array { return [