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
b0265f41
Commit
b0265f41
authored
6 months ago
by
Christel Loftus
Browse files
Options
Downloads
Patches
Plain Diff
cleanup
parent
57d26adc
No related branches found
Branches containing commit
No related tags found
1 merge request
!14
Start webhook implementation
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Model/Carrier/BobGo.php
+19
-27
19 additions, 27 deletions
Model/Carrier/BobGo.php
Observer/ModifyShippingDescription.php
+2
-2
2 additions, 2 deletions
Observer/ModifyShippingDescription.php
Observer/OrderWebhookBase.php
+2
-25
2 additions, 25 deletions
Observer/OrderWebhookBase.php
with
23 additions
and
54 deletions
Model/Carrier/BobGo.php
+
19
−
27
View file @
b0265f41
...
@@ -1335,14 +1335,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
...
@@ -1335,14 +1335,8 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
\Magento\Store\Model\ScopeInterface
::
SCOPE_STORE
\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
// Convert the string to a boolean value
$isEnabled
=
filter_var
(
$isEnabled
,
FILTER_VALIDATE_BOOLEAN
);
$isEnabled
=
$this
->
isWebhookEnabled
();
$storeId
=
strval
(
$this
->
_storeManager
->
getStore
()
->
getId
());
$storeId
=
strval
(
$this
->
_storeManager
->
getStore
()
->
getId
());
...
@@ -1354,28 +1348,9 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
...
@@ -1354,28 +1348,9 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
];
];
try
{
try
{
// Generate the HMAC-SHA256 hash as raw binary data
$this
->
encodeWebhookAndPostRequest
(
$this
->
getWebhookUrl
(),
$payload
,
$storeId
,
$webhookKey
);
$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
);
$statusCode
=
$this
->
curl
->
getStatus
();
$statusCode
=
$this
->
curl
->
getStatus
();
$this
->
_logger
->
info
(
'Webhooks statuscode: '
.
$statusCode
);
$responseBody
=
$this
->
curl
->
getBody
();
$responseBody
=
$this
->
curl
->
getBody
();
$this
->
_logger
->
info
(
'Webhooks response: '
.
$responseBody
);
$response
=
json_decode
(
$responseBody
,
true
);
if
(
$statusCode
!=
200
)
{
if
(
$statusCode
!=
200
)
{
throw
new
LocalizedException
(
__
(
'Status code from BobGo: %1'
,
$statusCode
));
throw
new
LocalizedException
(
__
(
'Status code from BobGo: %1'
,
$statusCode
));
...
@@ -1386,4 +1361,21 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
...
@@ -1386,4 +1361,21 @@ class BobGo extends AbstractCarrierOnline implements \Magento\Shipping\Model\Car
return
true
;
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
);
}
}
}
This diff is collapsed.
Click to expand it.
Observer/ModifyShippingDescription.php
+
2
−
2
View file @
b0265f41
...
@@ -25,10 +25,10 @@ class ModifyShippingDescription implements ObserverInterface
...
@@ -25,10 +25,10 @@ class ModifyShippingDescription implements ObserverInterface
// Get the current shipping description
// Get the current shipping description
$shippingDescription
=
$order
->
getShippingDescription
();
$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
);
$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
;
$newDescription
=
$methodTitle
;
// Update the shipping description in the order
// Update the shipping description in the order
...
...
This diff is collapsed.
Click to expand it.
Observer/OrderWebhookBase.php
+
2
−
25
View file @
b0265f41
...
@@ -50,33 +50,10 @@ abstract class OrderWebhookBase implements ObserverInterface
...
@@ -50,33 +50,10 @@ abstract class OrderWebhookBase implements ObserverInterface
'webhooks_enabled'
=>
true
,
// If we get to this point webhooks are enabled
'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
// Generate the signature using the webhook key saved in config
$webhookKey
=
$this
->
scopeConfig
->
getValue
(
'carriers/bobgo/webhook_key'
,
\Magento\Store\Model\ScopeInterface
::
SCOPE_STORE
);
$webhookKey
=
$this
->
scopeConfig
->
getValue
(
'carriers/bobgo/webhook_key'
,
\Magento\Store\Model\ScopeInterface
::
SCOPE_STORE
);
// Generate the HMAC-SHA256 hash as raw binary data
// Send the webhook
$rawSignature
=
hash_hmac
(
'sha256'
,
$storeId
,
$webhookKey
,
true
);
$this
->
bobGo
->
encodeWebhookAndPostRequest
(
$url
,
$data
,
$storeId
,
$webhookKey
);
// 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
);
}
}
private
function
getWebhookUrl
():
string
private
function
getWebhookUrl
():
string
...
...
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