diff --git a/CmCIC.php b/CmCIC.php index ab08212..a8f6c1e 100755 --- a/CmCIC.php +++ b/CmCIC.php @@ -23,7 +23,6 @@ namespace CmCIC; -use CmCIC\Model\Config; use Propel\Runtime\Connection\ConnectionInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator; @@ -32,6 +31,7 @@ use Thelia\Core\HttpFoundation\Response; use Thelia\Core\Translation\Translator; use Thelia\Log\Tlog; +use Thelia\Model\ModuleConfigQuery; use Thelia\Model\ModuleImageQuery; use Thelia\Model\Order; use Thelia\Model\OrderAddress; @@ -43,12 +43,23 @@ class CmCIC extends AbstractPaymentModule { const DOMAIN_NAME = "cmcic"; - const JSON_CONFIG_PATH = "/Config/config.json"; - const CMCIC_CGI2_RECEIPT = "version=2\ncdr=%s"; const CMCIC_CGI2_MACOK = "0"; const CMCIC_CGI2_MACNOTOK = "1\n"; + const CMCIC_DEFAULT_CONF_VALUES = + [ + 'CMCIC_KEY' => '12345678901234567890123456789012345678P0', + 'CMCIC_TPE' => '0000001', + 'CMCIC_VERSION' => '3.0', + 'CMCIC_CODESOCIETE' => '4b18fba7070c8ae6bea8', + 'CMCIC_SERVER' => 'https://p.monetico-services.com/', + 'CMCIC_PAGE' => 'paiement.cgi', + 'CMCIC_DEBUG' => false, + 'CMCIC_ALLOWED_IPS' => null, + 'CMCIC_send_confirmation_message_only_if_paid' => false, + ]; + protected $config; /** @@ -62,11 +73,11 @@ class CmCIC extends AbstractPaymentModule */ public function isValidPayment() { - $debug = $this->getConfigValue('debug', false); + $debug = $this->getConfigValue('CMCIC_DEBUG', false); if ($debug) { // Check allowed IPs when in test mode. - $testAllowedIps = $this->getConfigValue('allowed_ips', ''); + $testAllowedIps = $this->getConfigValue('CMCIC_ALLOWED_IPS', ''); $raw_ips = explode("\n", $testAllowedIps); @@ -98,17 +109,9 @@ public function isValidPayment() public function postActivation(ConnectionInterface $con = null): void { /* insert the images from image folder if first module activation */ - $configFile = __DIR__ . self::JSON_CONFIG_PATH; - $configDistFile = __DIR__ . self::JSON_CONFIG_PATH . '.dist'; - - if (!file_exists($configFile)) { - if (!copy($configDistFile, $configFile)) { - throw new \Exception( - Translator::getInstance()->trans( - "Can't create file %file%. Please change the rights on the file and/or directory." - ) - ); - } + + foreach ($this::CMCIC_DEFAULT_CONF_VALUES as $nameConf => $valueConf){ + CmCIC::setConfigValue($nameConf, $valueConf); } $module = $this->getModuleModel(); @@ -148,7 +151,11 @@ public function update($currentVersion, $newVersion, ConnectionInterface $con = */ public function pay(Order $order) { - $c = Config::read(CmCIC::JSON_CONFIG_PATH); + + $c = []; + foreach (ModuleConfigQuery::create()->filterByModuleId(CmCIC::getModuleId())->find()->getData() as $moduleConf) { + $c[$moduleConf->getName()] = $moduleConf->getValue(); + } $currency = $order->getCurrency()->getCode(); $vars = array( diff --git a/Config/config.json.dist b/Config/config.json.dist deleted file mode 100644 index d31242b..0000000 --- a/Config/config.json.dist +++ /dev/null @@ -1 +0,0 @@ -{"CMCIC_TPE":"0000001","CMCIC_KEY":"12345678901234567890123456789012345678P0","CMCIC_CODESOCIETE":"4b18fba7070c8ae6bea8","CMCIC_VERSION":"3.0","CMCIC_SERVER":"https:\/\/p.monetico-services.com\/","CMCIC_PAGE":"paiement.cgi"} \ No newline at end of file diff --git a/Config/config.xml b/Config/config.xml index 90fb440..8bd8a9f 100755 --- a/Config/config.xml +++ b/Config/config.xml @@ -4,9 +4,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd"> - - - diff --git a/Controller/CmcicPayResponse.php b/Controller/CmcicPayResponse.php index 5af5c42..9ed2e9f 100755 --- a/Controller/CmcicPayResponse.php +++ b/Controller/CmcicPayResponse.php @@ -24,7 +24,6 @@ namespace CmCIC\Controller; use CmCIC\CmCIC; -use CmCIC\Model\Config; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Thelia\Controller\Front\BaseFrontController; use Thelia\Core\Event\Order\OrderEvent; @@ -81,8 +80,7 @@ public function receiveResponse(EventDispatcherInterface $eventDispatcher) /* * Retrieve HMac for CGI2 */ - $config = Config::read(CmCIC::JSON_CONFIG_PATH); - + $vars = $request->request->all(); unset($vars['MAC']); @@ -91,7 +89,7 @@ public function receiveResponse(EventDispatcherInterface $eventDispatcher) $computed_mac = CmCIC::computeHmac( $hashable, - CmCIC::getUsableKey($config["CMCIC_KEY"]) + CmCIC::getUsableKey(CmCIC::getConfigValue("CMCIC_KEY")) ); $response=CmCIC::CMCIC_CGI2_MACNOTOK.$hashable; diff --git a/Controller/CmcicSaveConfig.php b/Controller/CmcicSaveConfig.php index 5d54fa7..924bdea 100755 --- a/Controller/CmcicSaveConfig.php +++ b/Controller/CmcicSaveConfig.php @@ -25,7 +25,8 @@ use CmCIC\CmCIC; use CmCIC\Form\ConfigureCmCIC; -use CmCIC\Model\Config; +use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Thelia\Controller\Admin\BaseAdminController; use Thelia\Core\HttpFoundation\Request; use Thelia\Core\HttpFoundation\Response; @@ -57,14 +58,14 @@ public function downloadLog() if (empty($data)) { $data = Translator::getInstance()->trans("The CmCIC server log is currently empty.", [], CmCIC::DOMAIN_NAME); } - return Response::create( - $data, - 200, - array( - 'Content-type' => "text/plain", - 'Content-Disposition' => sprintf('Attachment;filename=log-cmcic.txt') - ) - ); + + $header = [ + 'Content-Type' => "text/plain", + 'Content-Disposition' => sprintf( + sprintf('Attachment;filename=log-cmcic.txt') + ), + ]; + return new Response($data, 200, $header); } public function save(Request $request) @@ -74,23 +75,18 @@ public function save(Request $request) } $error_message=""; - $conf = new Config(); $form = $this->createForm(ConfigureCmCIC::getName()); try { $vform = $this->validateForm($form); - CmCIC::setConfigValue('debug', $vform->get('debug')->getData()); - CmCIC::setConfigValue('allowed_ips', $vform->get('allowed_ips')->getData()); - CmCIC::setConfigValue('send_confirmation_message_only_if_paid', $vform->get('send_confirmation_message_only_if_paid')->getData()); - - // After post checks (PREG_MATCH) & create json file - if (preg_match("#^\d{7}$#", $vform->get('TPE')->getData()) && - preg_match("#^[a-z\d]{40}$#i", $vform->get('com_key')->getData()) && - preg_match("#^[a-z\-\d]+$#i", $vform->get('com_soc')->getData()) && - preg_match("#^cic|cm|obc|mon$#", $vform->get('server')->getData()) + // After post checks (PREG_MATCH) + if (preg_match("#^\d{7}$#", $vform->get('CMCIC_TPE')->getData()) && + preg_match("#^[a-z\d]{40}$#i", $vform->get('CMCIC_KEY')->getData()) && + preg_match("#^[a-z\-\d]+$#i", $vform->get('CMCIC_CODESOCIETE')->getData()) && + preg_match("#^cic|cm|obc|mon$#", $vform->get('CMCIC_SERVER')->getData()) ) { - $serv = $vform->get('server')->getData(); + $serv = $vform->get('CMCIC_SERVER')->getData(); switch($serv) { case 'mon': @@ -113,19 +109,20 @@ public function save(Request $request) throw new \InvalidArgumentException("Unknown server type '$serv'"); } - if ($vform->get('debug')->getData() === true) { + if ($vform->get('CMCIC_DEBUG')->getData() === true) { $serv .= 'test/'; } - $conf - ->setCMCICKEY($vform->get('com_key')->getData()) - ->setCMCICVERSION(self::CMCIC_VERSION) - ->setCMCICCODESOCIETE($vform->get('com_soc')->getData()) - ->setCMCICPAGE($vform->get('page')->getData()) - ->setCMCICTPE($vform->get('TPE')->getData()) - ->setCMCICSERVER($serv) - ->write(CmCIC::JSON_CONFIG_PATH) - ; + $data = $vform->getData(); + + $data['CMCIC_SERVER'] = $serv; + + foreach ($data as $name => $value) { + if (!preg_match('/^CMCIC/', $name)) { + continue; + } + CmCIC::setConfigValue($name, $value); + } } else { throw new \Exception($this->getTranslator()->trans("Error in form syntax, please check that your values are correct.")); } diff --git a/EventListeners/SendConfirmationEmail.php b/EventListeners/SendConfirmationEmail.php index 8749812..9d8084b 100644 --- a/EventListeners/SendConfirmationEmail.php +++ b/EventListeners/SendConfirmationEmail.php @@ -35,7 +35,7 @@ public function __construct(EventDispatcherInterface $dispatcher) */ public function sendConfirmationEmail(OrderEvent $event) { - if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) { + if (CmCIC::getConfigValue('CMCIC_send_confirmation_message_only_if_paid')) { // We send the order confirmation email only if the order is paid $order = $event->getOrder(); if (!$order->isPaid() && $order->getPaymentModuleId() == CmCIC::getModuleId()) { @@ -53,7 +53,7 @@ public function updateStatus(OrderEvent $event) $order = $event->getOrder(); if ($order->isPaid() && $order->getPaymentModuleId() == CmCIC::getModuleId()) { // Send confirmation email if required. - if (CmCIC::getConfigValue('send_confirmation_message_only_if_paid')) { + if (CmCIC::getConfigValue('CMCIC_send_confirmation_message_only_if_paid')) { $this->dispatcher->dispatch($event, TheliaEvents::ORDER_SEND_CONFIRMATION_EMAIL); } diff --git a/Form/ConfigureCmCIC.php b/Form/ConfigureCmCIC.php index c58ffc6..7792dcf 100755 --- a/Form/ConfigureCmCIC.php +++ b/Form/ConfigureCmCIC.php @@ -31,48 +31,50 @@ use Symfony\Component\Validator\Constraints\NotBlank; use Thelia\Core\Translation\Translator; use Thelia\Form\BaseForm; +use Thelia\Model\ModuleConfigQuery; class ConfigureCmCIC extends BaseForm { protected function buildForm() { - $values = null; - $path = __DIR__ . "/../" . CmCIC::JSON_CONFIG_PATH; - if (is_readable($path)) { - $values = json_decode(file_get_contents($path), true); + $values = []; + foreach (ModuleConfigQuery::create()->filterByModuleId(CmCIC::getModuleId())->find()->getData() as $moduleConf) { + $values[$moduleConf->getName()] = $moduleConf->getValue(); } + + $this->formBuilder - ->add('com_key', TextType::class, [ + ->add('CMCIC_KEY', TextType::class, [ 'label' => Translator::getInstance()->trans('Merchant key', [], CmCIC::DOMAIN_NAME), 'label_attr' => [ - 'for' => 'com_key' + 'for' => 'CMCIC_KEY' ], 'data' => (null === $values ? '' : $values["CMCIC_KEY"]), 'constraints' => [ new NotBlank() ] ]) - ->add('TPE', TextType::class, [ + ->add('CMCIC_TPE', TextType::class, [ 'label' => Translator::getInstance()->trans('TPE', [], CmCIC::DOMAIN_NAME), 'label_attr' => [ - 'for' => 'TPE' + 'for' => 'CMCIC_TPE' ], 'data' => (null === $values ? '' : $values["CMCIC_TPE"]), 'constraints' => [ new NotBlank() ] ]) - ->add('com_soc', TextType::class, [ + ->add('CMCIC_CODESOCIETE', TextType::class, [ 'label' => Translator::getInstance()->trans('Society code', [], CmCIC::DOMAIN_NAME), 'label_attr' => [ - 'for' => 'com_soc' + 'for' => 'CMCIC_CODESOCIETE' ], 'data' => (null === $values ? '' : $values["CMCIC_CODESOCIETE"]), 'constraints' => [ new NotBlank() ] ]) - ->add('server', ChoiceType::class, [ + ->add('CMCIC_SERVER', ChoiceType::class, [ 'label' => Translator::getInstance()->trans('server', [], CmCIC::DOMAIN_NAME), 'choices' => [ "CIC" => "cic", @@ -108,7 +110,7 @@ protected function buildForm() ], ]) - ->add('page', TextType::class, [ + ->add('CMCIC_PAGE', TextType::class, [ 'label' => Translator::getInstance()->trans('page', [], CmCIC::DOMAIN_NAME), 'label_attr' => [ 'help' => Translator::getInstance()->trans( @@ -122,28 +124,28 @@ protected function buildForm() new NotBlank() ] ]) - ->add('debug', CheckboxType::class, [ + ->add('CMCIC_DEBUG', CheckboxType::class, [ 'label' => Translator::getInstance()->trans('Run in test mode', [], CmCIC::DOMAIN_NAME), 'required' => false, 'label_attr' => [ - 'for' => 'debug', + 'for' => 'CMCIC_DEBUG', 'help' => Translator::getInstance()->trans( "Check this box to test the payment system, using test credit cards to simulate various situations.", [], CmCIC::DOMAIN_NAME ) ], - 'data' => boolval(CmCIC::getConfigValue('debug', false)) + 'data' => !empty($values['CMCIC_DEBUG']) ]) ->add( - 'allowed_ips', + 'CMCIC_ALLOWED_IPS', TextareaType::class, [ 'required' => false, 'label' => Translator::getInstance()->trans('Allowed IPs in test mode', [], CmCIC::DOMAIN_NAME), - 'data' => CmCIC::getConfigValue('allowed_ips', ''), + 'data' => $values['CMCIC_ALLOWED_IPS'] ?? "", 'label_attr' => [ - 'for' => 'allowed_ips', + 'for' => 'CMCIC_ALLOWED_IPS', 'help' => Translator::getInstance()->trans( 'List of IP addresses allowed to use this payment on the front-office when in test mode (your current IP is %ip). One address per line', ['%ip' => $this->getRequest()->getClientIp()], @@ -153,11 +155,11 @@ protected function buildForm() ] ] )->add( - 'send_confirmation_message_only_if_paid', + 'CMCIC_send_confirmation_message_only_if_paid', CheckboxType::class, [ 'value' => 1, - 'data' => !empty(CmCIC::getConfigValue('send_confirmation_message_only_if_paid', '')), + 'data' => !empty($values['CMCIC_send_confirmation_message_only_if_paid']), 'required' => false, 'label' => $this->translator->trans('Send order confirmation on payment success', [], CmCIC::DOMAIN_NAME), 'label_attr' => [ diff --git a/Loop/CheckRightsLoop.php b/Loop/CheckRightsLoop.php deleted file mode 100755 index c739e52..0000000 --- a/Loop/CheckRightsLoop.php +++ /dev/null @@ -1,82 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace CmCIC\Loop; - -use Thelia\Core\Template\Loop\Argument\ArgumentCollection; -use Thelia\Core\Template\Element\BaseLoop; -use Thelia\Core\Template\Element\LoopResultRow; -use Thelia\Core\Template\Element\LoopResult; -use Thelia\Core\Template\Element\ArraySearchLoopInterface; -use Thelia\Core\Translation\Translator; - -/** - * Class CheckRightsLoop - * @package CmCIC\Looop - * @author Thelia - */ - -class CheckRightsLoop extends BaseLoop implements ArraySearchLoopInterface -{ - protected function getArgDefinitions() - { - return new ArgumentCollection(); - } - - public function buildArray() - { - $ret = array(); - $dir = __DIR__."/../Config/"; - if (!is_readable($dir)) { - $ret[] = array("ERRMES"=>Translator::getInstance()->trans("Can't read Config directory"), "ERRFILE"=>""); - } - if (!is_writable($dir)) { - $ret[] = array("ERRMES"=>Translator::getInstance()->trans("Can't write Config directory"), "ERRFILE"=>""); - } - if ($handle = opendir($dir)) { - while (false !== ($file = readdir($handle))) { - if (strlen($file) > 5 && substr($file, -5) === ".json") { - if (!is_readable($dir.$file)) { - $ret[] = array("ERRMES"=>Translator::getInstance()->trans("Can't read file"), "ERRFILE"=>"CmCIC/Config/".$file); - } - if (!is_writable($dir.$file)) { - $ret[] = array("ERRMES"=>Translator::getInstance()->trans("Can't write file"), "ERRFILE"=>"CmCIC/Config/".$file); - } - } - } - } - - return $ret; - } - public function parseResults(LoopResult $loopResult) - { - foreach ($loopResult->getResultDataCollection() as $arr) { - $loopResultRow = new LoopResultRow(); - $loopResultRow->set("ERRMES", $arr["ERRMES"]) - ->set("ERRFILE", $arr["ERRFILE"]); - $loopResult->addRow($loopResultRow); - } - - return $loopResult; - } -} diff --git a/Model/Config.php b/Model/Config.php deleted file mode 100755 index 4a49c95..0000000 --- a/Model/Config.php +++ /dev/null @@ -1,142 +0,0 @@ -read(); - } catch (\Exception $e) {} - if ($config !== null) { - foreach ($config as $key=>$val) { - try { - $this->__set($key,$val); - } catch (\Exception $e) {} - } - } - } - - public function write($file=null) - { - $path = __DIR__."/../".$file; - if ((file_exists($path) ? is_writable($path):is_writable(__DIR__."/../Config/"))) { - $vars= get_object_vars($this); - $cond = true; - foreach($vars as $key=>$var) - $cond &= !empty($var); - if ($cond) { - $file = fopen($path, 'w'); - fwrite($file, json_encode($vars)); - fclose($file); - } - } else { - throw new \Exception(Translator::getInstance()->trans("Can't write file ").$file.". ". - Translator::getInstance()->trans("Please change the rights on the file and/or directory.")); - - } - } - /** - * @return array - */ - public static function read($file=null) - { - $path = __DIR__."/../".$file; - $ret = null; - if (is_readable($path)) { - $json = json_decode(file_get_contents($path), true); - if ($json !== null) { - $ret = $json; - } else { - throw new \Exception(Translator::getInstance()->trans("Can't read file ").$file.". ". - Translator::getInstance()->trans("The file is corrupted.")); - } - } elseif (!file_exists($path)) { - throw new \Exception(Translator::getInstance()->trans("The file ").$file. - Translator::getInstance()->trans(" doesn't exist. You have to create it in order to use this module. Please see module's configuration page.")); - } else { - throw new \Exception(Translator::getInstance()->trans("Can't read file ").$file.". ". - Translator::getInstance()->trans("Please change the rights on the file.")); - - } - - return $ret; - } - - /** - * @param string $CMCIC_PAGE - * @return $this - */ - public function setCMCICPAGE($CMCIC_PAGE) - { - $this->CMCIC_PAGE = $CMCIC_PAGE; - - return $this; - } - - /** - * @param string $CMCIC_KEY - * @return $this - */ - public function setCMCICKEY($CMCIC_KEY) - { - $this->CMCIC_KEY = $CMCIC_KEY; - - return $this; - } - - /** - * @param string $CMCIC_CODESOCIETE - * @return $this - */ - public function setCMCICCODESOCIETE($CMCIC_CODESOCIETE) - { - $this->CMCIC_CODESOCIETE = $CMCIC_CODESOCIETE; - - return $this; - } - /** - * @param string $CMCIC_SERVEUR - * @return $this - */ - public function setCMCICSERVER($CMCIC_SERVER) - { - $this->CMCIC_SERVER = $CMCIC_SERVER; - - return $this; - } - - /** - * @param string $CMCIC_TPE - * @return $this - */ - public function setCMCICTPE($CMCIC_TPE) - { - $this->CMCIC_TPE = $CMCIC_TPE; - - return $this; - } - - /** - * @param string $CMCIC_VERSION - * @return $this - */ - public function setCMCICVERSION($CMCIC_VERSION) - { - $this->CMCIC_VERSION = $CMCIC_VERSION; - - return $this; - } -} diff --git a/Model/ConfigInterface.php b/Model/ConfigInterface.php deleted file mode 100755 index ea4210f..0000000 --- a/Model/ConfigInterface.php +++ /dev/null @@ -1,41 +0,0 @@ -
- {render_form_field field="TPE"} - {render_form_field field="com_key"} - {render_form_field field="com_soc"} + {render_form_field field="CMCIC_TPE"} + {render_form_field field="CMCIC_KEY"} + {render_form_field field="CMCIC_CODESOCIETE"}

{intl d='cmcic.bo.default' l="URL de retour: %url" url={url path="/cmcic/validation"}}

{intl d='cmcic.bo.default' l="This is the value you should provide for the \"URL de Retour\" parameter in your bank back-office."}

@@ -49,11 +49,11 @@
- {render_form_field field="server"} - {render_form_field field="page"} - {render_form_field field="send_confirmation_message_only_if_paid"} - {render_form_field field="debug"} - {render_form_field field="allowed_ips"} + {render_form_field field="CMCIC_SERVER"} + {render_form_field field="CMCIC_PAGE"} + {render_form_field field="CMCIC_send_confirmation_message_only_if_paid"} + {render_form_field field="CMCIC_DEBUG"} + {render_form_field field="CMCIC_ALLOWED_IPS"}