forked from PrestaShop/ps_wirepayment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps_wirepayment.php
445 lines (398 loc) · 17.8 KB
/
ps_wirepayment.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/*
* 2007-2015 PrestaShop
*
* 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 [email protected] 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.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
if (!defined('_PS_VERSION_')) {
exit;
}
class Ps_Wirepayment extends PaymentModule
{
const FLAG_DISPLAY_PAYMENT_INVITE = 'BANK_WIRE_PAYMENT_INVITE';
protected $_html = '';
protected $_postErrors = array();
public $details;
public $owner;
public $address;
public $extra_mail_vars;
public function __construct()
{
$this->name = 'ps_wirepayment';
$this->tab = 'payments_gateways';
$this->version = '2.0.4';
$this->ps_versions_compliancy = array('min' => '1.7.1.0', 'max' => _PS_VERSION_);
$this->author = 'PrestaShop';
$this->controllers = array('payment', 'validation');
$this->is_eu_compatible = 1;
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$config = Configuration::getMultiple(array('BANK_WIRE_DETAILS', 'BANK_WIRE_OWNER', 'BANK_WIRE_ADDRESS', 'BANK_WIRE_RESERVATION_DAYS'));
if (!empty($config['BANK_WIRE_OWNER'])) {
$this->owner = $config['BANK_WIRE_OWNER'];
}
if (!empty($config['BANK_WIRE_DETAILS'])) {
$this->details = $config['BANK_WIRE_DETAILS'];
}
if (!empty($config['BANK_WIRE_ADDRESS'])) {
$this->address = $config['BANK_WIRE_ADDRESS'];
}
if (!empty($config['BANK_WIRE_RESERVATION_DAYS'])) {
$this->reservation_days = $config['BANK_WIRE_RESERVATION_DAYS'];
}
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->trans('Wire payment', array(), 'Modules.Wirepayment.Admin');
$this->description = $this->trans('Accept payments by bank transfer.', array(), 'Modules.Wirepayment.Admin');
$this->confirmUninstall = $this->trans('Are you sure about removing these details?', array(), 'Modules.Wirepayment.Admin');
if (!isset($this->owner) || !isset($this->details) || !isset($this->address)) {
$this->warning = $this->trans('Account owner and account details must be configured before using this module.', array(), 'Modules.Wirepayment.Admin');
}
if (!count(Currency::checkPaymentCurrencies($this->id))) {
$this->warning = $this->trans('No currency has been set for this module.', array(), 'Modules.Wirepayment.Admin');
}
$this->extra_mail_vars = array(
'{bankwire_owner}' => Configuration::get('BANK_WIRE_OWNER'),
'{bankwire_details}' => nl2br(Configuration::get('BANK_WIRE_DETAILS')),
'{bankwire_address}' => nl2br(Configuration::get('BANK_WIRE_ADDRESS')),
);
}
public function install()
{
Configuration::updateValue(self::FLAG_DISPLAY_PAYMENT_INVITE, true);
if (!parent::install() || !$this->registerHook('paymentReturn') || !$this->registerHook('paymentOptions')) {
return false;
}
return true;
}
public function uninstall()
{
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
if (!Configuration::deleteByName('BANK_WIRE_CUSTOM_TEXT', $lang['id_lang'])) {
return false;
}
}
if (!Configuration::deleteByName('BANK_WIRE_DETAILS')
|| !Configuration::deleteByName('BANK_WIRE_OWNER')
|| !Configuration::deleteByName('BANK_WIRE_ADDRESS')
|| !Configuration::deleteByName('BANK_WIRE_RESERVATION_DAYS')
|| !Configuration::deleteByName(self::FLAG_DISPLAY_PAYMENT_INVITE)
|| !parent::uninstall()) {
return false;
}
return true;
}
protected function _postValidation()
{
if (Tools::isSubmit('btnSubmit')) {
Configuration::updateValue(self::FLAG_DISPLAY_PAYMENT_INVITE,
Tools::getValue(self::FLAG_DISPLAY_PAYMENT_INVITE));
if (!Tools::getValue('BANK_WIRE_DETAILS')) {
$this->_postErrors[] = $this->trans('Account details are required.', array(), 'Modules.Wirepayment.Admin');
} elseif (!Tools::getValue('BANK_WIRE_OWNER')) {
$this->_postErrors[] = $this->trans('Account owner is required.', array(), "Modules.Wirepayment.Admin");
}
}
}
protected function _postProcess()
{
if (Tools::isSubmit('btnSubmit')) {
Configuration::updateValue('BANK_WIRE_DETAILS', Tools::getValue('BANK_WIRE_DETAILS'));
Configuration::updateValue('BANK_WIRE_OWNER', Tools::getValue('BANK_WIRE_OWNER'));
Configuration::updateValue('BANK_WIRE_ADDRESS', Tools::getValue('BANK_WIRE_ADDRESS'));
$custom_text = array();
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
if (Tools::getIsset('BANK_WIRE_CUSTOM_TEXT_'.$lang['id_lang'])) {
$custom_text[$lang['id_lang']] = Tools::getValue('BANK_WIRE_CUSTOM_TEXT_'.$lang['id_lang']);
}
}
Configuration::updateValue('BANK_WIRE_RESERVATION_DAYS', Tools::getValue('BANK_WIRE_RESERVATION_DAYS'));
Configuration::updateValue('BANK_WIRE_CUSTOM_TEXT', $custom_text);
}
$this->_html .= $this->displayConfirmation($this->trans('Settings updated', array(), 'Admin.Global'));
}
protected function _displayBankWire()
{
return $this->display(__FILE__, 'infos.tpl');
}
public function getContent()
{
if (Tools::isSubmit('btnSubmit')) {
$this->_postValidation();
if (!count($this->_postErrors)) {
$this->_postProcess();
} else {
foreach ($this->_postErrors as $err) {
$this->_html .= $this->displayError($err);
}
}
} else {
$this->_html .= '<br />';
}
$this->_html .= $this->_displayBankWire();
$this->_html .= $this->renderForm();
return $this->_html;
}
public function hookPaymentOptions($params)
{
if (!$this->active) {
return;
}
if (!$this->checkCurrency($params['cart'])) {
return;
}
$this->smarty->assign(
$this->getTemplateVarInfos()
);
$newOption = new PaymentOption();
$newOption->setModuleName($this->name)
->setCallToActionText($this->trans('Pay by bank wire', array(), 'Modules.Wirepayment.Shop'))
->setAction($this->context->link->getModuleLink($this->name, 'validation', array(), true))
->setAdditionalInformation($this->fetch('module:ps_wirepayment/views/templates/hook/ps_wirepayment_intro.tpl'));
$payment_options = [
$newOption,
];
return $payment_options;
}
public function hookPaymentReturn($params)
{
if (!$this->active || !Configuration::get(self::FLAG_DISPLAY_PAYMENT_INVITE)) {
return;
}
$state = $params['order']->getCurrentState();
if (
in_array(
$state,
array(
Configuration::get('PS_OS_BANKWIRE'),
Configuration::get('PS_OS_OUTOFSTOCK'),
Configuration::get('PS_OS_OUTOFSTOCK_UNPAID'),
)
)) {
$bankwireOwner = $this->owner;
if (!$bankwireOwner) {
$bankwireOwner = '___________';
}
$bankwireDetails = Tools::nl2br($this->details);
if (!$bankwireDetails) {
$bankwireDetails = '___________';
}
$bankwireAddress = Tools::nl2br($this->address);
if (!$bankwireAddress) {
$bankwireAddress = '___________';
}
$this->smarty->assign(array(
'shop_name' => $this->context->shop->name,
'total' => Tools::displayPrice(
$params['order']->getOrdersTotalPaid(),
new Currency($params['order']->id_currency),
false
),
'bankwireDetails' => $bankwireDetails,
'bankwireAddress' => $bankwireAddress,
'bankwireOwner' => $bankwireOwner,
'status' => 'ok',
'reference' => $params['order']->reference,
'contact_url' => $this->context->link->getPageLink('contact', true)
));
} else {
$this->smarty->assign(
array(
'status' => 'failed',
'contact_url' => $this->context->link->getPageLink('contact', true),
)
);
}
return $this->fetch('module:ps_wirepayment/views/templates/hook/payment_return.tpl');
}
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 function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->trans('Account details', array(), 'Modules.Wirepayment.Admin'),
'icon' => 'icon-envelope'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->trans('Account owner', array(), 'Modules.Wirepayment.Admin'),
'name' => 'BANK_WIRE_OWNER',
'required' => true
),
array(
'type' => 'textarea',
'label' => $this->trans('Account details', array(), 'Modules.Wirepayment.Admin'),
'name' => 'BANK_WIRE_DETAILS',
'desc' => $this->trans('Such as bank branch, IBAN number, BIC, etc.', array(), 'Modules.Wirepayment.Admin'),
'required' => true
),
array(
'type' => 'textarea',
'label' => $this->trans('Bank address', array(), 'Modules.Wirepayment.Admin'),
'name' => 'BANK_WIRE_ADDRESS',
'required' => true
),
),
'submit' => array(
'title' => $this->trans('Save', array(), 'Admin.Actions'),
)
),
);
$fields_form_customization = array(
'form' => array(
'legend' => array(
'title' => $this->trans('Customization', array(), 'Modules.Wirepayment.Admin'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->trans('Reservation period', array(), 'Modules.Wirepayment.Admin'),
'desc' => $this->trans('Number of days the items remain reserved', array(), 'Modules.Wirepayment.Admin'),
'name' => 'BANK_WIRE_RESERVATION_DAYS',
),
array(
'type' => 'textarea',
'label' => $this->trans('Information to the customer', array(), 'Modules.Wirepayment.Admin'),
'name' => 'BANK_WIRE_CUSTOM_TEXT',
'desc' => $this->trans('Information on the bank transfer (processing time, starting of the shipping...)', array(), 'Modules.Wirepayment.Admin'),
'lang' => true
),
array(
'type' => 'switch',
'label' => $this->trans('Display the invitation to pay in the order confirmation page', array(), 'Modules.Wirepayment.Admin'),
'name' => self::FLAG_DISPLAY_PAYMENT_INVITE,
'is_bool' => true,
'hint' => $this->trans('Your country\'s legislation may require you to send the invitation to pay by email only. Disabling the option will hide the invitation on the confirmation page.', array(), 'Modules.Wirepayment.Admin'),
'values' => array(
array(
'id' => 'active_on',
'value' => true,
'label' => $this->trans('Enabled', array(), 'Admin.Global'),
),
array(
'id' => 'active_off',
'value' => false,
'label' => $this->trans('Disabled', array(), 'Admin.Global'),
)
),
),
),
'submit' => array(
'title' => $this->trans('Save', array(), 'Admin.Actions'),
)
),
);
$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') ? : 0;
$this->fields_form = array();
$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 = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form, $fields_form_customization));
}
public function getConfigFieldsValues()
{
$custom_text = array();
$languages = Language::getLanguages(false);
foreach ($languages as $lang) {
$custom_text[$lang['id_lang']] = Tools::getValue(
'BANK_WIRE_CUSTOM_TEXT_'.$lang['id_lang'],
Configuration::get('BANK_WIRE_CUSTOM_TEXT', $lang['id_lang'])
);
}
return array(
'BANK_WIRE_DETAILS' => Tools::getValue('BANK_WIRE_DETAILS', Configuration::get('BANK_WIRE_DETAILS')),
'BANK_WIRE_OWNER' => Tools::getValue('BANK_WIRE_OWNER', Configuration::get('BANK_WIRE_OWNER')),
'BANK_WIRE_ADDRESS' => Tools::getValue('BANK_WIRE_ADDRESS', Configuration::get('BANK_WIRE_ADDRESS')),
'BANK_WIRE_RESERVATION_DAYS' => Tools::getValue('BANK_WIRE_RESERVATION_DAYS', Configuration::get('BANK_WIRE_RESERVATION_DAYS')),
'BANK_WIRE_CUSTOM_TEXT' => $custom_text,
self::FLAG_DISPLAY_PAYMENT_INVITE => Tools::getValue(self::FLAG_DISPLAY_PAYMENT_INVITE,
Configuration::get(self::FLAG_DISPLAY_PAYMENT_INVITE))
);
}
public function getTemplateVarInfos()
{
$cart = $this->context->cart;
$total = sprintf(
$this->trans('%1$s (tax incl.)', array(), 'Modules.Wirepayment.Shop'),
Tools::displayPrice($cart->getOrderTotal(true, Cart::BOTH))
);
$bankwireOwner = $this->owner;
if (!$bankwireOwner) {
$bankwireOwner = '___________';
}
$bankwireDetails = Tools::nl2br($this->details);
if (!$bankwireDetails) {
$bankwireDetails = '___________';
}
$bankwireAddress = Tools::nl2br($this->address);
if (!$bankwireAddress) {
$bankwireAddress = '___________';
}
$bankwireReservationDays = Configuration::get('BANK_WIRE_RESERVATION_DAYS');
if (false === $bankwireReservationDays) {
$bankwireReservationDays = 7;
}
$bankwireCustomText = Tools::nl2br(Configuration::get('BANK_WIRE_CUSTOM_TEXT', $this->context->language->id));
if (false === $bankwireCustomText) {
$bankwireCustomText = '';
}
return array(
'total' => $total,
'bankwireDetails' => $bankwireDetails,
'bankwireAddress' => $bankwireAddress,
'bankwireOwner' => $bankwireOwner,
'bankwireReservationDays' => (int)$bankwireReservationDays,
'bankwireCustomText' => $bankwireCustomText,
);
}
}