Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/Resources/Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
65 changes: 64 additions & 1 deletion test/unit/Resources/KeypairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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.
Expand Down
Loading