Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgo-magento-extension
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgo-magento-extension
Commits
6d003aac
Commit
6d003aac
authored
8 months ago
by
Christel Loftus
Browse files
Options
Downloads
Patches
Plain Diff
Add to webhooks
parent
dd64a2de
No related branches found
Branches containing commit
No related tags found
2 merge requests
!20
1.0.41
,
!14
Start webhook implementation
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Observer/OrderCreateWebhook.php
+37
-41
37 additions, 41 deletions
Observer/OrderCreateWebhook.php
Observer/OrderUpdateWebhook.php
+45
-7
45 additions, 7 deletions
Observer/OrderUpdateWebhook.php
with
82 additions
and
48 deletions
Observer/OrderCreateWebhook.php
+
37
−
41
View file @
6d003aac
...
...
@@ -18,91 +18,83 @@ class OrderCreateWebhook implements ObserverInterface
public
function
__construct
(
LoggerInterface
$logger
,
Curl
$curl
,
StoreManagerInterface
$storeManager
)
{
$this
->
logger
=
$logger
;
$this
->
curl
=
$curl
;
// Initialize the Curl instance
$this
->
curl
=
$curl
;
$this
->
storeManager
=
$storeManager
;
}
public
function
execute
(
Observer
$observer
)
{
$this
->
logger
->
info
(
'OrderCreateWebhook: execute method started'
);
$order
=
$observer
->
getEvent
()
->
getOrder
();
if
(
!
$order
)
{
$this
->
logger
->
error
(
'OrderCreateWebhook: No order object found in observer'
);
//
$this->logger->error('OrderCreateWebhook: No order object found in observer');
return
;
}
// Log order creation data
$this
->
logger
->
info
(
'Order Created:'
,
[
'order_id'
=>
$order
->
getId
(),
'order_data'
=>
$order
->
getData
()]);
// Extract order data and send to the webhook URL
$this
->
sendWebhook
(
$order
,
'order_created'
);
$this
->
logger
->
info
(
'OrderCreateWebhook: execute method finished'
);
$this
->
sendWebhook
(
$order
,
'order_create'
);
}
private
function
sendWebhook
(
$order
,
$eventType
)
{
$this
->
logger
->
info
(
'OrderCreateWebhook: sendWebhook method started'
);
// Webhook URL
$url
=
$this
->
getWebhookUrl
();
$this
->
logger
->
info
(
'OrderCreateWebhook: Webhook URL'
,
[
'url'
=>
$url
]);
// Get Store UUID and add to query parameters
$storeUuid
=
$this
->
getStoreUuid
();
$this
->
logger
->
info
(
'UUID: '
,
[
'uuid'
=>
$storeUuid
]);
$url
.
=
'?channel='
.
urlencode
(
$storeUuid
);
$this
->
logger
->
info
(
'Webhook URL'
,
[
'url'
=>
$url
]);
// Extract order items
$itemsData
=
[];
foreach
(
$order
->
getAllItems
()
as
$item
)
{
$itemsData
[]
=
$item
->
getData
();
}
// Extract shipping address
$shippingAddress
=
$order
->
getShippingAddress
();
$shippingAddressData
=
$shippingAddress
?
$shippingAddress
->
getData
()
:
[];
// Extract billing address
$billingAddress
=
$order
->
getBillingAddress
();
$billingAddressData
=
$billingAddress
?
$billingAddress
->
getData
()
:
[];
// Prepare payload
$data
=
[
'event'
=>
$eventType
,
'order_id'
=>
$order
->
getId
(),
'order_data'
=>
$order
->
getData
()
'channel_identifier'
=>
$this
->
getStoreUrl
(),
'store_id'
=>
$this
->
getStoreId
(),
'order_data'
=>
$order
->
getData
(),
'items'
=>
$itemsData
,
'shipping_address'
=>
$shippingAddressData
,
'billing_address'
=>
$billingAddressData
,
];
// Log webhook payload
$this
->
logger
->
info
(
'Sending Webhook:'
,
[
'url'
=>
$url
,
'payload'
=>
$data
]);
// Send the webhook
$this
->
makeHttpPostRequest
(
$url
,
$data
);
$this
->
logger
->
info
(
'OrderCreateWebhook: sendWebhook method finished'
);
}
private
function
makeHttpPostRequest
(
$url
,
$data
)
{
$this
->
logger
->
info
(
'URL:'
,
[
'url'
=>
$url
]);
$this
->
logger
->
info
(
'Data:'
,
[
'data'
=>
$data
]);
// 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
);
// Set headers and post the data
$this
->
curl
->
addHeader
(
'Content-Type'
,
'application/json'
);
$this
->
curl
->
addHeader
(
'X-M-Webhook-Signature'
,
$signature
);
// Add your custom header here
// Perform the API request
$payloadJson
=
json_encode
(
$data
);
if
(
$payloadJson
===
false
)
{
$this
->
logger
->
error
(
'Payload Webhook failed: Unable to encode JSON.'
);
//
$this->logger->error('Payload Webhook failed: Unable to encode JSON.');
throw
new
\RuntimeException
(
'Failed to encode payload to JSON.'
);
}
$this
->
logger
->
info
(
'Payload Webhook:'
,
[
'response'
=>
$payloadJson
]);
// Set headers and post the data
$this
->
curl
->
addHeader
(
'Content-Type'
,
'application/json'
);
$this
->
curl
->
post
(
$url
,
$payloadJson
);
$statusCode
=
$this
->
curl
->
getStatus
();
$responseBody
=
$this
->
curl
->
getBody
();
// Log status code and response body
$this
->
logger
->
info
(
'Webhook Response Status:'
,
[
'status'
=>
$statusCode
]);
$this
->
logger
->
info
(
'Webhook Response Body:'
,
[
'response'
=>
$responseBody
]);
// Decode the response
$response
=
json_decode
(
$responseBody
,
true
);
if
(
json_last_error
()
!==
JSON_ERROR_NONE
)
{
$this
->
logger
->
error
(
'Failed to decode webhook response:'
,
[
'error'
=>
json_last_error_msg
()]);
}
else
{
$this
->
logger
->
info
(
'Response Webhook:'
,
[
'response'
=>
$response
]);
}
}
private
function
getWebhookUrl
():
string
...
...
@@ -110,10 +102,14 @@ class OrderCreateWebhook implements ObserverInterface
return
UData
::
WEBHOOK_URL
;
}
private
function
getStore
Uui
d
():
string
private
function
getStore
I
d
():
string
{
$storeId
=
$this
->
storeManager
->
getStore
()
->
getId
();
return
$storeId
;
//return $this->storeManager->getStore()->getConfig('general/store_information/store_id');
}
private
function
getStoreUrl
():
string
{
return
$this
->
storeManager
->
getStore
()
->
getBaseUrl
();
}
}
This diff is collapsed.
Click to expand it.
Observer/OrderUpdateWebhook.php
+
45
−
7
View file @
6d003aac
...
...
@@ -5,9 +5,9 @@ namespace BobGroup\BobGo\Observer;
use
Magento\Framework\Event\Observer
;
use
Magento\Framework\Event\ObserverInterface
;
use
Magento\Framework\HTTP\Client\Curl
;
use
Magento\Store\Model\StoreManagerInterface
;
use
BobGroup\BobGo\Model\Carrier\UData
;
use
Psr\Log\LoggerInterface
;
use
Magento\Store\Model\StoreManagerInterface
;
class
OrderUpdateWebhook
implements
ObserverInterface
{
...
...
@@ -29,38 +29,70 @@ class OrderUpdateWebhook implements ObserverInterface
return
;
}
// Extract order data and send to the webhook URL
$this
->
sendWebhook
(
$order
,
'order_updated'
);
}
private
function
sendWebhook
(
$order
,
$eventType
)
{
// Webhook URL
$url
=
$this
->
getWebhookUrl
();
// Get Store UUID and add to query parameters
$storeUuid
=
$this
->
getStoreUuid
();
$url
.
=
'?channel='
.
urlencode
(
$storeUuid
);
// Extract order items
$itemsData
=
[];
foreach
(
$order
->
getAllItems
()
as
$item
)
{
$itemsData
[]
=
$item
->
getData
();
}
// Extract shipping address
$shippingAddress
=
$order
->
getShippingAddress
();
$shippingAddressData
=
$shippingAddress
?
$shippingAddress
->
getData
()
:
[];
// Extract billing address
$billingAddress
=
$order
->
getBillingAddress
();
$billingAddressData
=
$billingAddress
?
$billingAddress
->
getData
()
:
[];
// Prepare payload
$data
=
[
'event'
=>
$eventType
,
'order_id'
=>
$order
->
getId
(),
'order_data'
=>
$order
->
getData
()
'channel_identifier'
=>
$this
->
getStoreUrl
(),
'store_id'
=>
$this
->
getStoreId
(),
'order_data'
=>
$order
->
getData
(),
'items'
=>
$itemsData
,
'shipping_address'
=>
$shippingAddressData
,
'billing_address'
=>
$billingAddressData
,
];
// Send the webhook
$this
->
makeHttpPostRequest
(
$url
,
$data
);
}
private
function
makeHttpPostRequest
(
$url
,
$data
)
{
// 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
);
// Set headers and post the data
$this
->
curl
->
addHeader
(
'Content-Type'
,
'application/json'
);
$this
->
curl
->
addHeader
(
'X-M-Webhook-Signature'
,
$signature
);
// Add your custom header here
// Perform the API request
$payloadJson
=
json_encode
(
$data
);
if
(
$payloadJson
===
false
)
{
//$this->logger->error('Payload Webhook failed: Unable to encode JSON.');
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
);
$statusCode
=
$this
->
curl
->
getStatus
();
$responseBody
=
$this
->
curl
->
getBody
();
// Decode the response
$response
=
json_decode
(
$responseBody
,
true
);
}
...
...
@@ -69,8 +101,14 @@ class OrderUpdateWebhook implements ObserverInterface
return
UData
::
WEBHOOK_URL
;
}
private
function
getStoreUuid
():
string
private
function
getStoreId
():
string
{
$storeId
=
$this
->
storeManager
->
getStore
()
->
getId
();
return
$storeId
;
}
private
function
getStoreUrl
():
string
{
return
$this
->
storeManager
->
getStore
()
->
get
Config
(
'general/store_information/store_id'
);
return
$this
->
storeManager
->
getStore
()
->
get
BaseUrl
(
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment