cancel
Showing results for 
Search instead for 
Did you mean: 

Question about Adobe Commerce (Magento) enpoints

Question about Adobe Commerce (Magento) enpoints

Hello, how are you?

 

I have some questions about Adobe Commerce (Magento) and would appreciate your help:

 

Order Notification:

Is there any endpoint or webhook I can use to be notified whenever a user places an order?

 

Abandoned Carts:

Is there any endpoint or webhook available to view abandoned carts?

 

Thank you in advance for your attention!

 

2 REPLIES 2

Re: Question about Adobe Commerce (Magento) enpoints

We think there are two solutions for you to solve this:

Solution 1: Create a new module

To achieve this, you will need to modify the code, which may be somewhat complex. You can try the following steps.

 

1. Create the module directory structure:

app/code/Webhook/Notification/

 

2. Create the `events.xml` file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_save_after">
<observer name="webhook_order_update" instance="Webhook\Notification\Observer\OrderUpdateObserver"/>
</event>
<event name="checkout_cart_product_add_after">
<observer name="webhook_cart_update" instance="Webhook\Notification\Observer\CartUpdateObserver"/>
</event>
</config>

 

3. Create the `WebhookService.php` file:

<?php
namespace Webhook\Notification\Model;

use Magento\Framework\HTTP\Client\Curl;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

class WebhookService
{
    private $curl;
    private $json;
    private $storeManager;
    private $scopeConfig;

    public function __construct(
        Curl $curl,
        Json $json,
        StoreManagerInterface $storeManager,
        ScopeConfigInterface $scopeConfig
    ) {
        $this->curl = $curl;
        $this->json = $json;
        $this->storeManager = $storeManager;
        $this->scopeConfig = $scopeConfig;
    }

    public function sendWebhook($event, $data)
    {
        if (!$this->isEnabled()) {
            return false;
        }

        $webhookUrl = $this->getWebhookUrl();
        $secretKey = $this->getSecretKey();

        $payload = [
            'event' => $event,
            'data' => $data,
            'timestamp' => time()
        ];

        $signature = hash_hmac('sha256', $this->json->serialize($payload), $secretKey);

        $this->curl->addHeader('Content-Type', 'application/json');
        $this->curl->addHeader('X-Webhook-Signature', $signature);

        try {
            $this->curl->post($webhookUrl, $this->json->serialize($payload));
            return $this->curl->getStatus() === 200;
        } catch (\Exception $e) {
            return false;
        }
    }

    private function isEnabled()
    {
        return $this->scopeConfig->isSetFlag('webhook/notification/enabled');
    }

    private function getWebhookUrl()
    {
        return $this->scopeConfig->getValue('webhook/notification/webhook_url');
    }

    private function getSecretKey()
    {
        return $this->scopeConfig->getValue('webhook/notification/secret_key');
    }
}

 

4. Create the `OrderUpdateObserver.php` and `CartUpdateObserver.php` files in the `Observer` directory:

<?php
namespace Webhook\Notification\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Webhook\Notification\Model\WebhookService;

class OrderUpdateObserver implements ObserverInterface
{
private $webhookService;

public function __construct(WebhookService $webhookService)
{
$this->webhookService = $webhookService;
}

public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$this->webhookService->sendWebhook('order_update', $order->getData());
}
}

 

<?php
namespace Webhook\Notification\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Webhook\Notification\Model\WebhookService;

class CartUpdateObserver implements ObserverInterface
{
private $webhookService;

public function __construct(WebhookService $webhookService)
{
$this->webhookService = $webhookService;
}

public function execute(Observer $observer)
{
$cart = $observer->getEvent()->getCart();
$this->webhookService->sendWebhook('cart_update', $cart->getData());
}
}

 

5. Enable the module:
Run the following commands in the terminal:

php bin/magento module:enable Webhook_Notification
php bin/magento setup:upgrade
php bin/magento cache:clean

 

By following these steps, you will have created a basic Magento 2 module named `Webhook_Notification` that defines a web API endpoint for handling webhook notifications.

 

Solution 2: Use a Webhook extension

Alternatively, if you prefer not to develop a custom module, you can explore existing webhook extensions provided by third-party Magento vendors (like Mageplaza). Many extension providers offer ready-made solutions that allow you to configure and manage webhook notifications without requiring extensive coding.

Hope this works for you! Smiley Happy 

 

Mageplaza | Top-Rated Magento Extension and Solution Provider


Should you have any questions or concerns, feel free to contact us via consultant@mageplaza.com

Re: Question about Adobe Commerce (Magento) enpoints

Hello, everything is good? I need to develop an API that will receive the purchase data once it's finalized so I can send a message via messaging apps. For that, I would need the documentation for these endpoints and webhooks. Could you provide me with the necessary information to proceed with the development?