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-121: Adds configuration for resending notification forwards #294

Closed
wants to merge 4 commits into from
Closed
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
35 changes: 35 additions & 0 deletions src/Command/ResendNotifyCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace PayonePayment\Command;

use PayonePayment\Components\ResendNotifyHandler\ResendNotifyHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ResendNotifyCommand extends Command
{
protected static $defaultName = 'payone:send-notify';

public function __construct(
private readonly ResendNotifyHandler $resendNotifyHandler
) {
parent::__construct();
}

protected function configure(): void
{
$this->setDescription('Send notification forwarding queue');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Sending notification forwarding queue');

$this->resendNotifyHandler->send();

$output->writeln('Notification forwarding queue sent');

return 0;
}
}
51 changes: 51 additions & 0 deletions src/Components/ResendNotifyHandler/ResendNotifyHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Components\ResendNotifyHandler;

use PayonePayment\DataAbstractionLayer\Entity\NotificationQueue\PayonePaymentNotificationQueueEntity;
use PayonePayment\Payone\Webhook\MessageBus\Command\NotificationForwardCommand;
use PayonePayment\Payone\Webhook\MessageBus\MessageHandler\NotificationForwardHandler;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;

class ResendNotifyHandler
{
public function __construct(
private readonly EntityRepository $notificationQueueRepository,
private readonly NotificationForwardHandler $notificationForwardHandler
) {
}

public function send(): void
{
$criteria = new Criteria();
$currentDate = new \DateTime();
$criteria->addFilter(new RangeFilter('nextExecutionTime', [
RangeFilter::LTE => $currentDate->format(Defaults::STORAGE_DATE_TIME_FORMAT),
]));

$notificationQueue = $this->notificationQueueRepository->search($criteria, Context::createDefaultContext())->getEntities();

/** @var PayonePaymentNotificationQueueEntity $notification */
foreach ($notificationQueue as $notification) {
$messageDecode = null;
if ($notification->getMessage() !== null) {
$messageDecode = base64_decode($notification->getMessage());
}
if ($messageDecode !== null) {
/** @var NotificationForwardCommand $message */
$message = unserialize($messageDecode, []);

$this->notificationForwardHandler->handle($message, true);
$this->notificationQueueRepository->delete([['id' => $notification->getId()]], Context::createDefaultContext());
}
}

echo 'current time: ' . $currentDate->format(Defaults::STORAGE_DATE_TIME_FORMAT) . "\n";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PayonePayment\DataAbstractionLayer\Entity\NotificationQueue;

use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;

/**
* @extends EntityCollection<PayonePaymentNotificationQueueEntity>
*/
class PayonePaymentNotificationQueueCollection extends EntityCollection
{
protected function getExpectedClass(): string
{
return PayonePaymentNotificationQueueEntity::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace PayonePayment\DataAbstractionLayer\Entity\NotificationQueue;

use PayonePayment\DataAbstractionLayer\Entity\NotificationTarget\PayonePaymentNotificationTargetDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Field\BoolField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\DateTimeField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\PrimaryKey;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\Required;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IdField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\IntField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToOneAssociationField;
use Shopware\Core\Framework\DataAbstractionLayer\Field\StringField;
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;

class PayonePaymentNotificationQueueDefinition extends EntityDefinition
{
final public const ENTITY_NAME = 'payone_payment_notification_queue';

final public const STATUS_SCHEDULED = 'scheduled';

final public const STATUS_FINISHED = 'finished';

public function getEntityName(): string
{
return self::ENTITY_NAME;
}

public function getCollectionClass(): string
{
return PayonePaymentNotificationQueueCollection::class;
}

public function getEntityClass(): string
{
return PayonePaymentNotificationQueueEntity::class;
}

protected function defineFields(): FieldCollection
{
return new FieldCollection([
(new IdField('id', 'id'))->setFlags(new PrimaryKey(), new Required()),
new FkField('notification_target_id', 'notificationTargetId', PayonePaymentNotificationTargetDefinition::class),
new OneToOneAssociationField('notificationTarget', 'notification_target_id', 'id', PayonePaymentNotificationTargetDefinition::class, true),
new IntField('response_http_code', 'responseHttpCode'),
(new LongTextField('message', 'message')),
new DateTimeField('last_execution_time', 'lastExecutionTime'),
(new DateTimeField('next_execution_time', 'nextExecutionTime'))->addFlags(new Required()),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace PayonePayment\DataAbstractionLayer\Entity\NotificationQueue;

use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;

class PayonePaymentNotificationQueueEntity extends Entity
{
use EntityIdTrait;

protected string $notificationTargetId;

protected string $status;

protected ?int $responseHttpCode;

protected ?string $message;

protected \DateTimeInterface $lastExecutionTime;

protected \DateTimeInterface $nextExecutionTime;

public function getNotificationTargetId(): string
{
return $this->notificationTargetId;
}

public function setNotificationTargetId(string $notificationTargetId): void
{
$this->notificationTargetId = $notificationTargetId;
}

public function getResponseHttpCode(): ?int
{
return $this->responseHttpCode;
}

public function setResponseHttpCode(?int $responseHttpCode): void
{
$this->responseHttpCode = $responseHttpCode;
}

public function getMessage(): ?string
{
return $this->message;
}

public function setMessage(?string $message): void
{
$this->message = $message;
}

public function getLastExecutionTime(): \DateTimeInterface
{
return $this->lastExecutionTime;
}

public function setLastExecutionTime(\DateTimeInterface $lastExecutionTime): void
{
$this->lastExecutionTime = $lastExecutionTime;
}

public function setNextExecutionTime(\DateTimeInterface $nextExecutionTime): void
{
$this->nextExecutionTime = $nextExecutionTime;
}

public function getNextExecutionTime(): \DateTimeInterface
{
return $this->nextExecutionTime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ protected function defineFields(): FieldCollection
(new JsonField('txactions', 'txactions')),
(new StringField('username', 'username')),
(new StringField('password', 'password')),
(new BoolField('resend_notification', 'resendNotification')),
(new JsonField('resend_notification_time', 'resendNotificationTime')),
(new JsonField('resend_notification_status', 'resendNotificationStatus')),
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class PayonePaymentNotificationTargetEntity extends Entity

protected array $txactions;

protected bool $resendNotification;

protected array $resendNotificationTime;

protected array $resendNotificationStatus;

protected ?string $username = null;

protected ?string $password = null;
Expand Down Expand Up @@ -70,4 +76,34 @@ public function setPassword(?string $password): void
{
$this->password = $password;
}

public function getResendNotification(): bool
{
return $this->resendNotification;
}

public function setResendNotification(bool $resendNotification): void
{
$this->resendNotification = $resendNotification;
}

public function setResendNotificationTime(array $resendNotificationTime): void
{
$this->resendNotificationTime = $resendNotificationTime;
}

public function getResendNotificationTime(): array
{
return $this->resendNotificationTime;
}

public function setResendNotificationStatus(array $resendNotificationStatus): void
{
$this->resendNotificationStatus = $resendNotificationStatus;
}

public function getResendNotificationStatus(): array
{
return $this->resendNotificationStatus;
}
}
13 changes: 13 additions & 0 deletions src/DependencyInjection/commands.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<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\Command\ResendNotifyCommand">
<argument type="service" id="PayonePayment\Components\ResendNotifyHandler\ResendNotifyHandler"/>
<tag name="console.command"/>
</service>
</services>
</container>
4 changes: 4 additions & 0 deletions src/DependencyInjection/entities.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<tag name="shopware.entity.definition" entity="payone_payment_notification_forward"/>
</service>

<service id="PayonePayment\DataAbstractionLayer\Entity\NotificationQueue\PayonePaymentNotificationQueueDefinition">
<tag name="shopware.entity.definition" entity="payone_payment_notification_queue"/>
</service>

<service id="PayonePayment\DataAbstractionLayer\Entity\OrderActionLog\PayonePaymentOrderActionLogDefinition">
<tag name="shopware.entity.definition" entity="payone_payment_order_action_log"/>
</service>
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/handler/global_handlers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@
<argument type="service" id="translator" />
</service>
<service id="PayonePayment\Components\PaymentStateHandler\PaymentStateHandlerInterface" alias="PayonePayment\Components\PaymentStateHandler\PaymentStateHandler"/>

<service id="PayonePayment\Components\ResendNotifyHandler\ResendNotifyHandler">
<argument type="service" id="payone_payment_notification_queue.repository" />
<argument type="service" id="PayonePayment\Payone\Webhook\MessageBus\MessageHandler\NotificationForwardHandler" />
</service>
</services>
</container>
10 changes: 10 additions & 0 deletions src/DependencyInjection/scheduled_tasks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,15 @@
<argument type="service" id="PayonePayment\Util\Logger" />
<tag name="messenger.message_handler" />
</service>

<service id="PayonePayment\ScheduledTask\NotifySendRetry">
<tag name="shopware.scheduled.task" />
</service>

<service id="PayonePayment\ScheduledTask\NotifySendRetryHandler">
<argument type="service" id="scheduled_task.repository" />
<argument type="service" id="PayonePayment\Components\ResendNotifyHandler\ResendNotifyHandler"/>
<tag name="messenger.message_handler" />
</service>
</services>
</container>
1 change: 1 addition & 0 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="commands.xml"/>
</imports>

<parameters>
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/webhooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

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

<tag name="messenger.message_handler"/>
Expand Down
31 changes: 31 additions & 0 deletions src/Migration/Migration1709142760AlterNotificationTargetTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Migration;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1709142760AlterNotificationTargetTable extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1_709_142_760;
}

public function update(Connection $connection): void
{
$sql = 'ALTER TABLE `payone_payment_notification_target`
ADD `resend_notification` TINYINT(1) NULL DEFAULT \'0\' AFTER `password`,
ADD `resend_notification_time` JSON NULL AFTER `resend_notification`,
ADD `resend_notification_status` JSON NULL AFTER `resend_notification_time`;';

$connection->executeStatement($sql);
}

public function updateDestructive(Connection $connection): void
{
// implement update destructive
}
}
Loading
Loading