-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheque.php
407 lines (371 loc) · 12.5 KB
/
cheque.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* 2007-2016 PrestaShop
*
* Thirty Bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
* Copyright (C) 2017-2024 thirty bees
*
* 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:
* https://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 [email protected] so we can send you a copy immediately.
*
* @author Thirty Bees <[email protected]>
* @author PrestaShop SA <[email protected]>
* @copyright 2017-2024 thirty bees
* @copyright 2007-2016 PrestaShop SA
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* PrestaShop is an internationally registered trademark & property of PrestaShop SA
*/
if (!defined('_TB_VERSION_')) {
return;
}
/**
* Class Cheque
*/
class Cheque extends PaymentModule
{
const CHEQUE_NAME = 'CHEQUE_NAME';
const CHEQUE_ADDRESS = 'CHEQUE_ADDRESS';
const ORDER_STATUS = 'PS_OS_CHEQUE';
/**
* @var string $chequeName
*/
public $chequeName;
/**
* @var string $address
*/
public $address;
/**
* @var array $extraMailVars
*/
public $extraMailVars;
/**
* Cheque constructor.
*
* @throws PrestaShopException
*/
public function __construct()
{
$this->name = 'cheque';
$this->tab = 'payments_gateways';
$this->version = '3.2.0';
$this->author = 'thirty bees';
$this->controllers = ['payment', 'validation'];
$this->is_eu_compatible = 1;
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$config = Configuration::getMultiple([static::CHEQUE_NAME, static::CHEQUE_ADDRESS]);
if (isset($config['CHEQUE_NAME'])) {
$this->chequeName = $config['CHEQUE_NAME'];
}
if (isset($config['CHEQUE_ADDRESS'])) {
$this->address = $config['CHEQUE_ADDRESS'];
}
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Payments by cheque');
$this->description = $this->l('This module allows you to accept payments by cheque.');
$this->confirmUninstall = $this->l('Are you sure you want to delete these details?');
if ((empty($this->chequeName) || empty($this->address))) {
$this->warning = $this->l('The "Pay to the order of" and "Address" fields must be configured before using this module.');
}
$paymentCurrencies = Currency::checkPaymentCurrencies($this->id);
if (!is_array($paymentCurrencies) || empty($paymentCurrencies)) {
$this->warning = $this->l('No currency has been set for this module.');
}
$this->extraMailVars = [
'{cheque_name}' => Configuration::get(static::CHEQUE_NAME),
'{cheque_address}' => Configuration::get(static::CHEQUE_ADDRESS),
'{cheque_address_html}' => str_replace("\n", '<br />', Configuration::get(static::CHEQUE_ADDRESS)),
];
}
/**
* Install this module
*
* @return bool
* @throws PrestaShopException
*/
public function install()
{
return (
parent::install() &&
$this->registerHook('payment') &&
$this->registerHook('displayPaymentEU') &&
$this->registerHook('paymentReturn')
);
}
/**
* Uninstall this module
*
* @return bool
* @throws PrestaShopException
*/
public function uninstall()
{
if (!Configuration::deleteByName(static::CHEQUE_NAME) || !Configuration::deleteByName(static::CHEQUE_ADDRESS) || !parent::uninstall()) {
return false;
}
return true;
}
/**
* Module config page
*
* @return string
*
* @throws PrestaShopException
* @throws SmartyException
*/
public function getContent()
{
/** @var AdminController $controller */
$controller = $this->context->controller;
if (Tools::isSubmit('btnSubmit')) {
$this->postValidation();
if (!count($controller->errors)) {
$this->postProcess($controller);
}
}
return (
$this->display(__FILE__, 'infos.tpl') .
$this->renderForm($controller)
);
}
/**
* @param array $params
*
* @return string
*
* @throws PrestaShopException
* @throws SmartyException
*/
public function hookPayment($params)
{
if (!$this->active) {
return '';
}
if (!$this->checkCurrency($params['cart'])) {
return '';
}
$this->smarty->assign([
'this_path' => $this->_path,
'this_path_cheque' => $this->_path,
'this_path_ssl' => Tools::getShopDomainSsl(true, true).__PS_BASE_URI__.'modules/'.$this->name.'/',
]);
return $this->display(__FILE__, 'payment.tpl');
}
/**
* Displayed on advanced EU checkout
*
* @param array $params
*
* @return array
*
* @throws PrestaShopException
*/
public function hookDisplayPaymentEU($params)
{
if (!$this->active) {
return [];
}
if (!$this->checkCurrency($params['cart'])) {
return [];
}
return [
'cta_text' => $this->l('Pay by cheque'),
'logo' => Media::getMediaPath(_PS_MODULE_DIR_.$this->name.'/cheque.jpg'),
'action' => $this->context->link->getModuleLink($this->name, 'validation', [], true),
];
}
/**
* Displayed on order confirmation page
*
* @param array $params
*
* @return string
*
* @throws PrestaShopException
* @throws SmartyException
*/
public function hookPaymentReturn($params)
{
if (!$this->active) {
return '';
}
$state = $params['objOrder']->getCurrentState();
if (in_array($state, [$this->getOrderStatusId(), Configuration::get('PS_OS_OUTOFSTOCK'), Configuration::get('PS_OS_OUTOFSTOCK_UNPAID')])) {
$this->smarty->assign([
'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,
]);
if (isset($params['objOrder']->reference) && !empty($params['objOrder']->reference)) {
$this->smarty->assign('reference', $params['objOrder']->reference);
}
} else {
$this->smarty->assign('status', 'failed');
}
return $this->display(__FILE__, 'payment_return.tpl');
}
/**
* Check currency
*
* @param Cart $cart
*
* @return bool
*
* @throws PrestaShopException
*/
public function checkCurrency($cart)
{
$currencyId = (int)$cart->id_currency;
foreach (Currency::getPaymentCurrencies((int)$this->id) as $currencyModule) {
if ($currencyId === (int)$currencyModule['id_currency']) {
return true;
}
}
return false;
}
/**
* @param AdminController $controller
*
* @return string
*
* @throws PrestaShopException
* @throws SmartyException
*/
public function renderForm($controller)
{
$contactForm = [
'form' => [
'legend' => [
'title' => $this->l('Contact details'),
'icon' => 'icon-envelope',
],
'input' => [
[
'type' => 'text',
'label' => $this->l('Pay to the order of (name)'),
'name' => static::CHEQUE_NAME,
'required' => true,
],
[
'type' => 'textarea',
'label' => $this->l('Address'),
'desc' => $this->l('Address where the cheque should be sent to.'),
'name' => static::CHEQUE_ADDRESS,
'required' => true,
],
],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$settingsForm = [
'form' => [
'legend' => [
'title' => $this->l('Order status'),
'icon' => 'icon-envelope',
],
'input' => [
[
'type' => 'select',
'label' => $this->l('Order status'),
'name' => static::ORDER_STATUS,
'options' => [
'query' => OrderState::getOrderStates((int)$this->context->language->id),
'id' => 'id_order_state',
'name' => 'name',
'orderby' => 'id_order_state',
],
'class' => 'fixed-width-xxl',
],
],
'submit' => [
'title' => $this->l('Save'),
],
],
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int) Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->id = (int) Tools::getValue('id_carrier');
$helper->identifier = $this->identifier;
$helper->submit_action = 'btnSubmit';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = [
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $controller->getLanguages(),
'id_language' => $this->context->language->id,
];
return $helper->generateForm([
$settingsForm,
$contactForm
]);
}
/**
* @return array
* @throws PrestaShopException
*/
public function getConfigFieldsValues()
{
return [
static::CHEQUE_NAME => Tools::getValue(static::CHEQUE_NAME, Configuration::get(static::CHEQUE_NAME)),
static::CHEQUE_ADDRESS => Tools::getValue(static::CHEQUE_ADDRESS, Configuration::get(static::CHEQUE_ADDRESS)),
static::ORDER_STATUS => (int)Tools::getValue(static::ORDER_STATUS, $this->getOrderStatusId()),
];
}
/**
* Post validation
*/
protected function postValidation()
{
if (Tools::isSubmit('btnSubmit')) {
if (!Tools::getValue(static::CHEQUE_NAME)) {
$this->context->controller->errors[] = $this->l('The "Pay to the order of" field is required.');
} elseif (!Tools::getValue(static::CHEQUE_ADDRESS)) {
$this->context->controller->errors[] = $this->l('The "Address" field is required.');
}
}
}
/**
*
* Post process
*
* @param AdminController $controller
*
* @throws PrestaShopException
*/
protected function postProcess($controller)
{
if (Tools::isSubmit('btnSubmit')) {
Configuration::updateValue('CHEQUE_NAME', Tools::getValue('CHEQUE_NAME'));
Configuration::updateValue('CHEQUE_ADDRESS', Tools::getValue('CHEQUE_ADDRESS'));
}
$controller->confirmations[] = $this->l('Settings updated');
}
/**
* @return int
* @throws PrestaShopException
*/
public function getOrderStatusId()
{
$status = (int)Configuration::get(static::ORDER_STATUS);
if (! $status) {
$status = (int)Configuration::get('PS_OS_PAYMENT');
}
return $status;
}
}