diff --git a/Block/Adminhtml/Footer.php b/Block/Adminhtml/Footer.php index 4154e181..517c6b33 100644 --- a/Block/Adminhtml/Footer.php +++ b/Block/Adminhtml/Footer.php @@ -21,8 +21,8 @@ use Magento\Backend\Block\Template; use Magento\Backend\Block\Template\Context; +use Lengow\Connector\Helper\Config as ConfigHelper; use Lengow\Connector\Helper\Security as SecurityHelper; -use Lengow\Connector\Model\Connector as LengowConnector; class Footer extends Template { @@ -31,19 +31,27 @@ class Footer extends Template */ private $securityHelper; + /** + * @var ConfigHelper + */ + private $configHelper; + /** * Constructor * * @param Context $context Magento block context instance * @param SecurityHelper $securityHelper Lengow security helper instance + * @param ConfigHelper $configHelper Lengow config helper instance * @param array $data additional params */ public function __construct( Context $context, SecurityHelper $securityHelper, + ConfigHelper $configHelper, array $data = [] ) { $this->securityHelper = $securityHelper; + $this->configHelper = $configHelper; parent::__construct($context, $data); } @@ -64,7 +72,7 @@ public function getPluginVersion(): string */ public function isPreprodPlugin(): string { - return LengowConnector::LENGOW_URL === 'lengow.net'; + return !$this->configHelper->useProdEnvironment(); } /** diff --git a/Block/Adminhtml/Header.php b/Block/Adminhtml/Header.php index a6bbb755..8a0af9c4 100644 --- a/Block/Adminhtml/Header.php +++ b/Block/Adminhtml/Header.php @@ -88,7 +88,7 @@ public function debugModeIsEnabled(): bool */ public function getLengowSolutionUrl(): string { - return '//my.' . LengowConnector::LENGOW_URL; + return '//my.' . $this->configHelper->getLengowDomain(); } /** diff --git a/Block/Adminhtml/Main.php b/Block/Adminhtml/Main.php index 20f72d86..c82794ed 100644 --- a/Block/Adminhtml/Main.php +++ b/Block/Adminhtml/Main.php @@ -112,7 +112,7 @@ public function isNewMerchant(): bool */ public function isPreprodPlugin(): bool { - return LengowConnector::LENGOW_URL === 'lengow.net'; + return !$this->configHelper->useProdEnvironment(); } /** @@ -132,7 +132,7 @@ public function debugModeIsActive(): bool */ public function getLengowSolutionUrl(): string { - return '//my.' . LengowConnector::LENGOW_URL; + return '//my.' . $this->configHelper->getLengowDomain(); } /** diff --git a/Helper/Config.php b/Helper/Config.php index 3ac24705..f5d04059 100644 --- a/Helper/Config.php +++ b/Helper/Config.php @@ -20,6 +20,7 @@ namespace Lengow\Connector\Helper; use Exception; +use Lengow\Connector\Model\Config\Source\Environment as EnvironmentSourceModel; use Magento\Catalog\Model\Product; use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory as ConfigDataCollectionFactory; use Magento\Customer\Model\ResourceModel\Group\CollectionFactory as CustomerGroupCollectionFactory; @@ -42,7 +43,18 @@ class Config extends AbstractHelper { + /** + * @var string domain of Lengow Pre-Prod environment + */ + public const LENGOW_PRE_PROD_DOMAIN = 'lengow.net'; + + /** + * @var string domain of Lengow Prod environment + */ + public const LENGOW_PROD_DOMAIN = 'lengow.io'; + /* Settings database key */ + public const ENVIRONMENT = 'global_environment'; public const ACCOUNT_ID = 'global_account_id'; public const ACCESS_TOKEN = 'global_access_token'; public const SECRET = 'global_secret_token'; @@ -116,6 +128,7 @@ class Config extends AbstractHelper * @var array params correspondence keys for toolbox */ public static $genericParamKeys = [ + self::ENVIRONMENT => 'environment', self::ACCOUNT_ID => 'account_id', self::ACCESS_TOKEN => 'access_token', self::SECRET => 'secret', @@ -211,6 +224,12 @@ class Config extends AbstractHelper * @var array all Lengow options path */ public static $lengowSettings = [ + self::ENVIRONMENT => [ + self::PARAM_PATH => 'lengow_global_options/store_credential/global_environment', + self::PARAM_GLOBAL => true, + self::PARAM_NO_CACHE => true, + self::PARAM_EXPORT => false, + ], self::ACCOUNT_ID => [ self::PARAM_PATH => 'lengow_global_options/store_credential/global_account_id', self::PARAM_GLOBAL => true, @@ -1122,6 +1141,34 @@ public function getAllValues(int $storeId = null, bool $toolbox = false): array return $rows; } + /** + * Returns whether prod-environment is configured to be used + * @return bool + */ + public function useProdEnvironment(): bool + { + $configuredEnvironment = $this->get(self::ENVIRONMENT); + + if ($configuredEnvironment === EnvironmentSourceModel::PROD_ENVIRONMENT) { + return true; + } + + return false; + } + + /** + * Returns domain of the configured Lengow-environment + * @return string + */ + public function getLengowDomain(): string + { + if ($this->useProdEnvironment()) { + return static::LENGOW_PROD_DOMAIN; + } + + return self::LENGOW_PRE_PROD_DOMAIN; + } + /** * Check if a specific module is enabled * diff --git a/Model/Config/Source/Environment.php b/Model/Config/Source/Environment.php new file mode 100644 index 00000000..a3559b00 --- /dev/null +++ b/Model/Config/Source/Environment.php @@ -0,0 +1,39 @@ + + * @copyright 2022 Lengow SAS + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +namespace Lengow\Connector\Model\Config\Source; + +use Magento\Framework\Data\OptionSourceInterface; + +class Environment implements OptionSourceInterface +{ + public const PRE_PROD_ENVIRONMENT = 'pre-prod'; + + public const PROD_ENVIRONMENT = 'prod'; + + /** + * @return array[] + */ + public function toOptionArray(): array + { + return [ + ['value' => static::PRE_PROD_ENVIRONMENT, 'label' => __('Pre-Prod')], + ['value' => static::PROD_ENVIRONMENT, 'label' => __('Prod')] + ]; + } +} diff --git a/Model/Connector.php b/Model/Connector.php index a98b0d39..4b165a09 100644 --- a/Model/Connector.php +++ b/Model/Connector.php @@ -29,18 +29,6 @@ */ class Connector { - /** - * @var string url of Lengow solution - */ - // const LENGOW_URL = 'lengow.io'; - public const LENGOW_URL = 'lengow.net'; - - /** - * @var string url of the Lengow API - */ - // const LENGOW_API_URL = 'https://api.lengow.io'; - private const LENGOW_API_URL = 'https://api.lengow.net'; - /* Lengow API routes */ public const API_ACCESS_TOKEN = '/access/get_token'; public const API_ORDER = '/v3.0/orders'; @@ -551,7 +539,7 @@ private function makeRequest( $opts[CURLOPT_TIMEOUT] = $this->lengowUrls[$api]; } // get base url for a specific environment - $url = self::LENGOW_API_URL . $api; + $url = $this->getLengowApiUrl() . $api; $opts[CURLOPT_CUSTOMREQUEST] = strtoupper($type); $url = parse_url($url); if (isset($url['port'])) { @@ -652,4 +640,13 @@ private function format($data, string $format = self::FORMAT_JSON) return json_decode($data, true); } } + + /** + * Returns the url to the Lengow-api, considering configured environment + * @return string + */ + private function getLengowApiUrl() + { + return 'https://api.' . $this->configHelper->getLengowDomain(); + } } diff --git a/README.md b/README.md index 23c91566..9caaf50f 100644 --- a/README.md +++ b/README.md @@ -74,11 +74,8 @@ Lengow for Magento 2 is available under license (OSL-3.0). If you want to contri The `master` branch contains the latest stable version of the plugin. The `dev` branch contains the version under development. All Pull requests must be made on the `dev` branch and must be validated by reviewers working at Lengow. -By default the plugin is made to work on our pre-production environment (my.lengow.net). -To change this environment, you must modify the two constants present in the file `Lengow/Connector/Model/Connector.php` - - const LENGOW_URL = 'lengow.net'; - const LENGOW_API_URL = 'https://api.lengow.net'; +By default, the plugin is made to work on our pre-production environment (my.lengow.net). +The environment can be changed to production (my.lengow.io) in the settings of this module. ### Translation diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 85d94c5a..e21ac56f 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -13,6 +13,10 @@ + + + Lengow\Connector\Model\Config\Source\Environment + Your Lengow Account ID diff --git a/etc/config.xml b/etc/config.xml index 33bad4e7..6eb63d3f 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -3,6 +3,7 @@ + pre-prod 0 @@ -78,4 +79,4 @@ - \ No newline at end of file +