* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7095 $ * @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 Cheque extends PaymentModule { private $_html = ''; private $_postErrors = array(); public $chequeName; public $address; public function __construct() { $this->name = 'cheque'; $this->tab = 'payments_gateways'; $this->version = '2.3'; $this->author = 'PrestaShop'; $this->currencies = true; $this->currencies_mode = 'checkbox'; $config = Configuration::getMultiple(array('CHEQUE_NAME', 'CHEQUE_ADDRESS')); if (isset($config['CHEQUE_NAME'])) $this->chequeName = $config['CHEQUE_NAME']; if (isset($config['CHEQUE_ADDRESS'])) $this->address = $config['CHEQUE_ADDRESS']; parent::__construct(); $this->displayName = $this->l('Check'); $this->description = $this->l('Module for accepting payments by check.'); $this->confirmUninstall = $this->l('Are you sure you want to delete your details ?'); if (!isset($this->chequeName) OR !isset($this->address)) $this->warning = $this->l('\'To the order of\' and address must be configured in order to use this module correctly.'); if (!sizeof(Currency::checkPaymentCurrencies($this->id))) $this->warning = $this->l('No currency set for this module'); } public function install() { if (!parent::install() OR !$this->registerHook('payment') OR !$this->registerHook('paymentReturn')) return false; return true; } public function uninstall() { if (!Configuration::deleteByName('CHEQUE_NAME') OR !Configuration::deleteByName('CHEQUE_ADDRESS') OR !parent::uninstall()) return false; return true; } private function _postValidation() { if (Tools::isSubmit('btnSubmit')) { if (!Tools::getValue('name')) $this->_postErrors[] = $this->l('\'To the order of\' field is required.'); elseif (!Tools::getValue('address')) $this->_postErrors[] = $this->l('Address is required.'); } } private function _postProcess() { if (Tools::isSubmit('btnSubmit')) { Configuration::updateValue('CHEQUE_NAME', Tools::getValue('name')); Configuration::updateValue('CHEQUE_ADDRESS', Tools::getValue('address')); } $this->_html .= '
'.$this->l('OK').' '.$this->l('Settings updated').'
'; } private function _displayCheque() { $this->_html .= ''.$this->l('This module allows you to accept payments by check.').'

'.$this->l('If the client chooses this payment mode, the order status will change to \'Waiting for payment\'.').'
'.$this->l('Therefore, you will need to manually confirm the order as soon as you receive a check.').'


'; } private function _displayForm() { $this->_html .= '
'.$this->l('Contact details').'
'.$this->l('Please specify the name and address to which customers must send their check.').'.

'.$this->l('To the order of').'
'.$this->l('Address').'

'; } public function getContent() { $this->_html = '

'.$this->displayName.'

'; if (Tools::isSubmit('btnSubmit')) { $this->_postValidation(); if (!sizeof($this->_postErrors)) $this->_postProcess(); else foreach ($this->_postErrors AS $err) $this->_html .= '
'. $err .'
'; } else $this->_html .= '
'; $this->_displayCheque(); $this->_displayForm(); return $this->_html; } public function execPayment($cart) { return $this->actionPayment(true); } public function hookPayment($params) { if (!$this->active) return ; if (!$this->_checkCurrency($params['cart'])) return ; $this->context->smarty->assign(array( 'this_path' => $this->_path, 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/' )); return $this->display(__FILE__, 'payment.tpl'); } public function hookPaymentReturn($params) { if (!$this->active) return ; $state = $params['objOrder']->getCurrentState(); if ($state == Configuration::get('PS_OS_CHEQUE') OR $state == Configuration::get('PS_OS_OUTOFSTOCK')) $this->context->smarty->assign(array( 'total_to_pay' => Tools::displayPrice($params['total_to_pay'], $params['currencyObj'], false), 'chequeName' => $this->chequeName, 'chequeAddress' => Tools::nl2br($this->address), 'status' => 'ok', 'id_order' => $params['objOrder']->id )); else $this->context->smarty->assign('status', 'failed'); return $this->display(__FILE__, 'payment_return.tpl'); } private function _checkCurrency($cart) { $currency_order = new Currency((int)($cart->id_currency)); $currencies_module = $this->getCurrency((int)$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; } /** * This action display payment form * * @param bool $direct_call For retrocompatibility */ public function actionPayment($direct_call = false) { if (!$this->active) return ; $cart = $this->context->cart; if (!$this->_checkCurrency($cart)) Tools::redirect('index.php?controller=order'); $this->context->smarty->assign(array( 'nbProducts' => $cart->nbProducts(), 'cust_currency' => $cart->id_currency, 'currencies' => $this->getCurrency((int)$cart->id_currency), 'total' => $cart->getOrderTotal(true, Cart::BOTH), 'isoCode' => $this->context->language->iso_code, 'chequeName' => $this->chequeName, 'chequeAddress' => Tools::nl2br($this->address), 'this_path' => $this->_path, 'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/' )); if (!$direct_call) return $this->getTemplatePath('payment_execution.tpl'); else // For retrocompatibility return $this->display(__FILE__, 'payment_execution.tpl'); } /** * This action validate the payment */ public function actionValidation($direct_call = false) { $cart = $this->context->cart; if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->active) Tools::redirect('index.php?controller=order&step=1'); // Check that this payment option is still available in case the customer changed his address just before the end of the checkout process $authorized = false; foreach (Module::getPaymentModules() as $module) if ($module['name'] == 'cheque') { $authorized = true; break; } if (!$authorized) die(Tools::displayError('This payment method is not available.')); $customer = new Customer($cart->id_customer); if (!Validate::isLoadedObject($customer)) Tools::redirect('index.php?controller=order&step=1'); $currency = Tools::isSubmit('currency_payement') ? new Currency(Tools::getValue('currency_payement')) : $context->currency; $total = (float)$cart->getOrderTotal(true, Cart::BOTH); $mailVars = array( '{cheque_name}' => Configuration::get('CHEQUE_NAME'), '{cheque_address}' => Configuration::get('CHEQUE_ADDRESS'), '{cheque_address_html}' => str_replace("\n", '
', Configuration::get('CHEQUE_ADDRESS'))); $this->validateOrder((int)$cart->id, Configuration::get('PS_OS_CHEQUE'), $total, $this->displayName, NULL, $mailVars, (int)$currency->id, false, $customer->secure_key); Tools::redirect('index.php?controller=order-confirmation&id_cart='.(int)($cart->id).'&id_module='.(int)$this->id.'&id_order='.$this->currentOrder.'&key='.$customer->secure_key); } }