Skip to content

Commit

Permalink
feat: remove integration from db when marking 'use default' option
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaMelazzo committed Jul 17, 2024
1 parent 7ac92fa commit 3c48319
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 119 deletions.
78 changes: 36 additions & 42 deletions Concrete/Magento2CoreSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,28 @@

namespace Pagarme\Pagarme\Concrete;

use Magento\Framework\App\Config as Magento2StoreConfig;
use DateTimeZone;
use Magento\Config\Model\Config as Magento2ModelConfig;
use Magento\Framework\App\Config as Magento2StoreConfig;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Store\Model\StoreManager as MagentoStoreManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Website\Interceptor;
use Pagarme\Core\Kernel\Abstractions\AbstractModuleCoreSetup;
use Pagarme\Core\Kernel\Aggregates\Configuration;
use Pagarme\Core\Kernel\Factories\ConfigurationFactory;
use Pagarme\Core\Kernel\Services\MoneyService;
use Pagarme\Core\Kernel\ValueObjects\CardBrand;
use Pagarme\Core\Kernel\ValueObjects\Configuration\CardConfig;
use Pagarme\Pagarme\Gateway\Transaction\Base\Config\Config;
use Pagarme\Pagarme\Gateway\Transaction\CreditCard\Config\ConfigInterface;
use Pagarme\Pagarme\Model\Installments\Config\ConfigInterface as InstallmentConfigInterface;
use Pagarme\Pagarme\Helper\ModuleHelper;
use Pagarme\Pagarme\Model\Enum\CreditCardBrandEnum;
use Pagarme\Pagarme\Concrete\Magento2DatabaseDecorator;
use Pagarme\Pagarme\Concrete\Magento2PlatformOrderDecorator;
use Pagarme\Pagarme\Concrete\Magento2PlatformInvoiceDecorator;
use Pagarme\Pagarme\Concrete\Magento2PlatformCreditmemoDecorator;
use Pagarme\Pagarme\Concrete\Magento2DataService;
use Pagarme\Pagarme\Concrete\Magento2PlatformPaymentMethodDecorator;
use Pagarme\Pagarme\Concrete\Magento2PlatformProductDecorator;
use Pagarme\Pagarme\Model\ConfigNotification;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Website\Interceptor;
use Pagarme\Pagarme\Model\Enum\CreditCardBrandEnum;
use stdClass;
use Throwable;

final class Magento2CoreSetup extends AbstractModuleCoreSetup
{
Expand Down Expand Up @@ -134,7 +125,7 @@ public function loadModuleConfigurationFromPlatform($storeConfig = null)
$storeConfig = $objectManager->get(Magento2StoreConfig::class);
}

$configData = new \stdClass();
$configData = new stdClass();

$storeId = self::getCurrentStoreId();

Expand Down Expand Up @@ -167,7 +158,7 @@ public function loadModuleConfigurationFromPlatform($storeConfig = null)
self::$moduleConfig = $config;
self::$instance->setApiBaseUrl();

} catch (\Throwable $error) {
} catch (Throwable $error) {
$configErrorNotify = new ConfigNotification();
$configErrorNotify->addNotify($error);
}
Expand Down Expand Up @@ -212,13 +203,13 @@ static private function fillWithVoucherConfig(&$dataObj, $storeConfig)

$section = 'payment/pagarme_voucher/';

$voucherObject = new \stdClass();
$voucherObject = new stdClass();

$dataObj->voucherConfig = self::fillDataObj($storeConfig, $options, $voucherObject, $section);

$operation = Configuration::CARD_OPERATION_AUTH_ONLY;
if ($dataObj->voucherConfig->cardOperation === 'authorize_capture') {
$operation = Configuration::CARD_OPERATION_AUTH_AND_CAPTURE;
$operation = Configuration::CARD_OPERATION_AUTH_AND_CAPTURE;
}

$dataObj->voucherConfig->cardOperation = $operation;
Expand All @@ -237,7 +228,7 @@ static private function fillWithDebitConfig(&$dataObj, $storeConfig)

$section = 'payment/pagarme_debit/';

$debitObject = new \stdClass();
$debitObject = new stdClass();

$dataObj->debitConfig = self::fillDataObj($storeConfig, $options, $debitObject, $section);
$dataObj->debitConfig->cardOperation = Configuration::CARD_OPERATION_AUTH_AND_CAPTURE;
Expand Down Expand Up @@ -279,9 +270,10 @@ static private function fillWithCardConfig(&$dataObj, $storeConfig)
}

private static function fillWithPixConfig(
stdClass $configData,
stdClass $configData,
ScopeConfigInterface $storeConfig
) {
)
{
$options = [
'enabled' => 'active',
'expirationQrCode' => 'expiration_qrcode',
Expand All @@ -291,15 +283,16 @@ private static function fillWithPixConfig(

$section = 'payment/pagarme_pix/';

$pixObject = new \stdClass();
$pixObject = new stdClass();

$configData->pixConfig = self::fillDataObj($storeConfig, $options, $pixObject, $section);
}

private static function fillWithGooglePayConfig(
stdClass $configData,
stdClass $configData,
ScopeConfigInterface $storeConfig
) {
)
{
$options = [
'enabled' => 'active',
'title' => 'title',
Expand All @@ -309,7 +302,7 @@ private static function fillWithGooglePayConfig(

$section = 'payment/pagarme_googlepay/';

$googlePayObject = new \stdClass();
$googlePayObject = new stdClass();

$configData->googlePayConfig = self::fillDataObj($storeConfig, $options, $googlePayObject, $section);
}
Expand Down Expand Up @@ -380,7 +373,7 @@ static private function fillWithPagarmeKeys(&$dataObj, $storeConfig)

$section = 'pagarme_pagarme/global/';

$keys = new \stdClass;
$keys = new stdClass;

$dataObj->keys =
self::fillDataObj(
Expand Down Expand Up @@ -415,7 +408,7 @@ static private function fillWithAddressConfig(&$dataObj, $storeConfig)
];
$section = 'payment/pagarme_customer_address/';

$addressAttributes = new \stdClass();
$addressAttributes = new stdClass();
$dataObj->addressAttributes =
self::fillDataObj(
$storeConfig,
Expand Down Expand Up @@ -455,7 +448,7 @@ static private function getBrandConfig($storeConfig, $section)

$scope = ScopeInterface::SCOPE_WEBSITES;
$storeId = self::getCurrentStoreId();
$selectedBrands = $storeConfig->getValue($section . 'cctypes', $scope, $storeId) ?? '';
$selectedBrands = $storeConfig->getValue($section . 'cctypes', $scope, $storeId) ?? '';
$brands = array_merge([''], explode(
',',
$selectedBrands
Expand All @@ -482,12 +475,12 @@ static private function getBrandConfig($storeConfig, $section)
$max = $storeConfig->getValue($section . 'installments_number' . $brand, $scope, $storeId);
}

$minValue = $storeConfig->getValue($section . 'installment_min_amount' . $brand, $scope, $storeId);
$initial = $storeConfig->getValue($section . 'installments_interest_rate_initial' . $brand, $scope, $storeId);
$incremental = $storeConfig->getValue($section . 'installments_interest_rate_incremental' . $brand, $scope, $storeId);
$maxWithout = $storeConfig->getValue($section . 'installments_max_without_interest' . $brand, $scope, $storeId);
$minValue = $storeConfig->getValue($section . 'installment_min_amount' . $brand, $scope, $storeId);
$initial = $storeConfig->getValue($section . 'installments_interest_rate_initial' . $brand, $scope, $storeId);
$incremental = $storeConfig->getValue($section . 'installments_interest_rate_incremental' . $brand, $scope, $storeId);
$maxWithout = $storeConfig->getValue($section . 'installments_max_without_interest' . $brand, $scope, $storeId);

$interestByBrand = $storeConfig->getValue($section . 'installments_interest_by_issuer' . $brand, $scope, $storeId);
$interestByBrand = $storeConfig->getValue($section . 'installments_interest_by_issuer' . $brand, $scope, $storeId);
if (empty($interestByBrand)) {
$initial = 0;
$incremental = 0;
Expand Down Expand Up @@ -572,9 +565,9 @@ public static function getDefaultStoreId()
}

/**
* @return DateTimeZone
* @since 1.7.1
*
* @return \DateTimeZone
*/
protected function getPlatformStoreTimezone()
{
Expand All @@ -586,7 +579,7 @@ protected function getPlatformStoreTimezone()
$timezoneString = $timezone->getConfigTimezone(
ScopeInterface::SCOPE_STORE
);
$dateTimeZone = new \DateTimeZone($timezoneString);
$dateTimeZone = new DateTimeZone($timezoneString);

return $dateTimeZone;
}
Expand Down Expand Up @@ -616,7 +609,7 @@ static private function fillWithRecurrenceConfig(&$dataObj, $storeConfig)

$section = 'pagarme_pagarme/recurrence/';

$recurrenceConfig = new \stdClass();
$recurrenceConfig = new stdClass();
$dataObj->recurrenceConfig = self::fillDataObj(
$storeConfig,
$options,
Expand All @@ -626,9 +619,10 @@ static private function fillWithRecurrenceConfig(&$dataObj, $storeConfig)
}

static private function fillWithMarketplaceConfig(
stdClass $configData,
stdClass $configData,
ScopeConfigInterface $storeConfig
) {
)
{
$options = [
'enabled' => 'active',
'responsibilityForProcessingFees'
Expand All @@ -645,7 +639,7 @@ static private function fillWithMarketplaceConfig(

$section = 'pagarme_pagarme/marketplace/';

$marketplaceObject = new \stdClass();
$marketplaceObject = new stdClass();

$configData->marketplaceConfig = self::fillDataObj($storeConfig, $options, $marketplaceObject, $section);
}
Expand Down
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Hub/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function getScopeName(): string
/**
* @return string|null
*/
private function getScopeUrl()
public function getScopeUrl()
{
$request = $this->requestObject;

Expand Down
43 changes: 27 additions & 16 deletions Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Pagarme\Core\Kernel\Aggregates\Configuration;
use Pagarme\Core\Middle\Model\Account as AccountMiddle;
use Pagarme\Pagarme\Concrete\Magento2CoreSetup;
use Pagarme\Pagarme\Controller\Adminhtml\Hub\Index as HubControllerIndex;
use Pagarme\Pagarme\Model\Api\HubCommand;
use Pagarme\Pagarme\Service\AccountService;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -70,6 +70,11 @@ class Account
*/
protected $pagarmeConfigProvider;

/**
* @var HubControllerIndex
*/
protected $hubControllerIndex;

/**
* @param WriterInterface $configWriter
* @param StoreManagerInterface $storeManager
Expand All @@ -78,17 +83,21 @@ class Account
* @param CollectionFactory $configCollectionFactory
* @param LoggerInterface $logger
* @param Session $session
* @param PagarmeConfigProvider $pagarmeConfigProvider
* @param HubControllerIndex $hubControllerIndex
*/
public function __construct(
WriterInterface $configWriter,
WriterInterface $configWriter,
StoreManagerInterface $storeManager,
AccountService $accountService,
HubCommand $hubCommand,
CollectionFactory $configCollectionFactory,
LoggerInterface $logger,
Session $session,
PagarmeConfigProvider $pagarmeConfigProvider
) {
AccountService $accountService,
HubCommand $hubCommand,
CollectionFactory $configCollectionFactory,
LoggerInterface $logger,
Session $session,
PagarmeConfigProvider $pagarmeConfigProvider,
HubControllerIndex $hubControllerIndex
)
{
$this->configWriter = $configWriter;
$this->storeManager = $storeManager;
$this->accountService = $accountService;
Expand All @@ -97,11 +106,14 @@ public function __construct(
$this->logger = $logger;
$this->session = $session;
$this->pagarmeConfigProvider = $pagarmeConfigProvider;
$this->hubControllerIndex = $hubControllerIndex;
}

/**
* @param mixed $website
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function validateDashSettings($website)
{
Expand All @@ -119,7 +131,7 @@ public function validateDashSettings($website)
$this->configWriter->save(
PagarmeConfigProvider::PATH_DASH_ERRORS,
json_encode($account->getErrors()),
ScopeInterface::SCOPE_WEBSITES,
$this->hubControllerIndex->getScopeName(),
$website
);
$this->savePaymentTypes($account, $website);
Expand All @@ -145,9 +157,8 @@ public function saveAccountIdFromWebhook($account)
$this->configWriter->save(
PagarmeConfigProvider::PATH_ACCOUNT_ID,
$account['id'],
ScopeInterface::SCOPE_WEBSITES,
$this->storeManager->getStore()
->getWebsiteId()
$this->hubControllerIndex->getScopeName(),
$this->storeManager->getStore()->getWebsiteId()
);
}

Expand All @@ -161,7 +172,7 @@ public function getDashSettingsErrors()
$this->initializeConfig();
$collection = $this->configCollectionFactory->create();
$collection->addFieldToFilter('path', ['eq' => PagarmeConfigProvider::PATH_DASH_ERRORS]);
$collection->addFieldToFilter('scope', ['eq' => ScopeInterface::SCOPE_WEBSITES]);
$collection->addFieldToFilter('scope', ['eq' => $this->hubControllerIndex->getScopeName()]);
$collection->addFieldToFilter('scope_id', ['eq' => $this->session->getWebsiteId()]);

if ($collection->count() === 0) {
Expand Down Expand Up @@ -192,7 +203,7 @@ public function getPaymentType(string $paymentName, bool $gateway = true)
: PagarmeConfigProvider::PATH_IS_PAYMENT_PSP_TYPE;
$collection = $this->configCollectionFactory->create();
$collection->addFieldToFilter('path', ['eq' => sprintf($paymentType, $paymentName)]);
$collection->addFieldToFilter('scope', ['eq' => ScopeInterface::SCOPE_WEBSITES]);
$collection->addFieldToFilter('scope', ['eq' => $this->hubControllerIndex->getScopeName()]);
$collection->addFieldToFilter('scope_id', ['eq' => $this->session->getWebsiteId()]);

if ($collection->count() === 0) {
Expand Down Expand Up @@ -374,7 +385,7 @@ private function saveConfig($path, $value, $website)
$this->configWriter->save(
$path,
$value,
ScopeInterface::SCOPE_WEBSITES,
$this->hubControllerIndex->getScopeName(),
$website
);
}
Expand Down
15 changes: 3 additions & 12 deletions Model/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ class Notifications extends Message
* @param Registry $registry
* @param Account $account
* @param HubIntegration $hubIntegration
* @param UrlInterface $urlInterface
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
* @throws NoSuchEntityException
*/
public function __construct(
ConfigInterface $config,
Expand Down Expand Up @@ -271,18 +273,7 @@ private function addDashSettingsMessages()
*/
private function buildDashLink(string $label, string $dashPage = '')
{
if (!$this->account->hasMerchantAndAccountIds()) {
return $label;
}

$dashSettings = !empty($dashPage) ? "settings/{$dashPage}/" : '';
$url = $this->account->getDashUrl() . $dashSettings;

return sprintf(
'<a href="%s" target="_blank">%s</a>',
$url,
$label
);
return $label;
}

/**
Expand Down
Loading

0 comments on commit 3c48319

Please sign in to comment.