From 47a5873b51317b71f5d2776b393e4a1bc7729a6a Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 5 Jan 2024 13:37:59 +0100 Subject: [PATCH 1/5] GenericOnSite --- Controller/tpay/Create.php | 27 ++- Model/ApiFacade/CardTransaction/CardOpen.php | 2 + Model/Config/Source/OnsiteChannels.php | 43 ++++ Model/GenericOnSiteConfigProvider.php | 133 +++++++++++ Model/GenericOnsite.php | 206 ++++++++++++++++++ Model/GenericPaymentPlugin.php | 18 ++ Model/Tpay.php | 7 +- Model/TpayConfig.php | 46 ++++ etc/adminhtml/system.xml | 12 + etc/config.xml | 18 ++ etc/di.xml | 6 + etc/frontend/di.xml | 9 +- etc/payment.xml | 3 + view/frontend/layout/checkout_index_index.xml | 3 + .../method-renderer/tpay-generic-onsite.js | 37 ++++ .../web/js/view/payment/tpay-payments.js | 3 + .../template/payment/tpay-generic-onsite.html | 31 +++ 17 files changed, 592 insertions(+), 12 deletions(-) create mode 100644 Model/Config/Source/OnsiteChannels.php create mode 100644 Model/GenericOnSiteConfigProvider.php create mode 100644 Model/GenericOnsite.php create mode 100644 Model/GenericPaymentPlugin.php create mode 100644 Model/TpayConfig.php create mode 100644 view/frontend/web/js/view/payment/method-renderer/tpay-generic-onsite.js create mode 100644 view/frontend/web/template/payment/tpay-generic-onsite.html diff --git a/Controller/tpay/Create.php b/Controller/tpay/Create.php index fd98d71..3507169 100644 --- a/Controller/tpay/Create.php +++ b/Controller/tpay/Create.php @@ -5,6 +5,7 @@ use Magento\Checkout\Model\Session; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; +use Magento\Framework\App\CacheInterface; use Tpay\OriginApi\Utilities\Util; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade; @@ -25,23 +26,38 @@ class Create extends Action /** @var TransactionApiFacade */ private $transaction; - public function __construct(Context $context, TpayInterface $tpayModel, TpayService $tpayService, Session $checkoutSession) - { + /** @var CacheInterface */ + private $cache; + + /** + * {@inheritdoc} + * @param TpayInterface $tpayModel + * @param TpayService $tpayService + */ + public function __construct( + Context $context, + TpayInterface $tpayModel, + TpayService $tpayService, + Session $checkoutSession, + CacheInterface $cache + ) { $this->tpay = $tpayModel; $this->tpayService = $tpayService; $this->checkoutSession = $checkoutSession; + $this->cache = $cache; Util::$loggingEnabled = false; parent::__construct($context); } + /** {@inheritdoc} */ public function execute() { $orderId = $this->checkoutSession->getLastRealOrderId(); if ($orderId) { $payment = $this->tpayService->getPayment($orderId); $paymentData = $payment->getData(); - $this->transaction = new TransactionApiFacade($this->tpay); + $this->transaction = new TransactionApiFacade($this->tpay, $this->cache); $additionalPaymentInformation = $paymentData['additional_information']; $transaction = $this->prepareTransaction($orderId, $additionalPaymentInformation); @@ -108,12 +124,17 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio $this->handleBlikData($data, $additionalPaymentInformation['blik_code']); } else { $data['group'] = (int) $additionalPaymentInformation['group']; + $data['channel'] = (int) ($additionalPaymentInformation['channel'] ?? null); if ($this->tpay->redirectToChannel()) { $data['direct'] = 1; } } + if ($data['channel']) { + return $this->transaction->createWithInstantRedirection($data); + } + return $this->transaction->create($data); } diff --git a/Model/ApiFacade/CardTransaction/CardOpen.php b/Model/ApiFacade/CardTransaction/CardOpen.php index b032956..49d6bbc 100755 --- a/Model/ApiFacade/CardTransaction/CardOpen.php +++ b/Model/ApiFacade/CardTransaction/CardOpen.php @@ -76,6 +76,8 @@ private function processSavedCardPayment(string $orderId, int $cardId): string return 'magento2basic/tpay/success'; } + $paymentResult = $result['payments'] ?? []; + if (isset($paymentResult['status']) && 'declined' === $paymentResult['status']) { $this->tpayService->addCommentToHistory($orderId, 'Failed to pay by saved card, Elavon rejection code: '.$paymentResult['reason']); } else { diff --git a/Model/Config/Source/OnsiteChannels.php b/Model/Config/Source/OnsiteChannels.php new file mode 100644 index 0000000..d379b2a --- /dev/null +++ b/Model/Config/Source/OnsiteChannels.php @@ -0,0 +1,43 @@ +transactions = new TransactionApiFacade($tpay, $cache); + } + + public function getLabelFromValue(int $value): ?string + { + foreach ($this->toOptionArray() as $option) { + if ($option['value'] === $value) { + return $option['label']; + } + } + + return null; + } + + /** + * @inheritDoc + * + * @return array{array{value: int, label: string}} + */ + public function toOptionArray(): array + { + return array_map(function (array $channel) { + return ['value' => (int) $channel['id'], 'label' => $channel['fullName']]; + }, $this->transactions->channels()); + } +} diff --git a/Model/GenericOnSiteConfigProvider.php b/Model/GenericOnSiteConfigProvider.php new file mode 100644 index 0000000..64e6e13 --- /dev/null +++ b/Model/GenericOnSiteConfigProvider.php @@ -0,0 +1,133 @@ +paymentHelper = $paymentHelper; + $this->assetRepository = $assetRepository; + $this->methodList = $methods; + $this->scopeConfig = $scopeConfig; + $this->transactionApiFacade = $transactionApiFacade; + } + + /** + * @inheritDoc + */ + public function getConfig() + { + $tpay = $this->getPaymentMethodInstance(); + $onsites = explode(',', $this->scopeConfig->getValue('payment/tpaycom_magento2basic/onsite_channels', ScopeInterface::SCOPE_STORE)); + + $config = [ + 'tpay' => [ + 'payment' => [ + 'redirectUrl' => $tpay->getPaymentRedirectUrl(), + 'tpayLogoUrl' => $this->generateURL('tpaycom_magento2basic::images/logo_tpay.png'), + 'merchantId' => $tpay->getMerchantId(), + 'showPaymentChannels' => $this->showChannels(), + 'getTerms' => $this->getTerms(), + 'addCSS' => $this->createCSS('tpaycom_magento2basic::css/tpay.css'), + 'blikStatus' => $this->getPaymentMethodInstance()->checkBlikLevel0Settings(), + 'onlyOnlineChannels' => $this->getPaymentMethodInstance()->onlyOnlineChannels(), + 'getBlikChannelID' => TransactionModel::BLIK_CHANNEL, + 'isInstallmentsAmountValid' => $this->getPaymentMethodInstance()->getInstallmentsAmountValid(), + ], + ], + ]; + + $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 new file mode 100644 index 0000000..c8b84ae --- /dev/null +++ b/Model/GenericOnsite.php @@ -0,0 +1,206 @@ +_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 ''; + } +} diff --git a/Model/GenericPaymentPlugin.php b/Model/GenericPaymentPlugin.php new file mode 100644 index 0000000..44670a6 --- /dev/null +++ b/Model/GenericPaymentPlugin.php @@ -0,0 +1,18 @@ +getConfigData('redirect_directly_to_channel'); + return true; } public function useSandboxMode(): bool @@ -448,4 +448,9 @@ private function getMagentoVersion() return $productMetadata->getVersion(); } + + public function getClientId(): string + { + return $this->getConfigData('client_id'); + } } diff --git a/Model/TpayConfig.php b/Model/TpayConfig.php new file mode 100644 index 0000000..08a48f5 --- /dev/null +++ b/Model/TpayConfig.php @@ -0,0 +1,46 @@ +data = $data; + $this->scopeConfig = $scopeConfig; + $this->onsiteChannels = $onsiteChannels; + } + + public function afterGetAvailableMethods(MethodList $compiled, $result) + { + $onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE); + + foreach (explode(',', $onsiteChannels) as $onsiteChannel) { + $method = $this->data->getMethodInstance('generic'); + $method->setChannelId($onsiteChannel); + $method->setTitle($this->onsiteChannels->getLabelFromValue($onsiteChannel)); + $method->setCode("generic-".$onsiteChannel); + + $result[] = $method; + } + + return $result; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index a17c546..df4339f 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -21,6 +21,14 @@ + + + validate-length maximum-length-64 + + + + validate-length maximum-length-64 + validate-no-empty validate-number validate-length maximum-length-10 @@ -108,6 +116,10 @@ Leave empty for no limit validate-number + + + tpaycom\magento2basic\Model\Config\Source\OnsiteChannels + diff --git a/etc/config.xml b/etc/config.xml index f5303c0..a5e6c07 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -29,6 +29,24 @@ PLN 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 f4a2b78..7113f4b 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -11,4 +11,10 @@ + + + + + + diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 9c150f7..3300093 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -9,18 +9,11 @@ */ --> - - - - tpaycom\magento2basic\Model\Tpay::CODE - - - - tpaycom\magento2basic\Model\TpayConfigProvider + tpaycom\magento2basic\Model\GenericOnSiteConfigProvider diff --git a/etc/payment.xml b/etc/payment.xml index 66ff6ab..701b0d0 100644 --- a/etc/payment.xml +++ b/etc/payment.xml @@ -13,5 +13,8 @@ 1 + + 1 + diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index 1bd606d..9e0456f 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -32,6 +32,9 @@ false + + false + diff --git a/view/frontend/web/js/view/payment/method-renderer/tpay-generic-onsite.js b/view/frontend/web/js/view/payment/method-renderer/tpay-generic-onsite.js new file mode 100644 index 0000000..d5b4a9c --- /dev/null +++ b/view/frontend/web/js/view/payment/method-renderer/tpay-generic-onsite.js @@ -0,0 +1,37 @@ +/** + * + * @category payment gateway + * @package Tpaycom_Magento2.3 + * @author Tpay.com + * @copyright (https://tpay.com) + */ +define( + [ + 'Magento_Checkout/js/view/payment/default', + 'jquery' + ], + function (Component, $) { + 'use strict'; + + return Component.extend({ + defaults: { + template: 'tpaycom_magento2basic/payment/tpay-generic-onsite' + }, + + afterPlaceOrder: function () { + window.location.replace(window.checkoutConfig.tpay.payment.redirectUrl); + }, + + getLogoUrl: function (code) { + const id = code.slice(code.indexOf('-') + 1); + + return window.checkoutConfig.generic[id].logoUrl; }, + + redirectAfterPlaceOrder: false, + + isActive: function () { + return true; + } + }); + } +); diff --git a/view/frontend/web/js/view/payment/tpay-payments.js b/view/frontend/web/js/view/payment/tpay-payments.js index 25ecaed..c917960 100644 --- a/view/frontend/web/js/view/payment/tpay-payments.js +++ b/view/frontend/web/js/view/payment/tpay-payments.js @@ -20,6 +20,9 @@ define( component: 'tpaycom_magento2basic/js/view/payment/method-renderer/tpay-method' } ); + + Object.values(window.checkoutConfig.generic).forEach((element) => rendererList.push({type: `generic-${element.id}`, component: 'tpaycom_magento2basic/js/view/payment/method-renderer/tpay-generic-onsite'})) + /** Add view logic here if needed */ return Component.extend({}); } diff --git a/view/frontend/web/template/payment/tpay-generic-onsite.html b/view/frontend/web/template/payment/tpay-generic-onsite.html new file mode 100644 index 0000000..4253f95 --- /dev/null +++ b/view/frontend/web/template/payment/tpay-generic-onsite.html @@ -0,0 +1,31 @@ +
+
+ + +
+
+ +
+
+ +
+
+
+
From 9810f372a454f80d0e5442364b70e92f4fb1aca3 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 5 Jan 2024 14:46:27 +0100 Subject: [PATCH 2/5] 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 From 12ea7565f89dc8a0332ca5cb626a532a254f20a3 Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 5 Jan 2024 16:07:52 +0100 Subject: [PATCH 3/5] WIP: fix cards --- Api/TpayInterface.php | 3 +- Controller/tpay/Create.php | 6 +- Controller/tpay/Redirect.php | 6 +- Model/ApiFacade/OpenApi.php | 4 +- Model/ApiFacade/TpayConfig/ConfigFacade.php | 4 + Model/ApiFacade/TpayConfig/ConfigOpen.php | 2 +- .../Transaction/TransactionApiFacade.php | 1 + Model/GenericOnSiteConfigProvider.php | 21 +-- Model/Tpay.php | 2 +- etc/payment.xml | 3 + .../method-renderer/tpay-card-method.js | 106 ++++++++++++ .../web/js/view/payment/tpay-payments.js | 3 + .../web/template/payment/card-tpay-form.html | 6 +- .../web/template/payment/tpay-form.html | 153 ------------------ 14 files changed, 138 insertions(+), 182 deletions(-) create mode 100644 view/frontend/web/js/view/payment/method-renderer/tpay-card-method.js diff --git a/Api/TpayInterface.php b/Api/TpayInterface.php index f0bc35e..21cb7ec 100644 --- a/Api/TpayInterface.php +++ b/Api/TpayInterface.php @@ -10,7 +10,8 @@ interface TpayInterface { public const CODE = 'tpaycom_magento2basic'; - public const CHANNEL = 'group'; + public const GROUP = 'group'; + public const CHANNEL = 'channel'; public const BLIK_CODE = 'blik_code'; public const TERMS_ACCEPT = 'accept_tos'; public const CARDDATA = 'card_data'; diff --git a/Controller/tpay/Create.php b/Controller/tpay/Create.php index 3507169..3e5963b 100644 --- a/Controller/tpay/Create.php +++ b/Controller/tpay/Create.php @@ -79,7 +79,7 @@ public function execute() $paymentData['additional_information']['transaction_url'] = $transactionUrl; $payment->setData($paymentData)->save(); - if (6 === strlen($additionalPaymentInformation['blik_code']) && $this->tpay->checkBlikLevel0Settings()) { + if (6 === strlen($additionalPaymentInformation['blik_code'] ?? '') && $this->tpay->checkBlikLevel0Settings()) { if (true === $this->transaction->isOpenApiUse()) { return $this->_redirect('magento2basic/tpay/success'); } @@ -119,11 +119,11 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio { $data = $this->tpay->getTpayFormData($orderId); - if (6 === strlen($additionalPaymentInformation['blik_code'])) { + if (6 === strlen($additionalPaymentInformation['blik_code'] ?? '')) { $data['group'] = TransactionOriginApi::BLIK_CHANNEL; $this->handleBlikData($data, $additionalPaymentInformation['blik_code']); } else { - $data['group'] = (int) $additionalPaymentInformation['group']; + $data['group'] = (int) ($additionalPaymentInformation['group'] ?? null); $data['channel'] = (int) ($additionalPaymentInformation['channel'] ?? null); if ($this->tpay->redirectToChannel()) { diff --git a/Controller/tpay/Redirect.php b/Controller/tpay/Redirect.php index 36c6871..d1f3ba4 100644 --- a/Controller/tpay/Redirect.php +++ b/Controller/tpay/Redirect.php @@ -30,9 +30,11 @@ public function execute() { $uid = $this->getRequest()->getParam('uid'); $orderId = $this->checkoutSession->getLastRealOrderId(); + if (!$orderId || !$uid) { return $this->_redirect('checkout/cart'); } + $payment = $this->tpayService->getPayment($orderId); $paymentData = $payment->getData(); $additionalPaymentInfo = $paymentData['additional_information']; @@ -41,7 +43,9 @@ public function execute() return $this->_redirect('magento2basic/tpay/CardPayment'); } - if ((!array_key_exists(TpayInterface::CHANNEL, $additionalPaymentInfo) || (int) $additionalPaymentInfo[TpayInterface::CHANNEL] < 1) && (!array_key_exists(TpayInterface::BLIK_CODE, $additionalPaymentInfo) || 6 !== strlen($additionalPaymentInfo[TpayInterface::BLIK_CODE]))) { + + + if (empty(array_intersect(array_keys($additionalPaymentInfo), [TpayInterface::GROUP, TpayInterface::CHANNEL])) && (!array_key_exists(TpayInterface::BLIK_CODE, $additionalPaymentInfo) || 6 !== strlen($additionalPaymentInfo[TpayInterface::BLIK_CODE]))) { return $this->_redirect('checkout/cart'); } $this->tpayService->setOrderStatePendingPayment($orderId, true); diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 48b2884..db42742 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -62,11 +62,11 @@ private function handleDataStructure(array $data): array ]; } - if (isset($data['group'])) { + if ($data['group']) { $paymentData['pay'] = ['groupId' => $data['group']]; } - if (isset($data['channel'])) { + if ($data['channel']) { $paymentData['pay'] = ['channelId' => $data['channel']]; } diff --git a/Model/ApiFacade/TpayConfig/ConfigFacade.php b/Model/ApiFacade/TpayConfig/ConfigFacade.php index 686cf9e..b92d5a9 100755 --- a/Model/ApiFacade/TpayConfig/ConfigFacade.php +++ b/Model/ApiFacade/TpayConfig/ConfigFacade.php @@ -17,6 +17,10 @@ class ConfigFacade /** @var bool */ private $useOpenApi; + /** + * @var TpayInterface + */ + private $tpay; public function __construct(TpayInterface $tpay, Repository $assetRepository, TpayTokensService $tokensService) { diff --git a/Model/ApiFacade/TpayConfig/ConfigOpen.php b/Model/ApiFacade/TpayConfig/ConfigOpen.php index 68e0c88..155fd34 100755 --- a/Model/ApiFacade/TpayConfig/ConfigOpen.php +++ b/Model/ApiFacade/TpayConfig/ConfigOpen.php @@ -47,7 +47,7 @@ public function getConfig(): array ]; $config = array_merge($config, $this->getCardConfig()); - return $this->tpay->isAvailable() ? $config : []; + return $config; } public function generateURL(string $name): string diff --git a/Model/ApiFacade/Transaction/TransactionApiFacade.php b/Model/ApiFacade/Transaction/TransactionApiFacade.php index 4aac618..5c3114b 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -4,6 +4,7 @@ use Exception; use Magento\Framework\App\CacheInterface; +use Tpay\OpenApi\Utilities\TpayException; use tpaycom\magento2basic\Api\TpayInterface; use tpaycom\magento2basic\Model\ApiFacade\OpenApi; diff --git a/Model/GenericOnSiteConfigProvider.php b/Model/GenericOnSiteConfigProvider.php index 4c2bef0..89bae51 100644 --- a/Model/GenericOnSiteConfigProvider.php +++ b/Model/GenericOnSiteConfigProvider.php @@ -23,13 +23,15 @@ public function __construct( Repository $assetRepository, MethodList $methods, ScopeConfigInterface $scopeConfig, - TransactionApiFacade $transactionApiFacade + TransactionApiFacade $transactionApiFacade, + TpayConfigProvider $tpayConfigProvider ) { $this->paymentHelper = $paymentHelper; $this->assetRepository = $assetRepository; $this->methodList = $methods; $this->scopeConfig = $scopeConfig; $this->transactionApiFacade = $transactionApiFacade; + $this->tpayConfigProvider = $tpayConfigProvider; } /** @@ -40,22 +42,7 @@ public function getConfig() $tpay = $this->getPaymentMethodInstance(); $onsites = explode(',', $this->scopeConfig->getValue('payment/tpaycom_magento2basic/onsite_channels', ScopeInterface::SCOPE_STORE)); - $config = [ - 'tpay' => [ - 'payment' => [ - 'redirectUrl' => $tpay->getPaymentRedirectUrl(), - 'tpayLogoUrl' => $this->generateURL('tpaycom_magento2basic::images/logo_tpay.png'), - 'merchantId' => $tpay->getMerchantId(), - 'showPaymentChannels' => $this->showChannels(), - 'getTerms' => $this->getTerms(), - 'addCSS' => $this->createCSS('tpaycom_magento2basic::css/tpay.css'), - 'blikStatus' => $this->getPaymentMethodInstance()->checkBlikLevel0Settings(), - 'onlyOnlineChannels' => $this->getPaymentMethodInstance()->onlyOnlineChannels(), - 'getBlikChannelID' => TransactionOriginApi::BLIK_CHANNEL, - 'isInstallmentsAmountValid' => $this->getPaymentMethodInstance()->getInstallmentsAmountValid(), - ], - ], - ]; + $config = $this->tpayConfigProvider->getConfig(); $channels = $this->transactionApiFacade->channels(); diff --git a/Model/Tpay.php b/Model/Tpay.php index 7687ac7..4af2d7e 100755 --- a/Model/Tpay.php +++ b/Model/Tpay.php @@ -271,7 +271,7 @@ public function assignData(DataObject $data) $additionalData = $data->getData('additional_data'); $info = $this->getInfoInstance(); - $info->setAdditionalInformation(static::CHANNEL, array_key_exists(static::CHANNEL, $additionalData) ? $additionalData[static::CHANNEL] : ''); + $info->setAdditionalInformation(static::GROUP, array_key_exists(static::GROUP, $additionalData) ? $additionalData[static::GROUP] : ''); $info->setAdditionalInformation(static::BLIK_CODE, array_key_exists(static::BLIK_CODE, $additionalData) ? $additionalData[static::BLIK_CODE] : ''); diff --git a/etc/payment.xml b/etc/payment.xml index 701b0d0..d0d4023 100644 --- a/etc/payment.xml +++ b/etc/payment.xml @@ -13,6 +13,9 @@ 1 + + 1 + 1 diff --git a/view/frontend/web/js/view/payment/method-renderer/tpay-card-method.js b/view/frontend/web/js/view/payment/method-renderer/tpay-card-method.js new file mode 100644 index 0000000..c0dfd73 --- /dev/null +++ b/view/frontend/web/js/view/payment/method-renderer/tpay-card-method.js @@ -0,0 +1,106 @@ +/** + * + * @category payment gateway + * @package Tpaycom_Magento2.3 + * @author Tpay.com + * @copyright (https://tpay.com) + */ +define( + [ + 'Magento_Checkout/js/view/payment/default', + 'jquery', + 'Magento_Checkout/js/model/totals' + ], + function (Component, $) { + 'use strict'; + + return Component.extend({ + defaults: { + template: 'tpaycom_magento2basic/payment/card-tpay-form' + }, + + redirectAfterPlaceOrder: false, + + getCode: function () { + return 'tpaycom_magento2basic_cards'; + }, + + afterPlaceOrder: function () { + $("#card_number").val(''); + $("#cvc").val(''); + $("#expiry_date").val(''); + $("#loading_scr").fadeIn(); + window.location.replace(window.checkoutConfig.tpay.payment.redirectUrl); + }, + + showPaymentChannels: function () { + return window.checkoutConfig.tpay.payment.showPaymentChannels; + }, + + getTerms: function () { + return window.checkoutConfig.tpay.payment.getTerms; + }, + + getLogoUrl: function () { + return window.checkoutConfig.tpay.payment.tpayLogoUrl; + }, + + blikStatus: function () { + return window.checkoutConfig.tpay.payment.blikStatus; + }, + + addCSS: function () { + return window.checkoutConfig.tpay.payment.addCSS; + }, + + cardFetchJavaScripts: function () { + return window.checkoutConfig.tpaycards.payment.fetchJavaScripts; + }, + cardGetRSAkey: function () { + return window.checkoutConfig.tpaycards.payment.getRSAkey; + }, + cardGetLogoUrl: function () { + return window.checkoutConfig.tpaycards.payment.tpayLogoUrl; + }, + cardGetTpayLoadingGif: function () { + return window.checkoutConfig.tpaycards.payment.getTpayLoadingGif; + }, + cardAddCSS: function () { + return window.checkoutConfig.tpaycards.payment.addCSS; + }, + + cardShowSaveBox: function () { + if (window.checkoutConfig.tpaycards.payment.isCustomerLoggedIn + && window.checkoutConfig.tpaycards.payment.isSavingEnabled) { + $('#tpay-card-save-checkbox').css('display', 'block'); + } + }, + + getData: function () { + var savedId = 'new'; + $('input[id^=cardN]').each(function () { + if ($(this).is(":checked")) { + savedId = $(this).val(); + } + }); + var parent = this._super(), + paymentData = {}; + paymentData['group'] = $('#tpay-channel-input').val(); + paymentData['blik_code'] = $('#blik_code').val(); + paymentData['accept_tos'] = $('#accept_tos').is(':checked'); + + paymentData['card_data'] = $('input[name="card_data"]').val(); + paymentData['card_save'] = $('input[name="card_save"]').is(":checked"); + paymentData['card_id'] = savedId; + paymentData['card_vendor'] = $('input[name="card_vendor"]').val(); + paymentData['short_code'] = $('input[name="card_short_code"]').val(); + + return $.extend(true, parent, {'additional_data': paymentData}); + }, + + isActive: function () { + return true; + }, + }); + } +); diff --git a/view/frontend/web/js/view/payment/tpay-payments.js b/view/frontend/web/js/view/payment/tpay-payments.js index c917960..6f463c4 100644 --- a/view/frontend/web/js/view/payment/tpay-payments.js +++ b/view/frontend/web/js/view/payment/tpay-payments.js @@ -23,6 +23,9 @@ define( Object.values(window.checkoutConfig.generic).forEach((element) => rendererList.push({type: `generic-${element.id}`, component: 'tpaycom_magento2basic/js/view/payment/method-renderer/tpay-generic-onsite'})) + + rendererList.push({type: 'tpaycom_magento2basic_cards', component: 'tpaycom_magento2basic/js/view/payment/method-renderer/tpay-card-method'}); + /** Add view logic here if needed */ return Component.extend({}); } diff --git a/view/frontend/web/template/payment/card-tpay-form.html b/view/frontend/web/template/payment/card-tpay-form.html index 4c4ab06..dbfbdbb 100644 --- a/view/frontend/web/template/payment/card-tpay-form.html +++ b/view/frontend/web/template/payment/card-tpay-form.html @@ -1,16 +1,16 @@ -
+
-
+
- - - - -
-
- - -
-
-
-
- -
-
- -
-
- - -
-
- -
-
-
- - - - - - -
- - - -
-
- - - -
-
-
- -
-
-
-
From 15aac380267f7ab198191d2223f78a2588261763 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 8 Jan 2024 10:32:51 +0100 Subject: [PATCH 4/5] Fix notifications --- Model/ApiFacade/OpenApi.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index db42742..868d543 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -53,6 +53,7 @@ private function handleDataStructure(array $data): array "success" => $data['return_url'], "error" => $data['return_error_url'] ], + 'notification' => ['url' => $data['result_url']], ], ]; From d51afec801ec4aecc56335ec2b3a712d3e04d06c Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 8 Jan 2024 10:36:08 +0100 Subject: [PATCH 5/5] Fix deprecations --- Model/ApiFacade/OpenApi.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Model/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 868d543..729525b 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -10,7 +10,7 @@ class OpenApi extends TpayApi public function create(array $data): array { $transactionData = $this->handleDataStructure($data); - $transaction = $this->Transactions->createTransaction($transactionData); + $transaction = $this->transactions()->createTransaction($transactionData); return $this->updateRedirectUrl($transaction); } @@ -18,7 +18,7 @@ public function create(array $data): array public function createWithInstantRedirect(array $data) { $transactionData = $this->handleDataStructure($data); - $transaction = $this->Transactions->createTransactionWithInstantRedirection($transactionData); + $transaction = $this->transactions()->createTransactionWithInstantRedirection($transactionData); return $this->updateRedirectUrl($transaction); }