From 602d609bc8d903f097ec291bbec0b3bf2ca0c157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Beltra=CC=81n=20Soli=CC=81s?= Date: Thu, 24 Jan 2019 17:29:31 -0600 Subject: [PATCH] Fix errors and optimize Fix - Images for gateways - Webhook add to ComproPago Change - Assets structure - Reformat code --- .gitignore | 1 + README.md | 2 +- composer.json | 16 + compropago.php | 1123 ++++++++--------- config_es.xml | 2 +- controllers/front/index.php | 4 +- controllers/front/payment.php | 86 +- controllers/front/validation.php | 72 +- controllers/index.php | 4 +- index.php | 4 +- logo-badge-2.png | Bin 3327 -> 0 bytes logo-badge-3.png | Bin 3399 -> 0 bytes logo-badge.png | Bin 2733 -> 0 bytes logo-desc.png | Bin 3076 -> 0 bytes payment.php | 6 +- translations/en.php | 2 +- translations/es.php | 10 +- translations/index.php | 4 +- translations/mx.php | 10 +- validation.php | 49 +- .../assets/css}/cp-style.css | 0 .../assets/css}/ps-default.css | 0 views/assets/img/cp-cash.svg | 109 ++ views/assets/img/cp-spei.png | Bin 0 -> 3748 bytes .../assets/img/gateway-logo.png | Bin icon.png => views/assets/img/icon-config.png | Bin logo.png => views/assets/img/logo.png | Bin views/assets/index.php | 36 + .../assets/js}/providers.js | 0 .../assets/js}/ps-default.js | 2 +- views/index.php | 4 +- views/templates/front/index.php | 4 +- views/templates/front/payment_execution.tpl | 38 +- views/templates/hook/index.php | 2 +- views/templates/hook/infos.tpl | 16 +- views/templates/hook/payment.tpl | 4 +- views/templates/hook/payment_return.tpl | 2 +- views/templates/index.php | 2 +- webhook.php | 38 +- 39 files changed, 908 insertions(+), 744 deletions(-) delete mode 100644 logo-badge-2.png delete mode 100644 logo-badge-3.png delete mode 100644 logo-badge.png delete mode 100644 logo-desc.png rename {specificAssest => views/assets/css}/cp-style.css (100%) rename {specificAssest => views/assets/css}/ps-default.css (100%) create mode 100644 views/assets/img/cp-cash.svg create mode 100644 views/assets/img/cp-spei.png rename gateway-logo.png => views/assets/img/gateway-logo.png (100%) rename icon.png => views/assets/img/icon-config.png (100%) rename logo.png => views/assets/img/logo.png (100%) create mode 100644 views/assets/index.php rename {specificAssest => views/assets/js}/providers.js (100%) rename {specificAssest => views/assets/js}/ps-default.js (97%) diff --git a/.gitignore b/.gitignore index 6ad724c..4a282d4 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ composer.lock dev-notes .idea phpunit.xml +compropago.zip diff --git a/README.md b/README.md index 5ba2d92..0b07bcd 100644 --- a/README.md +++ b/README.md @@ -54,4 +54,4 @@ En caso de tener alguna pregunta o requerir el apoyo técnico, por favor contact - Teléfono local y celular. - Correo electrónico del propietario de la cuenta. - Texto detallado de la duda o requerimiento. -- En caso de presentar algún problema técnico, por favor enviar capturas de pantalla o evidencia para una respuesta más rápida. \ No newline at end of file +- En caso de presentar algún problema técnico, por favor enviar capturas de pantalla o evidencia para una respuesta más rápida. diff --git a/composer.json b/composer.json index 3c33a1f..14ea3a2 100644 --- a/composer.json +++ b/composer.json @@ -1,4 +1,20 @@ { + "name": "", + "description": "", + "homepage": "https://www.compropago.com", + "type": "plugin", + "version": "2.5.1.0", + "keywords": [ + "pagos internet", + "payment gateway", + "prestashop", + "compropago", + "cash", + "spei" + ], + "license": [ + "Apache-2.0" + ], "require": { "compropago/php-sdk": "4.0.1.1" } diff --git a/compropago.php b/compropago.php index b85d397..1dd82b1 100644 --- a/compropago.php +++ b/compropago.php @@ -10,39 +10,40 @@ require_once __DIR__.'/vendor/autoload.php'; + class Compropago extends PaymentModule { - private $_html = ''; - private $_postErrors = array(); - - public $publicKey; - public $privateKey; - public $extra_mail_vars; - public $modoExec; - public $filterStores; - - public $client; - - /** - * set & get module config - * @since 2.0.0 - */ - public function __construct() - { - //Current module version & config - $this->version = '2.5.0.0'; - $this->name = 'compropago'; - $this->tab = 'payments_gateways'; - $this->author = 'ComproPago'; - $this->controllers = array('payment', 'validation'); - $this->is_eu_compatible = 1; - - //currencies setup - $this->currencies = true; - $this->currencies_mode = 'checkbox'; - - // have module been set - if (Tools::isSubmit('btnSubmit')){ + private $_html = ''; + private $_postErrors = array(); + + public $publicKey; + public $privateKey; + public $extra_mail_vars; + public $modoExec; + public $filterStores; + + public $client; + + /** + * set & get module config + * @since 2.0.0 + */ + public function __construct() + { + //Current module version & config + $this->version = '2.5.1.0'; + $this->name = 'compropago'; + $this->tab = 'payments_gateways'; + $this->author = 'ComproPago'; + $this->controllers = ['payment', 'validation']; + $this->is_eu_compatible = 1; + + //currencies setup + $this->currencies = true; + $this->currencies_mode = 'checkbox'; + + // have module been set + if (Tools::isSubmit('btnSubmit')) { $config = [ 'COMPROPAGO_PUBLICKEY' => Tools::getValue('COMPROPAGO_PUBLICKEY'), 'COMPROPAGO_PRIVATEKEY' => Tools::getValue('COMPROPAGO_PRIVATEKEY'), @@ -62,46 +63,45 @@ public function __construct() $this->publicKey = $config['COMPROPAGO_PUBLICKEY']; } - if (isset($config['COMPROPAGO_PRIVATEKEY'])) { + if (isset($config['COMPROPAGO_PRIVATEKEY'])) { $this->privateKey = $config['COMPROPAGO_PRIVATEKEY']; } - $this->modoExec=(isset($config['COMPROPAGO_MODE'])) ? $config['COMPROPAGO_MODE'] : false; - - //most load selected - $this->filterStores = explode(',',$config['COMPROPAGO_PROVIDER'] ); + $this->modoExec=(isset($config['COMPROPAGO_MODE'])) ? $config['COMPROPAGO_MODE'] : false; + //most load selected + $this->filterStores = explode(',', $config['COMPROPAGO_PROVIDER'] ); - $this->bootstrap = true; - parent::__construct(); + $this->bootstrap = true; - //about ComproPago Module - $this->displayName = $this->l('ComproPago'); - $this->description = $this->l('This module allows you to accept payments in Mexico stores like OXXO, 7Eleven and More.'); - $this->confirmUninstall = $this->l('Are you sure you want to uninstall ComproPago?'); + parent::__construct(); - // need some keys - if (( !isset($this->publicKey) || !isset($this->privateKey) || empty($this->publicKey) || empty($this->privateKey) ) ){ - $this->warning = $this->l('The Public Key and Private Key must be configured before using this module.'); - } + //about ComproPago Module + $this->displayName = $this->l('ComproPago'); + $this->description = $this->l('This module allows you to accept payments in Mexico stores like OXXO, 7Eleven and More.'); + $this->confirmUninstall = $this->l('Are you sure you want to uninstall ComproPago?'); - $this->setComproPago($this->modoExec); + // need some keys + if (( !isset($this->publicKey) || !isset($this->privateKey) || empty($this->publicKey) || empty($this->privateKey) ) ){ + $this->warning = $this->l('The Public Key and Private Key must be configured before using this module.'); + } - if($this->context->employee){ + $this->setComproPago($this->modoExec); + + if ($this->context->employee) { $hook_data = $this->hookRetro(true, $this->publicKey, $this->privateKey, $this->modoExec); - if($hook_data[0]){ + if ($hook_data[0]) { $this->warning = $this->l($hook_data[1]); $this->stop = $hook_data[2]; } } - - if (!count(Currency::checkPaymentCurrencies($this->id))) { + if (!count(Currency::checkPaymentCurrencies($this->id))) { $this->warning = $this->l('No currency has been set for this module.'); } - } + } /** * Generacion de retro alimentacion de configuracion al guardar ; @@ -111,14 +111,11 @@ public function __construct() */ public function hookRetro($enabled, $publickey, $privatekey, $live) { - $error = array( - false, - '', - 'yes' - ); - if($enabled){ - if(!empty($publickey) && !empty($privatekey) ){ - try{ + $error = [false, '', 'yes']; + + if ($enabled) { + if(!empty($publickey) && !empty($privatekey) ) { + try { $client = new CompropagoSdk\Client( $publickey, $privatekey, @@ -126,41 +123,41 @@ public function hookRetro($enabled, $publickey, $privatekey, $live) ); $compropagoResponse = CompropagoSdk\Tools\Validations::evalAuth($client); //eval keys - if(!CompropagoSdk\Tools\Validations::validateGateway($client)){ - $error[1] = 'Invalid Keys, The Public Key and Private Key must be valid before using this module.'; + if (!CompropagoSdk\Tools\Validations::validateGateway($client)) { + $error[1] = 'Llaves invalidas, la llave pública y privada deben ser válidas para usar este módulo.'; $error[0] = true; - }else{ - if($compropagoResponse->mode_key != $compropagoResponse->livemode){ - $error[1] = 'Your Keys and Your ComproPago account are set to different Modes.'; + } else { + if ($compropagoResponse->mode_key != $compropagoResponse->livemode) { + $error[1] = 'Tu tienda y tu cuenta de ComproPago se encuentran en diferentes modos.'; $error[0] = true; - }else{ - if($live != $compropagoResponse->livemode){ - $error[1] = 'Your Store and Your ComproPago account are set to different Modes.'; + } else { + if ($live != $compropagoResponse->livemode) { + $error[1] = 'Tu tienda y tu cuenta de ComproPago se encuentran en diferentes modos.'; $error[0] = true; - }else{ - if($live != $compropagoResponse->mode_key){ - $error[1] = 'ComproPago ALERT:Your Keys are for a different Mode.'; + } else { + if ($live != $compropagoResponse->mode_key) { + $error[1] = 'Alarta: Tus llaves del API pertenecen a un modo distinto del de tu tienda.'; $error[0] = true; - }else{ - if(!$compropagoResponse->mode_key && !$compropagoResponse->livemode){ - $error[1] = 'WARNING: ComproPago account is Running in TEST Mode, NO REAL OPERATIONS'; + } else { + if (!$compropagoResponse->mode_key && !$compropagoResponse->livemode) { + $error[1] = 'Advertencia: La cuenta ComproPago se esta operando en Modo Pruebas. Ninguna de las operaciones es real.'; $error[0] = true; } } } } } - }catch (Exception $e) { + } catch (Exception $e) { $error[2] = 'no'; $error[1] = $e->getMessage(); $error[0] = true; } - }else{ + } else { $error[1] = 'The Public Key and Private Key must be set before using ComproPago'; $error[2] = 'no'; $error[0] = true; } - }else{ + } else { $error[1] = 'ComproPago is not Enabled'; $error[2] = 'no'; $error[0] = true; @@ -175,182 +172,184 @@ public function hookRetro($enabled, $publickey, $privatekey, $live) * @return boolean * @since 2.0.0 */ - private function setComproPago($moduleLive) - { - try{ - $this->client = new CompropagoSdk\Client( - $this->publicKey, - $this->privateKey, - $moduleLive - ); - }catch(\Exception $e){ - return false; - } - } - - /** - * Check against SDK if module is valid for use - * @return boolean - * @since 2.0.0 - */ - public function checkCompropago() - { - try { - return CompropagoSdk\Tools\Validations::validateGateway($this->client); - }catch (\Exception $e) { + private function setComproPago($moduleLive) + { + try { + $this->client = new CompropagoSdk\Client( + $this->publicKey, + $this->privateKey, + $moduleLive + ); + } catch(\Exception $e) { return false; - } - } - - /** - * get Providers View Config - * @return mixed Providers TPL config array - * @return false on Exception - * @since 2.0.0 - */ - public function getProvidersCompropago($limit = 0) - { - try{ - global $currency; - - $providers = $this->client->api->listProviders($limit, $currency->iso_code); - $default = explode(",", Configuration::get('COMPROPAGO_PROVIDER')); - $f_providers = []; - - foreach ($default as $def) { - foreach ($providers as $prov) { - if ($def == $prov->internal_name) { - $f_providers [] = $prov; - } - } - } - - if (isset($f_providers[0]) && ($f_providers[0] == NULL)) { - $provflag = 0; - $f_providers = 0; - } else { - $provflag = 1; - } + } + } + + /** + * Check against SDK if module is valid for use + * @return boolean + * @since 2.0.0 + */ + public function checkCompropago() + { + try { + return CompropagoSdk\Tools\Validations::validateGateway($this->client); + } catch (\Exception $e) { + return false; + } + } + + /** + * get Providers View Config + * @return mixed Providers TPL config array + * @return false on Exception + * @since 2.0.0 + */ + public function getProvidersCompropago($limit = 0) + { + try { + global $currency; + + $providers = $this->client->api->listProviders($limit, $currency->iso_code); + $default = explode(",", Configuration::get('COMPROPAGO_PROVIDER')); + $f_providers = []; + + foreach ($default as $def) { + foreach ($providers as $prov) { + if ($def == $prov->internal_name) { + $f_providers [] = $prov; + } + } + } + + if (isset($f_providers[0]) && ($f_providers[0] == NULL)) { + $provflag = 0; + $f_providers = 0; + } else { + $provflag = 1; + } $compropagoData['providers'] = $f_providers; - $compropagoData['flag'] = $provflag; - - $compropagoData['description'] = $this->l('ComproPago te permite pagar en tiendas de México como OXXO, 7Eleven y más.'); - $compropagoData['instrucciones'] = $this->l('Selecciona una tienda'); - - return $compropagoData; - }catch (Exception $e) { - return NULL; - } - } - - /** - * hook header options - * @param mixed $params - * @since 2.0.0 - */ - public function hookDisplayHeader($params) + $compropagoData['flag'] = $provflag; + $compropagoData['description'] = $this->l('ComproPago te permite pagar en tiendas de México como OXXO, 7Eleven y más.'); + $compropagoData['instrucciones'] = $this->l('Selecciona una tienda'); + + return $compropagoData; + } catch (Exception $e) { + return NULL; + } + } + + /** + * hook header options + * @param mixed $params + * @since 2.0.0 + */ + public function hookDisplayHeader($params) { - //add css - //$this->context->controller->addCSS($this->_path.'vendor/compropago/php-sdk/assets/css/compropago.css', 'all'); - $this->context->controller->addCSS($this->_path.'specificAssest/cp-style.css', 'all'); - $this->context->controller->addCSS($this->_path.'specificAssest/ps-default.css', 'all'); - - //add js - $this->context->controller->addJS($this->_path.'specificAssest/ps-default.js', 'all'); - $this->context->controller->addJS($this->_path.'specificAssest/providers.js', 'all'); - } - - /** - * Install Module - * @return boolean - * @since 2.0.0 - */ - public function install() - { - $this->installOrderStates(); + $assets_path = "{$this->_path}views/assets/"; + + # Add CSS + $this->context->controller->addCSS("{$assets_path}css/cp-style.css", 'all'); + $this->context->controller->addCSS("{$assets_path}css/ps-default.css", 'all'); + + # Add JS + $this->context->controller->addJS("{$assets_path}js/ps-default.js", 'all'); + $this->context->controller->addJS("{$assets_path}js/specificAssest/providers.js", 'all'); + } + + /** + * Install Module + * @return boolean + * @since 2.0.0 + */ + public function install() + { + $this->installOrderStates(); $this->sqlDropTables(); $this->sqlCreateTables(); - if (!parent::install() || !$this->registerHook('payment') || ! $this->registerHook('displayPaymentEU') || !$this->registerHook('paymentReturn') || !$this->registerHook('displayHeader')) + if (!parent::install() || !$this->registerHook('payment') || ! $this->registerHook('displayPaymentEU') || !$this->registerHook('paymentReturn') || !$this->registerHook('displayHeader')) { return false; + } + return true; - } - - /** - * Vertify is compropago tables exists - * @return boolean - * @since 2.0.0 - */ - public function verifyTables(){ - if(!Db::getInstance()->execute("SHOW TABLES LIKE '"._DB_PREFIX_ ."compropago_orders'") || - !Db::getInstance()->execute("SHOW TABLES LIKE '"._DB_PREFIX_ ."compropago_transactions'") - ){ + } + + /** + * Vertify is compropago tables exists + * @return boolean + * @since 2.0.0 + */ + public function verifyTables() { + if ( + !Db::getInstance()->execute("SHOW TABLES LIKE '" . _DB_PREFIX_ . "compropago_orders'") || + !Db::getInstance()->execute("SHOW TABLES LIKE '" . _DB_PREFIX_ . "compropago_transactions'")) { return false; - } - - return true; - } - - /** - * Install ComproPago Order Status - * @return boolean - * @since 2.0.0 - */ - protected function installOrderStates() - { + } + + return true; + } + + /** + * Install ComproPago Order Status + * @return boolean + * @since 2.0.0 + */ + protected function installOrderStates() + { $cp_order_states = array( - array( - 'label' => 'ComproPago - Pending Payment', - 'value' => 'COMPROPAGO_PENDING', - 'color' => array( - 'invoice' => 0, - 'send_email' => 0, - 'module_name' => pSQL($this->name), - 'color' => '#FFFF7F', - 'unremovable' => 0, - 'hidden' => 0, - 'logable' => 1, - 'delivery' => 0, - 'shipped' => 0, - 'paid' => 0, - 'deleted' => 0 - ) - ), - array( - 'label' => 'ComproPago - Payment received', - 'value' => 'COMPROPAGO_SUCCESS', - 'color' => array( - 'invoice' => 0, - 'send_email' => 0, - 'module_name' => pSQL($this->name), - 'color' => '#CCFF00', - 'unremovable' => 0, - 'hidden' => 0, - 'logable' => 1, - 'delivery' => 0, - 'shipped' => 0, - 'paid' => 0, - 'deleted' => 0 - ) - ), - array( - 'label' => 'ComproPago - Expired', - 'value' => 'COMPROPAGO_EXPIRED', - 'color' => array( - 'invoice' => 0, - 'send_email' => 0, - 'module_name' => pSQL($this->name), - 'color' => '#FF3300', - 'unremovable' => 0, - 'hidden' => 0, - 'logable' => 1, - 'delivery' => 0, - 'shipped' => 0, - 'paid' => 0, - 'deleted' => 0 - ) - ) + array( + 'label' => 'ComproPago - Pending Payment', + 'value' => 'COMPROPAGO_PENDING', + 'color' => [ + 'invoice' => 0, + 'send_email' => 0, + 'module_name' => pSQL($this->name), + 'color' => '#FFFF7F', + 'unremovable' => 0, + 'hidden' => 0, + 'logable' => 1, + 'delivery' => 0, + 'shipped' => 0, + 'paid' => 0, + 'deleted' => 0 + ] + ), + array( + 'label' => 'ComproPago - Payment received', + 'value' => 'COMPROPAGO_SUCCESS', + 'color' => [ + 'invoice' => 0, + 'send_email' => 0, + 'module_name' => pSQL($this->name), + 'color' => '#CCFF00', + 'unremovable' => 0, + 'hidden' => 0, + 'logable' => 1, + 'delivery' => 0, + 'shipped' => 0, + 'paid' => 0, + 'deleted' => 0 + ] + ), + array( + 'label' => 'ComproPago - Expired', + 'value' => 'COMPROPAGO_EXPIRED', + 'color' => [ + 'invoice' => 0, + 'send_email' => 0, + 'module_name' => pSQL($this->name), + 'color' => '#FF3300', + 'unremovable' => 0, + 'hidden' => 0, + 'logable' => 1, + 'delivery' => 0, + 'shipped' => 0, + 'paid' => 0, + 'deleted' => 0 + ] + ) ); /* @@ -359,39 +358,41 @@ protected function installOrderStates() * 1. Insert order state color * 2. Insert Compropago order states and attach state color using identifier */ - foreach ($cp_order_states as $state){ - // Check if we can insert order state color - if (! Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_state', $state['color'], 'INSERT')) { return false; } + foreach ($cp_order_states as $state) { + # Check if we can insert order state color + if (! Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_state', $state['color'], 'INSERT')) { + return false; + } - // Get ID and insert compropago order state. - $id_order_state = (int) Db::getInstance()->Insert_ID(); - $languages = Language::getLanguages(false); + # Get ID and insert compropago order state. + $id_order_state = (int) Db::getInstance()->Insert_ID(); + $languages = Language::getLanguages(false); foreach ($languages as $language) { - Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_state_lang', array( - 'id_order_state' => $id_order_state, - 'id_lang' => $language['id_lang'], - 'name' => $this->l($state['label']), - 'template' => '' - ), 'INSERT'); + Db::getInstance()->autoExecute(_DB_PREFIX_ . 'order_state_lang', [ + 'id_order_state' => $id_order_state, + 'id_lang' => $language['id_lang'], + 'name' => $this->l($state['label']), + 'template' => '' + ], 'INSERT'); } Configuration::updateValue($state['value'], $id_order_state); unset($id_order_state); } - } + } - /** - * Uninstall Module - * @return boolean - * @since 2.0.0 - */ - public function uninstall() - { + /** + * Uninstall Module + * @return boolean + * @since 2.0.0 + */ + public function uninstall() + { $this->sqlDropTables(); - if (!Configuration::deleteByName('COMPROPAGO_PUBLICKEY') + if (!Configuration::deleteByName('COMPROPAGO_PUBLICKEY') || !Configuration::deleteByName('COMPROPAGO_PRIVATEKEY') || !Configuration::deleteByName('COMPROPAGO_MODE') || !Configuration::deleteByName('COMPROPAGO_WEBHOOK') @@ -404,45 +405,45 @@ public function uninstall() return false; } - return true; - } - - /** - * Validate module config form - * @since 2.0.0 - */ - private function _postValidation() - { - if (Tools::isSubmit('btnSubmit')) - { - if (!Tools::getValue('COMPROPAGO_PUBLICKEY')){ - $this->_postErrors[] = $this->l('The Public Key is required'); - }elseif (!Tools::getValue('COMPROPAGO_PRIVATEKEY')){ - $this->_postErrors[] = $this->l('The Private Key is required'); - } - - } - } - - /** - *Refresh configed data after module config updated - * @since 2.0.0 - */ - private function _postProcess() - { - if (Tools::isSubmit('btnSubmit')) - { - /** - * Update values at database from form values Tools::getValue(form_field) - */ - Configuration::updateValue('COMPROPAGO_PUBLICKEY', Tools::getValue('COMPROPAGO_PUBLICKEY')); - Configuration::updateValue('COMPROPAGO_PRIVATEKEY', Tools::getValue('COMPROPAGO_PRIVATEKEY')); - Configuration::updateValue('COMPROPAGO_MODE', Tools::getValue('COMPROPAGO_MODE')); - $myproviders=implode(',',Tools::getValue('COMPROPAGO_PROVIDERS_selected')); - Configuration::updateValue('COMPROPAGO_PROVIDER',$myproviders ); - } - - if ($this->stop) { + return true; + } + + /** + * Validate module config form + * @since 2.0.0 + */ + private function _postValidation() + { + if (Tools::isSubmit('btnSubmit')) + { + if (!Tools::getValue('COMPROPAGO_PUBLICKEY')) { + $this->_postErrors[] = $this->l('The Public Key is required'); + } elseif (!Tools::getValue('COMPROPAGO_PRIVATEKEY')) { + $this->_postErrors[] = $this->l('The Private Key is required'); + } + + } + } + + /** + *Refresh configed data after module config updated + * @since 2.0.0 + */ + private function _postProcess() + { + if (Tools::isSubmit('btnSubmit')) + { + /** + * Update values at database from form values Tools::getValue(form_field) + */ + Configuration::updateValue('COMPROPAGO_PUBLICKEY', Tools::getValue('COMPROPAGO_PUBLICKEY')); + Configuration::updateValue('COMPROPAGO_PRIVATEKEY', Tools::getValue('COMPROPAGO_PRIVATEKEY')); + Configuration::updateValue('COMPROPAGO_MODE', Tools::getValue('COMPROPAGO_MODE')); + $myproviders=implode(',',Tools::getValue('COMPROPAGO_PROVIDERS_selected')); + Configuration::updateValue('COMPROPAGO_PROVIDER',$myproviders ); + } + + if ($this->stop) { if (!Tools::getValue('COMPROPAGO_PUBLICKEY') && !Tools::getValue('COMPROPAGO_PRIVATEKEY')) { return false; } else { @@ -450,7 +451,7 @@ private function _postProcess() $this->client->api->createWebhook(Tools::getValue('COMPROPAGO_WEBHOOK')); $this->_html .= $this->displayConfirmation($this->l('Opciones actualizadas')); } catch (\Exception $e) { - if ($e->getMessage() != 'Error: conflict.urls.create') { + if ( !in_array($e->getMessage(), ['Request error: 409', 'Error: conflict.urls.create']) ) { $this->_html .= $this->displayError($e->getMessage()); } } @@ -466,44 +467,43 @@ private function _postProcess() } } } - } - - /** - * Display Compropago description TPL at module configuration - * @since 2.0.0 - */ - private function _displayCompropago() - { - return $this->display(__FILE__, 'infos.tpl'); - } - - /** - * Show Errors & load description, and after submit information at admin configuration page - * @return mixed - * @since 2.0.0 - */ - public function getContent() - { - $this->_html = ''; - - if (Tools::isSubmit('btnSubmit')) - { - $this->_postValidation(); - if (!count($this->_postErrors)){ - $this->_postProcess(); - } - else{ - foreach ($this->_postErrors as $err){ - $this->_html .= $this->displayError($err); - } - } - } - - $this->_html .= $this->_displayCompropago(); - $this->_html .= $this->renderForm(); - - return $this->_html; - } + } + + /** + * Display Compropago description TPL at module configuration + * @since 2.0.0 + */ + private function _displayCompropago() + { + return $this->display(__FILE__, 'infos.tpl'); + } + + /** + * Show Errors & load description, and after submit information at admin configuration page + * @return mixed + * @since 2.0.0 + */ + public function getContent() + { + $this->_html = ''; + + if (Tools::isSubmit('btnSubmit')) + { + $this->_postValidation(); + if (!count($this->_postErrors)) { + $this->_postProcess(); + } else { + foreach ($this->_postErrors as $err) { + $this->_html .= $this->displayError($err); + } + } + } + + $this->_html .= $this->_displayCompropago(); + $this->_html .= $this->renderForm(); + + return $this->_html; + } /** * Show Compropago as checkout payment method @@ -512,271 +512,268 @@ public function getContent() * @since 2.0.0 * @return bool */ - public function hookPayment($params) - { - if (!$this->active) { + public function hookPayment($params) + { + if (!$this->active) { return false; } - if (!$this->checkCurrency($params['cart'])){ - return false; + + if (!$this->checkCurrency($params['cart'])){ + return false; } - if(!$this->checkCompropago()) { + if(!$this->checkCompropago()) { return false; } - $this->smarty->assign(array( - 'this_path' => $this->_path, - 'this_path_compropago' => $this->_path, - 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/' - )); - - return $this->display(__FILE__, 'payment.tpl'); - } - - /** - * Hook Compropago - * @param mixed $params - * @return mixed - * @since 2.0.0 - */ - public function hookDisplayPaymentEU($params) - { - if (!$this->active) { + $this->smarty->assign([ + 'this_path' => $this->_path, + 'this_path_compropago' => $this->_path, + 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/' + ]); + + return $this->display(__FILE__, 'payment.tpl'); + } + + /** + * Hook Compropago + * @param mixed $params + * @return mixed + * @since 2.0.0 + */ + public function hookDisplayPaymentEU($params) + { + if (!$this->active) { return false; } - if (!$this->checkCurrency($params['cart'])) { + if (!$this->checkCurrency($params['cart'])) { return false; } - if(!$this->checkCompropago()) { + if(!$this->checkCompropago()) { return false; } - $payment_options = array( - 'cta_text' => $this->l('Pay by ComproPago'), - 'logo' => Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/logo.png'), - 'action' => $this->context->link->getModuleLink($this->name, 'validation', array(), true) - ); - - return $payment_options; - } - - /** - * After payment options selected - * @param mixed $params - * @return mixed - * @since 2.0.0 - */ - public function hookPaymentReturn($params) - { - - if (!$this->active) { + $payment_options = [ + 'cta_text' => $this->l('Pay by ComproPago'), + 'logo' => Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/views/assets/img/logo.png'), + 'action' => $this->context->link->getModuleLink($this->name, 'validation', [], true) + ]; + + return $payment_options; + } + + /** + * After payment options selected + * @param mixed $params + * @return mixed + * @since 2.0.0 + */ + public function hookPaymentReturn($params) + { + + if (!$this->active) { return false; } - if(!$this->checkCompropago()) { + if(!$this->checkCompropago()) { return false; } - $state = $params['objOrder']->getCurrentState(); + $state = $params['objOrder']->getCurrentState(); - if( !isset($_REQUEST['compropagoId']) || !isset($_REQUEST['id_cart']) || !isset($_REQUEST['id_order']) || empty($_REQUEST['compropagoId']) || empty($_REQUEST['id_cart']) || empty($_REQUEST['id_order']) ){ - $compropagoStatus = 'fail'; - }else{ - $compropagoStatus = 'ok'; - } + if( !isset($_REQUEST['compropagoId']) || !isset($_REQUEST['id_cart']) || !isset($_REQUEST['id_order']) || empty($_REQUEST['compropagoId']) || empty($_REQUEST['id_cart']) || empty($_REQUEST['id_order']) ){ + $compropagoStatus = 'fail'; + }else{ + $compropagoStatus = 'ok'; + } - if (in_array($state, array(Configuration::get('COMPROPAGO_PENDING'), Configuration::get('PS_OS_OUTOFSTOCK'), Configuration::get('PS_OS_OUTOFSTOCK_UNPAID')))) - { - $this->smarty->assign(array( - 'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false), - 'status' => $compropagoStatus, - 'id_order' => $params['objOrder']->id, - 'order_id' => $_REQUEST['compropagoId'], - )); + if (in_array($state, array(Configuration::get('COMPROPAGO_PENDING'), Configuration::get('PS_OS_OUTOFSTOCK'), Configuration::get('PS_OS_OUTOFSTOCK_UNPAID')))) + { + $this->smarty->assign(array( + 'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false), + 'status' => $compropagoStatus, + 'id_order' => $params['objOrder']->id, + 'order_id' => $_REQUEST['compropagoId'], + )); - if (isset($params['objOrder']->reference) && !empty($params['objOrder']->reference)) { + if (isset($params['objOrder']->reference) && !empty($params['objOrder']->reference)) { $this->smarty->assign('reference', $params['objOrder']->reference); } - }else{ + }else{ $this->smarty->assign('status', 'failed'); } - return $this->display(__FILE__, 'payment_return.tpl'); - } - - /** - * Check if currency is valid for the module - * @param mixed $cart - * @return boolean - * @since 2.0.0 - */ - public function checkCurrency($cart) - { - //Compropago just accept Mexican Peso as currency: MXN iso 484 - $currency_order = new Currency((int)($cart->id_currency)); - - $codes = ["MXN", "USD", "EUR", "GBP"]; + return $this->display(__FILE__, 'payment_return.tpl'); + } - //Habilitar las monedas soportadas - if (in_array($currency_order->iso_code, $codes)) { - return true; - } + /** + * Check if currency is valid for the module + * @param mixed $cart + * @return boolean + * @since 2.0.0 + */ + public function checkCurrency($cart) + { + //Compropago just accept Mexican Peso as currency: MXN iso 484 + $currency_order = new Currency((int)($cart->id_currency)); - return false; - } + //Habilitar las monedas soportadas + return in_array( + $currency_order->iso_code, + ["MXN", "USD", "EUR", "GBP"] + ); + } /** * Config form for Admin configuration page * @return prestashop form helper * @since 2.0.0 */ - public function renderForm() - { - try { - if(!$this->publicKey && !$this->privateKey){ - $this->client = new CompropagoSdk\Client( - $this->publicKey, - $this->privateKey, - $this->modoExec - ); - $providers = $this->client->api->listDefaultProviders(); - }else{ - $providers = $this->client->api->listProviders(); - } - - if (Configuration::get('COMPROPAGO_SUCCESS') == false - || Configuration::get('COMPROPAGO_PENDING') == false - || Configuration::get('COMPROPAGO_EXPIRED') == false) - { - $this->installOrderStates(); - } - - $options = []; - $flag = false; - foreach ($providers as $provider){ - $options[] = [ - 'id_option' => $provider->internal_name, - 'name' => $provider->name - ]; - } - global $smarty; - $base_url = ( isset( $smarty->tpl_vars['base_dir']->value ) ) ? $smarty->tpl_vars['base_dir']->value : __DIR__; - - $fields_form = array( - 'form' => array( - 'legend' => array( - 'title' => $this->l('Configuración'), - 'image' => '../modules/compropago/icon.png' - ), - 'input' => array( - array( - 'type' => 'text', - 'label' => $this->l('Public Key'), - 'name' => 'COMPROPAGO_PUBLICKEY', - 'required' => true - ), - array( - 'type' => 'text', - 'label' => $this->l('Private Key'), - 'desc' => $this->l('Get your keys at ComproPago').': '.$this->l('ComproPago Panel').'', - 'name' => 'COMPROPAGO_PRIVATEKEY', - 'required' => true - ), - array( - 'type' => 'hidden', - 'name' => 'COMPROPAGO_WEBHOOK', - 'required' => false - ), - array( - 'type' => 'switch', - 'label' => $this->l('Live Mode'), - 'desc' => $this->l('Are you on live or testing?,Change your Keys according to the mode').':'.$this->l('ComproPago Panel').'', - 'name' => 'COMPROPAGO_MODE', - 'is_bool' => true, - 'required' => true, - 'values' => array( - array( - 'id' => 'active_on_bv', - 'value' => true, - 'label' => $this->l('Live Mode') - ), - array( - 'id' => 'active_off_bv', - 'value' => false, - 'label' => $this->l('Testing Mode') - ) - ) - ), - array( - 'type' => 'swap', - 'multiple' => true, - 'label' => $this->l('Tiendas:'), - 'desc' => $this->l('Seleccione las tiendas'), - 'name' => 'COMPROPAGO_PROVIDERS', - 'options' => array( - 'query' => $options, // $options contains the data itself. - 'id' => 'id_option', // The value of the 'id' key must be the same as the key for 'value' attribute of the