Skip to content

Commit 413aa46

Browse files
dimitriBouteillekhushboo-singhviDimitri BOUTEILLEcandemiralp
authored
Fix undefined array key "giftcard" in /Helper/GiftcardPayment.php on line 116 (#2883)
* Abstracting the code, removing the console.logs * Fix : Undefined array key "giftcard" in /Helper/GiftcardPayment.php * Revert "Abstracting the code, removing the console.logs" This reverts commit 495ec49. --------- Co-authored-by: khushboos <khushboo.singhvi@adyen.com> Co-authored-by: Dimitri BOUTEILLE <dimitri.bouteille@reflet-digital.com> Co-authored-by: Can Demiralp <can.demiralp@adyen.com> Co-authored-by: Can Demiralp <ecandemiralp@gmail.com>
1 parent 7b7f6e1 commit 413aa46

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Helper/GiftcardPayment.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,21 @@ public function getQuoteGiftcardTotalBalance(int $quoteId): int
108108
$totalBalance = 0;
109109

110110
foreach ($stateDataArray as $stateData) {
111-
$stateData = json_decode($stateData['state_data'], true);
111+
$state = $stateData['state_data'] ?? null;
112+
if (!is_string($state)) {
113+
continue;
114+
}
115+
116+
$stateData = json_decode($state, true);
117+
$giftCardValue = $stateData['giftcard']['balance']['value'] ?? null;
118+
if (!is_numeric($giftCardValue)) {
119+
continue;
120+
}
112121

113122
if (isset($stateData['paymentMethod']['type']) ||
114123
isset($stateData['paymentMethod']['brand']) ||
115124
$stateData['paymentMethod']['type'] === 'giftcard') {
116-
$totalBalance += $stateData['giftcard']['balance']['value'];
125+
$totalBalance += $giftCardValue;
117126
}
118127
}
119128

Test/Unit/Helper/GiftcardPaymentTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,39 @@ public function testGetQuoteGiftcardTotalBalance(): void
263263
$this->assertEquals($expectedGiftcardTotalBalance, $totalBalance);
264264
}
265265

266+
public function testGetQuoteGiftcardTotalBalanceWithInvalidState(): void
267+
{
268+
$quoteId = 1;
269+
270+
$state = <<<JSON
271+
{
272+
"paymentMethod": {
273+
"type": "giftcard",
274+
"brand": "svs",
275+
"encryptedCardNumber": "PLACEHOLDER_GIFT_CARD_NUMBER",
276+
"encryptedSecurityCode": "PLACEHOLDER_GIFT_CARD_PIN"
277+
}
278+
}
279+
JSON;
280+
281+
$adyenStateDataMock = $this->createConfiguredMock(StateDataCollection::class, [
282+
'getStateDataRowsWithQuoteId' => $this->createConfiguredMock(StateDataCollection::class, [
283+
'getData' => [
284+
[
285+
'entity_id' => 1,
286+
'quote_id' => 1,
287+
'state_data' => $state
288+
]
289+
]
290+
])
291+
]);
292+
293+
$giftcardPaymentHelper = $this->createGiftcardPaymentHelper($adyenStateDataMock);
294+
$totalBalance = $giftcardPaymentHelper->getQuoteGiftcardTotalBalance($quoteId);
295+
296+
$this->assertEquals(0, $totalBalance, 'The total must be equal to 0 because the giftcard key is undefined.');
297+
}
298+
266299
private static function discountTestDataProvider(): array
267300
{
268301
return [

0 commit comments

Comments
 (0)