roykin/modules/be2bill/be2bill.php
2016-10-21 11:39:06 +02:00

1241 lines
47 KiB
PHP

<?php
/**
* 1997-2016 Quadra Informatique
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0) that is available
* through the world-wide-web at this URL: http://www.opensource.org/licenses/OSL-3.0
* If you are unable to obtain it through the world-wide-web, please send an email
* to modules@quadra-informatique.fr so we can send you a copy immediately.
*
* @author Quadra Informatique <modules@quadra-informatique.fr>
* @copyright 1997-2016 Quadra Informatique
* @license http://www.opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once(dirname(__FILE__).'/classes/Be2billXml.php');
require_once(dirname(__FILE__).'/classes/Be2billAlias.php');
require_once(dirname(__FILE__).'/classes/Be2billApi.php');
require_once(dirname(__FILE__).'/classes/Be2billExeccode.php');
require_once(dirname(__FILE__).'/classes/Be2billExeccodeHistory.php');
require_once(dirname(__FILE__).'/classes/Be2billOrderReference.php');
require_once(dirname(__FILE__).'/classes/Be2billSchedule.php');
require_once(dirname(__FILE__).'/classes/Be2billTransaction.php');
require_once(dirname(__FILE__).'/models/MerchandConfiguration.php');
require_once(dirname(__FILE__).'/models/MerchandConfigurationOptions.php');
require_once(dirname(__FILE__).'/models/MerchandConfigurationCountries.php');
class Be2bill extends PaymentModule
{
private $_html = '';
/**
* Constructor
*/
public function __construct()
{
$this->name = 'be2bill';
$this->tab = 'payments_gateways';
$this->version = '2.0.11';
$this->author = 'Be2bill';
$this->module_key = '182f5ec2e5fb97d04e0008254b92819a';
$this->bootstrap = true;
$this->need_instance = 1;
parent::__construct();
$this->displayName = $this->l('Be2bill Internet Dynamic Payments');
$this->description = $this->l('The payment solution for the e-sellers.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
$this->ps_versions_compliancy = array(
'min' => '1.6',
'max' => _PS_VERSION_);
$this->module_key = '0b8e0275aec7993e370e2391d28cca53';
/* if (!Tools::isEmpty($old_version))
$this->runUpgrades($old_version, true); */
}
/**
* Install procedure
* @return boolean
*/
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
// Set default configuration
Configuration::updateGlobalValue('BE2BILL_LOGS', 0);
Configuration::updateGlobalValue('BE2BILL_SANDBOX', 0);
Configuration::updateGlobalValue('BE2BILL_URL_XML', 'https://template.be2bill.com/cms/xml/v1/configuration.xml');
Configuration::updateGlobalValue('BE2BILL_MAIN_SERVER', 'https://secure-magenta1.be2bill.com');
Configuration::updateGlobalValue('BE2BILL_SLAVE_SERVER', 'https://secure-magenta2.be2bill.com');
Configuration::updateGlobalValue('BE2BILL_SB_MAIN_SERVER', 'https://secure-test.be2bill.com');
Configuration::updateGlobalValue('BE2BILL_SB_SLAVE_SERVER', 'https://secure-test.be2bill.com');
Configuration::updateGlobalValue('BE2BILL_REDIRECT_PAYMENT', 1);
return parent::install() && $this->installBackOffice() &&
$this->createTables() &&
$this->registerOrderStatus() && $this->registerOrderDeferedStatus() &&
$this->registerOrderNxStatus() && $this->registerOrderShipStatus() &&
$this->registerHook('displayHeader') &&
$this->registerHook('displayPayment') &&
$this->registerHook('displayPaymentReturn') &&
$this->registerHook('displayBackOfficeHeader') &&
$this->registerHook('actionAdminControllerSetMedia') &&
$this->registerHook('displayAdminCustomers') &&
$this->registerHook('actionOrderStatusUpdate') &&
$this->registerHook('displayAdminOrder');
}
/**
* Uninstall procedure
* @return boolean
*/
public function uninstall()
{
return parent::uninstall() &&
$this->uninstallModuleTab('AdminManageBe2billConf') &&
Be2billXml::uninstallBe2billXmlTables() &&
Configuration::deleteByName('BE2BILL_XML_VERSION') &&
Configuration::deleteByName('BE2BILL_LOGS') &&
Configuration::deleteByName('BE2BILL_URL_XML') &&
Configuration::deleteByName('BE2BILL_SANDBOX') &&
Configuration::deleteByName('BE2BILL_MAIN_SERVER') &&
Configuration::deleteByName('BE2BILL_SLAVE_SERVER') &&
Configuration::deleteByName('BE2BILL_SB_MAIN_SERVER') &&
Configuration::deleteByName('BE2BILL_SB_SLAVE_SERVER') &&
Configuration::deleteByName('BE2BILL_REDIRECT_PAYMENT');
}
/**
* Create module tables procedure
* @return boolean
*/
public function createTables()
{
return Be2billXml::createBe2billXmlTables() &&
Be2billAlias::createBe2billAliasTable() &&
Be2billExeccode::createBe2billExeccodeTable() &&
Be2billExeccode::insertBe2billExeccodeDefaultValues() &&
Be2billExeccodeHistory::createBe2billExeccodeHistoryTable() &&
Be2billSchedule::createBe2billScheduleTable() &&
Be2billTransaction::createBe2billTransactionTable() &&
Be2billOrderReference::createBe2billOrderReferenceTable();
}
public function installBackOffice()
{
$id_lang_en = LanguageCore::getIdByIso('en');
$id_lang_fr = LanguageCore::getIdByIso('fr');
$id_root_tab = Tab::getIdFromClassName('AdminParentModules');
if (!$id_root_tab) {
$this->installModuleTab('be2bill', array(
$id_lang_fr => 'Be2bill',
$id_lang_en => 'Be2bill'), '0');
}
$this->installModuleTab('AdminManageBe2billConf', array(
$id_lang_fr => 'Configuration paiements Be2bill',
$id_lang_en => 'Be2bill Payments configuration'), $id_root_tab);
return true;
}
public function uninstallModuleTab($tabClass)
{
$idTab = Tab::getIdFromClassName($tabClass);
if ($idTab != 0) {
$tab = new Tab($idTab);
$tab->delete();
return true;
}
return false;
}
public function installModuleTab($tabClass, $tabName, $idTabParent)
{
$tab = new Tab();
$tab->name = $tabName;
$tab->class_name = $tabClass;
$tab->module = $this->name;
$tab->id_parent = (int)($idTabParent);
if (!$tab->save()) {
return false;
}
return true;
}
/**
* Launch upgrade process
*/
public function runUpgrades($registered_version, $install = false)
{
if (!$install) {
return false;
}
$list = array();
$upgrade_path = _PS_MODULE_DIR_.$this->name.'/upgrade/';
// Check if folder exist and it could be read
if (file_exists($upgrade_path) && ($files = scandir($upgrade_path))) {
// Read each file name
foreach ($files as $file) {
if (!in_array($file, array(
'.',
'..',
'.svn',
'index.php'))) {
$tab = explode('-', $file);
$file_version = basename($tab[1], '.php');
// Compare version, if minor than actual, we need to upgrade the module
if (count($tab) == 2 &&
(Tools::version_compare($file_version, $this->version, '<=') &&
Tools::version_compare($file_version, $registered_version, '>'))) {
$list[] = array(
'file' => $upgrade_path.$file,
'version' => $file_version,
'upgrade_function' => 'upgrade_module_'.str_replace('.', '_', $file_version));
}
}
}
}
if (count($list)) {
foreach ($list as $list_detail) {
include_once($list_detail['file']);
call_user_func($list_detail['upgrade_function'], $this, $install);
}
}
}
/**
* Create Be2bill order status pending
* @return boolean
*/
public function registerOrderStatus()
{
if ((int)Configuration::get('BE2BILL_OS_PENDING') > 0) {
return true;
}
$order_state = new OrderState();
$order_state->unremovable = true;
$order_state->name = array();
foreach (Language::getLanguages() as $language) {
if (Tools::strtolower($language['iso_code']) == 'fr') {
$order_state->name[$language['id_lang']] = 'En attente du paiement par Be2bill';
} else {
$order_state->name[$language['id_lang']] = 'Awaiting Be2bill payment';
}
}
$order_state->module_name = $this->name;
$order_state->color = 'RoyalBlue';
$order_state->hidden = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->invoice = false;
$order_state->send_email = false;
if ($order_state->save()) {
Tools::copy(
_PS_MODULE_DIR_.$this->name.'/views/img/be2bill.gif',
_PS_IMG_DIR_.'os/'.(int)$order_state->id.'.gif'
);
Configuration::updateValue('BE2BILL_OS_PENDING', (int)$order_state->id);
return true;
}
return false;
}
/**
* Create Be2billNx order status
* @return boolean
*/
public function registerOrderNxStatus()
{
if ((int)Configuration::get('BE2BILLNX_OS_PAYMENT') > 0) {
return true;
}
$id_order_state = (int)Configuration::get('BE2BILLNX_ORDER_STATE_ID');
if ($id_order_state) {
Configuration::deleteByName('BE2BILLNX_ORDER_STATE_ID');
}
$order_state = new OrderState($id_order_state);
$order_state->unremovable = true;
$order_state->name = array();
foreach (Language::getLanguages() as $language) {
if (Tools::strtolower($language['iso_code']) == 'fr') {
$order_state->name[$language['id_lang']] = 'Paiement en plusieurs fois';
} else {
$order_state->name[$language['id_lang']] = 'Payment in several times';
}
}
$order_state->module_name = $this->name;
$order_state->color = '#0074A1';
$order_state->hidden = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->invoice = true;
$order_state->send_email = false;
$order_state->template = '';
if ($order_state->save()) {
copy(
_PS_MODULE_DIR_.$this->name.'/views/img/be2bill.gif',
_PS_IMG_DIR_.'os/'.(int)$order_state->id.'.gif'
);
Configuration::updateValue('BE2BILLNX_OS_PAYMENT', (int)$order_state->id);
return true;
}
return false;
}
/**
* Create Be2billShip order status
* @return boolean
*/
public function registerOrderShipStatus()
{
if ((int)Configuration::get('BE2BILLSHIP_OS_PAYMENT') > 0) {
return true;
}
$id_order_state = (int)Configuration::get('BE2BILLSHIP_ORDER_STATE_ID');
if ($id_order_state) {
Configuration::deleteByName('BE2BILLSHIP_ORDER_STATE_ID');
}
$order_state = new OrderState($id_order_state);
$order_state->unremovable = true;
$order_state->name = array();
foreach (Language::getLanguages() as $language) {
if (Tools::strtolower($language['iso_code']) == 'fr') {
$order_state->name[$language['id_lang']] = 'Paiement à capturer lors de l\'expédition';
} else {
$order_state->name[$language['id_lang']] = 'Payment has to be captured at shipment';
}
}
$order_state->module_name = $this->name;
$order_state->color = '#0074A1';
$order_state->hidden = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->paid = false;
$order_state->invoice = true;
$order_state->send_email = false;
$order_state->template = '';
if ($order_state->save()) {
copy(
_PS_MODULE_DIR_.$this->name.'/views/img/be2bill.gif',
_PS_IMG_DIR_.'os/'.(int)$order_state->id.'.gif'
);
Configuration::updateValue('BE2BILLSHIP_OS_PAYMENT', (int)$order_state->id);
return true;
}
return false;
}
public function registerOrderDeferedStatus()
{
if ((int)Configuration::get('BE2BILLDEF_OS_PAYMENT') > 0) {
return true;
}
$id_order_state = (int)Configuration::get('BE2BILLDEF_ORDER_STATE_ID');
if ($id_order_state) {
Configuration::deleteByName('BE2BILLDEF_ORDER_STATE_ID');
}
$order_state = new OrderState($id_order_state);
$order_state->unremovable = true;
$order_state->name = array();
foreach (Language::getLanguages() as $language) {
if (Tools::strtolower($language['iso_code']) == 'fr') {
$order_state->name[$language['id_lang']] = 'Paiement différé';
} else {
$order_state->name[$language['id_lang']] = 'Deferred payment';
}
}
$order_state->module_name = $this->name;
$order_state->color = '#0074A1';
$order_state->hidden = false;
$order_state->delivery = false;
$order_state->logable = false;
$order_state->paid = false;
$order_state->invoice = true;
$order_state->send_email = false;
$order_state->template = '';
if ($order_state->save()) {
copy(
_PS_MODULE_DIR_.$this->name.'/views/img/be2bill.gif',
_PS_IMG_DIR_.'os/'.(int)$order_state->id.'.gif'
);
Configuration::updateValue('BE2BILLDEF_OS_PAYMENT', (int)$order_state->id);
return true;
}
return false;
}
/**
* Hook order status update : Caputre transaction
*
* @param array $params list of specific data
*/
public function hookActionOrderStatusUpdate($params)
{
if (!$this->active) {
return;
}
$order = new Order((int)$params['id_order']);
if ($order->module == $this->name || $order->module == 'be2billship') {
$id_account = Be2billTransaction::getMerchandAccountIdByOrderId($order->id);
$status_account = MerchandConfigurationOptions::getAccountOptionsParameterValue($id_account, 'delivery');
if ($status_account) {
$status_ids = explode(';', $status_account);
if (in_array($params['newOrderStatus']->id, $status_ids)) {
foreach (Be2billSchedule::getOrderScheduleToCapture($order->reference) as $schedule) {
$extradata = $this->name;
$result = Be2billApi::capture(
$order->reference,
$schedule['transaction_id'],
$id_account,
$schedule['amount'],
$extradata
);
if ($result['EXECCODE'] != '0000') {
die($result['MESSAGE']);
}
}
}
}
}
}
/**
* Hook Header : Add Media CSS & JS
*
* @param array $params list of specific data
*/
public function hookDisplayHeader($params)
{
$this->context->controller->addCSS(($this->_path).'views/css/fo_be2bill.css', 'all');
}
/**
* Hook Display Payment : Display payment smarty tpl
*
* @param array $params list of specific data
* @return string Html
*/
public function hookDisplayPayment($params)
{
$available_payments = $this->getAvailablePayments($params);
if (!$available_payments) {
return false;
}
if (!$this->canDisplayPayment($params)) {
return false;
}
$this->context->smarty->assign(array(
'available_payments' => $available_payments,
'redirect_auto' => (int)Configuration::getGlobalValue('BE2BILL_REDIRECT_PAYMENT')
));
return $this->display(__FILE__, 'payment.tpl');
}
/**
* Available Payments : return a list of available payment
*
* @param array $params list of specific data
* @return array of payment
*/
public function getAvailablePayments($params)
{
// Check if the payment method can be displayed
if (!$params['cart']) {
return false;
}
// retrieving currency
$currency = Currency::getCurrency((int)$params['cart']->id_currency);
if (!$currency) {
return false;
}
$iso_currency = $currency['iso_code'];
// retrieving country
$address = Address::getCountryAndState($params['cart']->id_address_invoice);
if (!$address) {
return false;
}
$iso_country = Country::getIsoById($address['id_country']);
if (!$iso_country) {
return false;
}
return MerchandConfiguration::getAvailablePayments(
$iso_currency,
$iso_country,
$params['cart']->getOrderTotal(true),
(int)$this->context->customer->id
);
}
public function canDisplayPayment($params)
{
if (!$this->active) {
return false;
}
if (!$this->checkCurrency($params['cart'])) {
return false;
}
// Check if the module is configure
if (/* Production mode */
(!Configuration::getGlobalValue('BE2BILL_SANDBOX') &&
(!Configuration::getGlobalValue('BE2BILL_MAIN_SERVER') || !Configuration::getGlobalValue('BE2BILL_SLAVE_SERVER'))) ||
/* Sandbox mode */
(Configuration::getGlobalValue('BE2BILL_SANDBOX') &&
(!Configuration::getGlobalValue('BE2BILL_SB_MAIN_SERVER') || !Configuration::getGlobalValue('BE2BILL_SB_SLAVE_SERVER')))) {
return false;
}
return true;
}
/**
* Hook Display Payment Return : Display payment return smarty tpl
*
* @param array $params list of specific data
* @return string Html
*/
public function hookDisplayPaymentReturn($params)
{
if (!$this->active) {
return;
}
$customer = new Customer($params['objOrder']->id_customer);
if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1) {
$controller = 'order-opc';
} else {
$controller = 'order';
}
if ($params['objOrder']->getCurrentState() != Configuration::get('BE2BILL_OS_PENDING') &&
$params['objOrder']->getCurrentState() != _PS_OS_ERROR_
&& $params['objOrder']->getCurrentState() != _PS_OS_OUTOFSTOCK_UNPAID_) {
$is_payment_ok = true;
$partial_paid = false;
} elseif ($params['objOrder']->getCurrentState() == _PS_OS_ERROR_
&& $params['objOrder']->total_paid_real < $params['objOrder']->total_paid
&& $params['objOrder']->total_paid_real > 0
&& $params['objOrder']->getCurrentState() != _PS_OS_OUTOFSTOCK_UNPAID_) {
$is_payment_ok = false;
$partial_paid = true;
} else {
$is_payment_ok = false;
$partial_paid = false;
}
$this->context->smarty->assign(array(
'is_waiting' => ($params['objOrder']->getCurrentState() == Configuration::get('BE2BILL_OS_PENDING')) ? true : false,
'is_payment_ok' => $is_payment_ok,
'partial_paid' => $partial_paid,
'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false),
'order' => $params['objOrder'],
'customer' => $customer,
'order_controller' => $controller
));
return $this->display(__FILE__, 'payment_return.tpl');
}
/**
* Hook Back office Header : Add Media for the administration panel
*
* @param array $params list of specific data
* @return string Html
*/
public function hookBackOfficeHeader($params)
{
if (in_array('addCSS', get_class_methods($this->context->controller))) {
$this->context->controller->addCSS(($this->_path).'views/css/bo_be2bill.css', 'all');
}
}
/**
* Hook Admin Controller Set Media : Add JS for the administration panel
*
* @param array $params list of specific data
* @return string Html
*/
public function hookActionAdminControllerSetMedia($params)
{
$this->context->controller->addJs(($this->_path).'views/js/adminbe2bill.js');
}
/**
* Hook Display Admin Customer : Display customer alias
*
* @param array $params list of specific data
* @return string Html
*/
public function hookDisplayAdminCustomers($params)
{
// Display be2bill customer alias data
$this->context->smarty->assign(array(
'aliases' => Be2billAlias::getAllByIdCustomer($params['id_customer'])
));
return $this->display(__FILE__, 'admin_customers.tpl');
}
/**
* Hook Display Admin Order : Display Transaction and Schedule data, enable
* refund, capture
*
* @param array $params list of specific data
* @return string Html
*/
public function hookDisplayAdminOrder($params)
{
$order = new Order($params['id_order']);
// Refund Submit
if (Tools::isSubmit('submitBe2billRefund')) {
if (!Tools::getValue('be2bill_refund_transaction')) {
$this->_errors[] = $this->l('Select a transaction to refund.');
}
if (!Tools::getValue('be2bill_refund_slip')) {
$this->_errors[] = $this->l('Select a slip to refund.');
}
if (!count($this->_errors)) {
$transaction_id = Tools::getValue('be2bill_refund_transaction');
// Get transaction
$transaction = Be2billTransaction::getByTransactionId($transaction_id);
if ($transaction) {
// Get slip
$slip_id = Tools::getValue('be2bill_refund_slip');
$slip = new OrderSlip($slip_id);
if ($slip->id) {
// Refund
$id_account = Be2billTransaction::getMerchandAccountIdByOrderId($order->id);
$result = Be2billApi::refund(
$order->reference,
$transaction_id,
$id_account,
$slip->amount,
'ORDER#'.$order->id.' SLIP#'.$slip->id
);
if ($result['EXECCODE'] != '0000') {
$this->_errors[] = $result['MESSAGE'];
}
} else {
$this->_errors[] = $this->l('Unknown slip ID:').' '.$slip_id;
}
} else {
$this->_errors[] = $this->l('Unknown transaction ID:').' '.$transaction_id;
}
}
}
$id_account = Be2billTransaction::getMerchandAccountIdByOrderId($order->id);
$merchand_configuration = new MerchandConfiguration($id_account);
$can_refund = Be2billApi::hasRefund($merchand_configuration->b2b_xml_account_type_code);
// Display be2bill order data
$this->context->smarty->assign(array(
'order' => $order,
'be2bill_module_dir' => _PS_MODULE_DIR_.'be2bill/',
'module_name' => $this->displayName,
'can_refund' => $can_refund,
'transaction_history' => Be2billTransaction::getOrderTransactionHistory($order->reference),
'transaction_currency' => $merchand_configuration->currency_iso,
'refundable_transaction' => Be2billTransaction::getOrderRefundableTransaction($order->reference),
'slips' => Be2billTransaction::getOrderRefundableSlip((int)$order->id, (int)$order->id_customer),
'schedule' => Be2billSchedule::getByOrderReference($order->reference),
'errors' => $this->_errors
));
return $this->display(__FILE__, 'admin_order.tpl');
}
private function postValidation()
{
if (Tools::isSubmit('submit'.$this->name)) {
if (!Tools::getValue('BE2BILL_URL_XML')
|| !Validate::isAbsoluteUrl(Tools::getValue('BE2BILL_URL_XML'))) {
$this->_errors[] = $this->l('XML URL is required.');
}
if (!Tools::getValue('BE2BILL_MAIN_SERVER') ||
!Validate::isAbsoluteUrl(Tools::getValue('BE2BILL_MAIN_SERVER'))) {
$this->_errors[] = $this->l('Main server URL is required.');
}
if (!Tools::getValue('BE2BILL_SLAVE_SERVER') ||
!Validate::isAbsoluteUrl(Tools::getValue('BE2BILL_SLAVE_SERVER'))) {
$this->_errors[] = $this->l('Second server URL is required.');
}
if (Tools::getValue('BE2BILL_SANDBOX') &&
(!Tools::getValue('BE2BILL_SB_MAIN_SERVER') ||
!Validate::isAbsoluteUrl(Tools::getValue('BE2BILL_SB_MAIN_SERVER')))) {
$this->_errors[] = $this->l('Sandbox main server URL is required.');
}
if (Tools::getValue('BE2BILL_SANDBOX') &&
(!Tools::getValue('BE2BILL_SB_SLAVE_SERVER') ||
!Validate::isAbsoluteUrl(Tools::getValue('BE2BILL_SB_SLAVE_SERVER')))) {
$this->_errors[] = $this->l('Sandbox second server URL is required.');
}
}
if (count($this->_errors) > 0) {
return false;
}
return true;
}
private function postProcess()
{
// Used for not validate form by changing shop
if (count($_POST) <= 1) {
return false;
}
if (Tools::getValue('proceed_import')) {
$this->proceedImport();
}
if (!$this->postValidation()) {
return false;
}
if (Tools::isSubmit('submit'.$this->name)) {
Configuration::updateGlobalValue('BE2BILL_URL_XML', Tools::getValue('BE2BILL_URL_XML'));
Configuration::updateGlobalValue('BE2BILL_LOGS', (int)Tools::getValue('BE2BILL_LOGS'));
Configuration::updateGlobalValue('BE2BILL_SANDBOX', (int)Tools::getValue('BE2BILL_SANDBOX'));
Configuration::updateGlobalValue('BE2BILL_MAIN_SERVER', Tools::getValue('BE2BILL_MAIN_SERVER'));
Configuration::updateGlobalValue('BE2BILL_SLAVE_SERVER', Tools::getValue('BE2BILL_SLAVE_SERVER'));
Configuration::updateGlobalValue('BE2BILL_SB_MAIN_SERVER', Tools::getValue('BE2BILL_SB_MAIN_SERVER'));
Configuration::updateGlobalValue('BE2BILL_SB_SLAVE_SERVER', Tools::getValue('BE2BILL_SB_SLAVE_SERVER'));
Configuration::updateGlobalValue('BE2BILL_REDIRECT_PAYMENT', Tools::getValue('BE2BILL_REDIRECT_PAYMENT'));
}
}
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit'.$this->name)) {
$this->postProcess();
if (count($this->_errors) > 0) {
$output .= $this->displayError(implode('<br />', $this->_errors));
} else {
if (Tools::getValue('proceed_import')) {
$output .= $this->displayConfirmation($this->l('Import successfully done, please check your new account setting.'));
} else {
$output .= $this->displayConfirmation($this->l('Server settings updated'));
}
}
}
// Display Advices
$this->context->smarty->assign(array(
'server_ip' => $_SERVER['SERVER_ADDR'],
'notification_url' => Tools::getHttpHost(true).str_replace(
_PS_ROOT_DIR_.'/',
__PS_BASE_URI__,
$this->_path
).'notification.php',
'validation_url' => Tools::getHttpHost(true).str_replace(
_PS_ROOT_DIR_.'/',
__PS_BASE_URI__,
$this->_path
).'validation.php',
'template_url' => Tools::getHttpHost(true).str_replace(
_PS_ROOT_DIR_.'/',
__PS_BASE_URI__,
$this->_path
).'template.php',
'ajax_url' => '//'.Tools::getHttpHost().str_replace(
_PS_ROOT_DIR_.'/',
__PS_BASE_URI__,
$this->_path
).'ajax.php',
'content_dir' => Tools::getHttpHost(true).str_replace(
_PS_ROOT_DIR_.'/',
__PS_BASE_URI__,
$this->_path
).'views/img/',
));
return $output.$this->displayForm();
}
public function displayForm()
{
// Get default Language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
// for ajax translate
$msg = $this->l('Reset complete');
$msg = $this->l('Udpate not needed, tables already up to date');
$msg = $this->l('Update complete');
// Init Fields form array
$fields_form = array();
if (Configuration::getGlobalValue('BE2BILL_NEED_IMPORT')) {
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Import parameters'),
'image' => _PS_ADMIN_IMG_.'cog.gif'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Identifer to import'),
'name' => 'BE2BILL_ID',
'desc' => $this->l('This is your identifier account to import with all payment informations'),
'size' => 50
),
),
'submit' => array(
'title' => $this->l('Run import'),
'class' => 'button',
'name' => 'proceed_import'
)
);
}
$fields_form[1]['form'] = array(
'legend' => array(
'title' => $this->l('Servers parameters'),
'image' => _PS_ADMIN_IMG_.'cog.gif'
),
'input' => array(
array(
'type' => 'textbutton',
'label' => $this->l('Update XML URL'),
'name' => 'BE2BILL_URL_XML',
'required' => true,
'desc' => $this->l('Default value: https://template.be2bill.com/cms/xml/v1/configuration.xml'),
'size' => 50,
'button' => array(
'label' => $this->l('Update your account list'),
'attributes' => array(
'onclick' => 'doClick();'
)
)
),
array(
'type' => 'text',
'label' => $this->l('Current XML Version'),
'name' => 'BE2BILL_XML_VERSION',
'required' => false,
'readonly' => true,
'size' => 10
),
array(
'type' => 'radio',
'label' => $this->l('Use Debug mode'),
'name' => 'BE2BILL_LOGS',
'desc' => $this->l('Enable or disable the Debug mode'),
'values' => array(
array(
'id' => 'debug_on',
'value' => 1,
'label' => $this->l('Yes')),
array(
'id' => 'debug_off',
'value' => 0,
'label' => $this->l('No')),
),
'is_bool' => true,
'class' => 't'
),
array(
'type' => 'text',
'label' => $this->l('Production main server URL'),
'name' => 'BE2BILL_MAIN_SERVER',
'required' => true,
'desc' => $this->l('Default value: https://secure-magenta1.be2bill.com'),
'size' => 50
),
array(
'type' => 'text',
'label' => $this->l('Production second server URL'),
'name' => 'BE2BILL_SLAVE_SERVER',
'required' => true,
'desc' => $this->l('Default value: https://secure-magenta2.be2bill.com'),
'size' => 50
),
array(
'type' => 'radio',
'label' => $this->l('Use Sandbox mode'),
'name' => 'BE2BILL_SANDBOX',
'desc' => $this->l('Enable or disable the Sandbox mode'),
'values' => array(
array(
'id' => 'sandbox_on',
'value' => 1,
'label' => $this->l('Yes')),
array(
'id' => 'sandbox_off',
'value' => 0,
'label' => $this->l('No')),
),
'is_bool' => true,
'class' => 't'
),
array(
'type' => 'text',
'label' => $this->l('Sandbox main server URL'),
'name' => 'BE2BILL_SB_MAIN_SERVER',
'desc' => $this->l('Default value: https://secure-test.be2bill.com'),
'size' => 50
),
array(
'type' => 'text',
'label' => $this->l('Sandbox second server URL'),
'name' => 'BE2BILL_SB_SLAVE_SERVER',
'desc' => $this->l('Default value: https://secure-test.be2bill.com'),
'size' => 50
),
array(
'type' => 'radio',
'label' => $this->l('Redirect directly to BE2BILL'),
'name' => 'BE2BILL_REDIRECT_PAYMENT',
'desc' => $this->l('Enable or disable the redirection directly to the payment page'),
'values' => array(
array(
'id' => 'redirect_on',
'value' => 1,
'label' => $this->l('Yes')),
array(
'id' => 'redirect_off',
'value' => 0,
'label' => $this->l('No')),
),
'is_bool' => true,
'class' => 't'
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
$helper = new HelperForm();
// Module, token and currentIndex
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
// Language
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$helper->base_tpl = 'form.tpl';
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit'.$this->name;
$helper->toolbar_btn = array(
'save' => array(
'desc' => $this->l('Save'),
'href' => $helper->currentIndex.'&save'.$this->name.'&token='.$helper->token,
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.$helper->token,
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['BE2BILL_ID'] = Configuration::getGlobalValue('BE2BILL_ID');
$helper->fields_value['BE2BILL_LOGS'] = Configuration::getGlobalValue('BE2BILL_LOGS');
$helper->fields_value['BE2BILL_URL_XML'] = Configuration::getGlobalValue('BE2BILL_URL_XML');
$helper->fields_value['BE2BILL_SANDBOX'] = Configuration::getGlobalValue('BE2BILL_SANDBOX');
$helper->fields_value['BE2BILL_MAIN_SERVER'] = Configuration::getGlobalValue('BE2BILL_MAIN_SERVER');
$helper->fields_value['BE2BILL_SLAVE_SERVER'] = Configuration::getGlobalValue('BE2BILL_SLAVE_SERVER');
$helper->fields_value['BE2BILL_SB_MAIN_SERVER'] = Configuration::getGlobalValue('BE2BILL_SB_MAIN_SERVER');
$helper->fields_value['BE2BILL_SB_SLAVE_SERVER'] = Configuration::getGlobalValue('BE2BILL_SB_SLAVE_SERVER');
$helper->fields_value['BE2BILL_REDIRECT_PAYMENT'] = Configuration::getGlobalValue('BE2BILL_REDIRECT_PAYMENT');
$helper->fields_value['BE2BILL_XML_VERSION'] = Configuration::getGlobalValue('BE2BILL_XML_VERSION');
return $helper->generateForm($fields_form);
}
/**
* Check the cart and the module currency
* @param Cart $cart
* @return boolean
*/
public function checkCurrency($cart)
{
$currency_order = new Currency($cart->id_currency);
$currencies_module = $this->getCurrency($cart->id_currency);
if (is_array($currencies_module)) {
foreach ($currencies_module as $currency_module) {
if ($currency_order->id == $currency_module['id_currency']) {
return true;
}
}
}
return false;
}
public static function log($message, $level = AbstractLogger::DEBUG)
{
if ((int)Configuration::getGlobalValue('BE2BILL_LOGS')) {
$logger = new FileLogger(AbstractLogger::DEBUG);
$logger->setFilename(_PS_ROOT_DIR_.'/log/'.date('Ymd').'_be2bill.log');
$logger->log(strip_tags($message), $level);
}
}
public static function getBe2billModule()
{
return Module::getInstanceByName('be2bill');
}
public function proceedImport()
{
if (Be2billXml::paymentsNeedUpgrade()) {
$this->_errors[] = $this->l('You need to upgrade your xml before import.');
return false;
}
// creating new account (cbmcvisa-fr)
$merchand_conf = new MerchandConfiguration();
$merchand_conf->b2b_xml_account_type_code = 'CB/Visa/MasterCard';
$merchand_conf->b2b_xml_mode_code = 'form';
$merchand_conf->currency_iso = 'EUR';
$merchand_conf->login_account = Configuration::getGlobalValue('BE2BILL_ID');
$merchand_conf->password = Configuration::getGlobalValue('BE2BILL_PWD');
// get default logo
$logo = Be2billApi::getLogoUrl($merchand_conf->b2b_xml_account_type_code);
if ($logo && isset($logo['b2b_xml_account_type_logo'])) {
$merchand_conf->logo_url = $logo['b2b_xml_account_type_logo'];
}
foreach (Language::getLanguages() as $language) {
$merchand_conf->name[$language['id_lang']] = 'cbvisamc-fr';
}
$merchand_conf->active = 1;
$merchand_conf->add();
// adding defaut options standard
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'standard';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '';
$option->active = 1;
$option->add();
// adding defaut options defered
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'defered';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '7';
$option->active = 0;
$option->add();
// adding defaut options delivery
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'delivery';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '4';
$option->active = 0;
$option->add();
// adding defaut options ntimes
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'ntimes';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '3';
$option->active = 0;
$option->add();
// adding defaut options secure
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'secure';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '';
$option->active = 0;
$option->add();
// adding defaut options oneclick
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'oneclick';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '';
$option->active = 0;
$option->add();
// adding defaut options oneclickcvv
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'oneclickcvv';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '';
$option->active = 0;
$option->add();
// adding defaut options displaycreatealias
$option = new MerchandConfigurationOptions();
$option->id_b2b_merchand_configuration_account = $merchand_conf->id;
$option->b2b_xml_option = 'displaycreatealias';
$option->min_amount = 0;
$option->max_amount = 0;
$option->b2b_xml_option_extra = '';
$option->active = 0;
$option->add();
// inject old alias
$alias_list = array();
$old_list = Be2billAlias::getOldList();
foreach ($old_list as $row) {
$b2b_alias = new Be2billAlias();
$b2b_alias->id_customer = $row['id_customer'];
$b2b_alias->id_merchand_account = $merchand_conf->id;
$b2b_alias->alias = $row['alias'];
$b2b_alias->card_type = $row['card_type'];
$b2b_alias->card_number = $row['card_number'];
$b2b_alias->date_end = $row['date_end'];
$b2b_alias->date_add = $row['date_add'];
$b2b_alias->add();
$alias_list[$row['id_be2bill_alias']] = (int)Db::getInstance()->Insert_ID();
}
// inject old order reference
$old_list = Be2billOrderReference::getOldList();
foreach ($old_list as $row) {
$b2b_ref = new Be2billOrderReference();
$b2b_ref->id_cart = $row['id_cart'];
$b2b_ref->reference = $row['reference'];
$b2b_ref->add();
}
// inject old transaction
$transaction_list = array();
$old_list = Be2billTransaction::getOldList();
foreach ($old_list as $row) {
$b2b_transac = new Be2billTransaction();
$b2b_transac->id_order = $row['id_order'];
$b2b_transac->id_merchand_account = $merchand_conf->id;
$b2b_transac->id_order_slip = $row['id_order_slip'];
$b2b_transac->transaction_id = $row['transaction_id'];
$b2b_transac->transaction_type = $row['transaction_type'];
$b2b_transac->amount = $row['amount'];
$b2b_transac->date_add = $row['date_add'];
$order = new Order($row['id_order']);
if ($order->module != 'be2billamex') {
$b2b_transac->add();
$transaction_list[$row['id_be2bill_transaction']] = (int)Db::getInstance()->Insert_ID();
}
}
// inject old execode history
$old_list = Be2billExeccodeHistory::getOldList();
foreach ($old_list as $row) {
if (isset($transaction_list[$row['id_be2bill_transaction']])) {
$b2b_exec = new Be2billExeccodeHistory();
$b2b_exec->id_b2b_execcode = $row['id_be2bill_execcode'];
$b2b_exec->id_b2b_transaction = $transaction_list[$row['id_be2bill_transaction']];
$b2b_exec->date_add = $row['date_add'];
$b2b_exec->add();
}
}
// inject old schedule
$old_list = Be2billSchedule::getOldList();
foreach ($old_list as $row) {
$b2b_schedule = new Be2billSchedule();
$b2b_schedule->id_order = $row['id_order'];
$b2b_schedule->schedule_id = $row['schedule_id'];
$b2b_schedule->id_b2b_alias = 0;
if ($row['id_be2bill_alias']) {
$b2b_schedule->id_b2b_alias = $alias_list[$row['id_be2bill_alias']];
}
$b2b_schedule->transaction_id = $row['transaction_id'];
$b2b_schedule->amount = $row['amount'];
$b2b_schedule->date_add = $row['date_add'];
$b2b_schedule->date_scheduled = $row['date_scheduled'];
$b2b_schedule->date_done = $row['date_done'];
$b2b_schedule->is_done = $row['is_done'];
$option = '';
$order = new Order($row['id_order']);
if ($order->id) {
switch ($order->module) {
case 'be2bill':
break;
case 'be2billamex':
$option = 'amex';
break;
case 'be2billdef':
$option = 'defered';
break;
case 'be2billship':
$option = 'delivery';
break;
case 'be2billnx':
$option = 'ntimes';
break;
}
}
$b2b_schedule->option = $option;
if ($option != 'amex') {
$b2b_schedule->add();
}
}
Configuration::updateGlobalValue('BE2BILL_NEED_IMPORT', 0);
return true;
}
}