Skip to content

Commit

Permalink
PAYOSWXP-156: webhooks: add sort-order for handlers to prevent wrong …
Browse files Browse the repository at this point in the history
…order
  • Loading branch information
rommelfreddy committed Sep 13, 2024
1 parent 54d0593 commit 7f10d49
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
27 changes: 15 additions & 12 deletions src/DependencyInjection/webhooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,36 @@
<argument key="$logger" type="service" id="monolog.logger.payone" />
</service>

<service id="PayonePayment\Payone\Webhook\Handler\TransactionStatusWebhookHandler">
<argument type="service" id="PayonePayment\Components\TransactionStatus\TransactionStatusService"/>
<service id="PayonePayment\Payone\Webhook\Handler\WebhookLogHandler">
<argument type="service" id="PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface" />
<argument type="service" id="PayonePayment\Components\DataHandler\WebhookLog\WebhookLogDataHandlerInterface" />
<argument type="service" id="monolog.logger.payone" />
<argument type="service" id="PayonePayment\Components\AutomaticCaptureService\AutomaticCaptureServiceInterface" />

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

<service id="PayonePayment\Payone\Webhook\Handler\WebhookLogHandler">
<service id="PayonePayment\Payone\Webhook\Handler\TransactionStatusWebhookHandler">
<argument type="service" id="PayonePayment\Components\TransactionStatus\TransactionStatusService"/>
<argument type="service" id="PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface" />
<argument type="service" id="PayonePayment\Components\DataHandler\WebhookLog\WebhookLogDataHandlerInterface" />
<argument type="service" id="monolog.logger.payone" />

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

<service id="PayonePayment\Payone\Webhook\Handler\PaymentStatusHandler" autowire="true">
<tag name="payone.webhook.handler" priority="200" />
</service>

<service id="PayonePayment\Payone\Webhook\Handler\AutoCaptureStatusHandler" autowire="true">
<tag name="payone.webhook.handler" priority="100" />
</service>

<service id="PayonePayment\Payone\Webhook\Handler\NotificationForwardHandler">
<argument type="service" id="payone_payment_notification_target.repository"/>
<argument type="service" id="PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface" />
<argument type="service" id="messenger.bus.shopware"/>

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

<service id="PayonePayment\Payone\Webhook\Handler\PaymentStatusHandler" autowire="true">
<tag name="payone.webhook.handler" />
<tag name="payone.webhook.handler" priority="0" />
</service>

<service id="PayonePayment\Payone\Webhook\MessageBus\MessageHandler\NotificationForwardHandler" autowire="true">
Expand Down
39 changes: 39 additions & 0 deletions src/Payone/Webhook/Handler/AutoCaptureStatusHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace PayonePayment\Payone\Webhook\Handler;

use PayonePayment\Components\AutomaticCaptureService\AutomaticCaptureServiceInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Struct\PaymentTransaction;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;

class AutoCaptureStatusHandler implements WebhookHandlerInterface
{
public function __construct(
private readonly TransactionDataHandlerInterface $transactionDataHandler,
private readonly AutomaticCaptureServiceInterface $automaticCaptureService
) {
}

public function process(SalesChannelContext $salesChannelContext, Request $request): void
{
$paymentTransaction = $this->transactionDataHandler->getPaymentTransactionByPayoneTransactionId(
$salesChannelContext->getContext(),
$request->request->getInt('txid')
);

if (!$paymentTransaction instanceof PaymentTransaction) {
return;
}

$this->automaticCaptureService->captureIfPossible($paymentTransaction, $salesChannelContext);
}

public function supports(SalesChannelContext $salesChannelContext, array $data): bool
{
return isset($data['txid']) && ($data['txaction'] ?? null) === 'appointed';
}
}
15 changes: 1 addition & 14 deletions src/Payone/Webhook/Handler/TransactionStatusWebhookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace PayonePayment\Payone\Webhook\Handler;

use PayonePayment\Components\AutomaticCaptureService\AutomaticCaptureServiceInterface;
use PayonePayment\Components\DataHandler\Transaction\TransactionDataHandlerInterface;
use PayonePayment\Components\TransactionStatus\TransactionStatusServiceInterface;
use PayonePayment\Struct\PaymentTransaction;
Expand All @@ -17,8 +16,7 @@ class TransactionStatusWebhookHandler implements WebhookHandlerInterface
public function __construct(
private readonly TransactionStatusServiceInterface $transactionStatusService,
private readonly TransactionDataHandlerInterface $transactionDataHandler,
private readonly LoggerInterface $logger,
private readonly AutomaticCaptureServiceInterface $automaticCaptureService
private readonly LoggerInterface $logger
) {
}

Expand Down Expand Up @@ -54,17 +52,6 @@ public function process(SalesChannelContext $salesChannelContext, Request $reque

$this->transactionDataHandler->saveTransactionData($paymentTransaction, $salesChannelContext->getContext(), $payoneTransactionData);
$this->transactionStatusService->transitionByConfigMapping($salesChannelContext, $paymentTransaction, $data);

// Reload the paymentTransaction for automatic capture
/** @var PaymentTransaction|null $paymentTransaction */
$paymentTransaction = $this->transactionDataHandler->getPaymentTransactionByPayoneTransactionId(
$salesChannelContext->getContext(),
(int) $data['txid']
);

if ($paymentTransaction) {
$this->automaticCaptureService->captureIfPossible($paymentTransaction, $salesChannelContext);
}
}

private function utf8EncodeRecursive(array $transactionData): array
Expand Down

0 comments on commit 7f10d49

Please sign in to comment.