Skip to content

Commit ed449a9

Browse files
committed
Fix setState() silent failure causing state/status desynchronization
When setState() cannot find a matching state in the $possibleStates array (from STATE_TRANSITION_MATRIX), it now falls back to a direct lookup in sales_order_status_state for the given status, preferring the default state mapping. This prevents the order state from being permanently stuck at its previous value (typically pending_payment) while the status advances through the order lifecycle. The fallback query uses addOrder('is_default', 'DESC') to prefer the default state when a status maps to multiple states, and setPageSize(1) to limit the SQL query at the database level. Uses getOrderContext() for consistent logging with the rest of the module. Fixes #3288
1 parent 11cda6a commit ed449a9

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

Helper/Order.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,39 @@ private function setState(MagentoOrder $order, $status, $possibleStates): Magent
493493
}
494494

495495
$this->adyenLogger->addAdyenNotification(
496-
'No new state assigned, status should be connected to one of the following states: ' . json_encode($possibleStates),
496+
'No new state assigned via preferred states, attempting fallback lookup. '
497+
. 'Status should be connected to one of the following states: ' . json_encode($possibleStates),
497498
[
498499
'pspReference' => $order->getPayment()->getData('adyen_psp_reference'),
499500
'merchantReference' => $order->getPayment()->getData('entity_id')
500501
]);
501502

503+
// Fallback: resolve state directly from sales_order_status_state without
504+
// constraining to $possibleStates. This prevents state/status desynchronization
505+
// when the configured status maps to a state not in STATE_TRANSITION_MATRIX.
506+
// See https://github.com/Adyen/adyen-magento2/issues/3288
507+
$fallbackStatus = $this->orderStatusCollectionFactory->create()
508+
->addFieldToFilter('main_table.status', $status)
509+
->joinStates()
510+
->addOrder('is_default', 'DESC')
511+
->setPageSize(1)
512+
->getFirstItem();
513+
514+
if ($fallbackStatus->getState()) {
515+
$order->setState($fallbackStatus->getState());
516+
$this->adyenLogger->addAdyenNotification(
517+
'State set via fallback to ' . $fallbackStatus->getState()
518+
. ' (status "' . $status . '" not mapped to preferred states: '
519+
. json_encode($possibleStates) . ')',
520+
array_merge(
521+
$this->adyenLogger->getOrderContext($order),
522+
['pspReference' => $order->getPayment()->getData('adyen_psp_reference')]
523+
)
524+
);
525+
526+
return $order;
527+
}
528+
502529
return $order;
503530
}
504531

0 commit comments

Comments
 (0)