checkout / checkout-magento2-plugin

Checkout.com Magento 2 official extension
MIT License
32 stars 32 forks source link

Webhook is not getting the store_id from the request. #528

Open kpitn opened 11 months ago

kpitn commented 11 months ago

Hi,

Currently, when a request comes from checkout.com to the /checkout_com/webhook/callback page, the store code is the defaut store code from the website.

//File : Controller/Webhook/Callback.php
// Get the store code
$storeCode = $this->storeManager->getStore()->getCode();

The problem arises if we have secret keys per store but only one webhook configured in the Checkout.com back office. In this case, the request doesn't work and returns "The endpoint did not accept the request. (Code: 404)."

Here is a simple and effective fix that retrieves the store code from the request content:

--- Controller/Webhook/Callback.php
+++ Controller/Webhook/Callback.php
@@ -178,6 +178,15 @@
                         // Get the store code
                         $storeCode = $this->storeManager->getStore()->getCode();

+                        if ($payload &&
+                            isset($payload->data->metadata->quote_data) &&
+                            ($quoteData = json_decode($payload->data->metadata->quote_data, true)) &&
+                            isset($quoteData['store_id'])
+                        ) {
+                            $storeCode = $quoteData['store_id'];
+                        }
+
+
                         // Initialize the API handler
                         $api = $this->apiHandler->init($storeCode, ScopeInterface::SCOPE_STORE);

2nd Problem: Moreover, you encounter the same issue in the back office when attempting to refund an invoice. It looks for the default secret key instead of the one associated with the invoice's store.

Again, this results in the same error: "The endpoint did not accept the request. (Code: 404)."

manuel-EGO commented 1 month ago

We had the same issue, we fixed it by creating different webhooks per store/website on checkout.com dashboard, each webhook, using the correct base url.