diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 729525b..82ddd80 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -15,7 +15,7 @@ public function create(array $data): array return $this->updateRedirectUrl($transaction); } - public function createWithInstantRedirect(array $data) + public function createWithInstantRedirect(array $data): array { $transactionData = $this->handleDataStructure($data); $transaction = $this->transactions()->createTransactionWithInstantRedirection($transactionData); @@ -23,9 +23,9 @@ public function createWithInstantRedirect(array $data) return $this->updateRedirectUrl($transaction); } - public function makeRefund(InfoInterface $payment, string $amount): array + public function makeRefund(InfoInterface $payment, float $amount): array { - return $this->Transactions->createRefundByTransactionId(['amount' => number_format($amount, 2)], $payment->getAdditionalInformation('transaction_id')); + return $this->transactions()->createRefundByTransactionId(['amount' => $amount], $payment->getAdditionalInformation('transaction_id')); } public function channels(): array diff --git a/Model/ApiFacade/TpayConfig/ConfigFacade.php b/Model/ApiFacade/TpayConfig/ConfigFacade.php index b92d5a9..562e744 100755 --- a/Model/ApiFacade/TpayConfig/ConfigFacade.php +++ b/Model/ApiFacade/TpayConfig/ConfigFacade.php @@ -17,14 +17,9 @@ class ConfigFacade /** @var bool */ private $useOpenApi; - /** - * @var TpayInterface - */ - private $tpay; public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService) { - $this->tpay = $tpay; $this->originApi = new ConfigOrigin($tpay, $assetRepository, $tokensService); $this->createOpenApiInstance($tpay, $assetRepository, $tokensService); } diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index 5c3114b..9d11d0f 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -32,17 +32,17 @@ public function __construct(TpayInterface $tpay, CacheInterface $cache) $this->cache = $cache; } - public function isOpenApiUse() + public function isOpenApiUse(): bool { return $this->useOpenApi; } - public function create(array $config) + public function create(array $config): array { return $this->getCurrentApi()->create($config); } - public function createWithInstantRedirection(array $config) + public function createWithInstantRedirection(array $config): array { if (!$this->useOpenApi) { throw new TpayException('OpenAPI not availabile - Failed to create transaction with instant redirection'); @@ -51,7 +51,7 @@ public function createWithInstantRedirection(array $config) return $this->openApi->createWithInstantRedirect($config); } - public function blik($blikTransactionId, $blikCode) + public function blik($blikTransactionId, $blikCode): array { return $this->originApi->blik($blikTransactionId, $blikCode); } @@ -72,7 +72,7 @@ public function channels(): array return $channel['available'] === true && empty($channel['constraints']) === true; }); - $this->cache->save(json_encode($channels), self::CHANNELS_CACHE_KEY, []); + $this->cache->save(json_encode($channels), self::CHANNELS_CACHE_KEY, [], self::CACHE_LIFETIME); return $channels; } diff --git a/Model/GenericOnSiteConfigProvider.php b/Model/GenericOnSiteConfigProvider.php deleted file mode 100644 index 89bae51..0000000 --- a/Model/GenericOnSiteConfigProvider.php +++ /dev/null @@ -1,120 +0,0 @@ -paymentHelper = $paymentHelper; - $this->assetRepository = $assetRepository; - $this->methodList = $methods; - $this->scopeConfig = $scopeConfig; - $this->transactionApiFacade = $transactionApiFacade; - $this->tpayConfigProvider = $tpayConfigProvider; - } - - /** - * @inheritDoc - */ - public function getConfig() - { - $tpay = $this->getPaymentMethodInstance(); - $onsites = explode(',', $this->scopeConfig->getValue('payment/tpaycom_magento2basic/onsite_channels', ScopeInterface::SCOPE_STORE)); - - $config = $this->tpayConfigProvider->getConfig(); - - $channels = $this->transactionApiFacade->channels(); - - foreach ($channels as $channel) { - $config['generic'][$channel['id']] = [ - 'id' => $channel['id'], - 'name' => $channel['fullName'], - 'logoUrl' => $channel['image']['url'], - ]; - } - - - return $config; - } - - /** - * @param string $name - * - * @return string - */ - public function generateURL($name) - { - return $this->assetRepository->createAsset($name)->getUrl(); - } - - /** @return null|string */ - public function showChannels() - { - $script = 'tpaycom_magento2basic::js/render_channels.js'; - - return $this->createScript($script); - } - - /** - * @param string $script - * - * @return string - */ - public function createScript($script) - { - return " - "; - } - - /** @return null|string */ - public function getTerms() - { - return $this->getPaymentMethodInstance()->getTermsURL(); - } - - /** - * @param string $css - * - * @return string - */ - public function createCSS($css) - { - return "generateURL($css)}\">"; - } - - /** @return MethodInterface|TpayInterface */ - protected function getPaymentMethodInstance() - { - if (null === $this->paymentMethod) { - $this->paymentMethod = $this->paymentHelper->getMethodInstance(TpayInterface::CODE); - } - - return $this->paymentMethod; - } -} diff --git a/Model/GenericOnsite.php b/Model/GenericOnsite.php deleted file mode 100644 index 64dbdae..0000000 --- a/Model/GenericOnsite.php +++ /dev/null @@ -1,266 +0,0 @@ -_isGateway = true; - $this->_canRefund = true; - $this->_canRefundInvoicePartial = true; - $this->_code = 'generic'; - $this->_title = 'generic'; - $this->storeManager = $storeManager; - } - - public function setCode(string $code): void - { - $this->_code = $code; - } - - public function setChannelId(int $channelId): void - { - $this->_channelId = $channelId; - } - - public function getChannelId(): int - { - return $this->_channelId; - } - - public function setTitle(string $title): void - { - $this->_title = $title; - } - - public function getConfigData($field, $storeId = null) - { - if (is_null($storeId)) { - $storeId = $this->storeManager->getStore()->getId(); - } - - return parent::getConfigData($field, $storeId); - } - - public function assignData(DataObject $data): GenericOnsite - { - /** @var array $additionalData */ - $additionalData = $data->getData('additional_data'); - - $info = $this->getInfoInstance(); - $info->setAdditionalInformation('channel', $additionalData['channel']); - - return $this; - } - - public function isActive($storeId = null) - { - return true; - } - - public function getTitle(): string - { - return $this->_title ?? $this->getConfigData('title'); - } - - public function getRedirectURL(): string - { - return ''; - } - - public function getTpayFormData($orderId = null): array - { - return []; - } - - public function getApiPassword(): string - { - return ''; - } - - public function getApiKey(): string - { - return ''; - } - - public function getSecurityCode(): string - { - return ''; - } - - public function getMerchantId(): int - { - return ''; - } - - public function checkBlikLevel0Settings(): bool - { - return false; - } - - public function getBlikLevelZeroStatus(): bool - { - return false; - } - - public function onlyOnlineChannels(): bool - { - return false; - } - - public function redirectToChannel(): bool - { - return true; - } - - public function getPaymentRedirectUrl(): string - { - return ''; - } - - public function getTermsURL(): string - { - return ''; - } - - public function getInvoiceSendMail(): string - { - return ''; - } - - public function getCheckProxy(): bool - { - return ''; - } - - public function getCheckTpayIP(): bool - { - return true; - } - - public function getInstallmentsAmountValid(): bool - { - return false; - } - - public function useSandboxMode(): bool - { - return true; - } - - public function getClientId(): string - { - return ''; - } - - 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/GenericPaymentPlugin.php b/Model/GenericPaymentPlugin.php index 44670a6..075fba6 100644 --- a/Model/GenericPaymentPlugin.php +++ b/Model/GenericPaymentPlugin.php @@ -3,14 +3,15 @@ namespace tpaycom\magento2basic\Model; use Magento\Quote\Model\Quote\Payment; +use tpaycom\magento2basic\Api\TpayInterface; class GenericPaymentPlugin { - public function beforeImportData(Payment $compiled, array $data) + public function beforeImportData(Payment $compiled, array $data): array { if (str_contains($data['method'], 'generic')) { $data['channel'] = explode('-', $data['method'])[1]; - $data['method'] = 'generic'; + $data['method'] = TpayInterface::CODE; } return [$data]; diff --git a/Model/TpayConfig.php b/Model/MethodListPlugin.php similarity index 55% rename from Model/TpayConfig.php rename to Model/MethodListPlugin.php index a232615..51eeec7 100644 --- a/Model/TpayConfig.php +++ b/Model/MethodListPlugin.php @@ -4,11 +4,13 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Payment\Helper\Data; +use Magento\Payment\Model\MethodInterface; use Magento\Payment\Model\MethodList; use Magento\Store\Model\ScopeInterface; +use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\Config\Source\OnsiteChannels; -class TpayConfig +class MethodListPlugin { private const CONFIG_PATH = 'payment/tpaycom_magento2basic/onsite_channels'; @@ -31,31 +33,26 @@ public function __construct(Data $data, ScopeConfigInterface $scopeConfig, Onsit public function afterGetAvailableMethods(MethodList $compiled, $result) { $onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE); + $channels = $onsiteChannels ? explode(',', $onsiteChannels) : []; - $secondTpay = false; - foreach ($result as $paymentMethod) { - if ($paymentMethod->getCode() == 'tpaycom_magento2basic') { - if ($secondTpay) { - $paymentMethod->setCode('tpaycom_magento2basic_cards'); - } else { - $secondTpay = true; - } - } - } + $result[] = $this->getMethodInstance('tpay.com - Płatność kartą', 'tpaycom_magento2basic_cards'); - if (!$onsiteChannels){ - return $result; + foreach ($channels as $onsiteChannel) { + $result[] = $this->getMethodInstance( + $this->onsiteChannels->getLabelFromValue($onsiteChannel), + "generic-$onsiteChannel" + ); } - foreach (explode(',', $onsiteChannels) as $onsiteChannel) { - $method = $this->data->getMethodInstance('generic'); - $method->setChannelId($onsiteChannel); - $method->setTitle($this->onsiteChannels->getLabelFromValue($onsiteChannel)); - $method->setCode("generic-".$onsiteChannel); + return $result; + } - $result[] = $method; - } + public function getMethodInstance(string $title, string $code): MethodInterface + { + $method = $this->data->getMethodInstance(TpayInterface::CODE); + $method->setTitle($title); + $method->setCode($code); - return $result; + return $method; } } diff --git a/Model/Tpay.php b/Model/Tpay.php index 4af2d7e..62078b4 100755 --- a/Model/Tpay.php +++ b/Model/Tpay.php @@ -112,6 +112,11 @@ public function getRedirectURL(): string return $this->redirectURL; } + public function setCode(string $code) + { + $this->_code = $code; + } + public function checkBlikLevel0Settings(): bool { if (!$this->getBlikLevelZeroStatus() || !$this->checkBlikAmount()) { @@ -236,6 +241,16 @@ public function redirectToChannel(): bool return true; } + public function setTitle(string $title): void + { + $this->_title = $title; + } + + public function getTitle(): string + { + return $this->_title ?? $this->getConfigData('title'); + } + public function useSandboxMode(): bool { return (bool) $this->getConfigData('use_sandbox'); @@ -275,6 +290,8 @@ public function assignData(DataObject $data) $info->setAdditionalInformation(static::BLIK_CODE, array_key_exists(static::BLIK_CODE, $additionalData) ? $additionalData[static::BLIK_CODE] : ''); + $info->setAdditionalInformation('channel', $additionalData['channel'] ?? null); + if (array_key_exists(static::TERMS_ACCEPT, $additionalData) && 1 === $additionalData[static::TERMS_ACCEPT]) { $info->setAdditionalInformation(static::TERMS_ACCEPT, 1); } diff --git a/Model/TpayConfigProvider.php b/Model/TpayConfigProvider.php index 57c6dc1..afb0094 100644 --- a/Model/TpayConfigProvider.php +++ b/Model/TpayConfigProvider.php @@ -7,9 +7,9 @@ use Magento\Checkout\Model\ConfigProviderInterface; use Magento\Framework\View\Asset\Repository; use Magento\Payment\Helper\Data as PaymentHelper; -use Magento\Payment\Model\MethodInterface; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\TpayConfig\ConfigFacade; +use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade; use tpaycom\magento2basic\Service\TpayTokensService; class TpayConfigProvider implements ConfigProviderInterface @@ -23,19 +23,37 @@ class TpayConfigProvider implements ConfigProviderInterface /** @var ConfigFacade */ protected $configFacade; - public function __construct(PaymentHelper $paymentHelper, Repository $assetRepository, TpayTokensService $tokensService) - { + /** @var TransactionApiFacade */ + protected $transactionApi; + + public function __construct( + PaymentHelper $paymentHelper, + Repository $assetRepository, + TpayTokensService $tokensService, + TransactionApiFacade $transactionApiFacade + ) { $this->paymentHelper = $paymentHelper; + $this->transactionApi = $transactionApiFacade; $this->configFacade = new ConfigFacade($this->getPaymentMethodInstance(), $assetRepository, $tokensService); } - public function getConfig() + public function getConfig(): array { - return $this->configFacade->getConfig(); + $config = $this->configFacade->getConfig(); + $channels = $this->transactionApi->channels(); + + foreach ($channels as $channel) { + $config['generic'][$channel['id']] = [ + 'id' => $channel['id'], + 'name' => $channel['fullName'], + 'logoUrl' => $channel['image']['url'], + ]; + } + + return $config; } - /** @return MethodInterface|TpayInterface */ - private function getPaymentMethodInstance() + private function getPaymentMethodInstance(): TpayInterface { if (null === $this->paymentMethod) { $this->paymentMethod = $this->paymentHelper->getMethodInstance(TpayInterface::CODE); diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 557feb0..f8bfec1 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -104,6 +104,7 @@ + 1 tpaycom\magento2basic\Model\Config\Source\OnsiteChannels diff --git a/etc/config.xml b/etc/config.xml index 3654c63..f5303c0 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -29,42 +29,6 @@ PLN 0 - - pending_payment - 0 - tpaycom\magento2basic\Model\Tpay - Karty - 0 - 0.10 - 1 - 1 - 0 - 0 - 1 - 0 - 1 - PLN - 1 - 0 - - - pending_payment - 0 - tpaycom\magento2basic\Model\GenericOnsite - Generic - 0 - 0.10 - 1 - 1 - 0 - 0 - 1 - 0 - 1 - PLN - 1 - 0 - diff --git a/etc/di.xml b/etc/di.xml index 7113f4b..bae4757 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -12,7 +12,7 @@ - + diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 3300093..7eaf0e8 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -13,7 +13,7 @@ - tpaycom\magento2basic\Model\GenericOnSiteConfigProvider + tpaycom\magento2basic\Model\TpayConfigProvider diff --git a/view/base/web/js/render_channels.js b/view/base/web/js/render_channels.js index 8b89277..7f299c7 100644 --- a/view/base/web/js/render_channels.js +++ b/view/base/web/js/render_channels.js @@ -155,7 +155,6 @@ require(['jquery', 'mage/translate'], function ($, $t) { } payButton.addClass('disabled'); }); - console.log(this) }); } );