Skip to content

Commit

Permalink
PAYOSWXP-121: notification-forward: retry on failure (3 retries)
Browse files Browse the repository at this point in the history
  • Loading branch information
rommelfreddy committed Mar 21, 2024
1 parent 5fd2292 commit aba6fc2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/webhooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<tag name="payone.webhook.handler" />
</service>

<service id="PayonePayment\Payone\Webhook\MessageBus\MessageHandler\NotificationForwardHandler">
<service id="PayonePayment\Payone\Webhook\MessageBus\MessageHandler\NotificationForwardHandler" autowire="true">
<argument key="$forwardTargetRepository" type="service" id="payone_payment_notification_target.repository" />
<argument key="$notificationForwardRepository" type="service" id="payone_payment_notification_forward.repository" />
<argument key="$logger" type="service" id="monolog.logger.payone_transaction_forward" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public function __construct(
private readonly string $notificationTargetId,
private readonly array $requestData,
private readonly string $paymentTransactionId,
private readonly string $clientIp
private readonly string $clientIp,
private int $attempt = 1
) {
}

Expand All @@ -35,4 +36,14 @@ public function getClientIp(): string
{
return $this->clientIp;
}

public function getAttempt(): int
{
return $this->attempt;
}

public function setAttempt(int $attempt): void
{
$this->attempt = $attempt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DelayStamp;

#[AsMessageHandler(handles: NotificationForwardMessage::class)]
class NotificationForwardHandler
{
private const ATTEMPT_WAIT_TIME_MAPPING = [2 => 5, 3 => 30, 4 => 120]; // maybe we will make this configurable in the future

public function __construct(
private readonly EntityRepository $forwardTargetRepository,
private readonly EntityRepository $notificationForwardRepository,
private readonly LoggerInterface $logger,
private readonly MessageBusInterface $messageBus
) {
}

Expand All @@ -45,10 +51,23 @@ public function __invoke(NotificationForwardMessage $message): void

$responseContent = (string)curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$statusCode = curl_getinfo($ch, \CURLINFO_RESPONSE_CODE);
curl_close($ch);

$this->statusLogger($responseInfo, $responseContent, $message);
$this->saveNotificationForward($responseInfo, $responseContent, $message);
$this->saveNotificationForward($responseContent, $message);

if ($statusCode < 200 || $statusCode >= 300) {
$newMessage = clone $message;
$newMessage->setAttempt($message->getAttempt() + 1);
$waitForNextAttempt = self::ATTEMPT_WAIT_TIME_MAPPING[$newMessage->getAttempt()] ?? null;
if ($waitForNextAttempt === null) {
return; // too many errors - we will not try it again.
}

$newMessage = new Envelope($newMessage, [new DelayStamp($waitForNextAttempt * 1000 * 60)]);
$this->messageBus->dispatch($newMessage);
}
}

public static function getHandledMessages(): iterable
Expand Down

0 comments on commit aba6fc2

Please sign in to comment.