From 99a031e3f676b8497585155c7248b95f3665b4af Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Thu, 15 Feb 2024 12:29:06 +0100 Subject: [PATCH] Feature: Retrieve the Profile ID from Mollie --- .../System/Config/Form/DisabledInput.php | 24 ++++++ Config.php | 34 +++++--- Model/Adminhtml/Backend/ChangeApiMode.php | 85 +++++++++++++++++++ Model/Adminhtml/Backend/DoNoUpdate.php | 19 +++++ Model/Adminhtml/Backend/FlushMollieCache.php | 7 +- Model/Adminhtml/Backend/SaveApiKey.php | 22 +++++ Model/Adminhtml/Backend/UpdateProfileId.php | 47 ++++++++++ etc/adminhtml/system.xml | 7 +- 8 files changed, 232 insertions(+), 13 deletions(-) create mode 100644 Block/Adminhtml/System/Config/Form/DisabledInput.php create mode 100644 Model/Adminhtml/Backend/ChangeApiMode.php create mode 100644 Model/Adminhtml/Backend/DoNoUpdate.php create mode 100644 Model/Adminhtml/Backend/UpdateProfileId.php diff --git a/Block/Adminhtml/System/Config/Form/DisabledInput.php b/Block/Adminhtml/System/Config/Form/DisabledInput.php new file mode 100644 index 00000000000..b072ef60870 --- /dev/null +++ b/Block/Adminhtml/System/Config/Form/DisabledInput.php @@ -0,0 +1,24 @@ +setReadonly(true, true); + return parent::_getElementHtml($element); + } +} diff --git a/Config.php b/Config.php index 3a7292fc2e8..8f35d6c1a96 100644 --- a/Config.php +++ b/Config.php @@ -209,20 +209,35 @@ public function getApiKey($storeId = null) } if (!$this->isProductionMode($storeId)) { - $apiKey = trim($this->getPath(static::GENERAL_APIKEY_TEST, $storeId) ?? ''); - if (empty($apiKey)) { - $this->addToLog('error', 'Mollie API key not set (test modus)'); - } - - if (!preg_match('/^test_\w+$/', $apiKey)) { - $this->addToLog('error', 'Mollie set to test modus, but API key does not start with "test_"'); - } + $apiKey = $this->getTestApiKey($storeId === null ? null : (int)$storeId); $keys[$storeId] = $apiKey; return $apiKey; } - $apiKey = trim($this->getPath(static::GENERAL_APIKEY_LIVE, $storeId) ?? ''); + $apiKey = $this->getLiveApiKey($storeId === null ? null : (int)$storeId); + + $keys[$storeId] = $apiKey; + return $apiKey; + } + + public function getTestApiKey(int $storeId = null): string + { + $apiKey = trim((string)$this->getPath(static::GENERAL_APIKEY_TEST, $storeId) ?? ''); + if (empty($apiKey)) { + $this->addToLog('error', 'Mollie API key not set (test modus)'); + } + + if (!preg_match('/^test_\w+$/', $apiKey)) { + $this->addToLog('error', 'Mollie set to test modus, but API key does not start with "test_"'); + } + + return $apiKey; + } + + public function getLiveApiKey(int $storeId = null): string + { + $apiKey = trim((string)$this->getPath(static::GENERAL_APIKEY_LIVE, $storeId) ?? ''); if (empty($apiKey)) { $this->addToLog('error', 'Mollie API key not set (live modus)'); } @@ -231,7 +246,6 @@ public function getApiKey($storeId = null) $this->addToLog('error', 'Mollie set to live modus, but API key does not start with "live_"'); } - $keys[$storeId] = $apiKey; return $apiKey; } diff --git a/Model/Adminhtml/Backend/ChangeApiMode.php b/Model/Adminhtml/Backend/ChangeApiMode.php new file mode 100644 index 00000000000..7e632bf6229 --- /dev/null +++ b/Model/Adminhtml/Backend/ChangeApiMode.php @@ -0,0 +1,85 @@ +mollieConfig = $mollieConfig; + $this->flushMollieCache = $flushMollieCache; + $this->updateProfileId = $updateProfileId; + } + + public function beforeSave(): self + { + $this->flushMollieCache->flush(); + + return parent::beforeSave(); + } + + public function afterSave() + { + $apiKey = $this->getApiKey($this->getValue()); + $this->updateProfileId->execute($apiKey, $this->getScope(), $this->getScopeId()); + + return parent::afterSave(); + } + + private function getApiKey(string $mode): string + { + if ($mode === 'live') { + return $this->mollieConfig->getLiveApiKey((int)$this->getScopeId()); + } + + return $this->mollieConfig->getTestApiKey((int)$this->getScopeId()); + } +} diff --git a/Model/Adminhtml/Backend/DoNoUpdate.php b/Model/Adminhtml/Backend/DoNoUpdate.php new file mode 100644 index 00000000000..e81fe95986f --- /dev/null +++ b/Model/Adminhtml/Backend/DoNoUpdate.php @@ -0,0 +1,19 @@ +getOldValue() != $this->getValue()) { - $this->_cacheManager->clean(['mollie_payment', 'mollie_payment_methods']); + $this->flush(); } return $this; } + + public function flush(): void + { + $this->_cacheManager->clean(['mollie_payment', 'mollie_payment_methods']); + } } diff --git a/Model/Adminhtml/Backend/SaveApiKey.php b/Model/Adminhtml/Backend/SaveApiKey.php index 6715f0d7a6c..7821e1f381a 100644 --- a/Model/Adminhtml/Backend/SaveApiKey.php +++ b/Model/Adminhtml/Backend/SaveApiKey.php @@ -26,6 +26,15 @@ class SaveApiKey extends Encrypted * @var ApiKeyFallbackInterfaceFactory */ private $apiKeyFallbackFactory; + /** + * @var UpdateProfileId + */ + private $updateProfileId; + + /** + * @var bool|string + */ + private $shouldUpdateProfileId = false; public function __construct( Context $context, @@ -35,6 +44,7 @@ public function __construct( EncryptorInterface $encryptor, ApiKeyFallbackRepositoryInterface $apiKeyFallbackRepository, ApiKeyFallbackInterfaceFactory $apiKeyFallbackFactory, + UpdateProfileId $updateProfileId, AbstractResource $resource = null, AbstractDb $resourceCollection = null, array $data = [] @@ -52,6 +62,7 @@ public function __construct( $this->apiKeyFallbackRepository = $apiKeyFallbackRepository; $this->apiKeyFallbackFactory = $apiKeyFallbackFactory; + $this->updateProfileId = $updateProfileId; } public function beforeSave() @@ -65,6 +76,8 @@ public function beforeSave() // Validate the new API key before saving. (new MollieApiClient())->setApiKey($value); + $this->shouldUpdateProfileId = $value; + $this->saveApiKey(); $this->_cacheManager->clean(['mollie_payment', 'mollie_payment_methods']); } @@ -72,6 +85,15 @@ public function beforeSave() return $this; } + public function afterSave() + { + if ($this->shouldUpdateProfileId !== false) { + $this->updateProfileId->execute($this->shouldUpdateProfileId, $this->getScope(), $this->getScopeId()); + } + + return parent::afterSave(); + } + private function saveApiKey(): void { /** @var ApiKeyFallbackInterface $model */ diff --git a/Model/Adminhtml/Backend/UpdateProfileId.php b/Model/Adminhtml/Backend/UpdateProfileId.php new file mode 100644 index 00000000000..1513ce9d456 --- /dev/null +++ b/Model/Adminhtml/Backend/UpdateProfileId.php @@ -0,0 +1,47 @@ +mollieApiClient = $mollieApiClient; + $this->configWriter = $configWriter; + } + + public function execute(string $apiKey, string $scope, int $scopeId): void + { + $client = $this->mollieApiClient->loadByApiKey($apiKey); + $profile = $client->profiles->get('me'); + $profileId = $profile->id; + + $this->configWriter->save( + Config::GENERAL_PROFILEID, + $profileId, + $scope, + $scopeId + ); + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 56e02c17273..56e0d74a2ea 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -51,7 +51,7 @@ - Mollie\Payment\Model\Adminhtml\Backend\FlushMollieCache + Mollie\Payment\Model\Adminhtml\Backend\ChangeApiMode Mollie\Payment\Model\Adminhtml\Source\ApiKey payment/mollie_general/type @@ -69,9 +69,12 @@ payment/mollie_general/apikey_live - + Mollie\Payment\Model\Adminhtml\Backend\DoNoUpdate + Mollie\Payment\Block\Adminhtml\System\Config\Form\DisabledInput + When you save the api key or change the mode, this value is automatically updated. payment/mollie_general/profileid