Fix duplicate domain activation from stale order object during checkout (#3948)

* Fix duplicate domain activation caused by stale order object

Cart checkout can activate an order that was already activated
synchronously via an invoice paid with account credit, since the
in-memory order held by the checkout loop is never refreshed after
the credit-payment path activates it through its own freshly-loaded
copy. activateOrder() now re-fetches the order before checking its
status, closing the gap that let EPP domain registration run twice
and fail with "Domain is not available: In use".

* Treat already-active orders as a no-op in activateOrder

The reload alone turned the stale-order race into a thrown exception
for orders that were already correctly activated by another code
path in the same request. Cart::checkoutCart()'s catch block then
tried to record an 'error' order status, which isn't a valid status
and threw a second exception that broke the whole checkout. An
already-active order (without force) should just be a no-op success.

* Update src/modules/Order/Service.php

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This commit is contained in:
Adam Daleyandcubic-dev-ai[bot] authored and GitHub committed 2026-07-09 16:13:13 +01:00
1 parent 81ae63c820
commit 60ef50108d
2 files changed
+83 -4

No files matched your search

+15 -4
View File
@@ -879,14 +879,25 @@ class Service implements InjectionAwareInterface
public function activateOrder(\Model_ClientOrder $order, $data = []): bool
{
// re-fetch in case the caller's order object is stale (e.g. already activated by another code path)
$orderId = $order->id;
$order = $this->di['db']->load('ClientOrder', $orderId);
if (!$order instanceof \Model_ClientOrder) {
throw new \FOSSBilling\Exception('Order :id not found', [':id' => $orderId]);
}
$force = !empty($data['force']);
// Already active and not a forced re-activation: nothing to do.
if ($order->status === \Model_ClientOrder::STATUS_ACTIVE && !$force) {
return true;
}
$statues = [
\Model_ClientOrder::STATUS_PENDING_SETUP,
\Model_ClientOrder::STATUS_FAILED_SETUP,
];
if (!in_array($order->status, $statues)) {
if (!isset($data['force']) || !$data['force']) {
throw new \FOSSBilling\Exception('Only pending setup or failed orders can be activated');
}
if (!in_array($order->status, $statues) && !$force) {
throw new \FOSSBilling\Exception('Only pending setup or failed orders can be activated');
}
$event_params = ['id' => $order->id];
@@ -1669,7 +1669,14 @@ test('activateOrder throws for non-pending order', function (): void {
$clientOrderModel->loadBean(new Tests\Helpers\DummyBean());
$clientOrderModel->status = Model_ClientOrder::STATUS_CANCELED;
$dbMock = Mockery::mock(Box_Database::class);
$dbMock->shouldReceive('load')->atLeast()->once()->with('ClientOrder', Mockery::any())->andReturn($clientOrderModel);
$di = container();
$di['db'] = $dbMock;
$svc = new Service();
$svc->setDi($di);
expect(fn (): bool => $svc->activateOrder($clientOrderModel))
->toThrow(FOSSBilling\Exception::class, 'Only pending setup or failed orders can be activated');
@@ -1681,10 +1688,14 @@ test('activateOrder activates pending order', function (): void {
$clientOrderModel->status = Model_ClientOrder::STATUS_PENDING_SETUP;
$clientOrderModel->group_master = 1;
$dbMock = Mockery::mock(Box_Database::class);
$dbMock->shouldReceive('load')->atLeast()->once()->with('ClientOrder', Mockery::any())->andReturn($clientOrderModel);
$eventMock = Mockery::mock(Box_EventManager::class);
$eventMock->shouldReceive('fire')->atLeast()->once();
$di = container();
$di['db'] = $dbMock;
$di['events_manager'] = $eventMock;
$di['logger'] = new Box_Log();
@@ -1700,6 +1711,63 @@ test('activateOrder activates pending order', function (): void {
expect($result)->toBeTrue();
});
test('activateOrder is a no-op when order was already activated by a stale reference', function (): void {
$staleOrderModel = new Model_ClientOrder();
$staleOrderModel->loadBean(new Tests\Helpers\DummyBean());
$staleOrderModel->status = Model_ClientOrder::STATUS_PENDING_SETUP;
$activeOrderModel = new Model_ClientOrder();
$activeOrderModel->loadBean(new Tests\Helpers\DummyBean());
$activeOrderModel->status = Model_ClientOrder::STATUS_ACTIVE;
$activeOrderModel->group_master = 1;
$dbMock = Mockery::mock(Box_Database::class);
$dbMock->shouldReceive('load')->atLeast()->once()->with('ClientOrder', Mockery::any())->andReturn($activeOrderModel);
$di = container();
$di['db'] = $dbMock;
$serviceMock = Mockery::mock(Service::class)->makePartial();
$serviceMock->shouldAllowMockingProtectedMethods();
$serviceMock->shouldReceive('createFromOrder')->never();
$serviceMock->shouldReceive('activateOrderAddons')->never();
$serviceMock->setDi($di);
$result = $serviceMock->activateOrder($staleOrderModel);
expect($result)->toBeTrue();
});
test('activateOrder force re-activates an already active order', function (): void {
$activeOrderModel = new Model_ClientOrder();
$activeOrderModel->loadBean(new Tests\Helpers\DummyBean());
$activeOrderModel->status = Model_ClientOrder::STATUS_ACTIVE;
$activeOrderModel->group_master = 1;
$dbMock = Mockery::mock(Box_Database::class);
$dbMock->shouldReceive('load')->atLeast()->once()->with('ClientOrder', Mockery::any())->andReturn($activeOrderModel);
$eventMock = Mockery::mock(Box_EventManager::class);
$eventMock->shouldReceive('fire')->atLeast()->once();
$di = container();
$di['db'] = $dbMock;
$di['events_manager'] = $eventMock;
$di['logger'] = new Box_Log();
$serviceMock = Mockery::mock(Service::class)->makePartial();
$serviceMock->shouldAllowMockingProtectedMethods();
$serviceMock->shouldReceive('createFromOrder')->atLeast()->once()->andReturn([]);
$serviceMock->shouldReceive('activateOrderAddons')->atLeast()->once();
$serviceMock->setDi($di);
$result = $serviceMock->activateOrder($activeOrderModel, ['force' => true]);
expect($result)->toBeTrue();
});
test('activateOrderAddons activates addons', function (): void {
$order = new Model_ClientOrder();
$order->loadBean(new Tests\Helpers\DummyBean());