diff --git a/src/Model/Config/Backend/ApiVersion.php b/src/Model/Config/Backend/ApiVersion.php index f8fa2f7..a55388f 100644 --- a/src/Model/Config/Backend/ApiVersion.php +++ b/src/Model/Config/Backend/ApiVersion.php @@ -8,7 +8,6 @@ class ApiVersion extends \Magento\Framework\App\Config\Value { private $api; private $request; - private $helper; private $integrationModule; /** @@ -20,7 +19,6 @@ class ApiVersion extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection * @param \Magento\Framework\App\Request\Http $request - * @param \Retailcrm\Retailcrm\Helper\Data $helper * @param \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule * @param ApiClient $api * @param array $data @@ -33,14 +31,12 @@ class ApiVersion extends \Magento\Framework\App\Config\Value \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, \Magento\Framework\App\Request\Http $request, - \Retailcrm\Retailcrm\Helper\Data $helper, \Retailcrm\Retailcrm\Model\Service\IntegrationModule $integrationModule, ApiClient $api, array $data = [] ) { $this->api = $api; $this->request = $request; - $this->helper = $helper; $this->integrationModule = $integrationModule; parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); @@ -107,13 +103,9 @@ class ApiVersion extends \Magento\Framework\App\Config\Value */ private function sendModuleConfiguration($api) { - $clientId = $this->helper->getGeneralSettings('client_id_in_crm'); - - if (!$clientId) { - $this->integrationModule->setApiVersion($api->getVersion()); - $this->integrationModule->setAccountUrl($this->request->getUriString()); - $this->integrationModule->sendConfiguration($api); - } + $this->integrationModule->setApiVersion($api->getVersion()); + $this->integrationModule->setAccountUrl($this->request->getUriString()); + $this->integrationModule->sendConfiguration($api); } /** diff --git a/src/Model/Service/IntegrationModule.php b/src/Model/Service/IntegrationModule.php index 1233c0f..0d877ab 100644 --- a/src/Model/Service/IntegrationModule.php +++ b/src/Model/Service/IntegrationModule.php @@ -7,18 +7,28 @@ use Retailcrm\Retailcrm\Helper\Data as Helper; class IntegrationModule { const LOGO = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b846b1fef57e-magento.svg'; - const CODE = 'magento'; + const INTEGRATION_CODE = 'magento'; const NAME = 'Magento 2'; private $accountUrl = null; - private $resourceConfig; private $apiVersion = 'v5'; private $configuration = []; + private $resourceConfig; + private $clientId; + private $helper; + /** + * IntegrationModule constructor. + * + * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig + * @param \Retailcrm\Retailcrm\Helper\Data $helper + */ public function __construct( - \Magento\Config\Model\ResourceModel\Config $resourceConfig + \Magento\Config\Model\ResourceModel\Config $resourceConfig, + Helper $helper ) { $this->resourceConfig = $resourceConfig; + $this->helper = $helper; } /** @@ -52,21 +62,25 @@ class IntegrationModule */ private function setConfiguration($active) { + $this->clientId = $this->helper->getGeneralSettings('client_id_in_crm'); + + if (!$this->clientId) { + $this->clientId = uniqid(); + } + if ($this->apiVersion == 'v4') { $this->configuration = [ 'name' => self::NAME, - 'code' => self::CODE, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, 'logo' => self::LOGO, 'configurationUrl' => $this->accountUrl, 'active' => $active ]; } else { - $clientId = hash('md5', date('Y-m-d H:i:s')); - $this->configuration = [ - 'clientId' => $clientId, - 'code' => self::CODE, - 'integrationCode' => self::CODE, + 'clientId' => $this->clientId, + 'code' => self::INTEGRATION_CODE . '-' . $this->clientId, + 'integrationCode' => self::INTEGRATION_CODE, 'active' => $active, 'name' => self::NAME, 'logo' => self::LOGO, @@ -95,8 +109,11 @@ class IntegrationModule return false; } - if ($response->isSuccessful() && isset($clientId)) { - $this->resourceConfig->saveConfig(Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', $clientId); + if ($response->isSuccessful() && $active == true) { + $this->resourceConfig->saveConfig( + Helper::XML_PATH_RETAILCRM . 'general/client_id_in_crm', + $this->clientId + ); return true; } diff --git a/src/Setup/Uninstall.php b/src/Setup/Uninstall.php index 7cf13c9..b444255 100644 --- a/src/Setup/Uninstall.php +++ b/src/Setup/Uninstall.php @@ -20,6 +20,7 @@ class Uninstall implements \Magento\Framework\Setup\UninstallInterface public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) { - $this->integrationModule->sendConfiguration($this->apiClient, $this->apiClient->getVersion(), false); + $this->integrationModule->setApiVersion($this->apiClient->getVersion()); + $this->integrationModule->sendConfiguration($this->apiClient, false); } }