How to trigger an event on payment received in magento?

How to trigger an event on payment received in magento?

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to trigger an event on payment received in magento?

Triggering Events on Payment Confirmation in Magento

Hello! I understand your need for a comprehensive guide to trigger an event when an order is confirmed as paid in Magento. Let me walk you through the process step by step.

Understanding the Problem Domain

In this scenario, we want to create an automated system where:

  1. A specific customer (customer ID 1) meets certain conditions.
  2. An event is triggered when those conditions are met.
  3. The event changes the customer’s group to ‘Silver VIP’ (group ID 4).

Prerequisites

Before we dive into the code, ensure you have a Magento installation up and running. You’ll need access to the admin panel with sufficient permissions.

Setting Up Events in Magento

Magento uses an Observer Pattern for events. To create a new event:

  1. Navigate to app/etc/modules and open your custom module’s configuration file (e.g., App_Company_MyModule.xml).

  2. Add or modify the <events> section to include your new event definition:

<events>
    <sales_order_payment_status_set>
        <observers>
            <my_module_observer>
                <type>singleton</type>
                <class>App\Company\MyModule\Observer\PaymentStatusSetObserver</class>
                <method>execute</method>
            </my_module_observer>
        </observers>
    </sales_order_payment_status_set>
</events>

Replace sales_order_payment_status_set with the proper Magento event that triggers when an order payment status changes.

Creating the Observer

Next, create the observer class in your module’s Observer directory:

namespace App\Company\MyModule\Observer;

use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
use Magento\Customer\Model\Session;

class PaymentStatusSetObserver implements ObserverInterface
{
    private $session;
    private $logger;

    public function __construct(
        Session $session,
        LoggerInterface $logger
    ) {
        $this->session = $session;
        $this->logger = $logger;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Your custom logic here to check the customer ID and conditions.
        // If conditions are met, change the customer group:
        if ($this->checkCustomerConditions()) {
            $customerId = $this->session->getCustomer()->getId();
            try {
                $customerGroup = 4; // Silver VIP
                $this->changeCustomerGroup($customerId, $customerGroup);
                $this->logger->info('Customer ID: ' . $customerId . ' has been updated to group ID: ' . $customerGroup);
            } catch (\Exception $e) {
                $this->logger->error('Error updating customer group: ' . $e->getMessage());
            }
        }
    }

    // Implement your custom methods here, e.g., checkCustomerConditions() and changeCustomerGroup()
}

Integrating with LaravelCompany

If you’re looking for more advanced functionality or a guided setup process, I recommend checking out LaravelCompany’s Magento Integration Guide. They provide in-depth tutorials on connecting your custom backends and APIs.

Conclusion

Triggering events based on payment status changes in Magento requires understanding the event system architecture. By following this guide, you should now be able to create a fully functional observer that responds to order payment status updates and applies custom logic accordingly.

Remember to replace the placeholders with your actual module names and implement the necessary checks and customer group changes as per your specific requirements.

Feel free to explore LaravelCompany’s Magento resources for more detailed tutorials on Magento development.

Security Extensions Admin Configuration

Our network