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
Merge requests
!14
Start webhook implementation
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Start webhook implementation
magento_webhooks
into
dev
Overview
3
Commits
32
Pipelines
0
Changes
5
Merged
Christel Loftus
requested to merge
magento_webhooks
into
dev
9 months ago
Overview
3
Commits
32
Pipelines
0
Changes
3
Expand
0
0
Merge request reports
Compare
version 3
version 15
e841e5b6
8 months ago
version 14
b0265f41
8 months ago
version 13
57d26adc
8 months ago
version 12
d40f977b
8 months ago
version 11
b77abc38
8 months ago
version 10
dbcb4eb7
8 months ago
version 9
366c40ca
9 months ago
version 8
bb9b08a1
9 months ago
version 7
2c7e23cd
9 months ago
version 6
9d91f05a
9 months ago
version 5
716ecb04
9 months ago
version 4
10570227
9 months ago
version 3
4b9bbb9f
9 months ago
version 2
6d003aac
9 months ago
version 1
dd64a2de
9 months ago
dev (base)
and
version 7
latest version
62241942
32 commits,
8 months ago
version 15
e841e5b6
15 commits,
8 months ago
version 14
b0265f41
14 commits,
8 months ago
version 13
57d26adc
13 commits,
8 months ago
version 12
d40f977b
12 commits,
8 months ago
version 11
b77abc38
11 commits,
8 months ago
version 10
dbcb4eb7
10 commits,
8 months ago
version 9
366c40ca
9 commits,
9 months ago
version 8
bb9b08a1
8 commits,
9 months ago
version 7
2c7e23cd
7 commits,
9 months ago
version 6
9d91f05a
6 commits,
9 months ago
version 5
716ecb04
5 commits,
9 months ago
version 4
10570227
4 commits,
9 months ago
version 3
4b9bbb9f
3 commits,
9 months ago
version 2
6d003aac
2 commits,
9 months ago
version 1
dd64a2de
1 commit,
9 months ago
Show latest version
3 files
+
36
−
163
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
Observer/OrderCreateWebhook.php
+
2
−
114
Options
@@ -3,129 +3,17 @@
namespace
BobGroup\BobGo\Observer
;
use
Magento\Framework\Event\Observer
;
use
Magento\Framework\Event\ObserverInterface
;
use
Magento\Framework\HTTP\Client\Curl
;
use
BobGroup\BobGo\Model\Carrier\UData
;
use
Psr\Log\LoggerInterface
;
use
Magento\Store\Model\StoreManagerInterface
;
class
OrderCreateWebhook
implem
en
t
s
O
bserverInterfac
e
class
OrderCreateWebhook
ext
en
d
s
O
rderWebhookBas
e
{
protected
Curl
$curl
;
protected
LoggerInterface
$logger
;
protected
StoreManagerInterface
$storeManager
;
public
function
__construct
(
LoggerInterface
$logger
,
Curl
$curl
,
StoreManagerInterface
$storeManager
)
{
$this
->
logger
=
$logger
;
$this
->
curl
=
$curl
;
$this
->
storeManager
=
$storeManager
;
}
public
function
execute
(
Observer
$observer
)
{
$order
=
$observer
->
getEvent
()
->
getOrder
();
if
(
!
$order
)
{
//$this->logger->error('OrderCreateWebhook: No order object found in observer');
return
;
}
// Extract order data and send to the webhook URL
$this
->
sendWebhook
(
$order
,
'order_create'
);
}
private
function
sendWebhook
(
$order
,
$eventType
)
{
// Webhook URL
$url
=
$this
->
getWebhookUrl
();
// 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
(),
'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
);
}
private
function
getWebhookUrl
():
string
{
return
UData
::
WEBHOOK_URL
;
}
private
function
getStoreId
():
string
{
$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
;
$this
->
sendWebhook
(
$order
);
}
}
Loading