PHP / Magento Dev Blog

  • Home

Magento 2 duplicate orders – lack of idempotency in checkout

by Henryk Tews / Sunday, 05 July 2026 / Published in Magento / Adobe Commerce

The shop was generating duplicate orders – a customer clicked “Order” once but two identical orders were created with double payment. The problem affected ~2% of orders, mainly on slow internet connections. Fix time: 6 hours.

Symptoms

  • Admin panel shows pairs of identical orders seconds apart
  • Customers complain about double card charges
  • Problem more frequent on slow connections and mobile devices
  • Logs show two POST requests to /rest/V1/carts/mine/payment-information at the same time

Cause

The “Order” button was not disabled after the first click. On slow connections users clicked again thinking nothing had happened. Two parallel requests reached the server – both succeeded because there was no idempotency mechanism.

Solution

<?php
class PlaceOrderIdempotencyPlugin
{
    private const LOCK_TTL = 30;

    public function __construct(
        private \Magento\Framework\Cache\FrontendInterface $cache,
    ) {}

    public function aroundSavePaymentInformationAndPlaceOrder(
        \Magento\Checkout\Api\PaymentInformationManagementInterface $subject,
        callable $proceed,
        int $cartId,
        \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
        ?\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
    ): int {
        $lockKey = 'order_lock_' . $cartId;

        if ($this->cache->load($lockKey)) {
            throw new \Magento\Framework\Exception\LocalizedException(
                __('Your order is being processed. Please wait.')
            );
        }

        $this->cache->save('1', $lockKey, [], self::LOCK_TTL);

        try {
            return $proceed($cartId, $paymentMethod, $billingAddress);
        } finally {
            $this->cache->remove($lockKey);
        }
    }
}

Takeaways

Any operation that should execute exactly once requires an idempotency mechanism. In checkout: disable button on the JS side + idempotency key on the server side. Redis with TTL is the ideal mechanism for short-lived locks.

About Henryk Tews

What you can read next

Xdebug – configuration, PHPStorm, debugging Magento plugins
Strategy pattern in PHP – and how Magento 2 uses it in pricing

© 2026 Created by

TOP
Zarządzaj zgodą
Aby zapewnić jak najlepsze wrażenia, korzystamy z technologii, takich jak pliki cookie, do przechowywania i/lub uzyskiwania dostępu do informacji o urządzeniu. Zgoda na te technologie pozwoli nam przetwarzać dane, takie jak zachowanie podczas przeglądania lub unikalne identyfikatory na tej stronie. Brak wyrażenia zgody lub wycofanie zgody może niekorzystnie wpłynąć na niektóre cechy i funkcje.
Funkcjonalne Always active
Przechowywanie lub dostęp do danych technicznych jest ściśle konieczny do uzasadnionego celu umożliwienia korzystania z konkretnej usługi wyraźnie żądanej przez subskrybenta lub użytkownika, lub wyłącznie w celu przeprowadzenia transmisji komunikatu przez sieć łączności elektronicznej.
Preferencje
Przechowywanie lub dostęp techniczny jest niezbędny do uzasadnionego celu przechowywania preferencji, o które nie prosi subskrybent lub użytkownik.
Statystyka
Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do celów statystycznych. Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do anonimowych celów statystycznych. Bez wezwania do sądu, dobrowolnego podporządkowania się dostawcy usług internetowych lub dodatkowych zapisów od strony trzeciej, informacje przechowywane lub pobierane wyłącznie w tym celu zwykle nie mogą być wykorzystywane do identyfikacji użytkownika.
Marketing
Przechowywanie lub dostęp techniczny jest wymagany do tworzenia profili użytkowników w celu wysyłania reklam lub śledzenia użytkownika na stronie internetowej lub na kilku stronach internetowych w podobnych celach marketingowych.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Zobacz preferencje
  • {title}
  • {title}
  • {title}