From b0af6b717d878ae72d13e1d691a679fd02472a6a Mon Sep 17 00:00:00 2001
From: "@ChristelLoftus" <christel@bob.co.za>
Date: Thu, 29 Aug 2024 10:55:10 +0200
Subject: [PATCH] webhook signature

---
 Observer/OrderCreateWebhook.php | 28 +++++++++++++++++-----------
 Observer/OrderUpdateWebhook.php | 25 +++++++++++++++----------
 2 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/Observer/OrderCreateWebhook.php b/Observer/OrderCreateWebhook.php
index 010bec5..cf90443 100644
--- a/Observer/OrderCreateWebhook.php
+++ b/Observer/OrderCreateWebhook.php
@@ -31,7 +31,7 @@ class OrderCreateWebhook implements ObserverInterface
         }
 
         // Extract order data and send to the webhook URL
-        $this->sendWebhook($order, 'order_create');
+        $this->sendWebhook($order, 'order_created');
     }
 
     private function sendWebhook($order, $eventType)
@@ -53,28 +53,34 @@ class OrderCreateWebhook implements ObserverInterface
         $billingAddress = $order->getBillingAddress();
         $billingAddressData = $billingAddress ? $billingAddress->getData() : [];
 
+        $storeId = $this->getStoreId();
+
         // Prepare payload
         $data = [
             'event' => $eventType,
             'order_id' => $order->getId(),
             'channel_identifier' => $this->getStoreUrl(),
-            'store_id' => $this->getStoreId(),
-            'order_data' => $order->getData(),
-            'items' => $itemsData,
-            'shipping_address' => $shippingAddressData,
-            'billing_address'  => $billingAddressData,
+            'store_id' => $storeId,
+            //'order_data' => $order->getData(),
+            //'items' => $itemsData,
+            //'shipping_address' => $shippingAddressData,
+            //'billing_address'  => $billingAddressData,
         ];
 
         // Send the webhook
-        $this->makeHttpPostRequest($url, $data);
+        $this->makeHttpPostRequest($url, $data, $storeId);
     }
 
-    private function makeHttpPostRequest($url, $data)
+    private function makeHttpPostRequest($url, $data, $storeId)
     {
         // Generate the signature using a secret key and the payload (example using HMAC SHA256)
-        $secretKey = 'your_secret_key';
-        $payloadJson = json_encode($data);
-        $signature = hash_hmac('sha256', $payloadJson, $secretKey);
+        $secretKey = 'KaJGW2cxx1-6z_qjGhSq5Hj4qh_OXl0R1tUPurVs66A';
+        // Generate the HMAC-SHA256 hash as raw binary data
+        $rawSignature = hash_hmac('sha256', $storeId, $secretKey, true);
+
+        // Encode the binary data in Base64
+        $signature = base64_encode($rawSignature);
+
 
         // Set headers and post the data
         $this->curl->addHeader('Content-Type', 'application/json');
diff --git a/Observer/OrderUpdateWebhook.php b/Observer/OrderUpdateWebhook.php
index 01b7695..095c887 100644
--- a/Observer/OrderUpdateWebhook.php
+++ b/Observer/OrderUpdateWebhook.php
@@ -52,28 +52,33 @@ class OrderUpdateWebhook implements ObserverInterface
         $billingAddress = $order->getBillingAddress();
         $billingAddressData = $billingAddress ? $billingAddress->getData() : [];
 
+        $storeId = $this->getStoreId();
+
         // Prepare payload
         $data = [
             'event' => $eventType,
             'order_id' => $order->getId(),
             'channel_identifier' => $this->getStoreUrl(),
-            'store_id' => $this->getStoreId(),
-            'order_data' => $order->getData(),
-            'items' => $itemsData,
-            'shipping_address' => $shippingAddressData,
-            'billing_address'  => $billingAddressData,
+            'store_id' => $storeId,
+            //'order_data' => $order->getData(),
+            //'items' => $itemsData,
+            //'shipping_address' => $shippingAddressData,
+            //'billing_address'  => $billingAddressData,
         ];
 
         // Send the webhook
-        $this->makeHttpPostRequest($url, $data);
+        $this->makeHttpPostRequest($url, $data, $storeId);
     }
 
-    private function makeHttpPostRequest($url, $data)
+    private function makeHttpPostRequest($url, $data, $storeId)
     {
         // Generate the signature using a secret key and the payload (example using HMAC SHA256)
-        $secretKey = 'your_secret_key';
-        $payloadJson = json_encode($data);
-        $signature = hash_hmac('sha256', $payloadJson, $secretKey);
+        $secretKey = 'KaJGW2cxx1-6z_qjGhSq5Hj4qh_OXl0R1tUPurVs66A';
+        // Generate the HMAC-SHA256 hash as raw binary data
+        $rawSignature = hash_hmac('sha256', $storeId, $secretKey, true);
+
+        // Encode the binary data in Base64
+        $signature = base64_encode($rawSignature);
 
         // Set headers and post the data
         $this->curl->addHeader('Content-Type', 'application/json');
-- 
GitLab