Skip to content
Snippets Groups Projects
Select Git revision
  • a092e7cc9bfb79d94f27535a560a9be37991ab59
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

OrderWebhookBase.php

Blame
  • OrderWebhookBase.php 2.21 KiB
    <?php
    
    namespace BobGroup\BobGo\Observer;
    
    use BobGroup\BobGo\Model\Carrier\UData;
    use Magento\Framework\App\Config\ScopeConfigInterface;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\HTTP\Client\Curl;
    use Magento\Store\Model\StoreManagerInterface;
    use Psr\Log\LoggerInterface;
    use BobGroup\BobGo\Model\Carrier\BobGo;
    
    abstract class OrderWebhookBase implements ObserverInterface
    {
        protected Curl $curl;
        protected LoggerInterface $logger;
        protected StoreManagerInterface $storeManager;
        protected ScopeConfigInterface $scopeConfig;
        protected $bobGo;
    
        public function __construct(LoggerInterface $logger, Curl $curl, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, BobGo $bobGo)
        {
            $this->logger = $logger;
            $this->curl = $curl;
            $this->storeManager = $storeManager;
            $this->scopeConfig = $scopeConfig;
            $this->bobGo = $bobGo;
        }
    
        protected function sendWebhook($order)
        {
            // Return early if not enabled
            if (!$this->bobGo->isWebhookEnabled()) {
                return;
            }
    
            // Webhook URL
            $url = $this->getWebhookUrl();
    
            $storeId = $this->getStoreId();
    
            $orderId = $order->getId();
    
            // Prepare payload
            $data = [
                'event' => 'order_updated',
                'order_id' => $orderId,
                'channel_identifier' => $this->bobGo->getBaseUrl(),
                'store_id' => $storeId,
                'webhooks_enabled' => true, // If we get to this point webhooks are enabled
            ];
    
            // Generate the signature using the webhook key saved in config
            $webhookKey = $this->scopeConfig->getValue('carriers/bobgo/webhook_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
            // Check if the webhook key is empty and return false
            if (empty($webhookKey)) {
                return;
            }
    
            // Send the webhook
            $this->bobGo->encodeWebhookAndPostRequest($url, $data, $storeId, $webhookKey);
        }
    
        private function getWebhookUrl(): string
        {
            return UData::WEBHOOK_URL;
        }
    
        private function getStoreId(): string
        {