Skip to content

[v10] Fix setState() silent failure causing state/status desynchronization#3290

Open
acourtiol wants to merge 1 commit into
Adyen:mainfrom
acourtiol:fix/setstate-silent-failure-v10
Open

[v10] Fix setState() silent failure causing state/status desynchronization#3290
acourtiol wants to merge 1 commit into
Adyen:mainfrom
acourtiol:fix/setstate-silent-failure-v10

Conversation

@acourtiol

Copy link
Copy Markdown

This is the v10 equivalent of #3289 (submitted against main-9).

What this PR does

Adds a fallback in Helper/Order.php::setState() so that when the $possibleStates constraint (from STATE_TRANSITION_MATRIX) doesn't produce a match, the method falls back to a direct lookup in sales_order_status_state for the given status. This prevents the order state from being permanently stuck at its previous value while the status advances.

Why this is needed

Currently, setState() iterates $possibleStates and if no match is found, it logs a notification and returns without changing the state. Meanwhile, the status was already updated by the caller (addStatusHistoryComment calls setStatus() internally). This leaves the order with a mismatched state/status that never gets corrected.

On our production instance, this resulted in 63,805 orders with state=pending_payment + status=complete/closed since 2021. The code is identical in v9.20.13 and v10.10.0.

How it works

After the existing $possibleStates loop fails, the fix does a direct lookup in sales_order_status_state:

$fallbackStatus = $this->orderStatusCollectionFactory->create()
    ->addFieldToFilter('main_table.status', $status)
    ->joinStates()
    ->getFirstItem();

if ($fallbackStatus->getState()) {
    $order->setState($fallbackStatus->getState());
    return $order;
}

The preferred STATE_TRANSITION_MATRIX states are still tried first. The fallback only activates when no preferred match is found.

Related

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a fallback mechanism in the setState method to resolve order states directly from the database when preferred state mappings are unavailable, preventing state/status desynchronization. Feedback suggests improving logging consistency by using the getOrderContext method and ensuring deterministic results in the fallback query by ordering by the is_default flag.

Comment thread Helper/Order.php
Comment thread Helper/Order.php Outdated
Comment thread Helper/Order.php Outdated
@acourtiol acourtiol requested a review from a team as a code owner April 11, 2026 18:37
@acourtiol acourtiol force-pushed the fix/setstate-silent-failure-v10 branch from 86f3e42 to ed449a9 Compare April 11, 2026 18:37
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 Adyen#3288
@acourtiol acourtiol force-pushed the fix/setstate-silent-failure-v10 branch from ed449a9 to 95561f1 Compare April 11, 2026 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Helper/Order.php: setState() silently fails when status maps to a state not in STATE_TRANSITION_MATRIX, causing permanent state/status desync

1 participant