From 5c320b2df5db473aa9233772406a303a518213db Mon Sep 17 00:00:00 2001 From: "@ChristelLoftus" <christel@bob.co.za> Date: Thu, 3 Oct 2024 10:10:05 +0200 Subject: [PATCH] webhooks test update and webhooks payload --- Model/Carrier/BobGo.php | 4 --- Observer/ConfigChangeObserver.php | 14 ++++---- Observer/OrderCreateWebhook.php | 1 - Observer/OrderWebhookBase.php | 57 +++++-------------------------- 4 files changed, 17 insertions(+), 59 deletions(-) diff --git a/Model/Carrier/BobGo.php b/Model/Carrier/BobGo.php index 77b8e88..0e750fd 100644 --- a/Model/Carrier/BobGo.php +++ b/Model/Carrier/BobGo.php @@ -1074,10 +1074,6 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car // Convert the string to a boolean value $isEnabled = filter_var($isEnabled, FILTER_VALIDATE_BOOLEAN); -// if (!$webhookKey) { -// $this->_logger->error('Webhook key not configured.'); -// return false; -// } $storeId = strval($this->_storeManager->getStore()->getId()); diff --git a/Observer/ConfigChangeObserver.php b/Observer/ConfigChangeObserver.php index 528dd82..9cbde5d 100644 --- a/Observer/ConfigChangeObserver.php +++ b/Observer/ConfigChangeObserver.php @@ -74,21 +74,23 @@ class ConfigChangeObserver implements ObserverInterface // Test for webhooks if (is_array($changedPaths) && in_array('carriers/bobgo/enable_webhooks', $changedPaths)) { -// if ($this->bobGo->isWebhookEnabled()) { - $this->logger->info('Webhooks test start: '); - $result = $this->bobGo->triggerWebhookTest(); - $this->logger->info('Webhooks test end: ' . $result); + $this->logger->info('Webhooks test start: '); + $result = $this->bobGo->triggerWebhookTest(); + $this->logger->info('Webhooks test end: ' . $result); + if ($this->bobGo->isWebhookEnabled()) { if ($result) { $this->messageManager->addSuccessMessage( __('Webhook validation successful.') ); } else { $this->messageManager->addErrorMessage( - __('Webhook validation failed. Please check the webhook key and try again.') + __('Webhook validation failed. Please check your internet connection + and get the webhook key for your channel on Bob Go sales channels page. + https://my.bobgo.co.za/sales-channels') ); } -// } + } } } } diff --git a/Observer/OrderCreateWebhook.php b/Observer/OrderCreateWebhook.php index f605801..74f1ce4 100644 --- a/Observer/OrderCreateWebhook.php +++ b/Observer/OrderCreateWebhook.php @@ -15,6 +15,5 @@ class OrderCreateWebhook extends OrderWebhookBase // Extract order data and send to the webhook URL $this->sendWebhook($order); - $this->logger->info('Webhooks sent'); } } diff --git a/Observer/OrderWebhookBase.php b/Observer/OrderWebhookBase.php index db27d61..6a5163f 100644 --- a/Observer/OrderWebhookBase.php +++ b/Observer/OrderWebhookBase.php @@ -8,6 +8,7 @@ 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 { @@ -15,66 +16,48 @@ abstract class OrderWebhookBase implements ObserverInterface protected LoggerInterface $logger; protected StoreManagerInterface $storeManager; protected ScopeConfigInterface $scopeConfig; + protected $bobGo; - public function __construct(LoggerInterface $logger, Curl $curl, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig) + 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) { - $enabled = $this->scopeConfig->getValue('carriers/bobgo/enable_webhooks', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); - - // Return early if webhooks is disabled - if (!$enabled) { - $this->logger->info('Webhooks are disabled. Exiting webhook process for order: ' . $order->getIncrementId()); + // Return early if not enabled + if (!$this->bobGo->isWebhookEnabled()) { return; } // Webhook URL $url = $this->getWebhookUrl(); - $this->logger->info('Webhooks url: ' . $url); $storeId = $this->getStoreId(); $orderId = $order->getId(); - // Get the order creation time - $createdAt = strtotime($order->getCreatedAt()); - $currentTime = time(); - - // Define a time threshold to consider the order as newly created - $threshold = 5; // 5 seconds - - // Determine the event type based on the creation time - if (($currentTime - $createdAt) < $threshold) { - $eventType = 'order_created'; - } else { - $eventType = 'order_updated'; - } - // Prepare payload $data = [ - 'event' => $eventType, + 'event' => 'order_updated', 'order_id' => $orderId, - 'channel_identifier' => $this->getStoreUrl(), + 'channel_identifier' => $this->bobGo->getBaseUrl(), 'store_id' => $storeId, + 'webhooks_enabled' => true, // If we get to this point webhooks are enabled ]; // Send the webhook $this->makeHttpPostRequest($url, $data, $storeId); - $this->logger->info('Webhooks sent'); } private function makeHttpPostRequest($url, $data, $storeId) { // Generate the signature using the webhook key saved in config $webhookKey = $this->scopeConfig->getValue('carriers/bobgo/webhook_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); - $this->logger->info('Webhooks - key: ' . $webhookKey); -// $secretKey = 'KaJGW2cxx1-6z_qjGhSq5Hj4qh_OXl0R1tUPurVs66A'; // Generate the HMAC-SHA256 hash as raw binary data $rawSignature = hash_hmac('sha256', $storeId, $webhookKey, true); @@ -94,7 +77,6 @@ abstract class OrderWebhookBase implements ObserverInterface // Set headers and post the data $this->curl->addHeader('Content-Type', 'application/json'); $this->curl->post($url, $payloadJson); - $this->logger->info('Webhooks payload: ' . $payloadJson); } private function getWebhookUrl(): string @@ -107,25 +89,4 @@ abstract class OrderWebhookBase implements ObserverInterface $storeId = $this->storeManager->getStore()->getId(); return $storeId; } - - 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; - } } -- GitLab