From 2b016428157f02a21b60d9141983cdb240e7f50b Mon Sep 17 00:00:00 2001 From: David Owusu <33735090+Ryouzanpaku@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:24:13 +0100 Subject: [PATCH 1/2] [CC-4255] Fix Keypair plain-endpoint payment-type duplication Keypair::handleResponse() appended availablePaymentTypes to $paymentTypes, but for the plain endpoint AbstractUnzerResource::updateValues() had already populated the list via the setter (the items are strings, not objects, so updateValues does not skip them). This doubled every entry. Replace the list instead of appending, which also makes repeated handling idempotent. The detailed endpoint is unaffected (its items are objects, skipped by updateValues). The existing unit test used assertArrayContains, which passed on the duplicated list; tightened to assertSame. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Resources/Keypair.php | 11 ++++------- test/unit/Resources/KeypairTest.php | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Resources/Keypair.php b/src/Resources/Keypair.php index bf4c6bd0..1d259360 100755 --- a/src/Resources/Keypair.php +++ b/src/Resources/Keypair.php @@ -280,15 +280,12 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI { parent::handleResponse($response, $method); - $paymentTypes = []; + // Replace rather than append: for the plain endpoint updateValues() already fills the + // list from the availablePaymentTypes string array, so appending here duplicates every entry. if (isset($response->paymentTypes)) { - $paymentTypes = $response->paymentTypes; + $this->paymentTypes = array_values($response->paymentTypes); } elseif (isset($response->availablePaymentTypes)) { - $paymentTypes = $response->availablePaymentTypes; - } - - foreach ($paymentTypes as $paymentType) { - $this->paymentTypes[] = $paymentType; + $this->paymentTypes = array_values($response->availablePaymentTypes); } } diff --git a/test/unit/Resources/KeypairTest.php b/test/unit/Resources/KeypairTest.php index 8801f995..fd15a415 100755 --- a/test/unit/Resources/KeypairTest.php +++ b/test/unit/Resources/KeypairTest.php @@ -94,7 +94,7 @@ public function aKeypairShouldBeUpdatedThroughResponseHandling(): void $keypair->handleResponse($testResponse); // then - $this->assertArrayContains($paymentTypes, $keypair->getPaymentTypes()); + $this->assertSame($paymentTypes, $keypair->getPaymentTypes()); $this->assertEquals('s-pub-1234', $keypair->getPublicKey()); $this->assertEquals('s-priv-4321', $keypair->getPrivateKey()); $this->assertTrue($keypair->isCof()); @@ -205,4 +205,29 @@ public function exposeContainsAllRelevantKeyDataExceptPrivateKeyAndDetailed(): v $this->assertArrayNotHasKey('privateKey', $exposed); $this->assertArrayNotHasKey('detailed', $exposed); } + + /** + * Verify the plain keypair response (string payment types) is not duplicated, + * stays idempotent across repeated handling, and exposes the de-duplicated list. + * + * @test + */ + public function plainKeypairPaymentTypesAreNotDuplicated(): void + { + $keypair = new Keypair(); + $paymentTypes = ['card', 'paypal', 'ideal', 'sofort']; + $response = (object)['publicKey' => 's-pub-1234', 'availablePaymentTypes' => $paymentTypes]; + + $keypair->handleResponse($response); + $this->assertSame($paymentTypes, $keypair->getPaymentTypes()); + $this->assertSame($paymentTypes, $keypair->getAvailablePaymentTypes()); + + // Handling the same response again must not add further entries. + $keypair->handleResponse($response); + $this->assertSame($paymentTypes, $keypair->getPaymentTypes()); + + // expose() and jsonSerialize() must return the same, de-duplicated list. + $this->assertSame($paymentTypes, $keypair->expose()['paymentTypes']); + $this->assertSame($paymentTypes, json_decode($keypair->jsonSerialize(), true)['paymentTypes']); + } } From 0f01225ef293febe601cf6363a799c439d727985 Mon Sep 17 00:00:00 2001 From: David Owusu <33735090+Ryouzanpaku@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:12:50 +0100 Subject: [PATCH 2/2] [CC-4255] Harden payment-type list handling against object-shaped responses array_values() rejects a stdClass, so a payment-type list that arrives with non-sequential keys (decoded as a JSON object, e.g. after an upstream array_filter) crashed fetchKeypair() with a TypeError on both the plain and detailed endpoints. Cast to array first to restore the tolerant behaviour the previous foreach had, keeping the de-duplication fix intact. Addresses QA finding on PR #250 and preserves the "detailed behaviour unchanged" AC. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Resources/Keypair.php | 6 +++-- test/unit/Resources/KeypairTest.php | 38 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Resources/Keypair.php b/src/Resources/Keypair.php index b736738f..7af52ce4 100755 --- a/src/Resources/Keypair.php +++ b/src/Resources/Keypair.php @@ -282,10 +282,12 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI // Replace rather than append: for the plain endpoint updateValues() already fills the // list from the availablePaymentTypes string array, so appending here duplicates every entry. + // Cast to array first: a list with non-sequential keys decodes to stdClass, which + // array_values() would reject, so this keeps the tolerant behaviour of the previous foreach. if (isset($response->paymentTypes)) { - $this->paymentTypes = array_values($response->paymentTypes); + $this->paymentTypes = array_values((array) $response->paymentTypes); } elseif (isset($response->availablePaymentTypes)) { - $this->paymentTypes = array_values($response->availablePaymentTypes); + $this->paymentTypes = array_values((array) $response->availablePaymentTypes); } // Booleans may arrive as native values or as strings ("true"/"false"); normalise both. diff --git a/test/unit/Resources/KeypairTest.php b/test/unit/Resources/KeypairTest.php index 0dcafb27..0c072a89 100755 --- a/test/unit/Resources/KeypairTest.php +++ b/test/unit/Resources/KeypairTest.php @@ -231,6 +231,44 @@ public function plainKeypairPaymentTypesAreNotDuplicated(): void $this->assertSame($paymentTypes, json_decode($keypair->jsonSerialize(), true)['paymentTypes']); } + /** + * A payment-type list with non-sequential keys decodes to a stdClass rather than an + * array. handleResponse() must still read it as a plain, re-indexed list instead of + * throwing a TypeError from array_values(). + * + * @test + * + * @dataProvider objectShapedPaymentTypeListDP + * + * @param \stdClass $response + * @param array $expected + */ + public function objectShapedPaymentTypeListIsReadAsList(\stdClass $response, array $expected): void + { + $keypair = new Keypair(); + $keypair->handleResponse($response); + + $this->assertEquals($expected, $keypair->getPaymentTypes()); + $this->assertSame([0, 1], array_keys($keypair->getPaymentTypes())); + } + + /** + * @return array + */ + public function objectShapedPaymentTypeListDP(): array + { + return [ + 'plain, gapped keys' => [ + json_decode('{"publicKey":"s-pub-stub","availablePaymentTypes":{"0":"card","2":"paypal"}}'), + ['card', 'paypal'], + ], + 'detailed, gapped keys' => [ + json_decode('{"publicKey":"s-pub-stub","paymentTypes":{"0":{"type":"card"},"2":{"type":"paypal"}}}'), + [(object)['type' => 'card'], (object)['type' => 'paypal']], + ], + ]; + } + /** * Verify boolean flags are parsed correctly whether the API sends them as native * booleans or as strings, and that expose() keeps them native booleans.