From 692db32cdf934b171b8a6a333c3fa6f37d40986a Mon Sep 17 00:00:00 2001 From: s4ddly <110701663+s4ddly@users.noreply.github.com> Date: Mon, 8 Jan 2024 10:36:35 +0100 Subject: [PATCH] GenericOnSite (#41) --- Api/TpayInterface.php | 3 +- Controller/tpay/Create.php | 33 ++- Controller/tpay/Redirect.php | 6 +- Model/ApiFacade/CardTransaction/CardOpen.php | 2 + Model/ApiFacade/OpenApi.php | 38 ++- Model/ApiFacade/TpayConfig/ConfigFacade.php | 4 + Model/ApiFacade/TpayConfig/ConfigOpen.php | 2 +- .../Transaction/TransactionApiFacade.php | 43 ++- Model/Config/Source/OnsiteChannels.php | 43 +++ Model/GenericOnSiteConfigProvider.php | 120 ++++++++ Model/GenericOnsite.php | 266 ++++++++++++++++++ Model/GenericPaymentPlugin.php | 18 ++ Model/Tpay.php | 11 +- Model/TpayConfig.php | 46 +++ etc/adminhtml/system.xml | 15 +- etc/config.xml | 18 ++ etc/di.xml | 6 + etc/frontend/di.xml | 9 +- etc/payment.xml | 6 + view/frontend/layout/checkout_index_index.xml | 3 + .../method-renderer/tpay-card-method.js | 106 +++++++ .../method-renderer/tpay-generic-onsite.js | 37 +++ .../web/js/view/payment/tpay-payments.js | 6 + .../web/template/payment/card-tpay-form.html | 6 +- .../web/template/payment/tpay-form.html | 153 ---------- .../template/payment/tpay-generic-onsite.html | 31 ++ 26 files changed, 832 insertions(+), 199 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-card-method.js 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/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 fd98d71..3e5963b 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); @@ -63,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'); } @@ -103,17 +119,22 @@ 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()) { $data['direct'] = 1; } } + if ($data['channel']) { + return $this->transaction->createWithInstantRedirection($data); + } + return $this->transaction->create($data); } 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/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/ApiFacade/OpenApi.php b/Model/ApiFacade/OpenApi.php index 10157a1..729525b 100755 --- a/Model/ApiFacade/OpenApi.php +++ b/Model/ApiFacade/OpenApi.php @@ -10,7 +10,15 @@ 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); + } + + public function createWithInstantRedirect(array $data) + { + $transactionData = $this->handleDataStructure($data); + $transaction = $this->transactions()->createTransactionWithInstantRedirection($transactionData); return $this->updateRedirectUrl($transaction); } @@ -20,6 +28,11 @@ 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,17 +48,12 @@ 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'] ], + 'notification' => ['url' => $data['result_url']], ], ]; @@ -55,6 +63,14 @@ private function handleDataStructure(array $data): array ]; } + if ($data['group']) { + $paymentData['pay'] = ['groupId' => $data['group']]; + } + + if ($data['channel']) { + $paymentData['pay'] = ['channelId' => $data['channel']]; + } + return $paymentData; } 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 cabe2bc..5c3114b 100755 --- a/Model/ApiFacade/Transaction/TransactionApiFacade.php +++ b/Model/ApiFacade/Transaction/TransactionApiFacade.php @@ -3,11 +3,16 @@ namespace tpaycom\magento2basic\Model\ApiFacade\Transaction; use Exception; +use Magento\Framework\App\CacheInterface; +use Tpay\OpenApi\Utilities\TpayException; 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 +22,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 +42,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/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..89bae51 --- /dev/null +++ b/Model/GenericOnSiteConfigProvider.php @@ -0,0 +1,120 @@ +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 new file mode 100644 index 0000000..64dbdae --- /dev/null +++ b/Model/GenericOnsite.php @@ -0,0 +1,266 @@ +_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 new file mode 100644 index 0000000..44670a6 --- /dev/null +++ b/Model/GenericPaymentPlugin.php @@ -0,0 +1,18 @@ +getConfigData('open_api_client_id'); + return $this->getConfigData('open_api_client_id') ?? ''; } public function getSecurityCode(): string @@ -233,7 +233,7 @@ public function onlyOnlineChannels(): bool public function redirectToChannel(): bool { - return (bool) $this->getConfigData('redirect_directly_to_channel'); + return true; } public function useSandboxMode(): bool @@ -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] : ''); @@ -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..557feb0 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -37,8 +37,6 @@ validate-no-empty no-whitespace validate-length maximum-length-40 - - validate-no-empty no-whitespace validate-length maximum-length-126 @@ -61,12 +59,8 @@ - tpaycom\magento2cards\Model\Config\Source\HashTypes + tpaycom\magento2basic\Model\Config\Source\HashTypes - - - - Magento\Config\Model\Config\Source\Yesno @@ -108,8 +102,10 @@ Leave empty for no limit validate-number - - + + + tpaycom\magento2basic\Model\Config\Source\OnsiteChannels + validate-length maximum-length-64 @@ -118,7 +114,6 @@ validate-length maximum-length-64 - validate-no-empty validate-length maximum-length-32 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..d0d4023 100644 --- a/etc/payment.xml +++ b/etc/payment.xml @@ -13,5 +13,11 @@ 1 + + 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-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/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..6f463c4 100644 --- a/view/frontend/web/js/view/payment/tpay-payments.js +++ b/view/frontend/web/js/view/payment/tpay-payments.js @@ -20,6 +20,12 @@ 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'})) + + + 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 @@ -
+
-
+
- - - - -
-
- - -
-
-
-
- -
-
- -
-
- - -
-
- -
-
-
- - - - - - -
- - - -
-
- - - -
-
-
- -
-
-
-
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 @@ +
+
+ + +
+
+ +
+
+ +
+
+
+