Skip to content

Commit

Permalink
Merge pull request #203 from Invertus/SV-37/logging-controllers
Browse files Browse the repository at this point in the history
SV-37 Added logging to front controllers
  • Loading branch information
MarijusCoding authored Oct 8, 2024
2 parents 848d702 + 24aa0a1 commit e2eeea3
Show file tree
Hide file tree
Showing 37 changed files with 983 additions and 96 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"phpunit/phpunit": "*",
"behat/behat": "*",
"symfony/translation": "*",
"prestashop/php-dev-tools": "^3.16",
"invertus/knapsack": "^10.0"
"prestashop/php-dev-tools": "^3.16"
},
"scripts": {
"test-integration": "./vendor/bin/phpunit --configuration ./tests/Integration/phpunit.xml",
Expand Down
57 changes: 32 additions & 25 deletions controllers/admin/AdminSaferPayOfficialLogsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
*@license SIX Payment Services
*/

use Invertus\SaferPay\Adapter\LegacyContext;
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\Saferpay\Context\GlobalShopContext;
use Invertus\SaferPay\Controller\AbstractAdminSaferPayController;
use Invertus\SaferPay\Enum\PermissionType;
use Invertus\SaferPay\Logger\Formatter\LogFormatter;
use Invertus\SaferPay\Logger\LoggerInterface;
use Invertus\SaferPay\Repository\SaferPayLogRepository;
use Invertus\SaferPay\Utility\ExceptionUtility;
use Invertus\SaferPay\Utility\VersionUtility;
use Invertus\SaferPay\Logger\Logger;
use Invertus\SaferPay\Adapter\Tools;

if (!defined('_PS_VERSION_')) {
exit;
Expand Down Expand Up @@ -150,7 +156,8 @@ public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);

$context = $this->module->getService(\Invertus\SaferPay\Adapter\LegacyContext::class);
/** @var LegacyContext $context */
$context = $this->module->getService(LegacyContext::class);

Media::addJsDef([
'saferpayofficial' => [
Expand Down Expand Up @@ -239,19 +246,19 @@ public function displayAjaxGetLog()
return;
}

/** @var \Invertus\SaferPay\Adapter\Tools $tools */
$tools = $this->module->getService(\Invertus\SaferPay\Adapter\Tools::class);
/** @var Invertus\SaferPay\Adapter\Tools $tools */
$tools = $this->module->getService(Tools::class);

/** @var \Invertus\SaferPay\Repository\SaferPayLogRepository $logRepository */
$logRepository = $this->module->getService(\Invertus\SaferPay\Repository\SaferPayLogRepository::class);
/** @var Invertus\SaferPay\Repository\SaferPayLogRepository $logRepository */
$logRepository = $this->module->getService(SaferPayLogRepository::class);

/** @var \Invertus\SaferPay\Context\GlobalShopContext $shopContext */
$globalShopContext = $this->module->getService(\Invertus\SaferPay\Context\GlobalShopContext::class);
/** @var Invertus\SaferPay\Context\GlobalShopContext $shopContext */
$globalShopContext = $this->module->getService(GlobalShopContext::class);

$logId = $tools->getValueAsInt('log_id');

// /** @var LoggerInterface $logger */
// $logger = $this->module->getService(LoggerInterface::class);
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

try {
/** @var \SaferPayLog|null $log */
Expand All @@ -260,13 +267,13 @@ public function displayAjaxGetLog()
'id_shop' => $globalShopContext->getShopId(),
]);
} catch (Exception $exception) {
// $logger->error('Failed to find log', [
// 'context' => [
// 'id_log' => $logId,
// 'id_shop' => $globalShopContext->getShopId(),
// ],
// 'exceptions' => ExceptionUtility::getExceptions($exception),
// ]);
$logger->error($exception->getMessage(), [
'context' => [
'id_log' => $logId,
'id_shop' => $globalShopContext->getShopId(),
],
'exceptions' => ExceptionUtility::getExceptions($exception),
]);

$this->ajaxResponse(json_encode([
'error' => true,
Expand All @@ -275,13 +282,13 @@ public function displayAjaxGetLog()
}

if (!isset($log)) {
// $logger->error('No log information found.', [
// 'context' => [
// 'id_log' => $logId,
// 'id_shop' => $globalShopContext->getShopId(),
// ],
// 'exceptions' => [],
// ]);
$logger->error('No log information found.', [
'context' => [
'id_log' => $logId,
'id_shop' => $globalShopContext->getShopId(),
],
'exceptions' => [],
]);

$this->ajaxRender(json_encode([
'error' => true,
Expand Down Expand Up @@ -316,8 +323,8 @@ public function processExport($textDelimiter = '"')
/** @var Configuration $configuration */
$configuration = $this->module->getService(Configuration::class);

/** @var \Invertus\SaferPay\Adapter\LegacyContext $context */
$context = $this->module->getService(\Invertus\SaferPay\Adapter\LegacyContext::class);
/** @var LegacyContext $context */
$context = $this->module->getService(LegacyContext::class);

$storeInfo = [
'PrestaShop Version' => _PS_VERSION_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __construct()
public function setMedia($isNewTheme = false)
{
parent::setMedia($isNewTheme);

$this->addCSS("{$this->module->getPathUri()}views/css/admin/payment_method.css");
$this->addJS("{$this->module->getPathUri()}views/js/admin/chosen_countries.js");
$this->addJS("{$this->module->getPathUri()}views/js/admin/payment_method_all.js");
Expand Down
21 changes: 21 additions & 0 deletions controllers/front/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
use Invertus\SaferPay\Controller\Front\CheckoutController;
use Invertus\SaferPay\Core\Payment\DTO\CheckoutData;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Logger\LoggerInterface;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Utility\ExceptionUtility;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -40,6 +42,11 @@ class SaferPayOfficialAjaxModuleFrontController extends ModuleFrontController

public function postProcess()
{
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

switch (Tools::getValue('action')) {
case 'submitHostedFields':
$this->submitHostedFields();
Expand Down Expand Up @@ -75,6 +82,10 @@ protected function processGetStatus()
]));
}

/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);
$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

$this->ajaxDie(json_encode([
'saferpayOrder' => json_encode($saferPayOrder),
'isFinished' => $saferPayOrder->authorized || $saferPayOrder->captured || $saferPayOrder->pending,
Expand Down Expand Up @@ -123,6 +134,9 @@ private function getSuccessControllerName($isBusinessLicence, $fieldToken)

private function submitHostedFields()
{
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

try {
if (Order::getOrderByCartId($this->context->cart->id)) {
$this->ajaxDie(json_encode([
Expand Down Expand Up @@ -151,11 +165,18 @@ private function submitHostedFields()
$redirectUrl = $this->getRedirectionToControllerUrl('successHosted');
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

$this->ajaxDie(json_encode([
'error' => false,
'url' => $redirectUrl,
]));
} catch (Exception $e) {
$logger->error($e->getMessage(), [
'context' => [],
'exceptions' => ExceptionUtility::getExceptions($e),
]);

$this->ajaxDie(json_encode([
'error' => true,
'message' => $e->getMessage(),
Expand Down
10 changes: 10 additions & 0 deletions controllers/front/creditCards.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Logger\LoggerInterface;
use Invertus\SaferPay\Repository\SaferPayCardAliasRepository;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -54,6 +55,7 @@ public function display()
private function initCardList()
{
$customerId = $this->context->customer->id;

/** @var SaferPayCardAliasRepository $cardAliasRep */
$cardAliasRep = $this->module->getService(SaferPayCardAliasRepository::class);
$savedCustomerCards = $cardAliasRep->getSavedCardsByCustomerId($customerId);
Expand Down Expand Up @@ -83,6 +85,11 @@ private function initCardList()

public function postProcess()
{
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$selectedCard = Tools::getValue('saved_card_id');
if ($selectedCard) {
$cardAlias = new SaferPayCardAlias($selectedCard);
Expand All @@ -92,6 +99,9 @@ public function postProcess()
}
$this->errors[] = $this->module->l('Failed to removed credit card', self::FILENAME);
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

parent::postProcess();
}

Expand Down
9 changes: 9 additions & 0 deletions controllers/front/creditCards16.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Logger\LoggerInterface;
use Invertus\SaferPay\Repository\SaferPayCardAliasRepository;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -91,6 +92,11 @@ private function initCardList()

public function postProcess()
{
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$selectedCard = Tools::getValue('saved_card_id');
if ($selectedCard) {
$cardAlias = new SaferPayCardAlias($selectedCard);
Expand All @@ -100,6 +106,9 @@ public function postProcess()
}
$this->errors[] = $this->module->l('Failed to removed credit card', self::FILENAME);
}

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

parent::postProcess();
}

Expand Down
8 changes: 8 additions & 0 deletions controllers/front/fail.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Service\CartDuplicationService;
use Invertus\SaferPay\Logger\LoggerInterface;
use PrestaShop\PrestaShop\Adapter\Order\OrderPresenter;

if (!defined('_PS_VERSION_')) {
Expand Down Expand Up @@ -93,13 +94,20 @@ public function initContent()
{
parent::initContent();

/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$orderLink = $this->context->link->getPageLink(
'order',
true,
null
);
$this->warning[] = $this->module->l('We couldn\'t authorize your payment. Please try again.', self::FILENAME);

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

if (!SaferPayConfig::isVersion17()) {
$this->redirectWithNotifications($orderLink);
}
Expand Down
10 changes: 10 additions & 0 deletions controllers/front/failIFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Enum\ControllerName;
use Invertus\SaferPay\Logger\LoggerInterface;

if (!defined('_PS_VERSION_')) {
exit;
Expand All @@ -48,22 +49,31 @@ public function initContent()
{
parent::initContent();

/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$orderLink = $this->context->link->getPageLink(
'order',
true,
null
);

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

if (SaferPayConfig::isVersion17()) {
$this->setTemplate(SaferPayConfig::SAFERPAY_TEMPLATE_LOCATION . '/front/loading.tpl');
return;
}

$this->context->smarty->assign([
'cssUrl' => "{$this->module->getPathUri()}views/css/front/loading.css",
'jsUrl' => "{$this->module->getPathUri()}views/js/front/saferpay_iframe.js",
'redirectUrl' => $orderLink,
]);
$this->setTemplate('loading_16.tpl');

}

public function setMedia()
Expand Down
8 changes: 8 additions & 0 deletions controllers/front/failValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

use Invertus\SaferPay\Controller\AbstractSaferPayController;
use Invertus\SaferPay\Logger\LoggerInterface;
use Invertus\SaferPay\Repository\SaferPayOrderRepository;
use Invertus\SaferPay\Service\CartDuplicationService;

Expand All @@ -35,6 +36,11 @@ class SaferPayOfficialFailValidationModuleFrontController extends AbstractSaferP

public function postProcess()
{
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILE_NAME));

$cartId = Tools::getValue('cartId');
$orderId = Tools::getValue('orderId');
$secureKey = Tools::getValue('secureKey');
Expand Down Expand Up @@ -86,6 +92,8 @@ public function postProcess()
true
);

$logger->debug(sprintf('%s - Controller action ended', self::FILE_NAME));

Tools::redirect($failUrl);
}
}
9 changes: 9 additions & 0 deletions controllers/front/hostedIframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

use Invertus\SaferPay\Config\SaferPayConfig;
use Invertus\SaferPay\Logger\LoggerInterface;
use PrestaShop\PrestaShop\Core\Checkout\TermsAndConditions;

if (!defined('_PS_VERSION_')) {
Expand All @@ -34,6 +35,11 @@ class SaferPayOfficialHostedIframeModuleFrontController extends ModuleFrontContr

public function initContent()
{
/** @var LoggerInterface $logger */
$logger = $this->module->getService(LoggerInterface::class);

$logger->debug(sprintf('%s - Controller called', self::FILENAME));

parent::initContent();

$paymentMethod = Tools::getValue('saved_card_method');
Expand All @@ -49,6 +55,8 @@ public function initContent()
'saferpay_selected_card' => $selectedCard,
]);

$logger->debug(sprintf('%s - Controller action ended', self::FILENAME));

if (SaferPayConfig::isVersion17()) {
$this->setTemplate(
SaferPayConfig::SAFERPAY_HOSTED_TEMPLATE_LOCATION .
Expand All @@ -63,6 +71,7 @@ public function initContent()
'_16.tpl'
);
}

}

public function setMedia()
Expand Down
Loading

0 comments on commit e2eeea3

Please sign in to comment.