diff --git a/src/Resources/Keypair.php b/src/Resources/Keypair.php index 4f2956f6..7af52ce4 100755 --- a/src/Resources/Keypair.php +++ b/src/Resources/Keypair.php @@ -280,15 +280,14 @@ 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. + // 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)) { - $paymentTypes = $response->paymentTypes; + $this->paymentTypes = array_values((array) $response->paymentTypes); } elseif (isset($response->availablePaymentTypes)) { - $paymentTypes = $response->availablePaymentTypes; - } - - foreach ($paymentTypes as $paymentType) { - $this->paymentTypes[] = $paymentType; + $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 49fa711c..0c072a89 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()); @@ -206,6 +206,69 @@ public function exposeContainsAllRelevantKeyDataExceptPrivateKeyAndDetailed(): v $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']); + } + + /** + * 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.