Skip to content

Commit

Permalink
PAYOSWXP-120 and PAYOSWXP-121: Fixes Notification Forward: Now capabl…
Browse files Browse the repository at this point in the history
…e of sending notifications to a URL. Adds new log feature: Notification Forward logs are now saved in .
  • Loading branch information
amirinterlutions committed Jan 24, 2024
1 parent 3a467b0 commit 213a189
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/DependencyInjection/logger.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="PayonePayment\Util\Logger" class="Monolog\Logger">
<factory service="Shopware\Core\Framework\Log\LoggerFactory" method="createRotating"/>

<argument type="string">payone_payment</argument>
</service>

<service id="PayonePayment\Util\NotificationForwardLogger" class="Monolog\Logger">
<factory service="Shopware\Core\Framework\Log\LoggerFactory" method="createRotating"/>

<argument type="string">payone_transaction_forward</argument>
</service>
</services>
</container>
7 changes: 1 addition & 6 deletions src/DependencyInjection/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<import resource="webhooks.xml"/>
<import resource="requestParameter/*.xml"/>
<import resource="payment_method_filter.xml"/>
<import resource="logger.xml"/>
</imports>

<parameters>
Expand Down Expand Up @@ -66,12 +67,6 @@
</service>
<service id="PayonePayment\Payone\Client\PayoneClientInterface" alias="PayonePayment\Payone\Client\PayoneClient"/>

<service id="PayonePayment\Util\Logger" class="Monolog\Logger">
<factory service="Shopware\Core\Framework\Log\LoggerFactory" method="createRotating"/>

<argument type="string">payone_payment</argument>
</service>

<service id="payone.app_system.guzzle" class="GuzzleHttp\Client">
</service>

Expand Down
3 changes: 2 additions & 1 deletion src/DependencyInjection/webhooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
<argument type="service" id="payone_payment_notification_forward.repository" />
<argument type="service" id="PayonePayment\Components\DataHandler\Transaction\TransactionDataHandler" />
<argument type="service" id="messenger.bus.shopware"/>
<!-- <argument type="service" id="PayonePayment\Util\NotificationForwardLogger" />-->

<tag name="payone.webhook.handler" />
</service>

<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 type="service" id="PayonePayment\Util\NotificationForwardLogger" />
<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 All @@ -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 = [];

Expand Down Expand Up @@ -110,14 +120,16 @@ private function getForwardRequests(\CurlMultiHandle $multiHandle, EntitySearchR

$forwardRequests[$id] = curl_init();

$serialize = unserialize($forward->getContent(), []);
/** @var array<int, string>|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);
Expand All @@ -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(),
Expand All @@ -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(),
]
);
}
}

0 comments on commit 213a189

Please sign in to comment.