Skip to content

Commit

Permalink
PAYOSWXP-120: notification targets: log notification target responses…
Browse files Browse the repository at this point in the history
… to logfile (#290)

* PAYOSWXP-120: notification targets: log notification target responses to logfile

* PAYOSWXP-120: Delete the commented service from xml

* PAYOSWXP-120: Addresses ECS issues and reverts the changes made in PAYOSWXP-121 (I created a separate branch for this).

* PAYOSWXP-120: notification forwarding: use correct logger registration

---------

Co-authored-by: Frederik Rommel <[email protected]>
  • Loading branch information
amirinterlutions and rommelfreddy authored Feb 23, 2024
1 parent 74024dd commit 6b46c16
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/DependencyInjection/webhooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

<service id="PayonePayment\Payone\Webhook\MessageBus\MessageHandler\NotificationForwardHandler">
<argument type="service" id="payone_payment_notification_forward.repository" />
<argument type="service" id="PayonePayment\Util\Logger" />
<argument key="$logger" type="service" id="monolog.logger.payone_transaction_forward" />

<tag name="messenger.message_handler"/>
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

namespace PayonePayment\Payone\Webhook\MessageBus\MessageHandler;

use Monolog\Level;
use Monolog\Logger;
use PayonePayment\DataAbstractionLayer\Entity\NotificationForward\PayonePaymentNotificationForwardEntity;
use PayonePayment\DataAbstractionLayer\Entity\NotificationTarget\PayonePaymentNotificationTargetEntity;
use PayonePayment\Payone\Webhook\MessageBus\Command\NotificationForwardCommand;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;

class NotificationForwardHandler implements MessageSubscriberInterface
{
public function __construct(
private readonly EntityRepository $notificationForwardRepository,
private readonly LoggerInterface $logger
private readonly Logger $logger
) {
}

Expand Down Expand Up @@ -52,6 +54,14 @@ public function handle(NotificationForwardCommand $message): void

$this->updateResponses($multiHandle, $notificationForwards, $forwardRequests, $message->getContext());

foreach ($forwardRequests as $id => $handle) {
$responseInfo = curl_getinfo($handle);
$responseContent = curl_multi_getcontent($handle);
$this->statusLogger($responseInfo, $responseContent, $id);
curl_multi_remove_handle($multiHandle, $handle);
curl_close($handle);
}

curl_multi_close($multiHandle);
}

Expand Down Expand Up @@ -149,4 +159,23 @@ private function buildHeaders(

return $headers;
}

private function statusLogger(array $responseInfo, ?string $responseContent, string $id): void
{
$statusCode = $responseInfo['http_code'];

$response = new Response($responseContent, $statusCode, $responseInfo);
$logLevel = $response->isSuccessful() ? 'info' : 'error';
$statusText = Response::$statusTexts[$statusCode] ?? 'Unknown Status Code';

$this->logger->addRecord(
Level::fromName($logLevel),
'Forwarding notification - ' . $statusText,
[
'id' => $id,
'information' => $responseInfo,
'content' => $response->getContent(),
]
);
}
}
18 changes: 18 additions & 0 deletions src/PayonePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@
use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

class PayonePayment extends Plugin
{
Expand All @@ -33,6 +38,19 @@ public function build(ContainerBuilder $container): void
$loader->load('services.xml');

parent::build($container);

$locator = new FileLocator('Resources/config');

$resolver = new LoaderResolver([
new YamlFileLoader($container, $locator),
new GlobFileLoader($container, $locator),
new DirectoryLoader($container, $locator),
]);

$configLoader = new DelegatingLoader($resolver);

$confDir = \rtrim($this->getPath(), '/') . '/Resources/config';
$configLoader->load($confDir . '/{packages}/*.yaml', 'glob');
}

public function install(InstallContext $installContext): void
Expand Down
9 changes: 9 additions & 0 deletions src/Resources/config/packages/monolog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
monolog:
channels: [ "payone_transaction_forward" ]

handlers:
payoneTransactionForwardLogger:
type: rotating_file
path: "%kernel.logs_dir%/payone_transaction_forward.log"
level: info
channels: [ "payone_transaction_forward" ]

0 comments on commit 6b46c16

Please sign in to comment.