Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYOSWXP-120: notification targets: log notification target responses to logfile #290

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/DependencyInjection/webhooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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" ]
Loading