Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private function resolvePersistedSelection(array $paymentOptions): array
$selectedPaymentSelectionKey = $this->getSelectedPaymentSelectionKey();

if ($selectedPaymentModule === '' && $selectedPaymentSelectionKey === '') {
return ['', ''];
return $this->resolveDefaultSelection($paymentOptions);
}

if ($this->hasValidPersistedSelection($paymentOptions, $selectedPaymentSelectionKey, $selectedPaymentModule)) {
Expand All @@ -230,6 +230,40 @@ private function resolvePersistedSelection(array $paymentOptions): array

$this->clearPersistedSelection();

return $this->resolveDefaultSelection($paymentOptions);
}

/**
* Default to the first available payment option when the buyer holds no (valid) choice yet.
*
* Merchants order payment modules by position, so the first option is the shop's preferred
* method; preselecting it lets most buyers proceed without a mandatory extra decision at an
* unselected list, while any other option stays one click away. The existing render and
* client-side confirmation paths for a server-selected option are reused as-is.
*
* @param array<string, mixed> $paymentOptions
*
* @return array{0: string, 1: string}
*/
private function resolveDefaultSelection(array $paymentOptions): array
{
foreach ($paymentOptions as $moduleName => $moduleOptions) {
if (!is_array($moduleOptions)) {
continue;
}

foreach ($moduleOptions as $paymentOption) {
if (!is_array($paymentOption)) {
continue;
}

return [
(string) ($paymentOption['module_name'] ?? $moduleName),
(string) ($paymentOption['selection_key'] ?? ''),
];
}
}

return ['', ''];
}

Expand Down
55 changes: 55 additions & 0 deletions tests/php/Unit/Checkout/Ajax/OpcPaymentMethodsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,61 @@ public function write(): void
self::assertFalse($response['is_free']);
}

public function testItDefaultsToTheFirstPaymentOptionWhenNoSelectionIsPersisted(): void
{
$firstOption = [
'module_name' => 'ps_checkpayment',
'call_to_action_text' => 'Pay by Check',
'action' => '/module/ps_checkpayment/validation',
];
$secondOption = [
'module_name' => 'ps_wirepayment',
'call_to_action_text' => 'Wire payment',
'action' => '/module/ps_wirepayment/validation',
];
$expectedSelectionKey = (new PaymentSelectionKeyBuilder())->buildSelectionKey($firstOption);

$context = CheckoutTestFixtures::context([
'cart' => CheckoutTestFixtures::pricedCart(42.0, ['id' => 1]),
'country' => CheckoutTestFixtures::country(),
'shop' => CheckoutTestFixtures::shop(1),
]);
$context->cookie = new class {
public function __get(string $name)
{
return null;
}

public function __unset(string $name): void
{
}

public function write(): void
{
}
};

$finder = $this->getMockBuilder(\PaymentOptionsFinder::class)
->disableOriginalConstructor()
->onlyMethods(['present'])
->getMock();
$finder->method('present')->willReturn([
'ps_checkpayment' => [
0 => $firstOption,
],
'ps_wirepayment' => [
0 => $secondOption,
],
]);

$handler = new OnePageCheckoutPaymentMethodsHandler($context, $finder);
$response = $handler->handle();

self::assertTrue($response['success']);
self::assertSame('ps_checkpayment', $response['selected_payment_module']);
self::assertSame($expectedSelectionKey, $response['selected_payment_selection_key']);
}

public function testItReturnsStructuredErrorWhenCartIsMissing(): void
{
$context = CheckoutTestFixtures::context([
Expand Down