1<?php 2/** 3 * Copyright since 2007 PrestaShop SA and Contributors 4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Academic Free License 3.0 (AFL-3.0) 9 * that is bundled with this package in the file LICENSE.md. 10 * It is also available through the world-wide-web at this URL: 11 * https://opensource.org/licenses/AFL-3.0 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to https://devdocs.prestashop.com/ for more information. 21 * 22 * @author PrestaShop SA and Contributors <contact@prestashop.com> 23 * @copyright Since 2007 PrestaShop SA and Contributors 24 * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) 25 */ 26 27use PrestaShop\PrestaShop\Core\Payment\PaymentOption; 28 29if (!defined('_PS_VERSION_')) { 30 exit; 31} 32 33class Ps_Checkpayment extends PaymentModule 34{ 35 private $_html = ''; 36 private $_postErrors = []; 37 38 public $checkName; 39 public $address; 40 public $extra_mail_vars; 41 42 public function __construct() 43 { 44 $this->name = 'ps_checkpayment'; 45 $this->tab = 'payments_gateways'; 46 $this->version = '2.0.5'; 47 $this->author = 'PrestaShop'; 48 $this->controllers = ['payment', 'validation']; 49 50 $this->currencies = true; 51 $this->currencies_mode = 'checkbox'; 52 53 $config = Configuration::getMultiple(['CHEQUE_NAME', 'CHEQUE_ADDRESS']); 54 if (isset($config['CHEQUE_NAME'])) { 55 $this->checkName = $config['CHEQUE_NAME']; 56 } 57 if (isset($config['CHEQUE_ADDRESS'])) { 58 $this->address = $config['CHEQUE_ADDRESS']; 59 } 60 61 $this->bootstrap = true; 62 parent::__construct(); 63 64 $this->displayName = $this->trans('Payments by check', [], 'Modules.Checkpayment.Admin'); 65 $this->description = $this->trans('This module allows you to accept payments by check.', [], 'Modules.Checkpayment.Admin'); 66 $this->confirmUninstall = $this->trans('Are you sure you want to delete these details?', [], 'Modules.Checkpayment.Admin'); 67 $this->ps_versions_compliancy = ['min' => '1.7.1.0', 'max' => _PS_VERSION_]; 68 69 if ((!isset($this->checkName) || !isset($this->address) || empty($this->checkName) || empty($this->address))) { 70 $this->warning = $this->trans('The "Payee" and "Address" fields must be configured before using this module.', [], 'Modules.Checkpayment.Admin'); 71 } 72 if (!count(Currency::checkPaymentCurrencies($this->id))) { 73 $this->warning = $this->trans('No currency has been set for this module.', [], 'Modules.Checkpayment.Admin'); 74 } 75 76 $this->extra_mail_vars = [ 77 '{check_name}' => Configuration::get('CHEQUE_NAME'), 78 '{check_address}' => Configuration::get('CHEQUE_ADDRESS'), 79 '{check_address_html}' => Tools::nl2br(Configuration::get('CHEQUE_ADDRESS')), 80 ]; 81 } 82 83 public function install() 84 { 85 return parent::install() 86 && $this->registerHook('paymentOptions') 87 && $this->registerHook('paymentReturn') 88 ; 89 } 90 91 public function uninstall() 92 { 93 return Configuration::deleteByName('CHEQUE_NAME') 94 && Configuration::deleteByName('CHEQUE_ADDRESS') 95 && parent::uninstall() 96 ; 97 } 98 99 private function _postValidation() 100 { 101 if (Tools::isSubmit('btnSubmit')) { 102 if (!Tools::getValue('CHEQUE_NAME')) { 103 $this->_postErrors[] = $this->trans('The "Payee" field is required.', [], 'Modules.Checkpayment.Admin'); 104 } elseif (!Tools::getValue('CHEQUE_ADDRESS')) { 105 $this->_postErrors[] = $this->trans('The "Address" field is required.', [], 'Modules.Checkpayment.Admin'); 106 } 107 } 108 } 109 110 private function _postProcess() 111 { 112 if (Tools::isSubmit('btnSubmit')) { 113 Configuration::updateValue('CHEQUE_NAME', Tools::getValue('CHEQUE_NAME')); 114 Configuration::updateValue('CHEQUE_ADDRESS', Tools::getValue('CHEQUE_ADDRESS')); 115 } 116 $this->_html .= $this->displayConfirmation($this->trans('Settings updated', [], 'Admin.Notifications.Success')); 117 } 118 119 private function _displayCheck() 120 { 121 return $this->display(__FILE__, './views/templates/hook/infos.tpl'); 122 } 123 124 public function getContent() 125 { 126 $this->_html = ''; 127 128 if (Tools::isSubmit('btnSubmit')) { 129 $this->_postValidation(); 130 if (!count($this->_postErrors)) { 131 $this->_postProcess(); 132 } else { 133 foreach ($this->_postErrors as $err) { 134 $this->_html .= $this->displayError($err); 135 } 136 } 137 } 138 139 $this->_html .= $this->_displayCheck(); 140 $this->_html .= $this->renderForm(); 141 142 return $this->_html; 143 } 144 145 public function hookPaymentOptions($params) 146 { 147 if (!$this->active) { 148 return; 149 } 150 if (!$this->checkCurrency($params['cart'])) { 151 return; 152 } 153 154 $this->smarty->assign( 155 $this->getTemplateVars() 156 ); 157 158 $newOption = new PaymentOption(); 159 $newOption->setModuleName($this->name) 160 ->setCallToActionText($this->trans('Pay by Check', [], 'Modules.Checkpayment.Admin')) 161 ->setAction($this->context->link->getModuleLink($this->name, 'validation', [], true)) 162 ->setAdditionalInformation($this->fetch('module:ps_checkpayment/views/templates/front/payment_infos.tpl')); 163 164 return [$newOption]; 165 } 166 167 public function hookPaymentReturn($params) 168 { 169 if (!$this->active) { 170 return; 171 } 172 173 $state = $params['order']->getCurrentState(); 174 $rest_to_paid = $params['order']->getOrdersTotalPaid() - $params['order']->getTotalPaid(); 175 if (in_array($state, [Configuration::get('PS_OS_CHEQUE'), Configuration::get('PS_OS_OUTOFSTOCK'), Configuration::get('PS_OS_OUTOFSTOCK_UNPAID')])) { 176 $this->smarty->assign([ 177 'total_to_pay' => Tools::displayPrice( 178 $rest_to_paid, 179 new Currency($params['order']->id_currency), 180 false 181 ), 182 'shop_name' => $this->context->shop->name, 183 'checkName' => $this->checkName, 184 'checkAddress' => Tools::nl2br($this->address), 185 'status' => 'ok', 186 'id_order' => $params['order']->id, 187 ]); 188 if (isset($params['order']->reference) && !empty($params['order']->reference)) { 189 $this->smarty->assign('reference', $params['order']->reference); 190 } 191 } else { 192 $this->smarty->assign('status', 'failed'); 193 } 194 195 return $this->fetch('module:ps_checkpayment/views/templates/hook/payment_return.tpl'); 196 } 197 198 public function checkCurrency($cart) 199 { 200 $currency_order = new Currency((int) ($cart->id_currency)); 201 $currencies_module = $this->getCurrency((int) $cart->id_currency); 202 203 if (is_array($currencies_module)) { 204 foreach ($currencies_module as $currency_module) { 205 if ($currency_order->id == $currency_module['id_currency']) { 206 return true; 207 } 208 } 209 } 210 211 return false; 212 } 213 214 public function renderForm() 215 { 216 $fields_form = [ 217 'form' => [ 218 'legend' => [ 219 'title' => $this->trans('Contact details', [], 'Modules.Checkpayment.Admin'), 220 'icon' => 'icon-envelope', 221 ], 222 'input' => [ 223 [ 224 'type' => 'text', 225 'label' => $this->trans('Payee (name)', [], 'Modules.Checkpayment.Admin'), 226 'name' => 'CHEQUE_NAME', 227 'required' => true, 228 ], 229 [ 230 'type' => 'textarea', 231 'label' => $this->trans('Address', [], 'Modules.Checkpayment.Admin'), 232 'desc' => $this->trans('Address where the check should be sent to.', [], 'Modules.Checkpayment.Admin'), 233 'name' => 'CHEQUE_ADDRESS', 234 'required' => true, 235 ], 236 ], 237 'submit' => [ 238 'title' => $this->trans('Save', [], 'Admin.Actions'), 239 ], 240 ], 241 ]; 242 243 $helper = new HelperForm(); 244 $helper->show_toolbar = false; 245 $helper->id = (int) Tools::getValue('id_carrier'); 246 $helper->identifier = $this->identifier; 247 $helper->submit_action = 'btnSubmit'; 248 $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; 249 $helper->token = Tools::getAdminTokenLite('AdminModules'); 250 $helper->tpl_vars = [ 251 'fields_value' => $this->getConfigFieldsValues(), 252 ]; 253 254 return $helper->generateForm([$fields_form]); 255 } 256 257 public function getConfigFieldsValues() 258 { 259 return [ 260 'CHEQUE_NAME' => Tools::getValue('CHEQUE_NAME', Configuration::get('CHEQUE_NAME')), 261 'CHEQUE_ADDRESS' => Tools::getValue('CHEQUE_ADDRESS', Configuration::get('CHEQUE_ADDRESS')), 262 ]; 263 } 264 265 public function getTemplateVars() 266 { 267 $cart = $this->context->cart; 268 $total = $this->trans( 269 '%amount% (tax incl.)', 270 [ 271 '%amount%' => Tools::displayPrice($cart->getOrderTotal(true, Cart::BOTH)), 272 ], 273 'Modules.Checkpayment.Admin' 274 ); 275 276 $checkOrder = Configuration::get('CHEQUE_NAME'); 277 if (!$checkOrder) { 278 $checkOrder = '___________'; 279 } 280 281 $checkAddress = Tools::nl2br(Configuration::get('CHEQUE_ADDRESS')); 282 if (!$checkAddress) { 283 $checkAddress = '___________'; 284 } 285 286 return [ 287 'checkTotal' => $total, 288 'checkOrder' => $checkOrder, 289 'checkAddress' => $checkAddress, 290 ]; 291 } 292} 293