Calling a Magento event Observer AFTER payment capture

Calling a Magento event Observer AFTER payment capture

October 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Calling a Magento event Observer AFTER payment capture

Magento Event Observer After Successful Payment Capture

As a senior developer specializing in eCommerce platforms, I often encounter challenges related to executing code after successful payment processing in Magento. In this post, we’ll explore the best practice for calling an observer after a Magento order is created and the payment has been successfully captured.

Understanding the Magento Event Lifecycle

Magento’s event-driven architecture allows you to tap into various points in the order lifecycle. Some key events include:

  • checkout_submit_all_after: Fires after all checkout steps are completed.
  • sales_order_payment_place_end: Executes after placing an order payment.
  • sales_order_place_after, sales_order_payment_pay, and sales_order_payment_capture: Relate to the order placement process but do not guarantee successful capture.

However, these events may fire before the payment is actually captured or in case of a failed transaction. This ambiguity makes it challenging to ensure that your observer only triggers on successful orders.

A Better Approach: sales_order_place_after

The sales_order_place_after event appears promising, as it fires immediately after an order is placed and before any other processes are executed. However, this event doesn’t guarantee the payment status or the final success of the transaction.

To overcome this limitation, we can combine this event with a custom observer to check for specific conditions before executing our desired functionality.

Implementing a Custom Observer

Let’s create a custom observer that checks if the order has been successfully captured before firing our logic. Follow these steps:

  1. Create your own module and define an observer in etc/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer name="your_module_observer" instance="Your\Module\Model\Observer"/>
    </event>
</config>
  1. Implement the observer in Model/Observer.php:
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

class Observer implements ObserverInterface
{
    private $orderRepository;

    public function __construct(OrderRepositoryInterface $orderRepository)
    {
        $this->orderRepository = $orderRepository;
    }

    public function execute(\Magento\Framework\Event\Observer $observer): void
    {
        $orderId = $observer->getOrder()->getEntityId();
        $order = $this->orderRepository->get($orderId);

        if ($order->getPayment()->getTransactionCollection()->getSize() > 0) {
            // Payment has been captured, perform your actions here.
            // Your logic goes here
        }
    }
}
  1. Enable and deploy the module in your Magento installation.

Conclusion

To ensure that your observer only triggers after a successful payment capture in Magento, we recommend using the sales_order_place_after event combined with custom logic to verify if the transaction has been processed.

By implementing this approach, you can confidently execute your functionality knowing it will only run on orders where the payment was successfully captured. This method provides a clean separation of concerns and adheres to Magento’s best practices for extension development.

Remember to thoroughly test your implementation in different scenarios to guarantee its reliability. If you need further assistance or want to learn more about Magento’s event system, feel free to explore our comprehensive guide on Magento event observers.

Happy coding!

Checkout Extensions Orders

Our network