Skip to content

Commit

Permalink
Notification handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Wysocki committed Nov 7, 2024
1 parent 527616d commit 3bb10ba
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Controller/Tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
use Magento\Sales\Model\Order;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactoryInterface;
use Tpay\Magento2\Service\TpayService;
use Tpay\Magento2\Service\TpayTokensService;
use Tpay\OpenApi\Webhook\JWSVerifiedPaymentNotification;
use Tpay\OriginApi\Utilities\Util;
use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification as OriginJWSVerifiedPaymentNotification;
use tpaySDK\Webhook\JWSVerifiedPaymentNotification;

class Notification implements CsrfAwareActionInterface
{
Expand All @@ -30,6 +31,9 @@ class Notification implements CsrfAwareActionInterface
/** @var TpayService */
protected $tpayService;

/** @var NotificationProcessorFactoryInterface */
protected $notificationProcessorFactory;

/** @var TpayTokensService */
private $tokensService;

Expand All @@ -41,27 +45,41 @@ public function __construct(
TpayConfigInterface $tpayConfig,
TpayService $tpayService,
TpayTokensService $tokensService,
ResponseInterface $response
ResponseInterface $response,
NotificationProcessorFactoryInterface $notificationProcessorFactory
) {
$this->tpay = $tpayModel;
$this->tpayConfig = $tpayConfig;
$this->tpayService = $tpayService;
$this->tokensService = $tokensService;
$this->response = $response;
$this->notificationProcessorFactory = $notificationProcessorFactory;
Util::$loggingEnabled = false;
}

public function execute(): ?Response
{
if (isset($_POST['card'])) {
$orderId = base64_decode($_POST['order_id']);
try {
if (isset($_POST['card'])) {
$orderId = base64_decode($_POST['order_id']);

return $this->extractCardNotification($this->getOrderStore($orderId));
}
return $this->extractCardNotification($this->getOrderStore($orderId));
}

$orderId = base64_decode($_POST['tr_crc']);
if (isset($_POST['event'])) {
return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('kurwa mac');
}

$orderId = base64_decode($_POST['tr_crc']);

$strategy = $this->notificationProcessorFactory->create($_POST);

$strategy->process($this->getOrderStore($orderId));
} catch (\Throwable $e) {
return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent($e->getMessage());
}

return $this->extractNotification($this->getOrderStore($orderId));
return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
}

public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
Expand Down
13 changes: 13 additions & 0 deletions Model/Alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tpay\Magento2\Model;

use Magento\Framework\Model\AbstractModel;

class Alias extends AbstractModel
{
public function __construct()
{
$this->_init(ResourceModel\Alias::class);
}
}
13 changes: 13 additions & 0 deletions Model/ResourceModel/Alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tpay\Magento2\Model\ResourceModel;

use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class Alias extends AbstractDb
{
protected function _construct(): void
{
$this->_init('tpay_blik_aliases', 'id');
}
}
23 changes: 23 additions & 0 deletions Notification/NotificationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tpay\Magento2\Notification;

use Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactoryInterface;

class NotificationProcessor
{
/** @var NotificationProcessorFactoryInterface */
protected $factory;

public function __construct(NotificationProcessorFactoryInterface $factory)
{
$this->factory = $factory;
}

public function process()
{
$strategy = $this->factory->create();

return $strategy->process();
}
}
10 changes: 10 additions & 0 deletions Notification/Strategy/BlikAliasNotificationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tpay\Magento2\Notification\Strategy;

class BlikAliasNotificationProcessor implements NotificationProcessorInterface
{
public function process(?int $storeId)
{
}
}
10 changes: 10 additions & 0 deletions Notification/Strategy/CardNotificationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tpay\Magento2\Notification\Strategy;

class CardNotificationProcessor implements NotificationProcessorInterface
{
public function process(?int $storeId)
{
}
}
43 changes: 43 additions & 0 deletions Notification/Strategy/DefaultNotificationProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tpay\Magento2\Notification\Strategy;

use Laminas\Http\Response;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Service\TpayService;
use Tpay\OpenApi\Webhook\JWSVerifiedPaymentNotification;

class DefaultNotificationProcessor implements NotificationProcessorInterface
{
/** @var TpayConfigInterface */
protected $tpayConfig;

/** @var TpayService */
protected $tpayService;

public function __construct(TpayConfigInterface $tpayConfig, TpayService $tpayService)
{
$this->tpayConfig = $tpayConfig;
$this->tpayService = $tpayService;
}

public function process(?int $storeId)
{
$notification = (new JWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode($storeId),
!$this->tpayConfig->useSandboxMode($storeId)
))->getNotification();

$notification = $notification->getNotificationAssociative();
$orderId = base64_decode($notification['tr_crc']);

if ('PAID' === $notification['tr_status']) {
$response = $this->getPaidTransactionResponse($orderId);

return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent($response);
}

$this->saveCard($notification, $orderId);
$this->tpayService->setOrderStatus($orderId, $notification, $this->tpayConfig);
}
}
29 changes: 29 additions & 0 deletions Notification/Strategy/Factory/NotificationProcessorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Tpay\Magento2\Notification\Strategy\Factory;

use Tpay\Magento2\Notification\Strategy\NotificationProcessorInterface;

class NotificationProcessorFactory implements NotificationProcessorFactoryInterface
{
/** @var NotificationProcessorInterface[] */
protected $strategies;

public function __construct(array $strategies = [])
{
$this->strategies = $strategies;
}

public function create(array $data): NotificationProcessorInterface
{
if (isset($_POST['card'])) {
return $this->strategies['card'];
}

if (isset($_POST['event'])) {
return $this->strategies['blikAlias'];
}

return $this->strategies['default'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tpay\Magento2\Notification\Strategy\Factory;

use Tpay\Magento2\Notification\Strategy\NotificationProcessorInterface;

interface NotificationProcessorFactoryInterface
{
public function create(array $data): NotificationProcessorInterface;
}
8 changes: 8 additions & 0 deletions Notification/Strategy/NotificationProcessorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tpay\Magento2\Notification\Strategy;

interface NotificationProcessorInterface
{
public function process(?int $storeId);
}
10 changes: 10 additions & 0 deletions Service/TpayAliasesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Tpay\Magento2\Service;

class TpayAliasesService
{

}
10 changes: 10 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<preference for="Tpay\Magento2\Model\Api\Data\TokensInterface" type="Tpay\Magento2\Model\Token"/>
<preference for="Tpay\Magento2\Model\Api\TokenRepositoryInterface" type="Tpay\Magento2\Model\TokenRepository"/>
<preference for="Magento\Sales\Api\OrderPaymentRepositoryInterface" type="Magento\Sales\Model\Order\Payment\Repository"/>
<preference for="Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactoryInterface" type="Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactory" />
<type name="Magento\Payment\Model\MethodList">
<plugin name="tpay_generic_onsite" type="Tpay\Magento2\Model\MethodListPlugin" />
</type>
Expand All @@ -29,6 +30,15 @@
<argument name="valueHandlerPool" xsi:type="object">TpayPaymentMethodValueHandlerPool</argument>
</arguments>
</type>
<type name="Tpay\Magento2\Notification\Strategy\Factory\NotificationProcessorFactory">
<arguments>
<argument name="strategies" xsi:type="array">
<item name="default" xsi:type="object">Tpay\Magento2\Notification\Strategy\DefaultNotificationProcessor</item>
<item name="card" xsi:type="object">Tpay\Magento2\Notification\Strategy\CardNotificationProcessor</item>
<item name="blikAlias" xsi:type="object">Tpay\Magento2\Notification\Strategy\BlikAliasNotificationProcessor</item>
</argument>
</arguments>
</type>
<virtualType name="TpayPaymentMethodConfig" type="Magento\Payment\Gateway\Config\Config">
<arguments>
<argument name="methodCode" xsi:type="string">Tpay_Magento2</argument>
Expand Down

0 comments on commit 3bb10ba

Please sign in to comment.