Skip to content

Commit

Permalink
Scope update (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jul 16, 2024
1 parent 6a98a08 commit fe55e45
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.7
2.0.8
14 changes: 7 additions & 7 deletions Api/TpayConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ public function getTitle(): ?string;

public function getCardTitle(): ?string;

public function isOriginApiEnabled(): bool;
public function isOriginApiEnabled(?int $storeId = null): bool;

public function isOpenApiEnabled(): bool;
public function isOpenApiEnabled(?int $storeId = null): bool;

public function isCardEnabled(): bool;

public function isOriginApiCardUse(): bool;

public function getApiPassword(): ?string;
public function getApiPassword(?int $storeId = null): ?string;

public function getOpenApiPassword(): ?string;
public function getOpenApiPassword(?int $storeId = null): ?string;

public function getApiKey(): ?string;
public function getApiKey(?int $storeId = null): ?string;

public function getSecurityCode(?int $storeId = null): ?string;

public function getOpenApiClientId(): ?string;
public function getOpenApiClientId(?int $storeId = null): ?string;

public function getMerchantId(): ?int;
public function getMerchantId(?int $storeId = null): ?int;

public function getBlikLevelZeroStatus(): bool;

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ 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.0.8]
### Fixed
- Fixed admin scope config

## [2.0.7]
### Added
- Added regulations in english
Expand Down
63 changes: 40 additions & 23 deletions Controller/Tpay/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,17 @@ public function executeNotification(): ?Response

public function executeCardNotification(): ?Response
{
try {
$notification = (new OriginJWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode(),
!$this->tpayConfig->useSandboxMode()
))->getNotification();

$orderId = base64_decode($notification['order_id']);

$this->tpayService->setCardOrderStatus($orderId, $notification, $this->tpayConfig);
$this->saveOriginCard($notification, $orderId);
$response = null;

return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);
foreach ($this->storeManager->getStores() as $store) {
$response = $this->extractCardNotification($store);

return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE');
if (Response::STATUS_CODE_200 === $response->getStatusCode()) {
break;
}
}

return $response;
}

public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
Expand Down Expand Up @@ -213,4 +198,36 @@ private function extractNotification(StoreInterface $store): Response
return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE');
}
}

private function extractCardNotification(StoreInterface $store): ?Response
{
$storeId = (int) $store->getStoreId();

try {
$notification = (new OriginJWSVerifiedPaymentNotification(
$this->tpayConfig->getSecurityCode($storeId),
!$this->tpayConfig->useSandboxMode($storeId)
))->getNotification();

$orderId = base64_decode($notification['order_id']);

$this->tpayService->setCardOrderStatus($orderId, $notification, $this->tpayConfig);
$this->saveOriginCard($notification, $orderId);

return $this->response->setStatusCode(Response::STATUS_CODE_200)->setContent('TRUE');
} catch (Exception $e) {
Util::log(
'Notification exception',
sprintf(
'%s in file %s line: %d \n\n %s',
$e->getMessage(),
$e->getFile(),
$e->getLine(),
$e->getTraceAsString()
)
);

return $this->response->setStatusCode(Response::STATUS_CODE_400)->setContent('FALSE');
}
}
}
15 changes: 8 additions & 7 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ class OpenApi

private $cache;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
public function __construct(TpayConfigInterface $tpay, CacheInterface $cache, ?int $storeId = null)
{
$this->cache = $cache;
$this->tpayApi = new TpayApi($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode(), 'read', null, $tpay->buildMagentoInfo());
$token = $this->cache->load($this->getAuthTokenCacheKey($tpay));

$this->tpayApi = new TpayApi($tpay->getOpenApiClientId($storeId), $tpay->getOpenApiPassword($storeId), !$tpay->useSandboxMode($storeId));
$token = $this->cache->load($this->getAuthTokenCacheKey($tpay, $storeId));
if ($token) {
$this->tpayApi->setCustomToken(unserialize($token));
}

$this->tpayApi->authorization();

if (!$token) {
$this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay), [\Magento\Framework\App\Config::CACHE_TAG], 7100);
$this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay, $storeId), [\Magento\Framework\App\Config::CACHE_TAG], 7100);
}
}

Expand Down Expand Up @@ -217,12 +218,12 @@ private function waitForBlikAccept(array $result): array
return $result;
}

private function getAuthTokenCacheKey(TpayConfigInterface $tpay)
private function getAuthTokenCacheKey(TpayConfigInterface $tpay, ?int $storeId = null)
{
return sprintf(
self::AUTH_TOKEN_CACHE_KEY,
md5(
join('|', [$tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode()])
join('|', [$tpay->getOpenApiClientId($storeId), $tpay->getOpenApiPassword($storeId), !$tpay->useSandboxMode($storeId)])
)
);
}
Expand Down
16 changes: 10 additions & 6 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ class TransactionApiFacade
/** @var bool */
private $useOpenApi = false;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
/** @var null|int */
private $storeId;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache, ?int $storeId = null)
{
$this->tpay = $tpay;
$this->cache = $cache;
$this->storeId = $storeId;
}

public function isOpenApiUse(): bool
Expand Down Expand Up @@ -74,7 +78,7 @@ public function channels(): array
return [];
}

$cacheKey = 'tpay_channels_'.md5(join('|', [$this->tpay->getOpenApiClientId(), $this->tpay->getOpenApiPassword(), !$this->tpay->useSandboxMode()]));
$cacheKey = 'tpay_channels_'.md5(join('|', [$this->tpay->getOpenApiClientId($this->storeId), $this->tpay->getOpenApiPassword($this->storeId), !$this->tpay->useSandboxMode($this->storeId)]));

$channels = $this->cache->load($cacheKey);

Expand Down Expand Up @@ -136,30 +140,30 @@ private function connectApi()

private function createOriginApiInstance(TpayConfigInterface $tpay)
{
if (!$tpay->isOriginApiEnabled()) {
if (!$tpay->isOriginApiEnabled($this->storeId)) {
$this->originApi = null;

return;
}

try {
$this->originApi = new TransactionOriginApi($tpay->getApiPassword(), $tpay->getApiKey(), $tpay->getMerchantId(), $tpay->getSecurityCode(), !$tpay->useSandboxMode());
$this->originApi = new TransactionOriginApi($tpay->getApiPassword($this->storeId), $tpay->getApiKey($this->storeId), $tpay->getMerchantId($this->storeId), $tpay->getSecurityCode($this->storeId), !$tpay->useSandboxMode($this->storeId));
} catch (Exception $exception) {
$this->originApi = null;
}
}

private function createOpenApiInstance(TpayConfigInterface $tpay)
{
if (!$tpay->isOpenApiEnabled()) {
if (!$tpay->isOpenApiEnabled($this->storeId)) {
$this->openApi = null;
$this->useOpenApi = false;

return;
}

try {
$this->openApi = new OpenApi($tpay, $this->cache);
$this->openApi = new OpenApi($tpay, $this->cache, $this->storeId);
$this->useOpenApi = true;
} catch (Exception $exception) {
$this->openApi = null;
Expand Down
22 changes: 20 additions & 2 deletions Model/Config/Source/OnsiteChannels.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Tpay\Magento2\Model\Config\Source;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Data\OptionSourceInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Model\ApiFacade\Transaction\Dto\Channel;
use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionApiFacade;
Expand All @@ -13,9 +15,9 @@ class OnsiteChannels implements OptionSourceInterface
/** @var TransactionApiFacade */
private $transactions;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
public function __construct(TpayConfigInterface $tpay, Context $context, StoreManagerInterface $storeManager, CacheInterface $cache)
{
$this->transactions = new TransactionApiFacade($tpay, $cache);
$this->transactions = new TransactionApiFacade($tpay, $cache, $this->getStoreId($context, $storeManager));
}

public function getLabelFromValue(int $value): ?string
Expand All @@ -36,4 +38,20 @@ public function toOptionArray(): array
return ['value' => $channel->id, 'label' => $channel->fullName];
}, $this->transactions->channels());
}

private function getStoreId(Context $context, StoreManagerInterface $storeManager): ?int
{
$scope = $context->getRequest()->getParam('store', null);
$websiteScope = $context->getRequest()->getParam('website', null);
$storeId = null;

if (null !== $scope) {
$storeId = (int) $storeManager->getStore($scope)->getId();
} elseif (null !== $websiteScope) {
$website = $storeManager->getWebsite($websiteScope);
$storeId = (int) $website->getDefaultStore()->getId();
}

return $storeId;
}
}
28 changes: 14 additions & 14 deletions Provider/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ public function getTitle(): ?string
return $this->getConfigData('general_settings/title');
}

public function getApiKey(): ?string
public function getApiKey(?int $storeId = null): ?string
{
return $this->getConfigData('originapi_settings/api_key_tpay');
return $this->getConfigData('originapi_settings/api_key_tpay', $storeId);
}

public function getCardApiKey(): ?string
{
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/card_api_key_tpay');
}

public function getApiPassword(): ?string
public function getApiPassword(?int $storeId = null): ?string
{
return $this->getConfigData('originapi_settings/api_password');
return $this->getConfigData('originapi_settings/api_password', $storeId);
}

public function getCardApiPassword(): ?string
Expand Down Expand Up @@ -99,19 +99,19 @@ public function getRegulationsURL(): string
return $this->regulationsEnURL;
}

public function getOpenApiPassword(): ?string
public function getOpenApiPassword(?int $storeId = null): ?string
{
return $this->getConfigData('openapi_settings/open_api_password');
return $this->getConfigData('openapi_settings/open_api_password', $storeId);
}

public function getMerchantId(): ?int
public function getMerchantId(?int $storeId = null): ?int
{
return (int) $this->getConfigData('general_settings/merchant_id');
return (int) $this->getConfigData('general_settings/merchant_id', $storeId);
}

public function getOpenApiClientId(): ?string
public function getOpenApiClientId(?int $storeId = null): ?string
{
return $this->getConfigData('openapi_settings/open_api_client_id');
return $this->getConfigData('openapi_settings/open_api_client_id', $storeId);
}

public function getSecurityCode(?int $storeId = null): ?string
Expand All @@ -134,14 +134,14 @@ public function getCardTitle(): ?string
return $this->getConfigData('cardpayment_settings/card_title') ?? '';
}

public function isOriginApiEnabled(): bool
public function isOriginApiEnabled(?int $storeId = null): bool
{
return (bool) $this->getConfigData('originapi_settings/origin_api_active');
return (bool) $this->getConfigData('originapi_settings/origin_api_active', $storeId);
}

public function isOpenApiEnabled(): bool
public function isOpenApiEnabled(?int $storeId = null): bool
{
return (bool) $this->getConfigData('openapi_settings/open_api_active');
return (bool) $this->getConfigData('openapi_settings/open_api_active', $storeId);
}

public function isCardEnabled(): bool
Expand Down

0 comments on commit fe55e45

Please sign in to comment.