Skip to content

Commit

Permalink
Pt 564 magento 2 webhooks 400 response (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
tikohov20 authored Nov 16, 2023
1 parent 2ae3091 commit acbc09f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 54 deletions.
5 changes: 1 addition & 4 deletions Controller/Webhooks/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ public function execute()

try {
$content = $this->getRequest()->getContent();
if ($storeId = $this->getRequest()->getParam('storeId')) {
$this->_monduConfig->setContextCode($storeId);
}


$headers = $this->getRequest()->getHeaders()->toArray();
$signature = hash_hmac('sha256', $content, $this->_monduConfig->getWebhookSecret());
if ($signature !== ($headers['X-Mondu-Signature'] ?? null)) {
Expand Down
19 changes: 1 addition & 18 deletions Model/Request/Webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ class Webhooks extends CommonRequest implements RequestInterface
*/
protected $configProvider;

/**
* @var int
*/
protected $storeId = 0;

/**
* @param Curl $curl
* @param ConfigProvider $configProvider
Expand All @@ -50,7 +45,7 @@ public function request($params = null): Webhooks
$url = $this->configProvider->getApiUrl('webhooks');

$this->sendRequestWithParams('post', $url, json_encode([
'address' => $this->configProvider->getWebhookUrl($this->storeId),
'address' => $this->configProvider->getWebhookUrl(),
'topic' => $this->getTopic()
]));

Expand Down Expand Up @@ -78,16 +73,4 @@ private function getTopic()
{
return $this->topic;
}

/**
* Set store
*
* @param int $storeId
* @return $this
*/
public function setStore($storeId)
{
$this->storeId = $storeId;
return $this;
}
}
14 changes: 1 addition & 13 deletions Model/Request/Webhooks/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function checkSuccess(): Keys
*/
public function update(): Keys
{
$this->configProvider->updateWebhookSecret($this->getWebhookSecret(), $this->storeId);
$this->configProvider->updateWebhookSecret($this->getWebhookSecret());
return $this;
}

Expand All @@ -102,16 +102,4 @@ public function getWebhookSecret()
{
return $this->webhookSecret;
}

/**
* Set store
*
* @param int $storeId
* @return $this
*/
public function setStore($storeId)
{
$this->storeId = $storeId;
return $this;
}
}
16 changes: 4 additions & 12 deletions Model/Ui/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,10 @@ public function getDebug()
/**
* Get Webhook url
*
* @param string|mixed $storeId
* @return string
*/
public function getWebhookUrl($storeId): string
public function getWebhookUrl(): string
{
if ($storeId) {
return $this->urlBuilder->getBaseUrl().'mondu/webhooks/index?storeId='.$storeId;
}
return $this->urlBuilder->getBaseUrl().'mondu/webhooks/index';
}

Expand Down Expand Up @@ -253,13 +249,11 @@ public function getConfig()
* @param string $storeId
* @return $this
*/
public function updateWebhookSecret($webhookSecret = "", $storeId = 0): ConfigProvider
public function updateWebhookSecret($webhookSecret = ""): ConfigProvider
{
$this->resourceConfig->saveConfig(
'payment/mondu/'.$this->getMode(). '_webhook_secret',
$this->encryptor->encrypt($webhookSecret),
ScopeInterface::SCOPE_STORE,
$storeId
$this->encryptor->encrypt($webhookSecret)
);

return $this;
Expand Down Expand Up @@ -297,9 +291,7 @@ public function getWebhookSecret()
{
$val = $this->scopeConfig
->getValue(
'payment/mondu/' . $this->getMode().'_webhook_secret',
ScopeInterface::SCOPE_STORE,
$this->contextCode
'payment/mondu/' . $this->getMode().'_webhook_secret'
);
return $this->encryptor->decrypt($val);
}
Expand Down
5 changes: 0 additions & 5 deletions Observer/Config/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,27 @@ public function execute(Observer $observer)
$storeId = $this->storeManager->getStore()->getId();
$this->requestFactory->create(RequestFactory::WEBHOOKS_KEYS_REQUEST_METHOD)
->process()
->setStore($storeId)
->checkSuccess()
->update();

$this->requestFactory
->create(RequestFactory::WEBHOOKS_REQUEST_METHOD)
->setStore($storeId)
->setTopic('order/confirmed')
->process();

$this->requestFactory
->create(RequestFactory::WEBHOOKS_REQUEST_METHOD)
->setTopic('order/pending')
->setStore($storeId)
->process();

$this->requestFactory
->create(RequestFactory::WEBHOOKS_REQUEST_METHOD)
->setTopic('order/declined')
->setStore($storeId)
->process();

$this->requestFactory
->create(RequestFactory::WEBHOOKS_REQUEST_METHOD)
->setTopic('order/canceled')
->setStore($storeId)
->process();

$this->monduConfig->clearConfigurationCache();
Expand Down
66 changes: 66 additions & 0 deletions Setup/Patch/Data/WebhookSecretPatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace Mondu\Mondu\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

class WebhookSecretPatch implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

public function __construct(
ModuleDataSetupInterface $moduleDataSetup
) {
$this->moduleDataSetup = $moduleDataSetup;
}

/**
* @inheritdoc
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();

$liveWebhookSecret = $this->moduleDataSetup->getConnection()->fetchOne('SELECT `value` FROM core_config_data WHERE path = "payment/mondu/live_webhook_secret"');
$sandboxWebhookSecret = $this->moduleDataSetup->getConnection()->fetchOne('SELECT `value` FROM core_config_data WHERE path = "payment/mondu/sandbox_webhook_secret"');

$this->moduleDataSetup->getConnection()->delete('core_config_data', ['path = "payment/mondu/sandbox_webhook_secret"']);
$this->moduleDataSetup->getConnection()->delete('core_config_data', ['path = "payment/mondu/live_webhook_secret"']);

$this->moduleDataSetup->getConnection()->insert('core_config_data', [
'scope' => 'default',
'scope_id' => 0,
'path' => 'payment/mondu/sandbox_webhook_secret',
'value' => $sandboxWebhookSecret,
]);

$this->moduleDataSetup->getConnection()->insert('core_config_data', [
'scope' => 'default',
'scope_id' => 0,
'path' => 'payment/mondu/live_webhook_secret',
'value' => $liveWebhookSecret,
]);

$this->moduleDataSetup->getConnection()->endSetup();
}

/**
* @inheritdoc
*/
public static function getDependencies()
{
return [];
}


/**
* @inheritdoc
*/
public function getAliases()
{
return [];
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mondu_gmbh/magento2-payment",
"description": "Mondu payment method for magento 2",
"type": "magento2-module",
"version": "2.2.0",
"version": "2.2.1",
"license": [
"MIT"
],
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mondu_Mondu" setup_version="2.2.0">
<module name="Mondu_Mondu" setup_version="2.2.1">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Payment"/>
Expand Down

0 comments on commit acbc09f

Please sign in to comment.