-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomfino.php
More file actions
472 lines (394 loc) · 14.9 KB
/
Copy pathcomfino.php
File metadata and controls
472 lines (394 loc) · 14.9 KB
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
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* 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 license@prestashop.com 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 https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
if (!defined('_PS_VERSION_')) {
exit;
}
if (!defined('COMFINO_PS_17')) {
define('COMFINO_PS_17', version_compare(_PS_VERSION_, '1.7', '>='));
}
if (!defined('COMFINO_MODULE_NAME')) {
define('COMFINO_MODULE_NAME', 'comfino');
}
if (!defined('COMFINO_VERSION')) {
define('COMFINO_VERSION', '4.2.9');
}
if (!defined('COMFINO_BUILD_TS')) {
define('COMFINO_BUILD_TS', 1773836935);
}
if (!defined('WIDGET_INIT_SCRIPT_HASH')) {
define('WIDGET_INIT_SCRIPT_HASH', '0603f4e0904fd65e2aef1aded0c57c40');
}
if (!defined('WIDGET_INIT_SCRIPT_LAST_HASH')) {
define('WIDGET_INIT_SCRIPT_LAST_HASH', 'c0af7eac44e1da646156fb12f2b7dbd7');
}
/* Notice: source code of this script MUST be compatible with PHP 5.6 syntax. */
class Comfino extends PaymentModule
{
const MIN_PHP_VERSION_ID = 70100;
const MIN_PHP_VERSION = '7.1.0';
public function __construct()
{
$this->name = 'comfino';
$this->tab = 'payments_gateways';
$this->version = '4.2.9';
$this->author = 'Comfino';
$this->module_key = '3d3e14c65281e816da083e34491d5a7f';
$this->bootstrap = true;
$this->need_instance = 0;
$this->ps_versions_compliancy = ['min' => '1.6.1.11', 'max' => _PS_VERSION_];
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$this->controllers = [
'cacheinvalidate',
'configuration',
'error',
'payment',
'paymentstate',
'paywall',
'paywallitemdetails',
'script',
'transactionstatus',
'updatedismiss',
];
parent::__construct();
$this->displayName = $this->l('Comfino payments');
$this->confirmUninstall = $this->l('Are you sure to uninstall Comfino payments?');
$this->description = $this->l(
'Comfino is an innovative payment method for customers of e-commerce stores! ' .
'These are installment payments, deferred (buy now, pay later) and corporate ' .
'payments available on one platform with the help of quick integration. Grow your business with Comfino!'
);
if (!$this->checkEnvironment(true)) {
$this->description .= (' ' . $this->_errors[count($this->_errors) - 1]);
return;
}
if (is_readable(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
} else {
$errorMessage = 'File ' . __DIR__ . '/vendor/autoload.php is not readable.';
$this->description .= " $errorMessage";
$this->_errors[] = $errorMessage;
return;
}
try {
// Initialize Comfino plugin.
Comfino\Main::init();
} catch (Exception $e) {
if (!preg_match('/^(Class|Interface|Trait) [\'"][^\'"]+[\'"] not found$/', $e->getMessage())) {
$this->_errors[] = $e->getMessage();
}
}
}
/**
* @return bool
*/
public function install()
{
if (!$this->checkEnvironment(true) || !parent::install()) {
return false;
}
return Comfino\Main::install($this);
}
/**
* @return bool
*/
public function uninstall()
{
return Comfino\Main::uninstall($this) && parent::uninstall();
}
/**
* Renders configuration form.
*
* @return string
*/
public function getContent()
{
if (!$this->checkEnvironment(true)) {
return
base64_decode('PHAgc3R5bGU9ImZvbnQtd2VpZ2h0OiBib2xkOyBjb2xvcjogcmVkIj4=') .
$this->_errors[count($this->_errors) - 1] .
base64_decode('PC9wPg==');
}
return Comfino\Main::getContent();
}
/**
* Renders Comfino paywall iframe at payment methods list compatible with PrestaShop 1.6.
*
* @return string
*/
public function hookPayment(array $params)
{
return Comfino\Main::renderPaywallIframe($this, $params);
}
/**
* Renders Comfino paywall iframe at payment methods list compatible with PrestaShop 1.7+.
*
* @return PrestaShop\PrestaShop\Core\Payment\PaymentOption[]
*/
public function hookPaymentOptions(array $params)
{
return Comfino\Main::renderPaywallIframe($this, $params);
}
/**
* @return string
*
* @throws PrestaShop\PrestaShop\Core\Localization\Exception\LocalizationException
*/
public function hookPaymentReturn(array $params)
{
return Comfino\Main::processFinishedPaymentTransaction($this, $params);
}
/**
* Renders settings form in administration panel.
*
* @return string
*/
public function hookDisplayBackofficeComfinoForm(array $params)
{
return Comfino\View\FormManager::getSettingsForm($this, $params);
}
/**
* Order status update event handler.
*
* @return void
*/
public function hookActionOrderStatusPostUpdate(array $params)
{
Comfino\Order\ShopStatusManager::orderStatusUpdateEventHandler($params);
}
/**
* Page header section renderer for cart and product pages.
*
* @return void
*/
public function hookHeader()
{
if (!COMFINO_PS_17) {
// Handle notifications from redirectWithNotificationsPs16() for PrestaShop 1.6.
$this->displayNotificationsPs16();
}
if (empty($controller = $this->detectController())) {
return;
}
if (($controller === 'product') && Comfino\Configuration\ConfigManager::isWidgetEnabled()) {
// Widget initialization script
$product = $this->context->controller->getProduct();
if (!Validate::isLoadedObject($product)) {
return;
}
$allowedProductTypes = Comfino\Configuration\SettingsManager::getAllowedProductTypes(
Comfino\FinancialProduct\ProductTypesListTypeEnum::LIST_TYPE_WIDGET,
Comfino\Order\OrderManager::getShopCartFromProduct($product)
);
if ($allowedProductTypes === []) {
// Filters active - all product types disabled.
Comfino\DebugLogger::logEvent('[WIDGET]', 'Filters active - all product types disabled.');
return;
}
Comfino\Main::addScriptLink(
'comfino-widget',
Comfino\Api\ApiService::getControllerUrl('script', ['product_id' => $product->id]),
'bottom',
'defer'
);
} elseif (in_array($controller, ['cart', 'order', 'checkout'])) {
$iframeRenderer = Comfino\View\FrontendManager::getPaywallIframeRenderer();
$styles = Comfino\View\FrontendManager::registerExternalStyles($iframeRenderer->getStyles());
$scripts = Comfino\View\FrontendManager::registerExternalScripts($iframeRenderer->getScripts());
foreach ($scripts as $scriptId => $scriptUrl) {
Comfino\Main::addScriptLink($scriptId, $scriptUrl, 'head');
}
Comfino\Main::addScriptLink(
'comfino-paywall-init',
Comfino\View\FrontendManager::getLocalScriptUrl('paywall-init.js'),
'head'
);
foreach ($styles as $styleId => $styleUrl) {
Comfino\Main::addStyleLink($styleId, $styleUrl);
}
}
}
/**
* Page header script/style section renderer for backoffice admin pages.
*
* @return void
*/
public function hookActionAdminControllerSetMedia(array $params)
{
$this->context->controller->addJS(_MODULE_DIR_ . "$this->name/views/js/admin/tree.min.js");
// Check for plugin updates once per day.
$this->checkGithubVersion();
}
/**
* Renders admin header content including update notices.
*
* @return string
*/
public function hookDisplayBackOfficeHeader(array $params)
{
return Comfino\View\FrontendManager::displayGithubVersionNotice($this);
}
/**
* @param bool $useTranslations
*
* @return bool
*/
public function checkEnvironment($useTranslations = false)
{
if (PHP_VERSION_ID < self::MIN_PHP_VERSION_ID) {
$errorMessage = 'The Comfino module could not be installed. ' .
'The minimum PHP version required for Comfino is %s. You are running %s.';
if ($useTranslations) {
$errorMessage = $this->l($errorMessage);
}
$errorMessage = sprintf($errorMessage, self::MIN_PHP_VERSION, PHP_VERSION);
if (!in_array($errorMessage, $this->_errors, true)) {
$this->_errors[] = $errorMessage;
}
return false;
}
if (!extension_loaded('curl')) {
$errorMessage = 'The Comfino module could not be installed. It requires PHP cURL extension which is not ' .
'installed. More details: https://www.php.net/manual/en/book.curl.php';
if ($useTranslations) {
$errorMessage = $this->l($errorMessage);
}
if (!in_array($errorMessage, $this->_errors, true)) {
$this->_errors[] = $errorMessage;
}
return false;
}
return true;
}
/**
* Detects current controller across all PrestaShop versions (1.6.1.11 - 9.0+).
*
* @return string Controller identifier (product, cart, order, checkout) or empty string.
*/
private function detectController()
{
$controller = '';
// PS 1.7+
if (method_exists($this->context->controller, 'getPageName')
&& !empty($pageName = $this->context->controller->getPageName())
) {
$controller = Tools::strtolower($pageName);
}
// PS 1.7+
if (empty($controller) && property_exists($this->context->controller, 'page_name')
&& !empty($pageName = $this->context->controller->page_name)
) {
$controller = Tools::strtolower($pageName);
}
// PS 1.6+
if (empty($controller) && property_exists($this->context->controller, 'php_self')
&& !empty($phpSelf = $this->context->controller->php_self)
) {
$controller = Tools::strtolower($phpSelf);
}
if (empty($controller)) {
$controllerName = str_ireplace(['ModuleFront', 'Controller'], '', get_class($this->context->controller));
$controllerName = Tools::strtolower(trim($controllerName));
if (!empty($controllerName)) {
$controller = $controllerName;
}
}
// Handle checkout variations (checkout, supercheckout, onepagecheckout, etc.)
if (strpos($controller, 'checkout') !== false) {
return 'checkout';
}
if (stripos($controller, 'order') === 0) {
// Check if it's a specific order-related page that should not trigger paywall.
if (in_array(
$controller,
[
'orderconfirmation', 'orderdetail', 'orderhistory', 'orderfollowup', 'orderreturn', 'orderslip',
'order-confirmation', 'order-detail', 'order-opc'
],
true
)) {
return '';
}
return 'order';
}
// Return only known controllers for security and performance.
if (in_array($controller, ['product', 'cart'], true)) {
return $controller;
}
// Unknown or irrelevant controller.
return '';
}
/**
* Check for available GitHub updates (information only in PrestaShop, full updates available).
* Checks once per day and caches the result.
*
* @return void
*/
private function checkGithubVersion()
{
if (!class_exists('Comfino\Update\UpdateManager')) {
return;
}
// Check if we've already checked today.
$lastCheckTime = Configuration::get('COMFINO_GITHUB_VERSION_CHECK_TIME');
if ($lastCheckTime && (time() - (int) $lastCheckTime) < 86400) {
// Checked within last 24 hours - but bypass if the running version changed (e.g. after an update).
if (is_array($cachedInfo = json_decode(Configuration::get('COMFINO_GITHUB_VERSION_INFO'), true))
&& (isset($cachedInfo['current_version']) ? $cachedInfo['current_version'] : '') === COMFINO_VERSION
) {
return;
}
// Version changed since last cache - force refresh.
Comfino\Update\UpdateManager::forceCheckForUpdates();
}
$updateInfo = Comfino\Update\UpdateManager::checkForUpdates();
// Store the check time and update info.
Configuration::updateValue('COMFINO_GITHUB_VERSION_CHECK_TIME', time());
Configuration::updateValue('COMFINO_GITHUB_VERSION_INFO', json_encode($updateInfo));
}
/**
* Displays notifications stored in session for PrestaShop 1.6.
* Retrieves error messages stored by redirectWithNotificationsPs16() and adds them to the controller.
*
* @return void
*/
private function displayNotificationsPs16()
{
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['comfino_notifications'])) {
$notifications = json_decode($_SESSION['comfino_notifications'], true);
if (is_array($notifications) && isset($notifications['errors']) && is_array($notifications['errors'])) {
foreach ($notifications['errors'] as $error) {
$this->context->controller->errors[] = $error;
}
}
// Clear the notifications after displaying.
unset($_SESSION['comfino_notifications']);
}
}
}