Skip to content

Commit

Permalink
ref #92092 Added currency validation (retailcrm#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
uryvskiy-dima authored Sep 27, 2023
1 parent 31aafde commit 2d2708a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v3.6.2
* Добавлена валидация валют при настройке модуля

## v3.6.1
* Добавлены тесты для новых версий PrestaShop

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.1
3.6.2
39 changes: 28 additions & 11 deletions retailcrm/lib/settings/RetailcrmSettingsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,12 @@ public function getSuccess()
public function validate($validateFromRequestOnly = false)
{
// check url and apiKey
$crmUrl = $this->settings->getValueWithStored('url');
$crmApiKey = $this->settings->getValueWithStored('apiKey');

if (!$validateFromRequestOnly || $this->settings->issetValue('url') || $this->settings->issetValue('apiKey')) {
if ($this->validateCrmAddress($this->settings->getValueWithStored('url'))
&& $this->validateCrmApiKey($this->settings->getValueWithStored('apiKey'))
) {
$this->validateApiCredentials(
$this->settings->getValueWithStored('url'),
$this->settings->getValueWithStored('apiKey')
);
if ($this->validateCrmAddress($crmUrl) && $this->validateCrmApiKey($crmApiKey)) {
$this->validateApiCredentials($crmUrl, $crmApiKey);
}
}

Expand Down Expand Up @@ -304,12 +302,31 @@ private function validateMappingSelected($values)
public function validateApiCredentials($url, $apiKey)
{
/** @var RetailcrmProxy|RetailcrmApiClientV5 $api */
$api = new RetailcrmProxy(
$url,
$apiKey
$api = new RetailcrmProxy($url, $apiKey);

return $this->validateApiVersion($api) && $this->validateApiAccess($api) && $this->validateCurrency($api);
}

private function validateCurrency($api)
{
$response = $api->sitesList();

if ($response instanceof RetailcrmApiResponse && $response->isSuccessful() && isset($response['sites'])) {
$site = current($response['sites']);
}

$currencyId = (int) Configuration::get('PS_CURRENCY_DEFAULT');
$isoCode = Db::getInstance()->getValue(
'SELECT `iso_code` FROM ' . _DB_PREFIX_ . 'currency WHERE `id_currency` = ' . $currencyId
);

return $this->validateApiVersion($api) && $this->validateApiAccess($api);
if (isset($site['currency']) && $site['currency'] !== $isoCode) {
$this->addError('apiKey', 'errors.currency');

return false;
}

return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

class RetailCRM extends Module
{
const VERSION = '3.6.1';
const VERSION = '3.6.2';

const API_URL = 'RETAILCRM_ADDRESS';
const API_KEY = 'RETAILCRM_API_TOKEN';
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/views/js/app.js

Large diffs are not rendered by default.

0 comments on commit 2d2708a

Please sign in to comment.