Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PW-7270] - Fix auto configuration fails on local test environment #1756

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions Helper/ManagementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Adyen\AdyenException;
use Adyen\Service\Management;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\StoreManager;
use Adyen\Payment\Logger\AdyenLogger;
use Magento\Framework\Encryption\EncryptorInterface;
Expand Down Expand Up @@ -49,25 +50,34 @@ class ManagementHelper
*/
private $adyenLogger;

/**
* @var ManagerInterface
*/
protected $messageManager;

/**
* ManagementHelper constructor.
* @param StoreManager $storeManager
* @param EncryptorInterface $encryptor
* @param Data $dataHelper
* @param Config $configHelper
* @param AdyenLogger $adyenLogger
* @param ManagerInterface $messageManager
*/
public function __construct(
StoreManager $storeManager,
EncryptorInterface $encryptor,
Data $dataHelper,
Config $configHelper,
AdyenLogger $adyenLogger
AdyenLogger $adyenLogger,
ManagerInterface $messageManager
) {
$this->dataHelper = $dataHelper;
$this->storeManager = $storeManager;
$this->encryptor = $encryptor;
$this->configHelper = $configHelper;
$this->adyenLogger = $adyenLogger;
$this->messageManager = $messageManager;
}

/**
Expand Down Expand Up @@ -165,23 +175,34 @@ public function setupWebhookCredentials(
];
$webhookId = $this->configHelper->getWebhookId($storeId);
$savedMerchantAccount = $this->configHelper->getMerchantAccount($storeId);
// reuse saved webhookId if merchant account is the same.
if (!empty($webhookId) && $merchantId === $savedMerchantAccount) {
$management->merchantWebhooks->update($merchantId, $webhookId, $params);
} else {
$params['type'] = 'standard';
$response = $management->merchantWebhooks->create($merchantId, $params);
// save webhook_id to configuration
$webhookId = $response['id'];
$this->configHelper->setConfigData($webhookId, 'webhook_id', Config::XML_ADYEN_ABSTRACT_PREFIX);
}

// generate hmac key and save
$response = $management->merchantWebhooks->generateHmac($merchantId, $webhookId);
$hmacKey = $response['hmacKey'];
$hmac = $this->encryptor->encrypt($hmacKey);
$mode = $demoMode ? 'test' : 'live';
$this->configHelper->setConfigData($hmac, 'notification_hmac_key_' . $mode, Config::XML_ADYEN_ABSTRACT_PREFIX);
try {
// reuse saved webhookId if merchant account is the same.
if (!empty($webhookId) && $merchantId === $savedMerchantAccount) {
$management->merchantWebhooks->update($merchantId, $webhookId, $params);
} else {
$params['type'] = 'standard';
$response = $management->merchantWebhooks->create($merchantId, $params);
// save webhook_id to configuration
$webhookId = $response['id'];
$this->configHelper->setConfigData($webhookId, 'webhook_id', Config::XML_ADYEN_ABSTRACT_PREFIX);
}

// generate hmac key and save
$response = $management->merchantWebhooks->generateHmac($merchantId, $webhookId);
$hmacKey = $response['hmacKey'];
$hmac = $this->encryptor->encrypt($hmacKey);
$mode = $demoMode ? 'test' : 'live';
$this->configHelper->setConfigData($hmac, 'notification_hmac_key_' . $mode, Config::XML_ADYEN_ABSTRACT_PREFIX);
} catch (\Exception $exception) {
$this->adyenLogger->error($exception->getMessage());

if (!$demoMode) {
throw $exception;
}

$this->messageManager->addErrorMessage(__("Credentials saved but webhook and HMAC key couldn't be generated! Please check the error logs."));
}
}

/**
Expand Down Expand Up @@ -236,13 +257,13 @@ public function webhookTest(string $merchantId)
$client = $this->dataHelper->initializeAdyenClient();
$management = new Management($client);
$response = $management->merchantWebhooks->test($merchantId, $webhookId, $params);
$this->adyenLogger->addInfo(
$this->adyenLogger->info(
sprintf( 'response from webhook test %s',
json_encode($response))
);
return $response;
} catch (AdyenException $exception) {
$this->adyenLogger->addError($exception->getMessage());
$this->adyenLogger->error($exception->getMessage());
return $exception->getMessage();
}
}
Expand Down