forked from easytransac/EasyTransac-Prestashop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easytransac.php
921 lines (803 loc) · 33.3 KB
/
easytransac.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
<?php
require __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
/*
* EasyTransac's official Prestashop payment gateway.
*/
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
if (!defined('_PS_VERSION_'))
exit;
class EasyTransac extends PaymentModule
{
/**
* Development variable.
*/
private $debugLogEnabled = false;
/**
* Module init.
* @return void
*/
public function __construct()
{
$this->name = 'easytransac';
$this->tab = 'payments_gateways';
$this->version = '3.3.0';
$this->author = 'EasyTransac';
$this->is_eu_compatible = 1;
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->bootstrap = true;
$this->currencies = true;
$this->currencies_mode = 'checkbox';
parent::__construct();
$this->displayName = $this->l('Credit card payment via EasyTransac');
$this->description = $this->l('Website payment service');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('EASYTRANSAC_API_KEY'))
$this->warning = $this->l('No API key provided');
}
/**
* EasyTransac log init with conditional configuration.
* @return void
*/
public function loginit()
{
EasyTransac\Core\Logger::getInstance()
->setActive(Configuration::get('EASYTRANSAC_DEBUG'));
EasyTransac\Core\Logger::getInstance()
->setFilePath(_PS_ROOT_DIR_ . '/modules/easytransac/logs/');
}
/**
* Module install.
* @return boolean
*/
public function install()
{
if (!parent::install() ||
!$this->registerHook('paymentOptions') ||
!$this->registerHook('actionFrontControllerSetMedia') ||
!$this->registerHook('actionOrderSlipAdd') ||
!$this->registerHook('displayAdminOrderTabContent')
){
return false;
}
include_once(_PS_MODULE_DIR_ . $this->name . '/easytransac_install.php');
// unlink(_PS_CACHE_DIR_ . 'class_index.php');
$easytransac_install = new EasyTransacInstall();
$easytransac_install->updateConfiguration();
$easytransac_install->createTables();
$this->create_easytransac_order_state();
return true;
}
/**
* Module uninstall.
* @return boolean
*/
public function uninstall()
{
if (!parent::uninstall())
return false;
include_once(_PS_MODULE_DIR_ . $this->name . '/easytransac_install.php');
$easytransac_install = new EasyTransacInstall();
$easytransac_install->deleteConfiguration();
$easytransac_install->deleteTables();
return true;
}
/**
* Settings page.
* @return string
*/
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit' . $this->name)) {
$api_key = strval(Tools::getValue('EASYTRANSAC_API_KEY'));
if (!empty($api_key)) {
Configuration::updateValue('EASYTRANSAC_API_KEY', $api_key);
}
$enable_debug = strval(Tools::getValue('EASYTRANSAC_DEBUG'));
if (empty($enable_debug)) {
Configuration::updateValue('EASYTRANSAC_DEBUG', 0);
$this->loginit();
EasyTransac\Core\Logger::getInstance()->delete();
} else {
Configuration::updateValue('EASYTRANSAC_DEBUG', 1);
}
$enable_oneclick = strval(Tools::getValue('EASYTRANSAC_ONECLICK'));
if (empty($enable_oneclick)) {
Configuration::updateValue('EASYTRANSAC_ONECLICK', 0);
} else {
Configuration::updateValue('EASYTRANSAC_ONECLICK', 1);
}
# Payments in instalments
Configuration::updateValue('EASYTRANSAC_MULTIPAY', 0);
foreach([2, 3, 4] as $a){
$key = sprintf('EASYTRANSAC_MULTIPAY%dX', $a);
$isEnabled = strval(Tools::getValue($key));
if (empty($isEnabled)) {
Configuration::updateValue($key, 0);
} else {
Configuration::updateValue($key, 1);
Configuration::updateValue('EASYTRANSAC_MULTIPAY', 1);
}
}
# END Payments in instalments
$enable_icon= strval(Tools::getValue('EASYTRANSAC_ICONDISPLAY'));
if (empty($enable_icon)) {
Configuration::updateValue('EASYTRANSAC_ICONDISPLAY', 0);
} else {
Configuration::updateValue('EASYTRANSAC_ICONDISPLAY', 1);
}
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
return $output . $this->displayForm();
}
/**
* Helper function to get whether payment in instalments for $count
* times is enabled.
* @return bool
*/
private function instalment_payment_count_enabled($count) {
if(!in_array($count, [2, 3, 4])){
return false;
}
$key = 'EASYTRANSAC_MULTIPAY'.$count.'X';
return (bool)Configuration::get($key);
}
/**
* Get the latest version number of Prestashop 1.7 EasyTransac module.
* @return string
*/
public static function getLatestVersion(){
$uri = 'https://easytransac.com/files/prestashop1.7_module_version.txt';
$streamContext = stream_context_create(
array('http'=>
array(
'timeout' => 4, //120 seconds
)
)
);
//Pass this stream context to file_get_contents via the third parameter.
try {
return file_get_contents($uri, false, $streamContext);
} catch (\Throwable $th) {
return false;
}
}
/**
* Settings form.
* @return string
*/
public function displayForm()
{
// Get default language
$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$requirements_message = '';
// Requirements message;
$openssl_version_supported = OPENSSL_VERSION_NUMBER >= 0x10001000;
$curl_activated = function_exists('curl_version');
if ($openssl_version_supported) {
$info = sprintf('%s "%s" >= 1.0.1',
$this->l('[OK] OpenSSL version'), OPENSSL_VERSION_TEXT);
$requirements_message = sprintf('<div class="alert-success" style="padding:5px;">%s</div>',
$info);
} else {
$info = sprintf('%s "%s" < 1.0.1',
$this->l('[ERROR] OpenSSL version not supported'), OPENSSL_VERSION_TEXT);
$requirements_message = '<div class="alert-danger" style="padding:5px;">' . $info . '" < 1.0.1</div>';
}
if ($curl_activated) {
$requirements_message .= sprintf('<div class="alert-success" style="padding:5px;">%s</div>',
$this->l('[OK] cURL is installed'));
} else {
$requirements_message .= sprintf('<div class="alert-danger" style="padding:5px;">%s</div>',
$this->l('[ERROR] PHP cURL extension missing'));
}
# Message about a newer version of this module.
$latestVersion = self::getLatestVersion();
if (!empty($latestVersion)) {
$isActualVersion = $latestVersion === $this->version;
if($isActualVersion){
$requirements_message .=
sprintf('<div class="alert-success" style="padding:5px;">%s</div>',
$this->l('[OK] Latest module version installed'));
} else {
$requirements_message .=
sprintf('<div class="alert-danger" style="padding:5px;">%s : %s</div>',
$this->l('[ERROR] New module is available on www.easytransac.com'), $latestVersion);
}
}
// Init Fields form array
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'free',
'label' => $this->l('Requirements'),
'desc' => $requirements_message,
'name' => 'EASYTRANSAC_REQUIREMENTS_HELP',
'size' => 20,
),
array(
'type' => 'text',
'label' => $this->l('Api Key'),
'desc' => $this->l('Your Easytransac application API Key is available in your back office in E-commerce > Applications.'),
'name' => 'EASYTRANSAC_API_KEY',
'size' => 20,
'required' => true
),
array(
'type' => 'free',
'label' => $this->l('Notification URL'),
'desc' => $this->l('Notification URL to copy paste in your EasyTransac appplication settings'),
'name' => 'EASYTRANSAC_NOTIFICATION_URL',
'size' => 20,
),
array(
'type' => 'radio',
'label' => $this->l('One Click Payment'),
'desc' => $this->l('The credit card is stored for future payments.'),
'name' => 'EASYTRANSAC_ONECLICK',
'size' => 20,
'is_bool' => true,
'values' => array(
array(
'id' => 'active2_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'radio',
'label' => $this->l('Enable payment in 2 instalments'),
'name' => 'EASYTRANSAC_MULTIPAY2X',
'size' => 20,
'is_bool' => true,
'values' => array(
array(
'id' => 'active2_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'radio',
'label' => $this->l('Enable payment in 3 instalments'),
'name' => 'EASYTRANSAC_MULTIPAY3X',
'size' => 20,
'is_bool' => true,
'values' => array(
array(
'id' => 'active2_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'radio',
'label' => $this->l('Enable payment in 4 instalments'),
'name' => 'EASYTRANSAC_MULTIPAY4X',
'size' => 20,
'is_bool' => true,
'values' => array(
array(
'id' => 'active2_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'radio',
'label' => $this->l('Icon display'),
'name' => 'EASYTRANSAC_ICONDISPLAY',
'desc' => $this->l('Display Easytransac icon at checkout'),
'size' => 20,
'is_bool' => true,
'values' => array(// $values contains the data itself.
array(
'id' => 'active2_on', // The content of the 'id' attribute of the <input> tag, and of the 'for' attribute for the <label> tag.
'value' => 1, // The content of the 'value' attribute of the <input> tag.
'label' => $this->l('Enabled') // The <label> for this radio button.
),
array(
'id' => 'active2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'radio',
'label' => $this->l('Debug mode'),
'desc' => $this->l('Save the transaction log for debugging purpose.'),
'name' => 'EASYTRANSAC_DEBUG',
'size' => 20,
'is_bool' => true,
'values' => array(// $values contains the data itself.
array(
'id' => 'active2_on', // The content of the 'id' attribute of the <input> tag, and of the 'for' attribute for the <label> tag.
'value' => 1, // The content of the 'value' attribute of the <input> tag.
'label' => $this->l('Enabled') // The <label> for this radio button.
),
array(
'id' => 'active2_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
),
'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;
// Title and toolbar
$helper->title = $this->displayName;
$helper->show_toolbar = true; // false -> remove toolbar
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
$helper->submit_action = 'submit' . $this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex . '&configure=' .
$this->name . '&save' . $this->name .
'&token=' . Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex . '&token=' .
Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['EASYTRANSAC_ONECLICK'] = Configuration::get('EASYTRANSAC_ONECLICK');
# true if one instalment payment count is enabled
$helper->fields_value['EASYTRANSAC_MULTIPAY'] = Configuration::get('EASYTRANSAC_MULTIPAY');
$helper->fields_value['EASYTRANSAC_MULTIPAY2X'] = Configuration::get('EASYTRANSAC_MULTIPAY2X');
$helper->fields_value['EASYTRANSAC_MULTIPAY3X'] = Configuration::get('EASYTRANSAC_MULTIPAY3X');
$helper->fields_value['EASYTRANSAC_MULTIPAY4X'] = Configuration::get('EASYTRANSAC_MULTIPAY4X');
$helper->fields_value['EASYTRANSAC_DEBUG'] = Configuration::get('EASYTRANSAC_DEBUG');
$helper->fields_value['EASYTRANSAC_ICONDISPLAY'] = Configuration::get('EASYTRANSAC_ICONDISPLAY');
$helper->fields_value['EASYTRANSAC_API_KEY'] = Configuration::get('EASYTRANSAC_API_KEY');
$helper->fields_value['EASYTRANSAC_NOTIFICATION_URL'] =
Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'module/easytransac/notification';
$helper->fields_value['EASYTRANSAC_HELP'] = $this->l('Visit') .
' <a target="_blank" href="https://www.easytransac.com">www.easytransac.com</a> ' .
$this->l('in order to create an account and configure your application.');
$helper->fields_value['EASYTRANSAC_REQUIREMENTS_HELP'] =
Configuration::get('EASYTRANSAC_REQUIREMENTS_HELP');
return $helper->generateForm($fields_form);
}
/**
* Payment method choice.
* @return mixed
*/
public function hookPaymentOptions($params)
{
if (!$this->active) {
return;
}
$this->loginit();
$newOption = new PaymentOption();
$newOption->setCallToActionText($this->l('Pay by credit card'))
->setAction($this->context->link->getModuleLink($this->name, 'payment'));
if(Configuration::get('EASYTRANSAC_ICONDISPLAY')){
$newOption->setLogo(_MODULE_DIR_ . 'easytransac/views/img/icon.png');
}
// EasyTransac\Core\Logger::getInstance()->write($this->context->customer);
// Adding up templates.
$buffer = [];
// Customer should have paid at least one time using Easytransac.
if (Configuration::get('EASYTRANSAC_ONECLICK') &&
$this->isCustomerKnown())
{
$buffer[] = $this->context->smarty->fetch(
'module:easytransac/views/templates/hook/oneclick_payment.tpl');
}
$cart = $this->context->cart;
$total = 100 * $cart->getOrderTotal(true, Cart::BOTH);
if (Configuration::get('EASYTRANSAC_MULTIPAY')
&& $total >= 5000)
{
if($buffer){
$buffer[] = '<br/>';
}
# Template vars.
$enabled_vars = [];
foreach ([2, 3, 4] as $count) {
$enabled_vars['enableInstallment'.$count] =
$this->instalment_payment_count_enabled($count);
}
$this->context->smarty->assign($enabled_vars);
$multi = $this->context->smarty->fetch(
'module:easytransac/views/templates/hook/multiple_payments.tpl');
$buffer[] = $multi;
}
if($buffer){
$newOption->setAdditionalInformation(implode('', $buffer));
}
return [$newOption];
}
/**
* Returns whether the customer has already paid via Easytransac.
* @return bool
*/
public function isCustomerKnown(){
$client_id = $this->context->customer->getClient_id();
return !empty($client_id);
}
/**
* Helper function to fetch a template.
*/
public function fetchTemplate($name)
{
if (version_compare(_PS_VERSION_, '1.4', '<'))
$this->context->smarty->currentTemplate = $name;
elseif (version_compare(_PS_VERSION_, '1.5', '<')) {
$views = 'views/templates/';
if (@filemtime(dirname(__FILE__) . '/' . $name))
return $this->display(__FILE__, $name);
elseif (@filemtime(dirname(__FILE__) . '/' . $views . 'hook/' . $name))
return $this->display(__FILE__, $views . 'hook/' . $name);
elseif (@filemtime(dirname(__FILE__) . '/' . $views . 'front/' . $name))
return $this->display(__FILE__, $views . 'front/' . $name);
elseif (@filemtime(dirname(__FILE__) . '/' . $views . 'admin/' . $name))
return $this->display(__FILE__, $views . 'admin/' . $name);
}
return $this->display(__FILE__, $name);
}
/**
* Make an order out of a cart.
* @return void
*/
public function validateOrder($id_cart, $id_order_state, $amount_paid,
$payment_method = 'Unknown', $message = null,
$transaction = array(), $currency_special = null,
$dont_touch_amount = false, $secure_key = false,
Shop $shop = null)
{
if (!$this->active)
{
return;
}
parent::validateOrder((int)$id_cart, (int)$id_order_state,
(float)$amount_paid, $payment_method,
$message, $transaction, $currency_special,
$dont_touch_amount, $secure_key, $shop);
}
/**
* Plugin version info.
* @return string
*/
public function get_server_info_string()
{
$curl_info_string = function_exists('curl_version') ? 'enabled' : 'not found';
$openssl_info_string = OPENSSL_VERSION_NUMBER >= 0x10001000 ? 'TLSv1.2' : 'OpenSSL version deprecated';
$https_info_string = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'S' : '';
return sprintf('Prestashop %s [cURL %s, OpenSSL %s, HTTP%s]', $this->version, $curl_info_string,
$openssl_info_string, $https_info_string);
}
/**
* Creates EasyTransac order state if not already registered.
* State for payments in instalments added.
* Prupose : plugin update from older versions.
* @return void
*/
public function create_easytransac_order_state()
{
if (!(Configuration::get('EASYTRANSAC_ID_ORDER_STATE') > 0)) {
// for sites upgrading from older version
$OrderState = new OrderState();
$OrderState->id = 'EASYTRANSAC_STATE_ID';
$OrderState->name = array_fill(0, 10, "EasyTransac payment pending");
$OrderState->send_email = 0;
$OrderState->invoice = 0;
$OrderState->color = "#ff9900";
$OrderState->unremovable = false;
$OrderState->logable = 0;
$OrderState->add();
Configuration::updateValue('EASYTRANSAC_ID_ORDER_STATE', $OrderState->id);
}
if (!(Configuration::get('EASYTRANSAC_ID_PAYMENT_INSTALLMENT_STATE') > 0)) {
// for sites upgrading from older version
$OrderState = new OrderState();
$OrderState->id = 'EASYTRANSAC_ID_PAYMENT_INSTALLMENT_STATE';
$OrderState->name = array_fill(0, 10, "EasyTransac payment in instalments");
$OrderState->send_email = 0;
$OrderState->invoice = 0;
$OrderState->color = "#8067ee";
$OrderState->unremovable = false;
$OrderState->logable = 0;
$OrderState->paid = 0;
$OrderState->add();
Configuration::updateValue('EASYTRANSAC_ID_PAYMENT_INSTALLMENT_STATE', $OrderState->id);
}
}
/**
* Helper function to get Order payment in instalment state.
* @return int
*/
public function get_split_payment_state(){
return (int)Configuration::get('EASYTRANSAC_ID_PAYMENT_INSTALLMENT_STATE');
}
/**
* Helper function to get Order payment paymend pending state.
*/
public function get_pending_payment_state(){
return (int)Configuration::get('EASYTRANSAC_ID_ORDER_STATE');
}
// Hook for Prestashop version >= 1.7.7 for a block on order page.
public function hookDisplayAdminOrderTabContent($params){
$this->loginit();
// # Display transaction's saved messages.
$items = $this->getTransactionMessages($params['id_order']);
$notice = $this->l('No transactions yet.');
$show_history = false;
$history = [];
if(!empty($params)){
foreach ($items as $item) {
$item['amount'] = number_format($item['amount']/100, 2, '.', '');
$history[] = $item;
$show_history = true;
}
if($history){
$notice = '';
}
}
$vars = [
'notice' => $notice,
'show_history' => $show_history,
'history' => $history,
];
$this->context->smarty->assign($vars);
return $this->context->smarty->fetch(
'module:easytransac/views/templates/hook/adminordertab.tpl');
}
/**
* Order transaction refund.
* @return int
*/
public function hookActionOrderSlipAdd($params){
$this->loginit();
EasyTransac\Core\Logger::getInstance()->write('Start Refund');
$api_key = Configuration::get('EASYTRANSAC_API_KEY');
EasyTransac\Core\Services::getInstance()->provideAPIKey($api_key);
EasyTransac\Core\Logger::getInstance()->write(__FUNCTION__.' '. is_object($params['order']));
EasyTransac\Core\Logger::getInstance()->write('is partialRefund '. Tools::isSubmit('partialRefund'));
EasyTransac\Core\Logger::getInstance()->write('is partialRefundProduct '.
Tools::isSubmit('partialRefundProduct'));
EasyTransac\Core\Logger::getInstance()->write('is partialRefund value '.
json_encode(Tools::getValue('partialRefundProduct')));
$products = $params['order']->getProducts(true);
foreach ($products as $key => $item) {
EasyTransac\Core\Logger::getInstance()
->write('product amount '. $item->amount);
EasyTransac\Core\Logger::getInstance()
->write('product map '. json_encode($item));
}
$items = $params['order']->getOrderSlipsCollection()->getResults();
$total_refund_amount = 0;
foreach ($items as $key => $item) {
EasyTransac\Core\Logger::getInstance()
->write('refund amount for item '. $item->amount);
$total_refund_amount += $item->amount;
}
if($total_refund_amount > 0){
$logMsg = json_encode(print_r($params['order'], true));
EasyTransac\Core\Logger::getInstance()->write($logMsg);
EasyTransac\Core\Logger::getInstance()->write('ID cart : '.$params['order']->id_cart);
if( ! ($transactionId = $this->getTransactionId($params['order']->id_cart))){
$errorMsg = 'EasyTransac exception: not Tid for cart id: '.$params['order']->id_cart;
error_log($errorMsg);
EasyTransac\Core\Logger::getInstance()->write($errorMsg);
throw new Exception ($errorMsg);
return;
}
}
EasyTransac\Core\Logger::getInstance()
->write('Transaction id found', $transactionId);
# Fixing PHP's floating point issue.
$total_to_refund = 100 * $total_refund_amount;
$total_to_refund = intval(strval($total_to_refund));
try {
$refund = (new \EasyTransac\Entities\Refund)
->setTid($transactionId)
->setAmount($total_to_refund);
$request = (new EasyTransac\Requests\PaymentRefund);
$response = $request->execute($refund);
if (empty($response)) {
EasyTransac\Core\Logger::getInstance()
->write('EasyTransac Refund', 'empty response.');
}
else if (!$response->isSuccess()) {
throw new Exception ($response->getErrorMessage());
}
else if ($response->isSuccess()) {
EasyTransac\Core\Logger::getInstance()
->write('EasyTransac Refund', 'success');
}
}
catch (Exception $exc) {
error_log('EasyTransac Refund Exception: ' . $exc->getMessage());
}
}
/**
* Return the transaction id for an order.
* @return string
*/
function getTransactionId($order_id)
{
if($this->debugLogEnabled){
$a = Db::getInstance()->ExecuteS('SELECT * FROM `' . _DB_PREFIX_
. 'easytransac_transaction`');
EasyTransac\Core\Logger::getInstance()->write('easytransac debug list all: '. json_encode($a));
}
$sql = 'SELECT external_id FROM `' . _DB_PREFIX_ . 'easytransac_transaction` '
. ' WHERE id_order = \'' . intval($order_id) . '\'';
return Db::getInstance()->getValue($sql);
}
/**
* Save the transaction id.
* @return void
*/
function setTransactionId($order_id, $transaction_id)
{
Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'easytransac_transaction` '
. ' VALUES(' . intval($order_id) . ',\'' . $transaction_id . '\')');
}
/**
* Save the transaction message.
*/
function addTransactionMessage($order_id, $transaction_id, $message, $amount=null, $status='')
{
# amount can be null.
$save_amount = 'NULL';
if($amount !== null){
$save_amount = intval($amount);
}
# message max size.
$message = strip_tags($message);
if(strlen($message) > 255){
$message = substr($message, 0, 250);
}
# status max size.
$status = strip_tags($status);
if(strlen($status) > 20){
$status = substr($status, 0, 19);
}
/** @var \Db $db */
$db = \Db::getInstance();
$db->execute('INSERT INTO `'._DB_PREFIX_.'easytransac_message` '
. ' VALUES(' . intval($order_id) . ', NOW(), \'' . $message . '\', \'' . $status . '\', \'' . $transaction_id . '\', ' . $save_amount . ')');
}
/**
* Helper function to translate a transaction's capture status.
* @return string
*/
function translateStatus($status){
if(strtolower($status) == 'captured'){
$this->debugLog('translate', $this->l('Success'));
return $this->l('Success');
}elseif(strtolower($status) == 'failed'){
$this->debugLog('translate', $this->l('Failure'));
return $this->l('Failure');
}
return $status;
}
/**
* Return the saved transaction messages for an order.
* @return array [[id_order, date, message, external_id, amount]]
*/
function getTransactionMessages($order_id)
{
/** @var array $result */
$result = Db::getInstance()->ExecuteS('SELECT * FROM `' . _DB_PREFIX_
. 'easytransac_message` WHERE id_order = '.intval($order_id));
foreach ($result as &$item){
$item['status'] = $this->translateStatus($item['status']);
}
if($this->debugLogEnabled){
$this->debugLog('easytransac debug saved messages', json_encode($result));
}
return $result;
}
/**
* Log a message in debug file.
* @return void
*/
public function debugLog($title, $data=""){
if(!is_string($data))
$data = json_encode($data);
if(!is_string($title))
$title = json_encode($title);
$message = sprintf('%s : %s', $title, $data);
EasyTransac\Core\Logger::getInstance()->write($message);
}
/**
* Helper to add a message to an order.
* @return void
*/
public function addOrderMessage($order_id, $message){
$msg = new Message();
$msg->message = strip_tags($message, '<br>');
$msg->id_order = intval($order_id);
$msg->private = 1;
$msg->add();
$this->debugLog('Message added to order '
.$order_id, $message);
}
/**
* Call payment status endpoint.
* @return false|string Returns transaction status string.
*/
public function fetchStatus($order_id){
$this->debugLog('PaymentStatus status', 'in fetch function');
try
{
EasyTransac\Core\Services::getInstance()
->provideAPIKey(Configuration::get('EASYTRANSAC_API_KEY'));
$entity = (new EasyTransac\Entities\PaymentStatus())
->setOrderId($order_id);
$request = new EasyTransac\Requests\PaymentStatus();
$response = $request->execute($entity);
if (!$response){
throw new Exception('empty response');
}
if($response->getErrorMessage()){
throw new Exception($response->getErrorMessage());
}
}
catch (Exception $exc)
{
$this->debugLog('Fetch PaymentStatus error', $exc->getMessage());
error_log('EasyTransac error: ' . $exc->getMessage());
return false;
}
$status = $response->getContent();
$this->debugLog('PaymentStatus status', $status->getStatus());
return $status->getStatus();
}
/**
* Enable Prestashop v1.7.8 new translation system.
*/
public function isUsingNewTranslationSystem()
{
return false;
}
}