Skip to content

Commit

Permalink
ref #92047 Added currency validation (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
uryvskiy-dima authored Oct 3, 2023
1 parent 0566309 commit 4ea6015
Show file tree
Hide file tree
Showing 13 changed files with 86 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 @@
## 2023-10-02 4.6.12
* Added currency validation when configuring the module

## 2023-08-31 4.6.11
* Added the ability to work with coupons through the CRM system

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ local_test:
run_tests:
docker-compose --no-ansi up -d --build mysql
docker-compose --no-ansi run --rm --no-deps app make local_test
docker-compose stop
docker-compose down -v

coverage:
wget https://phar.phpunit.de/phpcov-2.0.2.phar && php phpcov-2.0.2.phar merge coverage/ --clover coverage.xml
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.6.11
4.6.12
8 changes: 7 additions & 1 deletion resources/pot/retailcrm-es_ES.pot
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,10 @@ msgid "Select warehouses to receive balances from CRM. To select several warehou
msgstr "Selecciona los almacenes para recibir el stock desde CRM. Para seleccionar varios mantén pulsado CTRL (para Windows y Linux) o ⌘ Command (para MacOS)"

msgid "I agree to receive promotional newsletters"
msgstr "Estoy de acuerdo en recibir los boletines informativos"
msgstr "Estoy de acuerdo en recibir los boletines informativos"

msgid "API key with one-shop access required"
msgstr "Se requiere clave API con acceso a una tienda"

msgid "The currency of the site differs from the currency of the store in CRM. For the integration to work correctly, the currencies in CRM and CMS must match"
msgstr "La moneda del sitio web es distinto a la tienda del CRM. Para el funcionamiento correcto de la integración, las monedas del CMS y CRM deben coincid"
6 changes: 6 additions & 0 deletions resources/pot/retailcrm-ru_RU.pot
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,9 @@ msgstr "Выберите склады для получения остатков

msgid "I agree to receive promotional newsletters"
msgstr "Согласен на рекламно-информационные рассылки"

msgid "API key with one-shop access required"
msgstr "Требуется API ключ с доступом к одному магазину"

msgid "The currency of the site differs from the currency of the store in CRM. For the integration to work correctly, the currencies in CRM and CMS must match"
msgstr "Валюта сайта отличается от валюты магазина в CRM. Для корректной работы интеграции, валюты в CRM и CMS должны совпадать"
46 changes: 39 additions & 7 deletions src/include/abstracts/class-wc-retailcrm-abstracts-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function ajax_generate_icml()
<?php
}


/**
* Initialize integration settings form fields.
*/
Expand All @@ -98,6 +97,11 @@ public function init_form_fields()
];

if ($this->apiClient) {
// The field is highlighted in red if the CRM site is invalid
$this->form_fields['api_key']['class'] = $this->isValidCrmSite($this->apiClient)
? ''
: 'red-selected-retailcrm';

if (
isset($_GET['page']) && $_GET['page'] == 'wc-settings'
&& isset($_GET['tab']) && $_GET['tab'] == 'integration'
Expand Down Expand Up @@ -816,20 +820,48 @@ public function validate_api_key_field($key, $value)
return $value;
}

$api = new WC_Retailcrm_Proxy($this->crmUrl, $value);
$response = $api->apiVersions();
$isValidCrmSite = $this->isValidCrmSite(new WC_Retailcrm_Proxy($this->crmUrl, $value));

if (empty($response) || !$response->isSuccessful()) {
$value = '';
// The field is highlighted in red if the CRM site is invalid
if ($isValidCrmSite) {
$this->form_fields['api_key']['class'] = '';

WC_Admin_Settings::add_error(esc_html__('Enter the correct API key', 'retailcrm'));
} else {
header("Refresh:0");
} else {
$value = '';
$this->form_fields['api_key']['class'] = 'red-selected-retailcrm';
}

return $value;
}

private function isValidCrmSite($api)
{
$errorMessage = '';
$isValidCrmSite = true;
$response = $api->sitesList();

if (empty($response['sites']) || !$response->isSuccessful()) {
$errorMessage = 'Enter the correct API key';
$isValidCrmSite = false;
} elseif (count($response['sites']) > 1) {
$errorMessage = 'API key with one-shop access required';
$isValidCrmSite = false;
} else {
$site = current($response['sites']);

if (get_woocommerce_currency() !== $site['currency']) {
$errorMessage = 'The currency of the site differs from the currency of the store in CRM. For the integration to work correctly, the currencies in CRM and CMS must match';
$isValidCrmSite = false;
}
}

if ('' !== $errorMessage) {
WC_Admin_Settings::add_error(esc_html__($errorMessage, 'retailcrm'));
}

return $isValidCrmSite;
}

/**
* Validate whatsapp phone number
Expand Down
Binary file modified src/languages/retailcrm-es_ES.mo
Binary file not shown.
Binary file modified src/languages/retailcrm-ru_RU.mo
Binary file not shown.
5 changes: 4 additions & 1 deletion src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: Интеграция, Simla.com, simla
Requires PHP: 7.0
Requires at least: 5.3
Tested up to: 6.2
Stable tag: 4.6.11
Stable tag: 4.6.12
License: GPLv1 or later
License URI: http://www.gnu.org/licenses/gpl-1.0.html

Expand Down Expand Up @@ -82,6 +82,9 @@ Asegúrate de tener una clave API específica para cada tienda. Las siguientes i


== Changelog ==
= 4.6.12 =
* Added currency validation when configuring the module

= 4.6.11 =
* Added the ability to work with coupons through the CRM system

Expand Down
2 changes: 1 addition & 1 deletion src/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Integration plugin for WooCommerce & Simla.com
* Author: RetailDriver LLC
* Author URI: http://retailcrm.pro/
* Version: 4.6.11
* Version: 4.6.12
* Tested up to: 6.2
* WC requires at least: 5.4
* WC tested up to: 7.8
Expand Down
2 changes: 1 addition & 1 deletion src/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @link https://wordpress.org/plugins/woo-retailcrm/
*
* @version 4.6.11
* @version 4.6.12
*
* @package RetailCRM
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/datasets/data-base-retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,12 @@ public static function getResponseStoreList()
]
];
}

public static function getResponseSitesList()
{
return [
'success' => true,
'sites' => ['woocommerce' => ['currency' => 'RUB']],
];
}
}
16 changes: 15 additions & 1 deletion tests/test-wc-retailcrm-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function setUp()
->disableOriginalConstructor()
->setMethods(
[
'sitesList',
'storesList',
'orderMethodsList',
'deliveryTypesList',
Expand All @@ -50,6 +51,7 @@ public function setUp()
$this->setMockStatuses();
$this->setMockCustomFields();
$this->setMockStoresList();
$this->setMockSitesList();

$_GET['page'] = 'wc-settings';
$_GET['tab'] = 'integration';
Expand Down Expand Up @@ -272,7 +274,6 @@ public function test_subscribed_checkbox()
ob_end_clean();
}


public function test_initialize_whatsapp()
{
ob_start();
Expand Down Expand Up @@ -426,6 +427,19 @@ private function setMockStoresList()
$this->responseMockStoresList->setResponse(DataBaseRetailCrm::getResponseStoreList());
$this->setMockResponse($this->apiMock, 'storesList', $this->responseMockStoresList);
}
private function setMockSitesList()
{
$this->responseMockStoresList = $this
->getMockBuilder('\WC_Retailcrm_Response_Helper')
->disableOriginalConstructor()
->setMethods(['isSuccessful'])
->getMock();

$this->setMockResponse($this->responseMockStoresList, 'isSuccessful', true);

$this->responseMockStoresList->setResponse(DataBaseRetailCrm::getResponseSitesList());
$this->setMockResponse($this->apiMock, 'storesList', $this->responseMockStoresList);
}

private function setMockOrderMethods()
{
Expand Down

0 comments on commit 4ea6015

Please sign in to comment.