Select Git revision
OrderUpdateWebhook.php
OrderUpdateWebhook.php 3.54 KiB
<?php
namespace BobGroup\BobGo\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\HTTP\Client\Curl;
use BobGroup\BobGo\Model\Carrier\UData;
use Psr\Log\LoggerInterface;
use Magento\Store\Model\StoreManagerInterface;
class OrderUpdateWebhook implements ObserverInterface
{
protected Curl $curl;
protected LoggerInterface $logger;
protected StoreManagerInterface $storeManager;
public function __construct(LoggerInterface $logger, Curl $curl, StoreManagerInterface $storeManager)
{
$this->logger = $logger;
$this->curl = $curl;
$this->storeManager = $storeManager;
}
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
if (!$order) {
return;
}
// Extract order data and send to the webhook URL
$this->sendWebhook($order, 'order_updated');
}
private function sendWebhook($order, $eventType)
{
// Webhook URL
$url = $this->getWebhookUrl();
// Extract order items
$itemsData = [];
foreach ($order->getAllItems() as $item) {
$itemsData[] = $item->getData();
}
// Extract shipping address
$shippingAddress = $order->getShippingAddress();
$shippingAddressData = $shippingAddress ? $shippingAddress->getData() : [];
// Extract billing address
$billingAddress = $order->getBillingAddress();
$billingAddressData = $billingAddress ? $billingAddress->getData() : [];
// Prepare payload
$data = [
'event' => $eventType,
'order_id' => $order->getId(),
'channel_identifier' => $this->getStoreUrl(),
'store_id' => $this->getStoreId(),
'order_data' => $order->getData(),
'items' => $itemsData,
'shipping_address' => $shippingAddressData,
'billing_address' => $billingAddressData,
];
// Send the webhook
$this->makeHttpPostRequest($url, $data);
}