-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuckaroo3.php
817 lines (691 loc) · 29.9 KB
/
buckaroo3.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
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* It is available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this file
*
* @author Buckaroo.nl <[email protected]>
* @copyright Copyright (c) Buckaroo B.V.
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
require_once _PS_MODULE_DIR_ . 'buckaroo3/vendor/autoload.php';
require_once _PS_MODULE_DIR_ . 'buckaroo3/api/paymentmethods/responsefactory.php';
require_once _PS_MODULE_DIR_ . 'buckaroo3/controllers/front/common.php';
include_once _PS_MODULE_DIR_ . 'buckaroo3/library/logger.php';
use Buckaroo\BuckarooClient;
use Buckaroo\PrestaShop\Classes\Issuers\Ideal as IssuersIdeal;
use Buckaroo\PrestaShop\Classes\Issuers\PayByBank as IssuersPayByBank;
use Buckaroo\PrestaShop\Src\Config\Config;
use Buckaroo\PrestaShop\Src\Form\Modifier\ProductFormModifier;
use Buckaroo\PrestaShop\Src\Install\DatabaseTableInstaller;
use Buckaroo\PrestaShop\Src\Install\DatabaseTableUninstaller;
use Buckaroo\PrestaShop\Src\Install\IdinColumnsRemover;
use Buckaroo\PrestaShop\Src\Install\Installer;
use Buckaroo\PrestaShop\Src\Install\Uninstaller;
use Buckaroo\PrestaShop\Src\Refund\Settings as RefundSettings;
use Buckaroo\PrestaShop\Src\Repository\RawBuckarooFeeRepository;
use Buckaroo\PrestaShop\Src\Repository\RawPaymentMethodRepository;
use Buckaroo\PrestaShop\Src\Service\BuckarooIdinService;
use PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException;
class Buckaroo3 extends PaymentModule
{
public $logger;
public function __construct()
{
$this->initializeModuleInfo();
parent::__construct();
$this->initializeLogger();
$this->initializeDisplayName();
$this->checkConfiguration();
}
private function initializeModuleInfo()
{
$this->name = 'buckaroo3';
$this->tab = 'payments_gateways';
$this->version = '4.4.0';
$this->author = 'Buckaroo';
$this->need_instance = 1;
$this->bootstrap = true;
$this->module_key = '8d2a2f65a77a8021da5d5ffccc9bbd2b';
$this->ps_versions_compliancy = ['min' => '1', 'max' => _PS_VERSION_];
$this->displayName = $this->l('Buckaroo Payments') . ' (v ' . $this->version . ')';
$this->description = $this->l('Buckaroo Payment module. Compatible with PrestaShop version 1.7.x + 8.1.6');
$this->confirmUninstall = $this->l('Are you sure you want to delete Buckaroo Payments module?');
$this->tpl_folder = 'buckaroo3';
}
private function initializeLogger()
{
$this->logger = new \Logger(CoreLogger::INFO, '');
}
private function initializeDisplayName()
{
$response = ResponseFactory::getResponse();
if ($response && $response->isValid()) {
if ($response->brq_transaction_type == 'I150') {
$this->displayName = 'Group transaction';
} elseif ($response->hasSucceeded()) {
$this->displayName = $response->payment_method;
} elseif (isset($response->status) && $response->status > 0) {
$this->displayName = (new RawPaymentMethodRepository())->getPaymentMethodsLabel($response->payment_method);
} else {
$this->displayName = $this->l('Buckaroo Payments (v 4.4.0)');
}
}
}
private function checkConfiguration()
{
if (!Configuration::get('BUCKAROO_MERCHANT_KEY')
|| !Configuration::get('BUCKAROO_SECRET_KEY')
|| !Configuration::get('BUCKAROO_ORDER_STATE_DEFAULT')
|| !Configuration::get('BUCKAROO_ORDER_STATE_SUCCESS')
|| !Configuration::get('BUCKAROO_ORDER_STATE_FAILED')) {
return '';
}
}
/**
* @throws PrestaShopException
* @throws PrestaShopDatabaseException
*/
public function hookDisplayAdminOrderMainBottom($params)
{
$order = new Order($params['id_order']);
if ($order->module !== 'buckaroo3') {
return;
}
$refundProvider = $this->get('buckaroo.refund.admin.provider');
$refunds = $refundProvider->get($order);
$this->context->smarty->assign($refunds);
$buckarooFeeData = (new RawBuckarooFeeRepository())->getFeeByOrderId($order->id);
// Ensure that $buckarooFeeData is an array
if (!is_array($buckarooFeeData)) {
$buckarooFeeData = [
'buckaroo_fee_tax_excl' => 0,
'buckaroo_fee_tax_incl' => 0,
'buckaroo_fee_tax' => 0
];
} else {
$buckarooFeeData['buckaroo_fee_tax'] = $buckarooFeeData['buckaroo_fee_tax_incl'] - $buckarooFeeData['buckaroo_fee_tax_excl'];
}
$this->context->smarty->assign([
'buckaroo_fee' => $buckarooFeeData,
'currency' => new Currency($order->id_currency)
]);
// Display both templates
return $this->display(__FILE__, 'views/templates/hook/payment-fee-table.tpl').
$this->display(__FILE__, 'views/templates/hook/refund-hook.tpl');
}
/**
* @throws LocalizationException
*/
public function hookDisplayOrderConfirmation(array $params)
{
$order = isset($params['objOrder']) ? $params['objOrder'] : null;
$order = isset($params['order']) ? $params['order'] : $order;
if (!$order || !($cart = new Cart($order->id_cart))) {
return '';
}
$buckarooFeeData = (new RawBuckarooFeeRepository())->getFeeByOrderId($order->id);
if (!$buckarooFeeData) {
return '';
}
$buckarooFee = (float) $buckarooFeeData['buckaroo_fee_tax_excl'];
$taxData = $this->calculateTax($cart, $buckarooFee);
$paymentFeeLabel = Configuration::get('PAYMENT_FEE_FRONTEND_LABEL');
// Assign data to Smarty
$this->context->smarty->assign([
'orderBuckarooFee' => $this->formatPrice($taxData['feeInclTax']),
'paymentFeeLabel' => $paymentFeeLabel,
]);
// Fetch and return the template content
return $this->display(__FILE__, 'views/templates/hook/order-confirmation-fee.tpl');
}
private function calculateTax($cart, $fee)
{
$address = new Address($cart->id_address_invoice);
$taxManager = TaxManagerFactory::getManager($address, (int) Configuration::get('PS_TAX'));
$taxCalculator = $taxManager->getTaxCalculator();
$taxRate = $taxCalculator->getTotalRate();
$taxAmount = $fee * ($taxRate / 100);
$feeInclTax = $fee + $taxAmount;
return [
'taxRate' => $taxRate,
'taxAmount' => $taxAmount,
'feeInclTax' => $feeInclTax,
];
}
/**
* @throws PrestaShopException
* @throws PrestaShopDatabaseException
*/
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install()) {
$this->_errors[] = $this->l('Unable to install module');
return false;
}
if (!$this->runInstallers()) {
return false;
}
(new RefundSettings())->install();
$this->configureOrderStates();
Configuration::updateValue('PS_COOKIE_SAMESITE', 'None');
return true;
}
private function runInstallers()
{
$databaseTableInstaller = new DatabaseTableInstaller();
$coreInstaller = new Installer($this, $databaseTableInstaller);
if (!$coreInstaller->install()) {
$this->_errors = array_merge($this->_errors, $coreInstaller->getErrors());
return false;
}
return true;
}
private function configureOrderStates()
{
$states = OrderState::getOrderStates((int)Configuration::get('PS_LANG_DEFAULT'));
$currentStates = [];
foreach ($states as $state) {
$state = (object)$state;
$currentStates[$state->id_order_state] = $state->name;
}
$defaultOrderState = $this->getOrCreateDefaultOrderState($currentStates);
Configuration::updateValue('BUCKAROO_ORDER_STATE_DEFAULT', $defaultOrderState->id);
Configuration::updateValue('BUCKAROO_ORDER_STATE_SUCCESS', Configuration::get('PS_OS_PAYMENT'));
Configuration::updateValue('BUCKAROO_ORDER_STATE_FAILED', Configuration::get('PS_OS_CANCELED'));
}
private function getOrCreateDefaultOrderState($currentStates)
{
if (($state_id = array_search($this->l('Awaiting for Remote payment'), $currentStates)) === false) {
return $this->createDefaultOrderState();
} else {
$defaultOrderState = new stdClass();
$defaultOrderState->id = $state_id;
return $defaultOrderState;
}
}
private function createDefaultOrderState()
{
$defaultOrderState = new OrderState();
$defaultOrderState->name = [Configuration::get('PS_LANG_DEFAULT') => $this->l('Awaiting for Remote payment')];
$defaultOrderState->module_name = $this->name;
$defaultOrderState->send_email = 0;
$defaultOrderState->template = '';
$defaultOrderState->invoice = 0;
$defaultOrderState->color = '#FFF000';
$defaultOrderState->unremovable = false;
$defaultOrderState->logable = 0;
if ($defaultOrderState->add()) {
$this->copyLogo($defaultOrderState->id);
}
return $defaultOrderState;
}
private function copyLogo($stateId)
{
$source = dirname(__FILE__) . '/logo.gif';
$destination = dirname(__FILE__) . '/../../img/os/' . (int)$stateId . '.gif';
if (!file_exists($destination)) {
copy($source, $destination);
}
}
public function uninstall()
{
if (!$this->runUninstallers()) {
return false;
}
try {
$refundSettingsService = $this->get('buckaroo.refund.settings');
if ($refundSettingsService) {
$refundSettingsService->uninstall();
}
} catch (\Exception $e) {
$this->_errors[] = 'Failed to uninstall buckaroo.refund.settings: ' . $e->getMessage();
}
return parent::uninstall();
}
private function runUninstallers()
{
$databaseTableUninstaller = new DatabaseTableUninstaller();
$databaseIdinColumnsRemover = new IdinColumnsRemover();
$uninstall = new Uninstaller($this, $databaseTableUninstaller, $databaseIdinColumnsRemover);
if (!$uninstall->uninstall()) {
$this->_errors[] = $uninstall->getErrors();
return false;
}
return true;
}
public function hookDisplayBackOfficeHeader()
{
if (Tools::getValue('controller') == 'AdminModules' && Tools::getValue('configure') == 'buckaroo3') {
$this->context->controller->addCSS($this->_path . 'views/css/buckaroo3.vue.css', 'all');
}
$this->context->controller->addCSS($this->_path . 'views/css/buckaroo3.admin.css', 'all');
}
public function getContent()
{
$tokenManager = $this->get('security.csrf.token_manager');
$userProvider = $this->get('prestashop.user_provider');
$token = $tokenManager->getToken($userProvider->getUsername())->getValue();
$this->context->smarty->assign([
'pathApp' => $this->_path . 'views/js/buckaroo.vue.js',
'baseUrl' => $this->context->shop->getBaseURL(true),
'adminUrl' => explode('?', $this->context->link->getAdminLink(AdminDashboard::class))[0],
'token' => $token,
]);
return $this->context->smarty->fetch('module:buckaroo3/views/templates/admin/app.tpl');
}
private function isActivated()
{
$websiteKey = Configuration::get('BUCKAROO_MERCHANT_KEY');
$secretKey = Configuration::get('BUCKAROO_SECRET_KEY');
return $this->active && $this->checkKeys($websiteKey, $secretKey);
}
private function checkKeys($websiteKey, $secretKey): bool
{
if (empty($websiteKey) || empty($secretKey)) {
return false;
}
$buckarooClient = new BuckarooClient($websiteKey, $secretKey);
return $buckarooClient->confirmCredential();
}
public function hookPaymentOptions($params)
{
if (!$this->isActivated()) {
return [];
}
$cookie = new Cookie('ps');
$cart = new Cart($params['cookie']->__get('id_cart'));
$customer = new Customer($cart->id_customer);
$cookie_id_lang = (int)$cookie->id_lang;
$id_lang = $cookie_id_lang ? $cookie_id_lang : (int)(Configuration::get('PS_LANG_DEFAULT'));
$addresses = $customer->getAddresses($id_lang);
$company = '';
$vat = '';
$firstNameBilling = '';
$firstNameShipping = '';
$lastNameBilling = '';
$lastNameShipping = '';
$phone = '';
$phone_mobile = '';
foreach ($addresses as $address) {
if ($address['id_address'] == $cart->id_address_delivery) {
$phone = $address['phone'];
$phone_mobile = $address['phone_mobile'];
$firstNameShipping = $address['firstname'];
$lastNameShipping = $address['lastname'];
}
if ($address['id_address'] == $cart->id_address_invoice) {
$company = $address['company'];
$vat = $address['vat_number'];
$phone_billing = $address['phone'];
$phone_mobile_billing = $address['phone_mobile'];
$firstNameBilling = $address['firstname'];
$lastNameBilling = $address['lastname'];
}
}
$phone_afterpay_shipping = '';
if (!empty($phone_mobile)) {
$phone_afterpay_shipping = $phone_mobile;
}
if (empty($phone_afterpay_shipping) && !empty($phone)) {
$phone_afterpay_shipping = $phone;
}
$phone_afterpay_billing = '';
if (!empty($phone_mobile_billing)) {
$phone_afterpay_billing = $phone_mobile_billing;
} elseif (!empty($phone_billing)) {
$phone_afterpay_billing = $phone_billing;
}
$address_differ = 0;
if ($cart->id_address_delivery != $cart->id_address_invoice) {
if ($lastNameShipping == $lastNameBilling
&& $firstNameShipping == $firstNameBilling) {
$address_differ = 2;
} else {
$address_differ = 1;
}
}
$buckarooConfigService = $this->getBuckarooConfigService();
$buckarooPaymentService = $this->get('buckaroo.config.api.payment.service');
try {
$this->context->smarty->assign(
[
'address_differ' => $address_differ,
'this_path' => $this->_path,
'customer_gender' => $customer->id_gender,
'customer_name' => $customer->firstname . ' ' . $customer->lastname,
'customer_email' => $customer->email,
'customer_birthday' => explode('-', $customer->birthday),
'customer_company' => $company,
'customer_vat' => $vat,
'phone' => $phone,
'phone_mobile' => $phone_mobile,
'phone_afterpay_shipping' => $phone_afterpay_shipping,
'phone_afterpay_billing' => $phone_afterpay_billing,
'total' => $cart->getOrderTotal(true, 3),
'country' => Country::getIsoById(Tools::getCountry()),
'afterpay_show_coc' => $buckarooPaymentService->showAfterpayCoc($cart),
'billink_show_coc' => $buckarooPaymentService->showBillinkCoc($cart),
'idealIssuers' => (new IssuersIdeal())->get(),
'idealDisplayMode' => $buckarooConfigService->getConfigValue('ideal', 'display_type'),
'paybybankIssuers' => (new IssuersPayByBank())->get(),
'payByBankDisplayMode' => $buckarooConfigService->getConfigValue('paybybank', 'display_type'),
'methodsWithFinancialWarning' => $buckarooPaymentService->paymentMethodsWithFinancialWarning(),
'creditcardIssuers' => $buckarooConfigService->getActiveCreditCards(),
'creditCardDisplayMode' => $buckarooConfigService->getConfigValue('creditcard', 'display_type'),
'giftCardDisplayMode' => $buckarooConfigService->getConfigValue('giftcard', 'display_in_checkout'),
'in3Method' => $this->get('buckaroo.classes.issuers.capayableIn3')->getMethod(),
'showIdealIssuers' => $buckarooConfigService->getConfigValue('ideal', 'show_issuers') ?? true,
'buckaroo_idin_test' => $buckarooConfigService->getConfigValue('idin', 'mode'),
'houseNumbersAreValid' => $buckarooPaymentService->areHouseNumberValidForCountryDE($cart)
]
);
} catch (Exception $e) {
$this->logger->logError('Buckaroo3::hookPaymentOptions - ' . $e->getMessage());
}
return $buckarooPaymentService->getPaymentOptions($cart);
}
/**
* @throws PrestaShopException
* @throws PrestaShopDatabaseException
* @throws LocalizationException
*/
public function hookPaymentReturn($params)
{
if (!$this->active) {
return;
}
if (Tools::getValue('response_received') || (Tools::getValue('id_order') && Tools::getValue('success'))) {
$order = new Order(Tools::getValue('id_order'));
$price = $this->formatPrice($order->getOrdersTotalPaid());
$isGuest = $this->context->customer->is_guest || !$this->context->customer->id;
if (Tools::getValue('response_received') == 'transfer') {
$this->context->smarty->assign([
'is_guest' => $isGuest,
'order' => $order,
'price' => $price,
'message' => $this->context->cookie->HtmlText,
]);
return $this->display(__FILE__, 'payment_return_redirectsuccess.tpl');
}
$this->context->smarty->assign([
'is_guest' => $isGuest,
'order' => $order,
'price' => $price,
]);
return $this->display(__FILE__, 'payment_return_success.tpl');
}
Tools::redirect('index.php?fc=module&module=buckaroo3&controller=error');
exit;
}
public function hookDisplayHeader()
{
Media::addJsDef([
'buckarooAjaxUrl' => $this->context->link->getModuleLink('buckaroo3', 'ajax'),
'buckarooFees' => $this->getBuckarooFeeService()->getBuckarooFees(),
'paymentFeeLabel' => Configuration::get('PAYMENT_FEE_FRONTEND_LABEL'),
'buckarooMessages' => [
'validation' => [
'date' => $this->l('Please enter correct birthdate date'),
'required' => $this->l('Field is required'),
'bank' => $this->l('Please select your bank'),
'agreement' => $this->l('Please accept licence agreements'),
'iban' => $this->l('A valid IBAN is required'),
'age' => $this->l('You must be at least 18 years old'),
],
],
]);
$this->context->controller->addCSS($this->_path . 'views/css/buckaroo3.css', 'all');
$this->context->controller->addJS($this->_path . 'views/js/buckaroo.js', 'all');
}
public static function resolveStatusCode($status_code, $id_order = null)
{
switch ($status_code) {
case BuckarooAbstract::BUCKAROO_SUCCESS:
return self::isOrderBackOrder($id_order) ?
Configuration::get('PS_OS_OUTOFSTOCK_PAID') :
(Configuration::get('BUCKAROO_ORDER_STATE_SUCCESS') ?: Configuration::get('PS_OS_PAYMENT'));
case BuckarooAbstract::BUCKAROO_PENDING_PAYMENT:
return Configuration::get('BUCKAROO_ORDER_STATE_DEFAULT');
case BuckarooAbstract::BUCKAROO_CANCELED:
case BuckarooAbstract::BUCKAROO_ERROR:
case BuckarooAbstract::BUCKAROO_FAILED:
case BuckarooAbstract::BUCKAROO_INCORRECT_PAYMENT:
return Configuration::get('BUCKAROO_ORDER_STATE_FAILED') ?
Configuration::get('BUCKAROO_ORDER_STATE_FAILED') : Configuration::get('PS_OS_CANCELED');
default:
return Configuration::get('PS_OS_ERROR');
}
}
private static function isOrderBackOrder($orderId)
{
if (!Configuration::get('PS_STOCK_MANAGEMENT')) {
return false; // If stock management is disabled, no order is a backorder
}
$order = new Order($orderId);
$orderDetails = $order->getOrderDetailList();
foreach ($orderDetails as $detail) {
$orderDetail = new OrderDetail($detail['id_order_detail']);
// If any product is in stock, the order is not a backorder
if ($orderDetail->product_quantity_in_stock < 0) {
return true;
}
}
// If all products are out of stock, the order is a backorder
return false;
}
public function getBuckarooFee($payment_method)
{
$buckarooFee = $this->getBuckarooFeeService()->getBuckarooFeeValue($payment_method);
if (!$buckarooFee) {
return null;
}
// Remove any whitespace from the fee.
$buckarooFee = trim($buckarooFee);
if (strpos($buckarooFee, '%') !== false) {
$buckarooFee = str_replace('%', '', $buckarooFee);
$buckarooFee = (float)$this->payment_request->amountDebit * ((float)$buckarooFee / 100);
} else {
$buckarooFee = (float)$buckarooFee;
}
$taxRate = $this->context->cart->getAverageProductsTaxRate();
$buckarooFeeTax = $buckarooFee * $taxRate;
$buckarooFeeTaxIncl = $buckarooFee + $buckarooFeeTax;
return [
'buckaroo_fee_tax_excl' => $buckarooFee,
'buckaroo_fee_tax' => $buckarooFeeTax,
'buckaroo_fee_tax_incl' => $buckarooFeeTaxIncl,
];
}
public function hookActionEmailSendBefore($params)
{
if (!isset($params['cart']->id)) {
return true;
}
$cart = new Cart($params['cart']->id);
$orderId = Order::getOrderByCartId($cart->id);
$order = new Order($orderId);
if (!Validate::isLoadedObject($order) || $order->module !== $this->name) {
return true;
}
if ($params['template'] == 'order_conf') {
$params['templatePath'] = _PS_MODULE_DIR_ . 'buckaroo3/mails/';
}
$templatesToModify = ['order_conf'];
if (in_array($params['template'], $templatesToModify)) {
$paymentMethodLabel = $order->payment;
$buckarooFeeService = $this->getBuckarooFeeService();
$paymentMethodName = $buckarooFeeService->getPaymentMethodByLabel($paymentMethodLabel);
$buckarooFee = $this->getBuckarooFee($paymentMethodName);
// Ensure buckarooFee is an array
if (!is_array($buckarooFee)) {
$buckarooFee = [
'buckaroo_fee_tax_excl' => 0,
'buckaroo_fee_tax' => 0,
'buckaroo_fee_tax_incl' => 0,
];
}
$buckarooFeeTaxExcl = $buckarooFee['buckaroo_fee_tax_excl'];
$buckarooFeeTaxIncl = $buckarooFee['buckaroo_fee_tax_incl'];
$paymentFeeLabel = Configuration::get('PAYMENT_FEE_FRONTEND_LABEL');
$params['templateVars']['{payment_fee_label}'] = $paymentFeeLabel;
if ($buckarooFeeTaxIncl > 0) {
$params['templateVars']['{payment_fee}'] = Tools::displayPrice($buckarooFeeTaxExcl);
$params['templateVars']['{payment_fee_tax}'] = Tools::displayPrice($buckarooFee['buckaroo_fee_tax']);
$params['templateVars']['{total_paid}'] = Tools::displayPrice($order->total_paid + $buckarooFeeTaxIncl);
// Include the total tax paid, which includes the payment fee tax
$totalTaxPaid = $order->total_paid_tax_incl - $order->total_paid_tax_excl + $buckarooFee['buckaroo_fee_tax'];
$params['templateVars']['{total_tax_paid}'] = Tools::displayPrice($totalTaxPaid);
} else {
$params['templateVars']['{payment_fee}'] = Tools::displayPrice(0);
}
}
return true;
}
public function hookDisplayPDFInvoice($params)
{
if ($params['object'] instanceof OrderInvoice) {
$order = $params['object']->getOrder();
$buckarooFeeData = (new RawBuckarooFeeRepository())->getFeeByOrderId($order->id);
if (!$buckarooFeeData) {
return;
}
$buckarooFee = (float) $buckarooFeeData['buckaroo_fee_tax_excl'];
$taxData = $this->calculateTax($order, $buckarooFee);
$this->context->smarty->assign([
'payment_fee_label' => Configuration::get('PAYMENT_FEE_FRONTEND_LABEL'),
'order_buckaroo_fee' => $this->formatPrice($taxData['feeInclTax']),
'order_buckaroo_fee_tax' => $this->formatPrice($taxData['taxAmount']),
]);
return $this->context->smarty->fetch($this->getLocalPath() . 'views/templates/admin/invoice_fee.tpl');
}
}
public function isPaymentModeActive($method)
{
$isLive = (int)\Configuration::get(Config::BUCKAROO_TEST);
$configArray = $this->getBuckarooConfigService()->getConfigArrayForMethod($method);
if (!empty($configArray) && isset($configArray['mode'])) {
if ($isLive === 0) {
return $configArray['mode'] === 'test';
} elseif ($isLive === 1) {
return $configArray['mode'] === 'live';
}
}
return false;
}
public function isIdinProductBoxShow($params)
{
if (!$this->isPaymentModeActive('idin')) {
return false;
}
switch ($this->getBuckarooConfigService()->getConfigValue('idin', 'display_mode')) {
case 'product':
return $this->isProductBuckarooIdinEnabled($params['product']->id);
case 'global':
return true;
default:
return false;
}
}
private function isProductBuckarooIdinEnabled(int $productId)
{
$sql = new DbQuery();
$sql->select('buckaroo_idin');
$sql->from('bk_product_idin');
$sql->where('product_id = ' . pSQL($productId));
$buckarooIdin = Db::getInstance()->getValue($sql);
return $buckarooIdin == 1;
}
public function isIdinCheckout($cart)
{
if (!$this->isPaymentModeActive('idin')) {
return false;
}
switch ($this->getBuckarooConfigService()->getConfigValue('idin', 'display_mode')) {
case 'product':
foreach ($cart->getProducts(true) as $value) {
return $this->isProductBuckarooIdinEnabled($value['id_product']);
}
break;
case 'global':
return true;
default:
return false;
}
return false;
}
public function getBuckarooConfigService()
{
return $this->get('buckaroo.config.api.config.service');
}
public function getBuckarooFeeService()
{
return $this->get('buckaroo.config.api.fee.service');
}
public function hookDisplayProductExtraContent($params)
{
if ($this->isIdinProductBoxShow($params)) {
$this->smarty->assign([
'this_path' => _MODULE_DIR_ . $this->tpl_folder . '/',
]);
$content = $this->display(__FILE__, 'views/templates/hook/idin_box.tpl');
$productExtraContent = new PrestaShop\PrestaShop\Core\Product\ProductExtraContent();
$productExtraContent->setTitle($this->l('iDIN Info'));
$productExtraContent->setContent($content);
return [$productExtraContent];
}
}
/**
* Modify product form builder
*
* @param array $params
*
* @throws Exception
*/
public function hookActionProductFormBuilderModifier(array $params): void
{
/** @var ProductFormModifier $productFormModifier */
$productFormModifier = $this->get(ProductFormModifier::class);
$productId = (int)$params['id'];
$productFormModifier->modify($productId, $params['form_builder']);
}
public function hookActionAfterUpdateProductFormHandler(array $params)
{
$this->updateProductFormHandler($params);
}
private function updateProductFormHandler(array $params)
{
$productId = $params['form_data']['id'];
$buckarooIdin = $params['form_data']['buckaroo_idin']['buckaroo_idin'];
$buckarooIdinService = new BuckarooIdinService();
try {
if ($buckarooIdinService->checkProductIdExists($productId)) {
$buckarooIdinService->updateProductData($productId, $buckarooIdin);
} else {
$buckarooIdinService->insertProductData($productId, $buckarooIdin);
}
} catch (Exception $e) {
$this->logger->logError('Buckaroo3::updateCustomerReviewStatus - ' . $e->getMessage());
}
}
/**
* @throws LocalizationException
*/
private function formatPrice($amount): string
{
$currency = \Context::getContext()->currency;
return \Tools::getContextLocale($this->context)->formatPrice($amount, $currency->iso_code);
}
}