Skip to content

Commit

Permalink
Api connection fix (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Mar 18, 2024
1 parent 225d997 commit ca48572
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Api/TpayConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
/** @api */
interface TpayConfigInterface
{
public function isTpayActive(): bool;

public function getTitle(): ?string;

public function getCardTitle(): ?string;
Expand Down
37 changes: 35 additions & 2 deletions Model/ApiFacade/CardTransaction/CardApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ class CardApiFacade
/** @var CardOpen */
private $cardOpen;

/** @var TpayInterface */
private $tpay;

/** @var TpayConfigInterface */
private $tpayConfig;

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

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

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

Expand All @@ -25,9 +37,11 @@ class CardApiFacade

public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, TpayTokensService $tokensService, TpayService $tpayService, StoreManagerInterface $storeManager)
{
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->tokensService = $tokensService;
$this->tpayService = $tpayService;
$this->storeManager = $storeManager;
$this->createCardOriginApiInstance($tpay, $tpayConfig, $tokensService, $tpayService);
$this->createOpenApiInstance($tpay, $tpayConfig, $tokensService, $tpayService);
}

public function makeCardTransaction(string $orderId, ?array $customerToken = null): string
Expand All @@ -37,14 +51,33 @@ public function makeCardTransaction(string $orderId, ?array $customerToken = nul

public function payTransaction(string $orderId, array $additionalPaymentInformation, ?string $transactionId = null, ?array $customerToken = null): string
{
$this->connectApi();

return $this->useOpenCard ? $this->cardOpen->payTransaction($orderId, $additionalPaymentInformation, $transactionId, $customerToken) : 'error';
}

public function isOpenApiUse(): bool
{
$this->connectApi();

return (bool) $this->useOpenCard;
}

private function getCurrent()
{
$this->connectApi();

return $this->useOpenCard ? $this->cardOpen : $this->cardOrigin;
}

private function connectApi()
{
if (null == $this->cardOpen && null === $this->cardOrigin) {
$this->createCardOriginApiInstance($this->tpay, $this->tpayConfig, $this->tokensService, $this->tpayService);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->tokensService, $this->tpayService);
}
}

private function createCardOriginApiInstance(TpayInterface $tpay, TpayConfigInterface $tpayConfig, TpayTokensService $tokensService, TpayService $tpayService)
{
if (!$tpayConfig->isCardEnabled()) {
Expand Down
12 changes: 10 additions & 2 deletions Model/ApiFacade/Refund/RefundApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class RefundApiFacade
public function __construct(TpayConfigInterface $tpay)
{
$this->tpay = $tpay;
$this->createRefundOriginApiInstance($tpay);
$this->createOpenApiInstance($tpay);
}

public function makeRefund(InfoInterface $payment, float $amount)
Expand All @@ -43,9 +41,19 @@ public function makeRefund(InfoInterface $payment, float $amount)

private function getCurrentApi()
{
$this->connectApi();

return $this->useOpenApi ? $this->openApi : $this->originApi;
}

private function connectApi()
{
if (null == $this->openApi && null === $this->originApi) {
$this->createRefundOriginApiInstance($this->tpay);
$this->createOpenApiInstance($this->tpay);
}
}

private function createRefundOriginApiInstance(TpayConfigInterface $tpay)
{
try {
Expand Down
36 changes: 34 additions & 2 deletions Model/ApiFacade/TpayConfig/CardConfigFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,41 @@ class CardConfigFacade
/** @var ConfigOpen */
private $openApi;

/** @var TpayInterface */
private $tpay;

/** @var TpayConfigInterface */
private $tpayConfig;

/** @var Repository */
private $assetRepository;

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

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

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

/** @var bool */
private $useOpenApi;

public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager, TpayService $tpayService)
{
$this->createOriginApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService, $tpayService);
$this->createOpenApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService, $storeManager);
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->assetRepository = $assetRepository;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
$this->tpayService = $tpayService;
}

public function getConfig(): array
{
$this->connectApi();

return $this->getCurrentApi() ? $this->getCurrentApi()->getCardConfig() : [];
}

Expand All @@ -38,6 +62,14 @@ private function getCurrentApi()
return $this->useOpenApi ? $this->openApi : $this->originApi;
}

private function connectApi()
{
if (null == $this->openApi && null === $this->originApi) {
$this->createOriginApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->tpayService);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager);
}
}

private function createOriginApiInstance(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, TpayService $tpayService)
{
if (!$tpayConfig->isCardEnabled()) {
Expand Down
38 changes: 35 additions & 3 deletions Model/ApiFacade/TpayConfig/ConfigFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,41 @@ class ConfigFacade
/** @var CardConfigFacade */
private $cardConfig;

/** @var TpayInterface */
private $tpay;

/** @var TpayConfigInterface */
private $tpayConfig;

/** @var Repository */
private $assetRepository;

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

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

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

/** @var bool */
private $useOpenApi;

public function __construct(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService, StoreManagerInterface $storeManager, TpayService $tpayService)
{
$this->createOriginApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService);
$this->createOpenApiInstance($tpay, $tpayConfig, $assetRepository, $tokensService, $storeManager);
$this->cardConfig = new CardConfigFacade($tpay, $tpayConfig, $assetRepository, $tokensService, $storeManager, $tpayService);
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
$this->assetRepository = $assetRepository;
$this->tokensService = $tokensService;
$this->storeManager = $storeManager;
$this->tpayService = $tpayService;
}

public function getConfig(): array
{
$this->connectApi();

return array_merge($this->getCurrentApi() ? $this->getCurrentApi()->getConfig() : [], $this->cardConfig->getConfig());
}

Expand All @@ -42,6 +65,15 @@ private function getCurrentApi()
return $this->useOpenApi ? $this->openApi : $this->originConfig;
}

private function connectApi()
{
if (null == $this->openApi && null === $this->originConfig) {
$this->createOriginApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService);
$this->createOpenApiInstance($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager);
$this->cardConfig = new CardConfigFacade($this->tpay, $this->tpayConfig, $this->assetRepository, $this->tokensService, $this->storeManager, $this->tpayService);
}
}

private function createOriginApiInstance(TpayInterface $tpay, TpayConfigInterface $tpayConfig, Repository $assetRepository, TpayTokensService $tokensService)
{
if (!$tpayConfig->isOriginApiEnabled()) {
Expand Down
32 changes: 24 additions & 8 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,25 @@ class TransactionApiFacade
/** @var OpenApi */
private $openApi;

/** @var bool */
private $useOpenApi;
/** @var TpayConfigInterface */
private $tpay;

/** @var CacheInterface */
private $cache;

/** @var bool */
private $useOpenApi = false;

public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
{
$this->createOriginApiInstance($tpay);
$this->createOpenApiInstance($tpay);
$this->tpay = $tpay;
$this->cache = $cache;
}

public function isOpenApiUse(): bool
{
$this->connectApi();

return $this->useOpenApi;
}

Expand Down Expand Up @@ -65,16 +69,18 @@ public function blik($blikTransactionId, $blikCode): array
/** @return list<Channel> */
public function channels(): array
{
$this->connectApi();

if (!$this->useOpenApi) {
return [];
}

$channels = $this->cache->load(self::CHANNELS_CACHE_KEY);

if ($channels) {
return unserialize($channels);
}

if (false === $this->useOpenApi) {
return [];
}

$channels = array_filter($this->openApi->channels(), function (Channel $channel) {
return true === $channel->available;
});
Expand Down Expand Up @@ -114,9 +120,19 @@ public function originApiFieldCorrect(array $data): array

private function getCurrentApi()
{
$this->connectApi();

return $this->useOpenApi ? $this->openApi : $this->originApi;
}

private function connectApi()
{
if (null == $this->openApi && null === $this->originApi) {
$this->createOriginApiInstance($this->tpay);
$this->createOpenApiInstance($this->tpay);
}
}

private function createOriginApiInstance(TpayConfigInterface $tpay)
{
if (!$tpay->isOriginApiEnabled()) {
Expand Down
11 changes: 10 additions & 1 deletion Model/MethodListPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class MethodListPlugin
{
private const CONFIG_PATH = 'payment/tpaycom_magento2basic/openapi_settings/onsite_channels';

/** @var TpayInterface */
protected $paymentMethod;

/** @var Data */
private $data;

Expand Down Expand Up @@ -54,7 +57,8 @@ public function __construct(
TpayConfigInterface $tpayConfig,
Session $checkoutSession,
TransactionApiFacade $transactions,
ConstraintValidator $constraintValidator
ConstraintValidator $constraintValidator,
TpayInterface $paymentMethod
) {
$this->data = $data;
$this->scopeConfig = $scopeConfig;
Expand All @@ -65,10 +69,15 @@ public function __construct(
$this->checkoutSession = $checkoutSession;
$this->transactions = $transactions;
$this->constraintValidator = $constraintValidator;
$this->paymentMethod = $paymentMethod;
}

public function afterGetAvailableMethods(MethodList $compiled, $result)
{
if (!$this->paymentMethod->isAvailable()) {
return $result;
}

$onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);
$channelList = $onsiteChannels ? explode(',', $onsiteChannels) : [];
$channels = $this->transactions->channels();
Expand Down
4 changes: 4 additions & 0 deletions Model/TpayConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function __construct(

public function getConfig(): array
{
if (!$this->paymentMethod->isAvailable()) {
return [];
}

$config = $this->configFacade->getConfig();
$channels = $this->transactionApi->channels();

Expand Down
4 changes: 4 additions & 0 deletions Model/TpayPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ public function getTpayFormData(?string $orderId = null): array

public function isAvailable(?CartInterface $quote = null)
{
if (!$this->configurationProvider->isTpayActive()) {
return false;
}

$minAmount = $this->configurationProvider->getMinOrderTotal();
$maxAmount = $this->configurationProvider->getMaxOrderTotal();

Expand Down
9 changes: 7 additions & 2 deletions Provider/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __construct(ScopeConfigInterface $scopeConfig, ProductMetadataIn
$this->productMetadataInterface = $productMetadataInterface;
}

public function isTpayActive(): bool
{
return (bool) $this->getConfigData('active');
}

public function getBlikLevelZeroStatus(): bool
{
return (bool) $this->getConfigData('general_settings/blik_level_zero');
Expand Down Expand Up @@ -147,12 +152,12 @@ public function getRSAKey(): ?string
return $this->getConfigData('cardpayment_settings/rsa_key');
}

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

public function getVerificationCode(): string
public function getVerificationCode(): ?string
{
return $this->getConfigData('cardpayment_settings/cardpayment_originapi_settings/verification_code');
}
Expand Down

0 comments on commit ca48572

Please sign in to comment.