Skip to content
Snippets Groups Projects
Select Git revision
  • 9d91f05abd105839cda7e36e85312fca08884a49
  • 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
  • Index.php 3.28 KiB
    <?php
    namespace BobGroup\BobGo\Controller\Tracking;
    
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Framework\Registry;
    use Psr\Log\LoggerInterface;
    use Magento\Framework\Controller\Result\JsonFactory;
    use Magento\Framework\HTTP\Client\Curl;
    use Magento\Store\Model\StoreManagerInterface;
    use BobGroup\BobGo\Model\Carrier\UData;
    
    class Index extends \Magento\Framework\App\Action\Action
    {
        protected $resultPageFactory;
        protected $jsonFactory;
        protected $curl;
        protected $logger;
        protected $registry;
        protected StoreManagerInterface $storeManager;
    
        public function __construct(
            Context $context,
            PageFactory $resultPageFactory,
            JsonFactory $jsonFactory,
            LoggerInterface $logger,
            StoreManagerInterface $storeManager,
            Curl $curl,
            Registry $registry // Add Registry
        ) {
            $this->resultPageFactory = $resultPageFactory;
            $this->jsonFactory = $jsonFactory;
            $this->logger = $logger;
            $this->storeManager = $storeManager;
            $this->curl = $curl;
            $this->registry = $registry; // Assign Registry
            parent::__construct($context);
        }
    
        public function execute()
        {
            $this->logger->info('Page Controller is executed.');
            $trackingReference = $this->getRequest()->getParam('order_reference');
    
            $this->logger->info('Tracking reference:', [$trackingReference]);
    
            $channel = $this->getStoreUrl();
    
            $this->logger->info('Channel:', [$channel]);
    
            if ($trackingReference) {
                $trackingUrl = sprintf(UData::TRACKING, $channel, $trackingReference);
    
                try {
                    $this->curl->get($trackingUrl);
                    $response = $this->curl->getBody();
    
                    $this->logger->info('Response:', [$response]);
    
                    $decodedResponse = json_decode($response, true);
                    $this->logger->info('Decoded Response:', [$decodedResponse]);
    
                    if (is_array($decodedResponse) && isset($decodedResponse[0])) {
                        $shipmentData = $decodedResponse[0];
    
                        // Save data to the registry
                        $this->registry->register('shipment_data', $shipmentData);
                        $this->logger->info('Shipment data registered in the registry.');
    
                    } else {
                        $this->logger->info('Unexpected response format.');
                    }
    
                } catch (\Exception $e) {
                    $this->logger->info('Track error: ' . $e->getMessage());
                    $this->messageManager->addErrorMessage(__('Unable to track the order.'));
                }
            }
    
            return $this->resultPageFactory->create();
        }
    
    
        private function getStoreUrl(): string
        {
            $url = $this->storeManager->getStore()->getBaseUrl();
    
            // Remove protocol (http:// or https://)
            $host = preg_replace('#^https?://#', '', $url);
    
            // Ensure $host is a string before using it in explode
            $host = $host ?? '';
    
            // Remove everything after the host (e.g., paths, query strings)
            $host = explode('/', $host)[0];
    
            // If the host starts with 'www.', remove it
            if (strpos($host, 'www.') === 0) {
                $host = substr($host, 4);
            }
    
            return $host;
        }
    }