Skip to content

Commit

Permalink
Scope update
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jul 9, 2024
1 parent 6a98a08 commit 403f6d6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 35 deletions.
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
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
14 changes: 9 additions & 5 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 int|null */
private $storeId = null;

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 @@ -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
21 changes: 19 additions & 2 deletions Model/Config/Source/OnsiteChannels.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Tpay\Magento2\Model\Config\Source;

use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Action\Context;
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,19 @@ 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 = 0;
if ($scope !== null) {
$storeId = $storeManager->getStore($scope)->getId();
} elseif ($websiteScope !== null) {
$website = $storeManager->getWebsite($websiteScope);
$storeId = $website->getDefaultStore()->getId();
}

return (int) $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 403f6d6

Please sign in to comment.