868 lines
37 KiB
PHP
868 lines
37 KiB
PHP
<?php
|
|
/**
|
|
* 2007-2017 SARL DECANET
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Academic Free License (AFL 3.0)
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://opensource.org/licenses/afl-3.0.php
|
|
* If you did not receive a copy of the license and are unable to
|
|
* obtain it through the world-wide-web, please send an email
|
|
* to license@decanet.fr so we can send you a copy immediately.
|
|
*
|
|
* DISCLAIMER
|
|
*
|
|
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
|
* versions in the future. If you wish to customize PrestaShop for your
|
|
* needs please refer to http://www.decanet.fr for more information.
|
|
*
|
|
* @author SARL DECANET <contact@decanet.fr>
|
|
* @copyright 2007-2017 SARL DECANET
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* International Registered Trademark & Property of PrestaShop SA
|
|
*/
|
|
|
|
if (!defined('_PS_VERSION_')) {
|
|
exit;
|
|
}
|
|
|
|
class PrelevementSEPA extends PaymentModule
|
|
{
|
|
public $conf_keys = array(
|
|
'ICS' => '',
|
|
'creancier' => '',
|
|
'IBAN' => '',
|
|
'BIC' => '',
|
|
'adresse' => '',
|
|
'label' => 'Payment for order [reference]',
|
|
'decanetlogin'=>'',
|
|
'decanetapikey'=>'',
|
|
'ENTETE' => '',
|
|
'FOOTER' => '',
|
|
'state' => '',
|
|
'state_sepa' => '',
|
|
'delay' => 2,
|
|
'delay_working' => 2,
|
|
'delay_groups' => array(),
|
|
'sendsepaemail' => 0,
|
|
'sepaemail' => '',
|
|
'sendsepaftp' => 0,
|
|
'ftp_host' => '',
|
|
'ftp_login' => '',
|
|
'ftp_password' => '',
|
|
'ftp_port' => 21,
|
|
'ftp_sslcon' => 0,
|
|
'ftp_dirpath' => '/',
|
|
'template' => 'mandate',
|
|
'token' => '',
|
|
'format' => 'ct',
|
|
'CBI' => ''
|
|
);
|
|
|
|
public $sms_solde = 0;
|
|
private $extensions = array('.txt', '.rtf', '.doc', '.docx', '.pdf', '.zip', '.png', '.jpeg', '.gif', '.jpg');
|
|
|
|
public function __construct()
|
|
{
|
|
$this->name = 'prelevementsepa';
|
|
$this->tab = 'payments_gateways';
|
|
$this->version = '2.5.0';
|
|
$this->author = 'Decanet';
|
|
$this->controllers = array('payment', 'validation');
|
|
$this->need_instance = 0;
|
|
$this->module_key = "620b373613a75fbc78efa15eabba2308";
|
|
$this->author_address = '0x45673a4Ef62C73f7167eb0dA4270fb614144FC5E';
|
|
$this->bootstrap = true;
|
|
$this->html = '';
|
|
|
|
parent::__construct();
|
|
if (!function_exists('curl_init')) {
|
|
$this->warning = $this->l('You need to have the PHP CURL extension enabled on your server.'.
|
|
'Please contact your web host. If CURL is not available on your server,'.
|
|
'you will not be able to use this module.');
|
|
return;
|
|
}
|
|
if (!function_exists('dom_import_simplexml')) {
|
|
$this->warning = $this->l('You need to have the PHP XML extension enabled on your server.'.
|
|
'Please contact your web host. If XML is not available on your server, '.
|
|
'you will not be able to use this module.');
|
|
return;
|
|
}
|
|
|
|
$this->displayName = $this->l('SEPA Direct Debit');
|
|
$this->description = $this->l('Propose SEPA Direct Debit as a payment method');
|
|
|
|
$this->confirmUninstall = $this->l('you really want to uninstall');
|
|
|
|
$id_shop_group = Context::getContext()->shop->id_shop_group;
|
|
$id_shop = Context::getContext()->shop->id;
|
|
|
|
if (Configuration::get('PRELSEPA_CONF', false, $id_shop_group, $id_shop)) {
|
|
foreach (unserialize(Configuration::get('PRELSEPA_CONF', false, $id_shop_group, $id_shop)) as $k => $v) {
|
|
$this->conf_keys[$k] = $v;
|
|
}
|
|
}
|
|
|
|
if (empty($this->conf_keys['delay_groups'])) {
|
|
foreach (Group::getGroups($this->context->language->id) as $group) {
|
|
$this->conf_keys['delay_groups'][$group['id_group']] = $this->conf_keys['delay'];
|
|
}
|
|
}
|
|
if (!isset($this->conf_keys['ICS']) || empty($this->conf_keys['ICS']) ||
|
|
!isset($this->conf_keys['creancier']) || empty($this->conf_keys['creancier'])) {
|
|
$this->warning = $this->l('You must configure the module SEPA Direct Debit before'.
|
|
'proposing this method of payment.');
|
|
}
|
|
|
|
if (!isset($this->conf_keys['token']) || empty($this->conf_keys['token'])) {
|
|
$this->conf_keys['token'] = Tools::encrypt('prelevementsepa');
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop);
|
|
}
|
|
}
|
|
|
|
public function install()
|
|
{
|
|
$id_shop_group = Context::getContext()->shop->id_shop_group;
|
|
$id_shop = Context::getContext()->shop->id;
|
|
if (empty($this->conf_keys['state'])) {
|
|
$os = new OrderState();
|
|
$os->logable = 1;
|
|
$os->color = '#ffffff';
|
|
foreach (Language::getLanguages() as $language) {
|
|
if (Tools::strtolower($language['iso_code']) == 'fr') {
|
|
$os->name[$language['id_lang']] = 'En attente de prélèvement SEPA';
|
|
} else {
|
|
$os->name[$language['id_lang']] = 'Waiting for SEPA Direct Debit';
|
|
}
|
|
}
|
|
$os->add();
|
|
$this->conf_keys['state'] = $os->id;
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop);
|
|
}
|
|
return (parent::install() && $this->installDb() &&
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop) &&
|
|
$this->installModuleTab('AdminSepaOrders', Tab::getIdFromClassName('AdminParentOrders')) &&
|
|
$this->installModuleTab('AdminSepaMandate', -1) &&
|
|
$this->registerHook('payment') &&
|
|
$this->registerHook('customerAccount') &&
|
|
$this->registerHook('actionValidateOrder') &&
|
|
$this->registerHook('paymentOptions') &&
|
|
$this->registerHook('paymentReturn'));
|
|
}
|
|
|
|
public function uninstall()
|
|
{
|
|
return (parent::uninstall() &&
|
|
Configuration::deleteByName('PRELSEPA_CONF') &&
|
|
$this->uninstallModuleTab('AdminSepaOrders'));
|
|
}
|
|
|
|
private function installDb()
|
|
{
|
|
$sql = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'prelevementsepa` (
|
|
`id_sepa` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`id_customer` int(10) unsigned NOT NULL,
|
|
`name` varchar(128) NOT NULL,
|
|
`address` varchar(255) NOT NULL,
|
|
`etat` tinyint(1) unsigned NOT NULL DEFAULT 0,
|
|
`rum` varchar(34) NOT NULL,
|
|
`iban` varchar(50) NOT NULL,
|
|
`bic` varchar(11) NOT NULL,
|
|
`sepa_type` ENUM("FRST","RCUR") NOT NULL DEFAULT "RCUR",
|
|
`phone` varchar(25) NOT NULL,
|
|
`code` varchar(11) NOT NULL,
|
|
`datevalid` varchar(20) NOT NULL,
|
|
PRIMARY KEY (`id_sepa`),
|
|
KEY `id_customer` (`id_customer`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
|
|
|
|
$sql2 = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'prelevementsepa_detail` (
|
|
`id_sepa_detail` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`id_sepa` int(10) unsigned NOT NULL,
|
|
`id_order` int(10) unsigned NOT NULL,
|
|
`sepa_date` DATE NOT NULL,
|
|
`traite` tinyint(1) NOT NULL,
|
|
PRIMARY KEY (`id_sepa_detail`),
|
|
KEY `id_sepa` (`id_sepa`,`id_order`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
|
|
|
|
$sql3 = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'prelevementsepa_traited` (
|
|
`id_sepa_traited` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
|
`date_add` datetime NOT NULL,
|
|
`date_upd` datetime NOT NULL,
|
|
`order_state` int(11) NOT NULL,
|
|
PRIMARY KEY (`id_sepa_traited`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
|
|
|
|
$sql4 = 'CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'prelevementsepa_traited_order` (
|
|
`id_sepa_traited` int(11) NOT NULL,
|
|
`id_order` int(11) NOT NULL,
|
|
PRIMARY KEY (`id_sepa_traited`, `id_order`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=latin1';
|
|
if (Db::getInstance()->Execute($sql) && Db::getInstance()->Execute($sql2) && Db::getInstance()->Execute($sql3) && Db::getInstance()->Execute($sql4)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private function repairDb()
|
|
{
|
|
require_once(dirname(__FILE__).'/upgrade/upgrade-'.$this->version.'.php');
|
|
$function_name = "upgrade_module_".str_replace('.', '_', $this->version);
|
|
$function_name($this);
|
|
die('Repair is OK');
|
|
}
|
|
|
|
private function postProcess()
|
|
{
|
|
$id_shop_group = Context::getContext()->shop->id_shop_group;
|
|
$id_shop = Context::getContext()->shop->id;
|
|
if (Tools::getIsset('repair')) {
|
|
$this->repairDb();
|
|
}
|
|
if (Tools::getIsset('viewconfiguration') && Tools::getIsset('id_sepa')) {
|
|
Tools::redirectAdmin(Context::getContext()->link->getAdminLink('AdminSepaMandate').
|
|
'&id_sepa='.(int)Tools::getValue('id_sepa'));
|
|
} elseif (Tools::isSubmit('sepa_iban')) {
|
|
require_once(dirname(__FILE__).'/classes/PrevSepa.php');
|
|
$sepa_obj = new PrevSepa((int)Tools::getValue('update_sepa'));
|
|
$sepa_obj->id_customer = (int)Tools::getValue('id_customer');
|
|
$sepa_obj->name = Tools::getValue('sepa_name');
|
|
$sepa_obj->address = Tools::getValue('sepa_address');
|
|
$sepa_obj->etat = Tools::getValue('sepa_state');
|
|
$sepa_obj->rum = Tools::getValue('sepa_rum');
|
|
$sepa_obj->iban = Tools::getValue('sepa_iban');
|
|
$sepa_obj->bic = Tools::getValue('sepa_bic');
|
|
$sepa_obj->sepa_type = Tools::getValue('sepa_type');
|
|
$sepa_obj->datevalid = Tools::getValue('datevalid');
|
|
$sepa_obj->save();
|
|
|
|
if (!is_dir(_PS_UPLOAD_DIR_.'sepa/')) {
|
|
@mkdir(_PS_UPLOAD_DIR_.'sepa/', 0777);
|
|
@copy(dirname(__FILE__).'/index.php', _PS_UPLOAD_DIR_.'sepa/index.php');
|
|
}
|
|
|
|
if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
|
|
if (in_array(Tools::substr($_FILES['file']['name'], -4), $this->extensions)) {
|
|
if ($_FILES['file']['size'] > (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024)) {
|
|
$this->errors[] = sprintf(
|
|
$this->l('The file is too large. Maximum size allowed is: %1$d kB.'
|
|
.'The file you are trying to upload is %2$d kB.'),
|
|
(Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024),
|
|
number_format(($_FILES['file']['size'] / 1024), 2, '.', '')
|
|
);
|
|
} else {
|
|
if (!is_dir(_PS_UPLOAD_DIR_.'sepa/')) {
|
|
@mkdir(_PS_UPLOAD_DIR_.'sepa/');
|
|
}
|
|
$_FILES['file']['name'] = sha1(Tools::getValue('sepa_rum').Tools::getValue('id_customer')).
|
|
Tools::substr($_FILES['file']['name'], -4);
|
|
if (file_exists(_PS_UPLOAD_DIR_.'sepa/'.$_FILES['file']['name'])) {
|
|
@unlink(_PS_UPLOAD_DIR_.'sepa/'.$_FILES['file']['name']);
|
|
}
|
|
if (!move_uploaded_file($_FILES['file']['tmp_name'], _PS_UPLOAD_DIR_.
|
|
'sepa/'.$_FILES['file']['name'])) {
|
|
$this->errors[] = $this->l('Failed to copy the file.');
|
|
}
|
|
$_POST['file_name'] = $_FILES['file']['name'];
|
|
@unlink($_FILES['file']['tmp_name']);
|
|
}
|
|
} else {
|
|
$this->errors[] = $this->l('The only authorized extensions are:').' '.
|
|
implode(', ', $this->extensions);
|
|
}
|
|
} elseif (array_key_exists('file', $_FILES) && (int)$_FILES['file']['error'] === 1) {
|
|
$max_upload = (int)ini_get('upload_max_filesize');
|
|
$max_post = (int)ini_get('post_max_size');
|
|
$upload_mb = min($max_upload, $max_post);
|
|
$this->errors[] = sprintf(
|
|
$this->l('The file %1$s exceeds the size allowed by the server. The limit is set to %2$d MB.'),
|
|
'<b>'.$_FILES['file']['name'].'</b> ',
|
|
'<b>'.$upload_mb.'</b>'
|
|
);
|
|
}
|
|
$return = array();
|
|
$return['step_name'] = 'Mandates';
|
|
$return['code'] = 1;
|
|
$return['error'] = 0;
|
|
return $return;
|
|
} elseif (Tools::isSubmit('prelevementsepa_ICS')) {
|
|
$return = array();
|
|
$return['step_name'] = 'Configurations';
|
|
$return['code'] = 1;
|
|
$return['error'] = 0;
|
|
$this->conf_keys['ICS'] = Tools::getValue('prelevementsepa_ICS');
|
|
$this->conf_keys['creancier'] = Tools::getValue('prelevementsepa_creancier');
|
|
$this->conf_keys['adresse'] = Tools::getValue('prelevementsepa_adresse');
|
|
$this->conf_keys['IBAN'] = Tools::getValue('prelevementsepa_IBAN');
|
|
$this->conf_keys['BIC'] = Tools::getValue('prelevementsepa_BIC');
|
|
$this->conf_keys['label'] = Tools::getValue('prelevementsepa_label');
|
|
$this->conf_keys['state'] = Tools::getValue('prelevementsepa_state');
|
|
$this->conf_keys['state_sepa'] = Tools::getValue('prelevementsepa_state_sepa');
|
|
$this->conf_keys['ENTETE'] = Tools::getValue('prelevementsepa_ENTETE');
|
|
$this->conf_keys['FOOTER'] = Tools::getValue('prelevementsepa_FOOTER');
|
|
$this->conf_keys['format'] = Tools::getValue('prelevementsepa_format');
|
|
$this->conf_keys['delay_working'] = Tools::getValue('prelevementsepa_delay_working');
|
|
$this->conf_keys['delay_groups'] = Tools::getValue('prelevementsepa_delay_groups');
|
|
$this->conf_keys['template'] = Tools::getValue('prelevementsepa_template');
|
|
$this->conf_keys['CBI'] = Tools::getValue('prelevementsepa_CBI');
|
|
// die(var_dump($this->conf_keys));
|
|
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop);
|
|
return $return;
|
|
} elseif (Tools::isSubmit('prelevementsepa_decanetlogin')) {
|
|
$return = array();
|
|
$return['step_name'] = 'SMS configuration';
|
|
$return['code'] = 1;
|
|
$return['error'] = 0;
|
|
$this->conf_keys['decanetlogin'] = Tools::getValue('prelevementsepa_decanetlogin');
|
|
$this->conf_keys['decanetapikey'] = Tools::getValue('prelevementsepa_decanetapikey');
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop);
|
|
return $return;
|
|
} elseif (Tools::isSubmit('submitLogout')) {
|
|
$return = array();
|
|
$return['step_name'] = 'SMS configuration';
|
|
$return['code'] = 1;
|
|
$this->conf_keys['decanetlogin'] = '';
|
|
$this->conf_keys['decanetapikey'] = '';
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop);
|
|
return $return;
|
|
} elseif (Tools::isSubmit('prelevementsepa_ftp_host')) {
|
|
$this->conf_keys['sendsepaemail'] = Tools::getValue('prelevementsepa_sendsepaemail', 0);
|
|
$this->conf_keys['sepaemail'] = Tools::getValue('prelevementsepa_sepaemail');
|
|
$this->conf_keys['sendsepaftp'] = Tools::getValue('prelevementsepa_sendsepaftp', 0);
|
|
$this->conf_keys['ftp_host'] = Tools::getValue('prelevementsepa_ftp_host');
|
|
$this->conf_keys['ftp_login'] = Tools::getValue('prelevementsepa_ftp_login');
|
|
$this->conf_keys['ftp_password'] = Tools::getValue('prelevementsepa_ftp_password');
|
|
$this->conf_keys['ftp_port'] = Tools::getValue('prelevementsepa_ftp_port');
|
|
$this->conf_keys['ftp_sslcon'] = Tools::getValue('prelevementsepa_ftp_sslcon', 0);
|
|
$this->conf_keys['ftp_dirpath'] = Tools::getValue('prelevementsepa_ftp_dirpath', '/');
|
|
Configuration::updateValue('PRELSEPA_CONF', serialize($this->conf_keys), false, $id_shop_group, $id_shop);
|
|
}
|
|
}
|
|
|
|
public function getContent()
|
|
{
|
|
$return_post_process = $this->postProcess();
|
|
|
|
$this->context->controller->addJqueryPlugin('validate');
|
|
$this->context->controller->addJS(_PS_JS_DIR_.'jquery/plugins/validate/localization/messages_'.$this->context->language->iso_code.'.js');
|
|
$this->context->controller->addJS($this->_path.'views/js/sepa_bo.js');
|
|
$states = new OrderState();
|
|
|
|
$files = glob(_PS_MODULE_DIR_.$this->name."/views/templates/front/pdf/*.tpl");
|
|
|
|
foreach ($files as &$value) {
|
|
$value = str_replace(array(_PS_MODULE_DIR_.$this->name."/views/templates/front/pdf/", '.tpl'), array('', ''), $value);
|
|
}
|
|
|
|
if (file_exists(_PS_THEME_DIR_.'modules/'.$this->name.'/views/templates/front/pdf')) {
|
|
$themes_files = glob(_PS_THEME_DIR_.'modules/'.$this->name.'/views/templates/front/pdf/*.tpl');
|
|
foreach ($themes_files as &$value) {
|
|
$value = str_replace(array(_PS_THEME_DIR_.'modules/'.$this->name.'/views/templates/front/pdf/', '.tpl'), array('', ''), $value);//.' ('.$current_theme['theme_name'].')';
|
|
}
|
|
|
|
foreach ($themes_files as $tf) {
|
|
if (!in_array($tf, $files)) {
|
|
$files[] = $tf;
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->context->smarty->assign(array(
|
|
"files_templates" => $files,
|
|
'connect' => $this->isConnected(),
|
|
'sms_solde' => $this->sms_solde,
|
|
'form_uri' => $_SERVER['REQUEST_URI'],
|
|
'module_active' => (int)$this->active,
|
|
'error_con' => (int)$return_post_process['error'],
|
|
'step_name' => pSQL($return_post_process['step_name']),
|
|
'is_submit'=> (int)$return_post_process['code'],
|
|
'html'=> $this->html,
|
|
'module_name' => $this->name,
|
|
'module_version' => $this->version,
|
|
'module_display' => $this->displayName,
|
|
'module_tab' => Tools::getValue('module_tab', 'configuration'),
|
|
'debug_mode' => (int)_PS_MODE_DEV_,
|
|
'multishop' => (int)Shop::isFeatureActive(),
|
|
'shops' => ShopCore::getShops(),
|
|
'ps_version' => (bool)version_compare(_PS_VERSION_, '1.6', '>'),
|
|
'guide_link' => 'docs/cloudflare_documentation_fr.pdf',
|
|
'url' => _PS_BASE_URL_.__PS_BASE_URI__,
|
|
'modToken' => Tools::getAdminTokenLite('AdminModules'),
|
|
'localLink' => $this->context->link->getAdminLink('AdminModules', false).
|
|
'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name,
|
|
'sepa_conf' => $this->conf_keys,
|
|
'sepa_list' => $this->getSepaList(),
|
|
'states' => $states->getOrderStates($this->context->language->id),
|
|
'contextshopid' => $this->context->shop->id,
|
|
'users_groups' => Group::getGroups($this->context->language->id),
|
|
'cron_file' => _PS_MODULE_DIR_.$this->name."/cron_generate_sepa.php",
|
|
'sslconnectexist' => function_exists('ftp_ssl_connect')
|
|
));
|
|
if ((int)$return_post_process['error']>0) {
|
|
$this->context->smarty->assign('error_con', (int)$return_post_process['error']);
|
|
}
|
|
if ((int)$this->conf_keys['sendsepaftp']) {
|
|
$this->checkFtpAccess();
|
|
}
|
|
return $this->display(__FILE__, 'views/templates/admin/configuration.tpl');
|
|
}
|
|
|
|
public function isConnected()
|
|
{
|
|
if (empty($this->conf_keys['decanetlogin'])) {
|
|
return false;
|
|
}
|
|
include_once(dirname(__FILE__).'/classes/SMSDecanet.php');
|
|
$api_obj = new SMSDecanet();
|
|
$solde = $api_obj->getCredit(array('LOGIN'=>$this->conf_keys['decanetlogin'],
|
|
'API_KEY'=>$this->conf_keys['decanetapikey'], 'TEST' => 1));
|
|
if (isset($solde->code) && $solde->code==1 && isset($solde->credit)) {
|
|
return false;
|
|
} else {
|
|
$this->sms_solde = (int)$solde->credit;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private function getSepaList()
|
|
{
|
|
require_once(dirname(__FILE__).'/classes/PrevSepa.php');
|
|
$fields_list = array(
|
|
'id_sepa' => array(
|
|
'title' => $this->l('Id'),
|
|
'width' => 140,
|
|
'type' => 'text',
|
|
'orderby' => false,
|
|
'search' => false,
|
|
),
|
|
'customer' => array(
|
|
'title' => $this->l('Customer'),
|
|
'width' => 140,
|
|
'type' => 'text',
|
|
'orderby' => false,
|
|
),
|
|
'rum' => array(
|
|
'title' => $this->l('RUM'),
|
|
'width' => 140,
|
|
'type' => 'text',
|
|
'orderby' => false,
|
|
),
|
|
'state' => array(
|
|
'title' => $this->l('State'),
|
|
'active' => 'status',
|
|
'type' => 'bool',
|
|
'class' => 'fixed-width-xs',
|
|
'align' => 'center',
|
|
'ajax' => true,
|
|
'orderby' => false
|
|
)
|
|
);
|
|
$helper = new HelperList();
|
|
|
|
$helper->shopLinkType = '';
|
|
|
|
$helper->simple_header = false;
|
|
$helper->list_id = $list_id = 'sepamandates';
|
|
|
|
// Actions to be displayed in the "Actions" column
|
|
$helper->actions = array('view');
|
|
|
|
$helper->identifier = 'id_sepa';
|
|
$helper->show_toolbar = true;
|
|
$helper->title = $this->l('SEPA Mandates');
|
|
$helper->toolbar_btn['new'] = array(
|
|
'href' => Context::getContext()->link->getAdminLink('AdminSepaMandate'),
|
|
'desc' => $this->l('Add new mandate', null, null, false),
|
|
'icon' => 'process-icon-new'
|
|
);
|
|
$helper->token = Tools::getAdminTokenLite('AdminModules');
|
|
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name.'&module_tab=mandates';
|
|
$sepa = PrevSepa::getAll();
|
|
$sepa_list = array();
|
|
foreach ($sepa as $s) {
|
|
$s['customer'] = new Customer((int)$s['id_customer']);
|
|
$s['file_exists'] = false;
|
|
foreach ($this->extensions as $e) {
|
|
if (file_exists(_PS_UPLOAD_DIR_.'sepa/'.sha1($s['rum'].$s['id_customer']).$e)) {
|
|
$s['file_exists'] = sha1($s['rum'].$s['id_customer']).$e;
|
|
break;
|
|
}
|
|
}
|
|
// $sepa_list[] = array(
|
|
// 'id_sepa' => $s['id_sepa'],
|
|
// 'customer' => ((Tools::strlen($s['customer']->company)>0)?$s['customer']->company.' ':'').
|
|
// $s['customer']->firstname.' '.$s['customer']->lastname,
|
|
// 'rum' => $s['rum'],
|
|
// 'state' => $s['etat']
|
|
// );
|
|
$to_add = 1;
|
|
$customer_name = ((Tools::strlen($s['customer']->company)>0)?$s['customer']->company.' ':'').$s['customer']->firstname.' '.$s['customer']->lastname;
|
|
|
|
if (!Tools::getIsset('submitResetsepamandates')) {
|
|
if (Tools::getIsset('sepamandatesFilter_customer') &&
|
|
Tools::getValue('sepamandatesFilter_customer')!=''
|
|
) {
|
|
if (stripos($customer_name, Tools::getValue('sepamandatesFilter_customer')) === false) {
|
|
$to_add = 0;
|
|
}
|
|
}
|
|
if (Tools::getIsset('sepamandatesFilter_rum') && Tools::getValue('sepamandatesFilter_rum')!='') {
|
|
if (stripos($s['rum'], Tools::getValue('sepamandatesFilter_rum')) === false) {
|
|
$to_add = 0;
|
|
}
|
|
}
|
|
if (Tools::getIsset('sepamandatesFilter_state') && Tools::getValue('sepamandatesFilter_state')!='') {
|
|
if ($s['etat'] != Tools::getValue('sepamandatesFilter_state')) {
|
|
$to_add = 0;
|
|
}
|
|
}
|
|
} else {
|
|
$prefix = $this->getCookieOrderByPrefix();
|
|
$filters = $this->context->cookie->getFamily($prefix.$list_id.'Filter_');
|
|
foreach ($filters as $cookie_key => $filter) {
|
|
if (strncmp($cookie_key, $prefix.$list_id.'Filter_', 7 + Tools::strlen($prefix.$list_id)) == 0) {
|
|
$key = Tools::substr($cookie_key, 7 + Tools::strlen($prefix.$list_id));
|
|
if (is_array($fields_list) && array_key_exists($key, $fields_list)) {
|
|
$this->context->cookie->$cookie_key = null;
|
|
}
|
|
unset($this->context->cookie->$cookie_key);
|
|
}
|
|
}
|
|
|
|
if (isset($this->context->cookie->{'submitFilter'.$list_id})) {
|
|
unset($this->context->cookie->{'submitFilter'.$list_id});
|
|
}
|
|
if (isset($this->context->cookie->{$prefix.$list_id.'Orderby'})) {
|
|
unset($this->context->cookie->{$prefix.$list_id.'Orderby'});
|
|
}
|
|
if (isset($this->context->cookie->{$prefix.$list_id.'Orderway'})) {
|
|
unset($this->context->cookie->{$prefix.$list_id.'Orderway'});
|
|
}
|
|
|
|
$_POST = array();
|
|
// $helper->filter = false;
|
|
// unset($helper->_filterHaving);
|
|
// unset($helper->_having);
|
|
}
|
|
|
|
if ($to_add == 1) {
|
|
$sepa_list[] = array(
|
|
'id_sepa' => $s['id_sepa'],
|
|
'customer' => $customer_name,
|
|
'rum' => $s['rum'],
|
|
'state' => $s['etat']
|
|
);
|
|
}
|
|
}
|
|
//print_r($sepa_list);die();
|
|
return $helper->generateList($sepa_list, $fields_list);
|
|
}
|
|
|
|
public function ajaxProcessStatusConfiguration()
|
|
{
|
|
require_once(dirname(__FILE__).'/classes/PrevSepa.php');
|
|
if (!$id_sepa = (int)Tools::getValue('id_sepa')) {
|
|
die(Tools::jsonEncode(array('success' => false, 'error' => true, 'text' => $this->l('Failed to update the status'))));
|
|
} else {
|
|
$sepa = new PrevSepa((int)$id_sepa);
|
|
if (Validate::isLoadedObject($sepa)) {
|
|
$sepa->etat = $sepa->etat == 1 ? 0 : 1;
|
|
if ($sepa->save()) {
|
|
if ($sepa->etat == 1) {
|
|
$cust_orders = Order::getCustomerOrders($sepa->id_customer);
|
|
$listods = array();
|
|
if ($cust_orders) {
|
|
foreach ($cust_orders as $o) {
|
|
$listods[] = $o['id_order'];
|
|
}
|
|
PrevSepa::updateUnTraitSepaIDByOids($sepa->id, $listods);
|
|
}
|
|
}
|
|
|
|
die(Tools::jsonEncode(array(
|
|
'success' => true,
|
|
'text' => $this->l('The status has been updated successfully'))));
|
|
} else {
|
|
die(Tools::jsonEncode(array(
|
|
'success' => false,
|
|
'error' => true,
|
|
'text' => $this->l('Failed to update the status'))));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function ajaxProcessCheckIban()
|
|
{
|
|
require(dirname(__FILE__).'/libs/oophp-iban.php');
|
|
$iban = str_replace(' ', '', Tools::getValue('check'));
|
|
$ibanObj = new IBAN($iban);
|
|
$res=$ibanObj->Verify()?'true':'false';
|
|
die(Tools::jsonEncode(array('result' => $res)));
|
|
}
|
|
|
|
public function ajaxProcessCheckAddress()
|
|
{
|
|
$address = new AddressCore(Address::getFirstCustomerAddressId(Tools::getValue('cid')));
|
|
echo $address->address1.' '.$address->postcode.', '.$address->city.' '.$address->country;
|
|
die();
|
|
}
|
|
|
|
public function ajaxProcessCheckBic()
|
|
{
|
|
$res = preg_match(
|
|
'#^([a-zA-Z]){4}([a-zA-Z]){2}([0-9a-zA-Z]){2}([0-9a-zA-Z]{3})?$#',
|
|
Tools::getValue('check')
|
|
)?'true':'false';
|
|
die(Tools::jsonEncode(array('result' => $res)));
|
|
}
|
|
|
|
public function installModuleTab($tabClass = null, $idTabParent = 0)
|
|
{
|
|
$pass = true;
|
|
if (!Tab::getIdFromClassName($tabClass)) {
|
|
$tabNameLang = array();
|
|
|
|
if (Tab::getIdFromClassName($tabClass)) {
|
|
return true;
|
|
}
|
|
|
|
@copy(_PS_MODULE_DIR_.$this->name.'/images/a16.gif', _PS_IMG_DIR_.'t/'.$tabClass.'.gif');
|
|
|
|
foreach (Language::getLanguages() as $language) {
|
|
if (Tools::strtolower($language['iso_code']) == 'fr') {
|
|
$tabNameLang[$language['id_lang']] = 'Prélèvement SEPA';
|
|
} else {
|
|
$tabNameLang[$language['id_lang']] = 'Prelevement SEPA';
|
|
}
|
|
}
|
|
$tab = new Tab();
|
|
$tab->name = $tabNameLang;
|
|
$tab->class_name = $tabClass;
|
|
$tab->module = $this->name;
|
|
$tab->id_parent = (int)$idTabParent;
|
|
|
|
$pass = $tab->save();
|
|
}
|
|
return $pass;
|
|
}
|
|
|
|
public function uninstallModuleTab($tabClass)
|
|
{
|
|
$pass = true;
|
|
@unlink(_PS_IMG_DIR_.'t/'.$tabClass.'.gif');
|
|
$idTab = Tab::getIdFromClassName($tabClass);
|
|
if ($idTab != 0) {
|
|
$tab = new Tab($idTab);
|
|
$pass = $tab->delete();
|
|
}
|
|
return $pass;
|
|
}
|
|
|
|
public function hookActionValidateOrder($params)
|
|
{
|
|
if ($params['order']->module==$this->name) {
|
|
$this->generateSEPA($params['order']->id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public function hookPayment($params)
|
|
{
|
|
return $this->display(__FILE__, '/views/templates/front/payment.tpl');
|
|
}
|
|
|
|
public function hookPaymentOptions($params)
|
|
{
|
|
if (!$this->active || version_compare(phpversion(), '5.3.0', '<')) {
|
|
return;
|
|
}
|
|
|
|
$this->smarty->assign(
|
|
$this->getTemplateVars()
|
|
);
|
|
|
|
$newOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
|
|
$newOption->setModuleName($this->name)
|
|
->setCallToActionText($this->l('Pay by SEPA Direct Debit'))
|
|
->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true))
|
|
->setAdditionalInformation($this->fetch('module:prelevementsepa/views/templates/front/1.7/payment_sepa_infos.tpl'));
|
|
|
|
return array($newOption);
|
|
}
|
|
|
|
public function hookPaymentReturn($params)
|
|
{
|
|
if (!$this->active) {
|
|
return;
|
|
}
|
|
$order_key = 'order';
|
|
if (version_compare(_PS_VERSION_, '1.7', '<')) {
|
|
$order_key = 'objOrder';
|
|
}
|
|
require_once(dirname(__FILE__).'/classes/PrevSepa.php');
|
|
require_once(dirname(__FILE__).'/classes/PrevSepaOrder.php');
|
|
|
|
$state = $params[$order_key]->getCurrentState();
|
|
if ($state == $this->conf_keys['state']) {
|
|
$this->smarty->assign(array(
|
|
'total_to_pay' => Tools::displayPrice(
|
|
$params[$order_key]->getOrdersTotalPaid(),
|
|
new Currency($params[$order_key]->id_currency),
|
|
false
|
|
),
|
|
'shop_name' => $this->context->shop->name,
|
|
'status' => 'ok',
|
|
'id_order' => $params[$order_key]->id
|
|
));
|
|
if (isset($params[$order_key]->reference) && !empty($params[$order_key]->reference)) {
|
|
$this->smarty->assign('reference', $params[$order_key]->reference);
|
|
}
|
|
$prevOrderObj = new PrevSepaOrder();
|
|
$id_sepa = $prevOrderObj->existOrder($params[$order_key]->id);
|
|
$prevOrderObj = new PrevSepaOrder($id_sepa);
|
|
$prev_obj = new PrevSepa($prevOrderObj->id_sepa);
|
|
$this->smarty->assign('sepa', $prev_obj);
|
|
} else {
|
|
$this->smarty->assign('status', 'failed');
|
|
}
|
|
if (version_compare(_PS_VERSION_, '1.7', '<')) {
|
|
return $this->display(__FILE__, 'payment_return.tpl');
|
|
} else {
|
|
return $this->display(__FILE__, '/views/templates/front/1.7/payment_return.tpl');
|
|
}
|
|
}
|
|
|
|
public function hookCustomerAccount($params)
|
|
{
|
|
if (version_compare(_PS_VERSION_, '1.7', '<')) {
|
|
return $this->display(__FILE__, '/views/templates/front/1.6/my-account.tpl');
|
|
} else {
|
|
return $this->display(__FILE__, '/views/templates/front/1.7/my-account.tpl');
|
|
}
|
|
}
|
|
|
|
public function generateSEPA($id_order)
|
|
{
|
|
require_once(dirname(__FILE__).'/classes/PrevSepa.php');
|
|
require_once(dirname(__FILE__).'/classes/PrevSepaOrder.php');
|
|
|
|
if (!PrevSepaOrder::existOrder($id_order)) {
|
|
$order = new Order($id_order);
|
|
$prev_obj = new PrevSepa();
|
|
$id_sepa = $prev_obj->existCustomer((int)$order->id_customer);
|
|
$cd_gid = Customer::getDefaultGroupId((int)$order->id_customer);
|
|
$prevOrderObj = new PrevSepaOrder();
|
|
$prevOrderObj->id_sepa = (int)$id_sepa;
|
|
$prevOrderObj->id_order = (int)$id_order;
|
|
if (!isset($this->conf_keys['delay_groups'][$cd_gid])) {
|
|
$this->conf_keys['delay_groups'][$cd_gid] = 2;
|
|
}
|
|
// if (isset($this->conf_keys['delay_groups'][$cd_gid]) && (int)$this->conf_keys['delay_groups'][$cd_gid]) {
|
|
$prevOrderObj->sepa_date = date('Y-m-d', time() + $this->conf_keys['delay_groups'][$cd_gid] * 86400);
|
|
// } else {
|
|
// $prevOrderObj->sepa_date = date('Y-m-d', time() + $this->conf_keys['delay'] * 86400);
|
|
// }
|
|
$prevOrderObj->traite = 0;
|
|
$prevOrderObj->save();
|
|
}
|
|
}
|
|
|
|
public function getTemplateVars()
|
|
{
|
|
$cart = $this->context->cart;
|
|
$total = sprintf(
|
|
$this->trans('%1$s (tax incl.)', array(), 'Modules.PrelevementSEPA.Shop'),
|
|
Tools::displayPrice($cart->getOrderTotal(true, Cart::BOTH))
|
|
);
|
|
|
|
return array(
|
|
'total' => $total,
|
|
'this_path' => $this->getPathUri(),
|
|
'this_path_sepa' => $this->getPathUri(),
|
|
'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/'
|
|
);
|
|
}
|
|
|
|
protected function getCookieOrderByPrefix()
|
|
{
|
|
return str_replace(array('admin', 'controller'), '', Tools::strtolower(get_class($this)));
|
|
}
|
|
|
|
public function checkFtpAccess()
|
|
{
|
|
$server = $this->conf_keys['ftp_host'];
|
|
$user = $this->conf_keys['ftp_login'];
|
|
$pwd = $this->conf_keys['ftp_password'];
|
|
$port = $this->conf_keys['ftp_port'];
|
|
$sslcon = $this->conf_keys['ftp_sslcon'];
|
|
if (!function_exists('ftp_ssl_connect')) {
|
|
$sslcon = 0;
|
|
}
|
|
$errors = array();
|
|
$success = array();
|
|
if (!$server || !$user || !$pwd) {
|
|
return false;
|
|
}
|
|
// Mise en place d'une connexion basique
|
|
if ($sslcon) {
|
|
if (!$conn_id = @ftp_ssl_connect($server, $port)) {
|
|
$errors[] = $this->l('Failed to connect to SSL-FTP ');
|
|
} else {
|
|
$success[] = $this->l('Connected to FTP ');
|
|
}
|
|
} elseif (!$conn_id = @ftp_connect($server, $port)) {
|
|
$errors[] = $this->l('Failed to connect to FTP ');
|
|
} else {
|
|
$success[] = $this->l('Connected to FTP ');
|
|
}
|
|
// Identification avec un nom d'utilisateur et un mot de passe
|
|
if (!$conn_id || ( $conn_id && !ftp_login($conn_id, $user, $pwd) )) {
|
|
$errors[] = $this->l('Failed to log in to FTP');
|
|
} elseif ($conn_id) {
|
|
$success[] = $this->l('Logged in to FTP ');
|
|
}
|
|
|
|
$this->context->smarty->assign(
|
|
array(
|
|
'tr_error' => $errors,
|
|
'tr_success' => $success
|
|
)
|
|
);
|
|
}
|
|
|
|
public function getWorkingDays($date_start, $nb_days)
|
|
{
|
|
$wd = 1;
|
|
$i = 1;
|
|
while ($wd <= $nb_days) {
|
|
$date_mk = $date_start + (86400 * $i);
|
|
if ((date("N", $date_mk) != 6) && (date("N", $date_mk) != 7)) {
|
|
$wd++;
|
|
}
|
|
$i++;
|
|
}
|
|
return $date_start +(86400 * ($i-1));
|
|
}
|
|
|
|
public function getWorkingDay($date_start, $nb_days)
|
|
{
|
|
$date_start = $date_start + (86400 * $nb_days);
|
|
|
|
if ((date("N", $date_start) != 6) && (date("N", $date_start) != 7)) {
|
|
return $date_start;
|
|
} else {
|
|
$date_start = $date_start + 86400;
|
|
if ((date("N", $date_start) != 6) && (date("N", $date_start) != 7)) {
|
|
return $date_start;
|
|
} else {
|
|
return $date_start + 86400;
|
|
}
|
|
}
|
|
}
|
|
}
|