From b0265f41023e1074c06cc45144ccd178ddb27129 Mon Sep 17 00:00:00 2001
From: "@ChristelLoftus" <christel@bob.co.za>
Date: Thu, 3 Oct 2024 14:09:45 +0200
Subject: [PATCH] cleanup

---
 Model/Carrier/BobGo.php                | 46 +++++++++++---------------
 Observer/ModifyShippingDescription.php |  4 +--
 Observer/OrderWebhookBase.php          | 27 ++-------------
 3 files changed, 23 insertions(+), 54 deletions(-)

diff --git a/Model/Carrier/BobGo.php b/Model/Carrier/BobGo.php
index 332ab93..67c7f0f 100644
--- a/Model/Carrier/BobGo.php
+++ b/Model/Carrier/BobGo.php
@@ -1335,14 +1335,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
             \Magento\Store\Model\ScopeInterface::SCOPE_STORE
         );
 
-        $isEnabled = $this->scopeConfig->getValue(
-            'carriers/bobgo/enable_webhooks',
-            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
-        );
-
         // Convert the string to a boolean value
-        $isEnabled = filter_var($isEnabled, FILTER_VALIDATE_BOOLEAN);
-
+        $isEnabled = $this->isWebhookEnabled();
 
         $storeId = strval($this->_storeManager->getStore()->getId());
 
@@ -1354,28 +1348,9 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
         ];
 
         try {
-            // Generate the HMAC-SHA256 hash as raw binary data
-            $rawSignature = hash_hmac('sha256', $storeId, $webhookKey, true);
-            // Encode the binary data in Base64
-            $signature = base64_encode($rawSignature);
-            // Set headers and post the data
-            $this->curl->addHeader('Content-Type', 'application/json');
-            $this->curl->addHeader('x-m-webhook-signature', $signature);
-
-            $payloadJson = json_encode($payload);
-            $this->_logger->info('Webhooks payload: ' . $payloadJson);
-            if ($payloadJson === false) {
-                throw new \RuntimeException('Failed to encode payload to JSON.');
-            }
-
-            $this->curl->addHeader('Content-Type', 'application/json');
-            $this->curl->post($this->getWebhookUrl(), $payloadJson);
+            $this->encodeWebhookAndPostRequest($this->getWebhookUrl(), $payload, $storeId, $webhookKey);
             $statusCode = $this->curl->getStatus();
-            $this->_logger->info('Webhooks statuscode: ' . $statusCode);
             $responseBody = $this->curl->getBody();
-            $this->_logger->info('Webhooks response: ' . $responseBody);
-
-            $response = json_decode($responseBody, true);
 
             if ($statusCode != 200) {
                 throw new LocalizedException(__('Status code from BobGo: %1', $statusCode));
@@ -1386,4 +1361,21 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
         return true;
     }
 
+    public function encodeWebhookAndPostRequest($url, $data, $storeId, $webhookKey) {
+        // Generate the HMAC-SHA256 hash as raw binary data
+        $rawSignature = hash_hmac('sha256', $storeId, $webhookKey, true);
+        // Encode the binary data in Base64
+        $signature = base64_encode($rawSignature);
+        // Set headers and post the data
+        $this->curl->addHeader('Content-Type', 'application/json');
+        $this->curl->addHeader('x-m-webhook-signature', $signature);
+
+        $payloadJson = json_encode($data);
+        if ($payloadJson === false) {
+            throw new \RuntimeException('Failed to encode payload to JSON.');
+        }
+
+        $this->curl->addHeader('Content-Type', 'application/json');
+        $this->curl->post($url, $payloadJson);
+    }
 }
diff --git a/Observer/ModifyShippingDescription.php b/Observer/ModifyShippingDescription.php
index 2684798..2e443af 100644
--- a/Observer/ModifyShippingDescription.php
+++ b/Observer/ModifyShippingDescription.php
@@ -25,10 +25,10 @@ class ModifyShippingDescription implements ObserverInterface
         // Get the current shipping description
         $shippingDescription = $order->getShippingDescription();
 
-        // Get the method title from the shipping description (which might already include the title)
+        // Get the method title from the shipping description
         $methodTitle = $this->extractMethodTitle($shippingDescription);
 
-        // Set the new dynamic shipping description based only on MethodTitle
+        // Set the new shipping description based only on MethodTitle
         $newDescription = $methodTitle;
 
         // Update the shipping description in the order
diff --git a/Observer/OrderWebhookBase.php b/Observer/OrderWebhookBase.php
index 75767cc..62988c4 100644
--- a/Observer/OrderWebhookBase.php
+++ b/Observer/OrderWebhookBase.php
@@ -50,33 +50,10 @@ abstract class OrderWebhookBase implements ObserverInterface
             'webhooks_enabled' => true, // If we get to this point webhooks are enabled
         ];
 
-        // Send the webhook
-        $this->makeHttpPostRequest($url, $data, $storeId);
-    }
-
-    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);
-        // Generate the HMAC-SHA256 hash as raw binary data
-        $rawSignature = hash_hmac('sha256', $storeId, $webhookKey, true);
-
-        // Encode the binary data in Base64
-        $signature = base64_encode($rawSignature);
-
-        // Set headers and post the data
-        $this->curl->addHeader('Content-Type', 'application/json');
-        $this->curl->addHeader('x-m-webhook-signature', $signature);
-
-        // Perform the API request
-        $payloadJson = json_encode($data);
-        if ($payloadJson === false) {
-            throw new \RuntimeException('Failed to encode payload to JSON.');
-        }
-
-        // Set headers and post the data
-        $this->curl->addHeader('Content-Type', 'application/json');
-        $this->curl->post($url, $payloadJson);
+        // Send the webhook
+        $this->bobGo->encodeWebhookAndPostRequest($url, $data, $storeId, $webhookKey);
     }
 
     private function getWebhookUrl(): string
-- 
GitLab