diff --git a/src/DependencyInjection/logger.xml b/src/DependencyInjection/logger.xml new file mode 100644 index 000000000..33e299b0a --- /dev/null +++ b/src/DependencyInjection/logger.xml @@ -0,0 +1,18 @@ + + + + + + + payone_payment + + + + + + payone_transaction_forward + + + diff --git a/src/DependencyInjection/services.xml b/src/DependencyInjection/services.xml index 95e1ff5c3..38dcd5642 100644 --- a/src/DependencyInjection/services.xml +++ b/src/DependencyInjection/services.xml @@ -17,6 +17,7 @@ + @@ -66,12 +67,6 @@ - - - - payone_payment - - diff --git a/src/DependencyInjection/webhooks.xml b/src/DependencyInjection/webhooks.xml index 72fd3ba10..451d84370 100644 --- a/src/DependencyInjection/webhooks.xml +++ b/src/DependencyInjection/webhooks.xml @@ -24,13 +24,14 @@ + - + diff --git a/src/Payone/Webhook/MessageBus/MessageHandler/NotificationForwardHandler.php b/src/Payone/Webhook/MessageBus/MessageHandler/NotificationForwardHandler.php index 7d9f6725e..853c66b1c 100644 --- a/src/Payone/Webhook/MessageBus/MessageHandler/NotificationForwardHandler.php +++ b/src/Payone/Webhook/MessageBus/MessageHandler/NotificationForwardHandler.php @@ -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 ) { } @@ -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); } @@ -71,10 +81,10 @@ private function getNotificationForwards(array $ids, Context $context): EntitySe } private function updateResponses( - \CurlMultiHandle $multiHandle, + \CurlMultiHandle $multiHandle, EntitySearchResult $notificationForwards, - array $forwardRequests, - Context $context + array $forwardRequests, + Context $context ): void { $data = []; @@ -110,14 +120,16 @@ private function getForwardRequests(\CurlMultiHandle $multiHandle, EntitySearchR $forwardRequests[$id] = curl_init(); - $serialize = unserialize($forward->getContent(), []); - /** @var array|string|false $content */ - $content = mb_convert_encoding((string) $serialize, 'ISO-8859-1', 'UTF-8'); + $content = unserialize($forward->getContent(), []); if (!\is_array($content)) { continue; } + foreach ($content as $key => $value) { + $content[$key] = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8'); + } + curl_setopt($forwardRequests[$id], \CURLOPT_URL, $target->getUrl()); curl_setopt($forwardRequests[$id], \CURLOPT_HEADER, false); curl_setopt($forwardRequests[$id], \CURLOPT_POST, true); @@ -136,7 +148,7 @@ private function getForwardRequests(\CurlMultiHandle $multiHandle, EntitySearchR private function buildHeaders( PayonePaymentNotificationForwardEntity $forward, - PayonePaymentNotificationTargetEntity $target + PayonePaymentNotificationTargetEntity $target ): array { $headers = [ 'X-Forwarded-For: ' . $forward->getIp(), @@ -149,4 +161,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(), + ] + ); + } }