Skip to content

Commit

Permalink
PayPal-Log consider Shop-ErrorLogLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
mariolorenz committed Dec 9, 2023
1 parent cba8a08 commit 7db3440
Show file tree
Hide file tree
Showing 17 changed files with 235 additions and 71 deletions.
24 changes: 20 additions & 4 deletions src/Controller/Admin/PayPalConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ protected function checkEligibility(): void

/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error("Error on checkEligibility", [$exception]);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error("Error on checkEligibility", [$exception]);
}
}
}

Expand Down Expand Up @@ -346,7 +350,11 @@ public function autoConfigurationFromCallback()
PayPalSession::storeOnboardingPayload($requestReader->getRawPost());
} catch (\Exception $exception) {
$logger = $this->getServiceFromContainer(LoggerInterface::class);
$logger->error($exception->getMessage(), [$exception]);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage(), [$exception]);
}
}

$result = [];
Expand Down Expand Up @@ -391,7 +399,11 @@ protected function autoConfiguration(): array
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error($exception->getMessage(), [$exception]);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage(), [$exception]);
}
}
return $credentials;
}
Expand All @@ -412,7 +424,11 @@ protected function registerWebhooks(): string
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error($exception->getMessage(), [$exception]);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage(), [$exception]);
}
}

return $webhookId;
Expand Down
13 changes: 11 additions & 2 deletions src/Controller/Admin/PayPalOrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OxidEsales\Eshop\Application\Model\Order;
use OxidEsales\Eshop\Core\Exception\StandardException;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Core\Constants;
use OxidSolutionCatalysts\PayPalApi\Exception\ApiException;
use OxidSolutionCatalysts\PayPalApi\Model\Orders\Capture;
Expand Down Expand Up @@ -97,7 +98,11 @@ public function executeFunction($functionName)

/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error($exception->getMessage());
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage());
}
}
}

Expand Down Expand Up @@ -160,7 +165,11 @@ public function render()
$this->addTplParam('error', $lang->translateString('OSC_PAYPAL_ERROR_' . $exception->getErrorIssue()));
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error($exception->getMessage());
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage());
}
}
} elseif (
$order->getFieldData('oxpaymenttype') == $this->payPalPlusPaymentType &&
Expand Down
42 changes: 31 additions & 11 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OxidEsales\Eshop\Core\DisplayError;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Core\Constants;
use OxidSolutionCatalysts\PayPal\Core\PayPalDefinitions;
use OxidEsales\Eshop\Application\Model\Order as EshopModelOrder;
Expand Down Expand Up @@ -147,7 +148,11 @@ public function createAcdcOrder(): void
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error($exception->getMessage(), [$exception]);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage(), [$exception]);
}
$this->outputJson(['acdcerror' => 'failed to execute shop order']);
return;
}
Expand Down Expand Up @@ -183,12 +188,17 @@ public function captureAcdcOrder(): void
$sessionAcdcOrderId = (string) PayPalSession::getCheckoutOrderId();
$acdcStatus = Registry::getSession()->getVariable(Constants::SESSION_ACDC_PAYPALORDER_STATUS);

/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);

if (
'COMPLETED' === $acdcStatus &&
$sessionOrderId &&
$sessionAcdcOrderId
) {
$logger->debug('captureAcdcOrder already COMPLETED for PayPal Order id ' . $sessionAcdcOrderId);
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug('captureAcdcOrder already COMPLETED for PayPal Order id ' . $sessionAcdcOrderId);
}

$result = [
'location' => [
Expand Down Expand Up @@ -234,7 +244,9 @@ public function captureAcdcOrder(): void
//track status in session
Registry::getSession()->setVariable(Constants::SESSION_ACDC_PAYPALORDER_STATUS, $response->status);
} catch (\Exception $exception) {
$logger->debug($exception->getMessage(), [$exception]);
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug($exception->getMessage(), [$exception]);
}
$this->getServiceFromContainer(PaymentService::class)->removeTemporaryOrder();
}

Expand Down Expand Up @@ -275,10 +287,14 @@ public function finalizepaypalsession(): string
} catch (PayPalException $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->debug(
'PayPal Checkout error during order finalization ' . $exception->getMessage(),
[$exception]
);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug(
'PayPal Checkout error during order finalization ' . $exception->getMessage(),
[$exception]
);
}
$this->cancelpaypalsession('cannot finalize order');
return 'payment?payerror=2';
}
Expand All @@ -301,10 +317,14 @@ public function finalizeacdc(): string
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error(
'failure during finalizeOrderAfterExternalPayment',
[$exception]
);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error(
'failure during finalizeOrderAfterExternalPayment',
[$exception]
);
}
$this->cancelpaypalsession('cannot finalize order');
$goNext = 'payment?payerror=2';
}
Expand Down
6 changes: 5 additions & 1 deletion src/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public function approveOrder()
} catch (Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error("Error on order capture call.", [$exception]);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error("Error on order capture call.", [$exception]);
}
}

if (!$this->getUser()) {
Expand Down
13 changes: 10 additions & 3 deletions src/Controller/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OxidEsales\Eshop\Application\Component\Widget\WidgetController;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Core\RequestReader;
use OxidSolutionCatalysts\PayPal\Core\Webhook\EventVerifier;
use OxidSolutionCatalysts\PayPal\Core\Webhook\EventDispatcher;
Expand All @@ -33,19 +34,25 @@ public function init()

/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);

try {
$requestReader = new RequestReader();
$verificationService = Registry::get(EventVerifier::class);
$dispatcher = Registry::get(EventDispatcher::class);

$logger->debug('PayPal Webhook request ' . $requestReader->getRawPost());
$logger->debug('PayPal Webhook headers ' . serialize($requestReader->getHeaders()));
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug('PayPal Webhook request ' . $requestReader->getRawPost());
$logger->debug('PayPal Webhook headers ' . serialize($requestReader->getHeaders()));
}

$webhookRequestHandler = new WebhookRequestHandler($requestReader, $verificationService, $dispatcher);
$webhookRequestHandler->process();
} catch (\Exception $exception) {
$logger->error($exception->getMessage(), [$exception]);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage(), [$exception]);
}
$this->sendErrorResponse();
}
//We need to return a 200 if the call could be processed successfully, the otherwise webhook event
Expand Down
13 changes: 13 additions & 0 deletions src/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,17 @@ public function getClientSandboxUrl(): string
{
return Client::SANDBOX_URL;
}

public function isLogLevel(string $level): bool
{
$possiblePayPalLevels = [
'error' => 400,
'info' => 200,
'debug' => 100
];
$logLevel = Registry::getConfig()->getConfigParam('sLogLevel') ?? 'error';
$logLevel = isset($possiblePayPalLevels[$logLevel]) ? $logLevel : 'error';
$level = isset($possiblePayPalLevels[$level]) ? $level : 'error';
return $possiblePayPalLevels[$logLevel] <= $possiblePayPalLevels[$level];
}
}
6 changes: 5 additions & 1 deletion src/Core/Onboarding/Onboarding.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public function fetchCredentials(): array
} catch (ApiException $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error($exception->getMessage(), [$exception]);
/** @var PayPalConfig $payPalConfig */
$payPalConfig = oxNew(PayPalConfig::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error($exception->getMessage(), [$exception]);
}
}

return $credentials;
Expand Down
12 changes: 8 additions & 4 deletions src/Core/Onboarding/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ public function registerWebhooks(): string
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error(
'PayPal Webhook creation failed: ' . $exception->getMessage(),
[$exception]
);
/** @var PayPalConfig $payPalConfig */
$payPalConfig = oxNew(PayPalConfig::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error(
'PayPal Webhook creation failed: ' . $exception->getMessage(),
[$exception]
);
}
}

return $webhookId;
Expand Down
13 changes: 9 additions & 4 deletions src/Core/Tracker/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OxidSolutionCatalysts\PayPal\Core\Tracker;

use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Core\ServiceFactory;
use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer;
use OxidSolutionCatalysts\PayPalApi\Service\GenericService;
Expand Down Expand Up @@ -55,10 +56,14 @@ public function sendtracking(
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->error(
'PayPal sending Tracker failed: ' . $exception->getMessage(),
[$exception]
);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('error')) {
$logger->error(
'PayPal sending Tracker failed: ' . $exception->getMessage(),
[$exception]
);
}
}

return $result;
Expand Down
15 changes: 10 additions & 5 deletions src/Core/Webhook/Handler/CheckoutOrderApprovedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use OxidEsales\Eshop\Application\Model\Order as EshopModelOrder;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Model\PayPalOrder as PayPalModelOrder;
use OxidSolutionCatalysts\PayPalApi\Exception\ApiException;
use OxidSolutionCatalysts\PayPalApi\Model\Orders\Capture;
Expand Down Expand Up @@ -42,11 +43,15 @@ public function handleWebhookTasks(
} catch (\Exception $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->debug(
"Error during " . self::WEBHOOK_EVENT_NAME . " for PayPal order_id '" .
$payPalOrderId . "'",
[$exception]
);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug(
"Error during " . self::WEBHOOK_EVENT_NAME . " for PayPal order_id '" .
$payPalOrderId . "'",
[$exception]
);
}
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/Core/Webhook/Handler/PaymentCaptureCompletedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace OxidSolutionCatalysts\PayPal\Core\Webhook\Handler;

use OxidEsales\EshopCommunity\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Core\ServiceFactory;
use OxidSolutionCatalysts\PayPalApi\Exception\ApiException;
use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as PayPalApiModelOrder;
Expand Down Expand Up @@ -47,10 +48,14 @@ protected function getPayPalOrderDetails(string $payPalOrderId): ?PayPalApiModel
} catch (ApiException $exception) {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->debug(
'Exception during PaymentCaptureCompletedHandler::getPayPalOrderDetails().',
[$exception]
);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug(
'Exception during PaymentCaptureCompletedHandler::getPayPalOrderDetails().',
[$exception]
);
}
}

return $apiOrder;
Expand Down
15 changes: 10 additions & 5 deletions src/Core/Webhook/Handler/WebhookHandlerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidSolutionCatalysts\PayPal\Core\Webhook\Handler;

use OxidSolutionCatalysts\PayPal\Core\Config;
use OxidSolutionCatalysts\PayPal\Core\Webhook\Event;
use OxidSolutionCatalysts\PayPal\Exception\NotFound;
use OxidSolutionCatalysts\PayPal\Exception\WebhookEventException;
Expand Down Expand Up @@ -61,11 +62,15 @@ public function handle(Event $event): void
} else {
/** @var LoggerInterface $logger */
$logger = $this->getServiceFromContainer('OxidSolutionCatalysts\PayPal\Logger');
$logger->debug(
"Not enough information to handle " . static::WEBHOOK_EVENT_NAME .
" with PayPal order_id '" . $payPalOrderId . "' and PayPal transaction id '" .
$payPalTransactionId . "'"
);
/** @var Config $payPalConfig */
$payPalConfig = oxNew(Config::class);
if ($payPalConfig->isLogLevel('debug')) {
$logger->debug(
"Not enough information to handle " . static::WEBHOOK_EVENT_NAME .
" with PayPal order_id '" . $payPalOrderId . "' and PayPal transaction id '" .
$payPalTransactionId . "'"
);
}
}

//Webhook is used to trigger unfinished order cleanup at the end of each webhook handle.
Expand Down
Loading

0 comments on commit 7db3440

Please sign in to comment.