Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.6.1
2.6.2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Tpay\Magento2\Api\Notification\Strategy;

use Tpay\OpenApi\Model\Objects\Objects;

interface NotificationProcessorFactoryInterface
{
public function create(array $data): NotificationProcessorInterface;
/** @param array|Objects $notification */
public function create($notification): NotificationProcessorInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface NotificationProcessorInterface
{
public function process(?int $storeId);
public function process($notification);
}
6 changes: 6 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.6.2
Comment thread
aleksanderkakol marked this conversation as resolved.

### Changed

- New SDK notifications support

## 2.6.1

### Changed
Expand Down
55 changes: 50 additions & 5 deletions Model/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tpay\Magento2\Model;

use Magento\Framework\App\CacheInterface;
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;
use Tpay\OpenApi\Model\Fields\ApiCredentials\Scope;
use Tpay\OpenApi\Model\Fields\Token\AccessToken;
use Tpay\OpenApi\Model\Fields\Token\ExpiresIn;
Expand All @@ -12,23 +13,25 @@
use Tpay\OpenApi\Model\Objects\Authorization\Token;
use Tpay\OpenApi\Utilities\Cache;

class CacheProvider extends Cache
class CacheProvider extends Cache implements PsrCacheInterface
{
/** @var CacheInterface */
private $cache;

public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
parent::__construct(null, $this);
}

public function set($key, $value, $ttl)
public function set($key, $value, $ttl = null)
{
$serialize = $this->serialize($value);
$this->cache->save($serialize, $key, [TpayConfigProvider::CACHE_TAG], $ttl);

return $this->cache->save($serialize, $key, [TpayConfigProvider::CACHE_TAG], $ttl);
}

public function get($key)
public function get($key, $default = null)
{
$json = $this->cache->load($key);

Expand All @@ -37,7 +40,7 @@ public function get($key)

public function delete($key)
{
$this->cache->remove($key);
return $this->cache->remove($key);
}

public function serialize($value): string
Expand Down Expand Up @@ -74,4 +77,46 @@ public function unserialize(string $json)
]
);
}

public function clear(): bool
{
return $this->cache->clean();
}

public function getMultiple($keys, $default = null)
{
$elements = [];
foreach ($keys as $key) {
$elements[] = $this->get($key, $default);
}

return $elements;
}

public function setMultiple($values, $ttl = null)
{
foreach ($values as $key => $value) {
if (false === $this->set($key, $value, $ttl)) {
return false;
}
}

return true;
}

public function deleteMultiple($keys)
{
foreach ($keys as $key) {
if (false === $this->delete($key)) {
return false;
}
}

return true;
}

public function has($key)
{
return null !== $this->get($key);
}
}
76 changes: 50 additions & 26 deletions Notification/NotificationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
namespace Tpay\Magento2\Notification;

use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\Notification\Strategy\NotificationProcessorFactoryInterface;
use Tpay\Magento2\Notification\Strategy\BlikAliasNotificationProcessor;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Model\CacheProvider;
use Tpay\Magento2\Service\TpayService;
use Tpay\OpenApi\Utilities\Cache;
use Tpay\OpenApi\Utilities\CacheCertificateProvider;
use Tpay\OpenApi\Webhook\JWSVerifiedPaymentNotification as OpenApiWebhook;
use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification as OriginApiWebhook;

class NotificationProcessor
{
Expand All @@ -18,43 +24,61 @@ class NotificationProcessor
/** @var RequestInterface */
private $request;

public function __construct(NotificationProcessorFactoryInterface $factory, TpayService $tpayService, RequestInterface $request)
{
/** @var TpayConfigInterface */
private $config;

/** @var StoreManagerInterface */
private $storeManager;

/** @var CacheProvider */
private $cacheProvider;

public function __construct(
RequestInterface $request,
NotificationProcessorFactoryInterface $factory,
TpayService $tpayService,
TpayConfigInterface $config,
StoreManagerInterface $storeManager,
CacheProvider $cacheProvider
) {
$this->request = $request;
$this->factory = $factory;
$this->tpayService = $tpayService;
$this->request = $request;
$this->config = $config;
$this->storeManager = $storeManager;
$this->cacheProvider = $cacheProvider;
}

public function process()
{
$strategy = $this->factory->create($this->request->getPost()->toArray());
$storeId = null;

if (!$strategy instanceof BlikAliasNotificationProcessor) {
$orderId = $this->getOrderId();
if ($orderId) {
$storeId = $this->getOrderStore($orderId);
}
}
$storeId = $this->storeManager->getStore()->getId();
$webhook = $this->createWebhook($storeId);

$strategy->process($storeId);
}

private function getOrderId(): ?string
{
$value = $this->request->getPost('order_id') ?? $this->request->getPost('tr_crc');
$notification = $webhook->getNotification();

if (null === $value) {
return null;
}
$strategy = $this->factory->create($notification);

return base64_decode($value);
$strategy->process($notification);
}

private function getOrderStore(string $orderId): ?int
/** @return OpenApiWebhook|OriginApiWebhook */
private function createWebhook(?int $storeId)
{
$order = $this->tpayService->getOrderById($orderId);
if (null !== $this->request->getPost('card')) {
return new OriginApiWebhook(
$this->config->getApiPassword($storeId),
!$this->config->useSandboxMode($storeId)
);
}

$certificateProvider = new CacheCertificateProvider(
new Cache(null, $this->cacheProvider)
);

return $order->getStoreId() ? (int) $order->getStoreId() : null;
return new OpenApiWebhook(
$certificateProvider,
$this->config->getSecurityCode($storeId),
!$this->config->useSandboxMode($storeId)
);
}
}
33 changes: 20 additions & 13 deletions Notification/Strategy/BlikAliasNotificationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,42 @@

namespace Tpay\Magento2\Notification\Strategy;

use Magento\Framework\App\RequestInterface;
use RuntimeException;
use Tpay\Magento2\Api\Notification\Strategy\NotificationProcessorInterface;
use Tpay\Magento2\Service\TpayAliasServiceInterface;
use Tpay\OpenApi\Model\Objects\NotificationBody\BlikAliasRegister;
use Tpay\OpenApi\Model\Objects\NotificationBody\BlikAliasUnregister;

class BlikAliasNotificationProcessor implements NotificationProcessorInterface
{
/** @var TpayAliasServiceInterface */
protected $aliasService;

/** @var RequestInterface */
private $request;

public function __construct(TpayAliasServiceInterface $aliasService, RequestInterface $request)
public function __construct(TpayAliasServiceInterface $aliasService)
{
$this->aliasService = $aliasService;
$this->request = $request;
}

public function process(?int $storeId = null)
public function process($notification)
{
$response = $this->request->getPost()->toArray();
$userId = (int) explode('-', $response['msg_value']['value'])[1];
if ($notification instanceof BlikAliasRegister) {
$alias = (string) $notification->value->getValue();
$userId = (int) explode('-', $alias)[1];

$this->aliasService->saveCustomerAlias($userId, $alias);

if ('ALIAS_REGISTER' === $response['event']) {
$this->aliasService->saveCustomerAlias($userId, $response['msg_value']['value']);
return;
}

if ('ALIAS_UNREGISTER' === $response['event']) {
$this->aliasService->removeCustomerAlias($userId, $response['msg_value']['value']);
if ($notification instanceof BlikAliasUnregister) {
$alias = (string) $notification->value->getValue();
$userId = (int) explode('-', $alias)[1];

$this->aliasService->removeCustomerAlias($userId, $alias);

return;
}

throw new RuntimeException('Unsupported BLIK notification type');
}
}
12 changes: 3 additions & 9 deletions Notification/Strategy/CardNotificationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Service\TpayService;
use Tpay\Magento2\Service\TpayTokensService;
use Tpay\OriginApi\Webhook\JWSVerifiedPaymentNotification;

class CardNotificationProcessor implements NotificationProcessorInterface
{
Expand All @@ -27,21 +26,16 @@ public function __construct(
TpayConfigInterface $tpayConfig,
TpayService $tpayService,
TpayTokensService $tokensService,
TpayInterface $tpayModel
TpayInterface $tpay
) {
$this->tpayConfig = $tpayConfig;
$this->tpayService = $tpayService;
$this->tokensService = $tokensService;
$this->tpay = $tpayModel;
$this->tpay = $tpay;
Comment thread
aleksanderkakol marked this conversation as resolved.
}

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

$orderId = base64_decode($notification['order_id']);
$order = $this->tpayService->getOrderById($orderId);

Expand Down
Loading
Loading