From 57d26adc7c75cc5c482206f2e4fa71f282e6ab78 Mon Sep 17 00:00:00 2001
From: "@ChristelLoftus" <christel@bob.co.za>
Date: Thu, 3 Oct 2024 13:38:30 +0200
Subject: [PATCH] change shipping description

---
 Model/Carrier/BobGo.php                | 15 ++++-----------
 Model/Carrier/UData.php                |  2 +-
 Observer/ConfigChangeObserver.php      |  4 +---
 Observer/ModifyShippingDescription.php | 12 ------------
 Observer/OrderWebhookBase.php          |  2 +-
 5 files changed, 7 insertions(+), 28 deletions(-)

diff --git a/Model/Carrier/BobGo.php b/Model/Carrier/BobGo.php
index fdb7283..332ab93 100644
--- a/Model/Carrier/BobGo.php
+++ b/Model/Carrier/BobGo.php
@@ -1360,7 +1360,7 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
             $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);
+            $this->curl->addHeader('x-m-webhook-signature', $signature);
 
             $payloadJson = json_encode($payload);
             $this->_logger->info('Webhooks payload: ' . $payloadJson);
@@ -1377,20 +1377,13 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
 
             $response = json_decode($responseBody, true);
 
-            if ($statusCode == 200 && isset($response['success']) && $response['success'] === true) {
-                $this->_logger->info('Webhook validation successful.');
-//                throw new LocalizedException(__('Rates received but id field is empty or invalid.'));
-                return true;
-            } else {
-                $this->_logger->error('Webhook validation failed: ' . ($response['message'] ?? 'Unknown error'));
-//                throw new LocalizedException(__('Rates received but id field is empty or invalid.'));
-                return false;
+            if ($statusCode != 200) {
+                throw new LocalizedException(__('Status code from BobGo: %1', $statusCode));
             }
         } catch (\Exception $e) {
-            $this->_logger->error('Webhook validation exception: ' . $e->getMessage());
-//            throw new LocalizedException(__('Rates received but id field is empty or invalid.'));
             return false;
         }
+        return true;
     }
 
 }
diff --git a/Model/Carrier/UData.php b/Model/Carrier/UData.php
index a7e5d2e..adf83a2 100644
--- a/Model/Carrier/UData.php
+++ b/Model/Carrier/UData.php
@@ -26,5 +26,5 @@ class UData
      *
      * @var string
      */
-    public const WEBHOOK_URL = 'https://api.dev.bobgo.co.za/webhook/channel';
+    public const WEBHOOK_URL = 'https://api.dev.bobgo.co.za/webhook/channel/magento';
 }
diff --git a/Observer/ConfigChangeObserver.php b/Observer/ConfigChangeObserver.php
index 9cbde5d..8362feb 100644
--- a/Observer/ConfigChangeObserver.php
+++ b/Observer/ConfigChangeObserver.php
@@ -73,10 +73,8 @@ class ConfigChangeObserver implements ObserverInterface
         }
 
         // Test for webhooks
-        if (is_array($changedPaths) && in_array('carriers/bobgo/enable_webhooks', $changedPaths)) {
-            $this->logger->info('Webhooks test start: ');
+        if ((is_array($changedPaths) && in_array('carriers/bobgo/enable_webhooks', $changedPaths)) || (is_array($changedPaths) && in_array('carriers/bobgo/webhook_key', $changedPaths))) {
             $result = $this->bobGo->triggerWebhookTest();
-            $this->logger->info('Webhooks test end: ' . $result);
 
             if ($this->bobGo->isWebhookEnabled()) {
                 if ($result) {
diff --git a/Observer/ModifyShippingDescription.php b/Observer/ModifyShippingDescription.php
index 3c19823..2684798 100644
--- a/Observer/ModifyShippingDescription.php
+++ b/Observer/ModifyShippingDescription.php
@@ -25,26 +25,14 @@ class ModifyShippingDescription implements ObserverInterface
         // Get the current shipping description
         $shippingDescription = $order->getShippingDescription();
 
-        // Log the original shipping description
-        $this->logger->info('Original Shipping Description: ' . $shippingDescription);
-
-        // Get the shipping method used in the order (e.g., bobgo_10303_39_1)
-        $shippingMethod = $order->getShippingMethod();
-
         // Get the method title from the shipping description (which might already include the title)
         $methodTitle = $this->extractMethodTitle($shippingDescription);
 
-        // Log the method title for debugging
-        $this->logger->info('Extracted Method Title: ' . $methodTitle);
-
         // Set the new dynamic shipping description based only on MethodTitle
         $newDescription = $methodTitle;
 
         // Update the shipping description in the order
         $order->setShippingDescription($newDescription);
-
-        // Optionally log the new shipping description
-        $this->logger->info('Updated Shipping Description: ' . $newDescription);
     }
 
     /**
diff --git a/Observer/OrderWebhookBase.php b/Observer/OrderWebhookBase.php
index 6a5163f..75767cc 100644
--- a/Observer/OrderWebhookBase.php
+++ b/Observer/OrderWebhookBase.php
@@ -66,7 +66,7 @@ abstract class OrderWebhookBase implements ObserverInterface
 
         // Set headers and post the data
         $this->curl->addHeader('Content-Type', 'application/json');
-        $this->curl->addHeader('X-M-Webhook-Signature', $signature);
+        $this->curl->addHeader('x-m-webhook-signature', $signature);
 
         // Perform the API request
         $payloadJson = json_encode($data);
-- 
GitLab