Skip to content

Commit

Permalink
Feature: Retrieve the Profile ID from Mollie
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Feb 15, 2024
1 parent ac30d84 commit 99a031e
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 13 deletions.
24 changes: 24 additions & 0 deletions Block/Adminhtml/System/Config/Form/DisabledInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Block\Adminhtml\System\Config\Form;

use Magento\Config\Block\System\Config\Form\Field;

class DisabledInput extends Field
{
/**
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @return string
*/
protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$element->setReadonly(true, true);
return parent::_getElementHtml($element);
}
}
34 changes: 24 additions & 10 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
}
Expand All @@ -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;
}

Expand Down
85 changes: 85 additions & 0 deletions Model/Adminhtml/Backend/ChangeApiMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Model\Adminhtml\Backend;

use Magento\Framework\App\Cache\TypeListInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Value;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Model\Context;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Registry;
use Mollie\Payment\Config;

class ChangeApiMode extends Value
{
/**
* @var Config
*/
private $mollieConfig;
/**
* @var FlushMollieCache
*/
private $flushMollieCache;
/**
* @var UpdateProfileId
*/
private $updateProfileId;

public function __construct(
Context $context,
Registry $registry,
ScopeConfigInterface $config,
TypeListInterface $cacheTypeList,
Config $mollieConfig,
FlushMollieCache $flushMollieCache,
UpdateProfileId $updateProfileId,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$config,
$cacheTypeList,
$resource,
$resourceCollection,
$data
);

$this->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());
}
}
19 changes: 19 additions & 0 deletions Model/Adminhtml/Backend/DoNoUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Model\Adminhtml\Backend;

use Magento\Framework\App\Config\Value;

class DoNoUpdate extends Value
{
public function save(): self
{
return $this;
}
}
7 changes: 6 additions & 1 deletion Model/Adminhtml/Backend/FlushMollieCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ public function beforeSave()
parent::beforeSave();

if ($this->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']);
}
}
22 changes: 22 additions & 0 deletions Model/Adminhtml/Backend/SaveApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,6 +44,7 @@ public function __construct(
EncryptorInterface $encryptor,
ApiKeyFallbackRepositoryInterface $apiKeyFallbackRepository,
ApiKeyFallbackInterfaceFactory $apiKeyFallbackFactory,
UpdateProfileId $updateProfileId,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
Expand All @@ -52,6 +62,7 @@ public function __construct(

$this->apiKeyFallbackRepository = $apiKeyFallbackRepository;
$this->apiKeyFallbackFactory = $apiKeyFallbackFactory;
$this->updateProfileId = $updateProfileId;
}

public function beforeSave()
Expand All @@ -65,13 +76,24 @@ 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']);
}

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 */
Expand Down
47 changes: 47 additions & 0 deletions Model/Adminhtml/Backend/UpdateProfileId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Model\Adminhtml\Backend;

use Magento\Framework\App\Config\Storage\WriterInterface;
use Mollie\Payment\Config;
use Mollie\Payment\Service\Mollie\MollieApiClient;

class UpdateProfileId
{
/**
* @var MollieApiClient
*/
private $mollieApiClient;
/**
* @var WriterInterface
*/
private $configWriter;

public function __construct(
MollieApiClient $mollieApiClient,
WriterInterface $configWriter
) {
$this->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
);
}
}
7 changes: 5 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<field id="type" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Modus</label>
<backend_model>Mollie\Payment\Model\Adminhtml\Backend\FlushMollieCache</backend_model>
<backend_model>Mollie\Payment\Model\Adminhtml\Backend\ChangeApiMode</backend_model>
<source_model>Mollie\Payment\Model\Adminhtml\Source\ApiKey</source_model>
<config_path>payment/mollie_general/type</config_path>
</field>
Expand All @@ -69,9 +69,12 @@
<config_path>payment/mollie_general/apikey_live</config_path>
<comment model="Mollie\Payment\Model\Adminhtml\Comment\CurrentApiKeyMeta" />
</field>
<field id="profileid" translate="label" type="text" sortOrder="50" showInDefault="1"
<field id="profileid" translate="label comment" type="text" sortOrder="50" showInDefault="1"
showInWebsite="1" showInStore="1">
<label>Profile ID</label>
<backend_model>Mollie\Payment\Model\Adminhtml\Backend\DoNoUpdate</backend_model>
<frontend_model>Mollie\Payment\Block\Adminhtml\System\Config\Form\DisabledInput</frontend_model>
<comment>When you save the api key or change the mode, this value is automatically updated.</comment>
<config_path>payment/mollie_general/profileid</config_path>
</field>
<field id="credentials_checker" translate="label" type="button" sortOrder="60" showInDefault="1"
Expand Down

0 comments on commit 99a031e

Please sign in to comment.