From 9810f372a454f80d0e5442364b70e92f4fb1aca3 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 5 Jan 2024 14:46:27 +0100 Subject: [PATCH] Working magento --- Model/ApiFacade/OpenApi.php | 35 +++++++---- .../Transaction/TransactionApiFacade.php | 42 ++++++++++++- Model/GenericOnSiteConfigProvider.php | 4 +- Model/GenericOnsite.php | 60 +++++++++++++++++++ Model/Tpay.php | 2 +- etc/adminhtml/system.xml | 19 +----- 6 files changed, 129 insertions(+), 33 deletions(-) diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 10157a1..48b2884 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -15,11 +15,24 @@ public function create(array $data): array return $this->updateRedirectUrl($transaction); } + public function createWithInstantRedirect(array $data) + { + $transactionData = $this->handleDataStructure($data); + $transaction = $this->Transactions->createTransactionWithInstantRedirection($transactionData); + + return $this->updateRedirectUrl($transaction); + } + public function makeRefund(InfoInterface $payment, string $amount): array { return $this->Transactions->createRefundByTransactionId(['amount' => number_format($amount, 2)], $payment->getAdditionalInformation('transaction_id')); } + public function channels(): array + { + return $this->transactions()->getChannels(); + } + private function handleDataStructure(array $data): array { $paymentData = [ @@ -35,16 +48,10 @@ private function handleDataStructure(array $data): array 'city' => $data['city'], 'country' => $data['country'], ], - 'pay' => [ - 'groupId' => $data['group'], - ], - 'callbacks' => [ - 'payerUrls' => [ - 'success' => $data['return_url'], - 'error' => $data['return_error_url'], - ], - 'notification' => [ - 'url' => $data['result_url'], + "callbacks" => [ + "payerUrls" => [ + "success" => $data['return_url'], + "error" => $data['return_error_url'] ], ], ]; @@ -55,6 +62,14 @@ private function handleDataStructure(array $data): array ]; } + if (isset($data['group'])) { + $paymentData['pay'] = ['groupId' => $data['group']]; + } + + if (isset($data['channel'])) { + $paymentData['pay'] = ['channelId' => $data['channel']]; + } + return $paymentData; } diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index cabe2bc..4aac618 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -3,11 +3,15 @@ namespace tpaycom\magento2basic\Model\ApiFacade\Transaction; use Exception; +use Magento\Framework\App\CacheInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\OpenApi; class TransactionApiFacade { + private const CHANNELS_CACHE_KEY = 'tpay_channels'; + private const CACHE_LIFETIME = 86400; + /** @var TransactionOriginApi */ private $originApi; @@ -17,10 +21,14 @@ class TransactionApiFacade /** @var bool */ private $useOpenApi; - public function __construct(TpayInterface $tpay) + /** @var CacheInterface */ + private $cache; + + public function __construct(TpayInterface $tpay, CacheInterface $cache) { $this->originApi = new TransactionOriginApi($tpay->getApiPassword(), $tpay->getApiKey(), $tpay->getMerchantId(), $tpay->getSecurityCode(), !$tpay->useSandboxMode()); - $this->createOpenApiInstance($tpay->getOpenApiClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode()); + $this->createOpenApiInstance($tpay->getClientId(), $tpay->getOpenApiPassword(), !$tpay->useSandboxMode()); + $this->cache = $cache; } public function isOpenApiUse() @@ -33,11 +41,41 @@ public function create(array $config) return $this->getCurrentApi()->create($config); } + public function createWithInstantRedirection(array $config) + { + if (!$this->useOpenApi) { + throw new TpayException('OpenAPI not availabile - Failed to create transaction with instant redirection'); + } + + return $this->openApi->createWithInstantRedirect($config); + } + public function blik($blikTransactionId, $blikCode) { return $this->originApi->blik($blikTransactionId, $blikCode); } + public function channels(): array + { + $channels = $this->cache->load(self::CHANNELS_CACHE_KEY); + + if ($channels) { + return json_decode($channels, true); + } + + if (false === $this->useOpenApi) { + return []; + } + + $channels = array_filter($this->openApi->channels()['channels'], function (array $channel) { + return $channel['available'] === true && empty($channel['constraints']) === true; + }); + + $this->cache->save(json_encode($channels), self::CHANNELS_CACHE_KEY, []); + + return $channels; + } + private function getCurrentApi() { return $this->useOpenApi ? $this->openApi : $this->originApi; diff --git a/Model/GenericOnSiteConfigProvider.php b/Model/GenericOnSiteConfigProvider.php index 64e6e13..4c2bef0 100644 --- a/Model/GenericOnSiteConfigProvider.php +++ b/Model/GenericOnSiteConfigProvider.php @@ -3,7 +3,6 @@ namespace tpaycom\magento2basic\Model; use Magento\Checkout\Model\ConfigProviderInterface; -use Magento\Framework\App\CacheInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\View\Asset\Repository; use Magento\Payment\Helper\Data as PaymentHelper; @@ -12,6 +11,7 @@ use Magento\Store\Model\ScopeInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade; +use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionOriginApi; #[\AllowDynamicProperties] class GenericOnSiteConfigProvider implements ConfigProviderInterface @@ -51,7 +51,7 @@ public function getConfig() 'addCSS' => $this->createCSS('tpaycom_magento2basic::css/tpay.css'), 'blikStatus' => $this->getPaymentMethodInstance()->checkBlikLevel0Settings(), 'onlyOnlineChannels' => $this->getPaymentMethodInstance()->onlyOnlineChannels(), - 'getBlikChannelID' => TransactionModel::BLIK_CHANNEL, + 'getBlikChannelID' => TransactionOriginApi::BLIK_CHANNEL, 'isInstallmentsAmountValid' => $this->getPaymentMethodInstance()->getInstallmentsAmountValid(), ], ], diff --git a/Model/GenericOnsite.php b/Model/GenericOnsite.php index c8b84ae..64dbdae 100644 --- a/Model/GenericOnsite.php +++ b/Model/GenericOnsite.php @@ -203,4 +203,64 @@ public function getOpenApiPassword(): string { return ''; } + + public function getOpenApiSecurityCode(): ?string + { + return ''; + } + + public function getCardApiKey(): ?string + { + return ''; + } + + public function getCardApiPassword(): ?string + { + return ''; + } + + public function getCardSaveEnabled(): bool + { + return ''; + } + + public function getCheckoutCustomerId(): ?string + { + return ''; + } + + public function getRSAKey(): string + { + return ''; + } + + public function isCustomerLoggedIn(): bool + { + return ''; + } + + public function getHashType(): string + { + return ''; + } + + public function getVerificationCode(): string + { + return ''; + } + + public function getCustomerId($orderId) + { + return ''; + } + + public function isCustomerGuest($orderId) + { + return ''; + } + + public function getOpenApiClientId() + { + return ''; + } } diff --git a/Model/Tpay.php b/Model/Tpay.php index e98b9c6..7687ac7 100755 --- a/Model/Tpay.php +++ b/Model/Tpay.php @@ -213,7 +213,7 @@ public function getMerchantId(): int public function getOpenApiClientId() { - return $this->getConfigData('open_api_client_id'); + return $this->getConfigData('open_api_client_id') ?? ''; } public function getSecurityCode(): string diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index df4339f..557feb0 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -21,14 +21,6 @@ - - - validate-length maximum-length-64 - - - - validate-length maximum-length-64 - validate-no-empty validate-number validate-length maximum-length-10 @@ -45,8 +37,6 @@ validate-no-empty no-whitespace validate-length maximum-length-40 - - validate-no-empty no-whitespace validate-length maximum-length-126 @@ -69,12 +59,8 @@ - tpaycom\magento2cards\Model\Config\Source\HashTypes + tpaycom\magento2basic\Model\Config\Source\HashTypes - - - - Magento\Config\Model\Config\Source\Yesno @@ -120,8 +106,6 @@ tpaycom\magento2basic\Model\Config\Source\OnsiteChannels - - validate-length maximum-length-64 @@ -130,7 +114,6 @@ validate-length maximum-length-64 - validate-no-empty validate-length maximum-length-32