Skip to content

Commit

Permalink
check is on site channels available (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Jul 8, 2024
1 parent 2e41e2c commit 2a1ddaf
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 80 deletions.
2 changes: 2 additions & 0 deletions Api/TpayConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function redirectToChannel(): bool;
/** Return url for a tpay.com terms */
public function getTermsURL(): string;

public function getRegulationsURL(): string;

/** Check if send an email about the new invoice to customer */
public function getInvoiceSendMail(): string;

Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(TpayConfigInterface $tpay, CacheInterface $cache)
}
$this->tpayApi->transactions()->setClientName($tpay->buildMagentoInfo());
if (!$token) {
$this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay));
$this->cache->save(serialize($this->tpayApi->getToken()), $this->getAuthTokenCacheKey($tpay), [\Magento\Framework\App\Config::CACHE_TAG], 7100);
}
}

Expand Down
8 changes: 7 additions & 1 deletion Model/ApiFacade/TpayConfig/ConfigOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function getConfig(): array
'tpayCardsLogoUrl' => $this->generateURL('Tpay_Magento2::images/card.svg'),
'showPaymentChannels' => $this->showChannels(),
'getTerms' => $this->getTerms(),
'getRegulations' => $this->getRegulations(),
'addCSS' => $this->createCSS('Tpay_Magento2::css/tpay.css'),
'blikStatus' => $this->tpay->checkBlikLevel0Settings(),
'getBlikChannelID' => TransactionOriginApi::BLIK_CHANNEL,
Expand Down Expand Up @@ -81,6 +82,11 @@ public function getTerms(): ?string
return $this->tpayConfig->getTermsURL();
}

public function getRegulations(): ?string
{
return $this->tpayConfig->getRegulationsURL();
}

public function createCSS(string $css): string
{
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->generateURL($css)}\">";
Expand Down Expand Up @@ -115,6 +121,7 @@ public function getCardConfig()
'customerTokens' => $customerTokensData,
'isSavingEnabled' => $this->tpayConfig->getCardSaveEnabled(),
'getTerms' => $this->getTerms(),
'getRegulations' => $this->getRegulations(),
],
],
];
Expand All @@ -128,7 +135,6 @@ public function fetchJavaScripts()
$script[] = 'Tpay_Magento2::js/string_routines.js';
$script[] = 'Tpay_Magento2::js/tpayCards.js';
$script[] = 'Tpay_Magento2::js/renderSavedCards.js';
$script[] = 'Tpay_Magento2::js/tpayGeneric.js';
$scripts = '';

foreach ($script as $key => $value) {
Expand Down
7 changes: 7 additions & 0 deletions Model/ApiFacade/TpayConfig/ConfigOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function getConfig(): array
'merchantId' => $this->tpayConfig->getMerchantId(),
'showPaymentChannels' => $this->showChannels(),
'getTerms' => $this->getTerms(),
'getRegulations' => $this->getRegulations(),
'addCSS' => $this->createCSS('Tpay_Magento2::css/tpay.css'),
'blikStatus' => $this->tpay->checkBlikLevel0Settings(),
'onlyOnlineChannels' => $this->tpayConfig->onlyOnlineChannels(),
Expand Down Expand Up @@ -82,6 +83,11 @@ public function getTerms(): ?string
return $this->tpayConfig->getTermsURL();
}

public function getRegulations(): ?string
{
return $this->tpayConfig->getRegulationsURL();
}

public function createCSS(string $css): string
{
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$this->generateURL($css)}\">";
Expand Down Expand Up @@ -115,6 +121,7 @@ public function getCardConfig()
'customerTokens' => $customerTokensData,
'isSavingEnabled' => $this->tpayConfig->getCardSaveEnabled(),
'getTerms' => $this->getTerms(),
'getRegulations' => $this->getRegulations(),
],
],
];
Expand Down
7 changes: 4 additions & 3 deletions Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

class TransactionApiFacade
{
private const CHANNELS_CACHE_KEY = 'tpay_channels';
private const CACHE_LIFETIME = 86400;

/** @var TransactionOriginApi */
Expand Down Expand Up @@ -75,7 +74,9 @@ public function channels(): array
return [];
}

$channels = $this->cache->load(self::CHANNELS_CACHE_KEY);
$cacheKey = 'tpay_channels_'.md5(join('|', [$this->tpay->getOpenApiClientId(), $this->tpay->getOpenApiPassword(), !$this->tpay->useSandboxMode()]));

$channels = $this->cache->load($cacheKey);

if ($channels) {
return unserialize($channels);
Expand All @@ -85,7 +86,7 @@ public function channels(): array
return true === $channel->available;
});

$this->cache->save(serialize($channels), self::CHANNELS_CACHE_KEY, [], self::CACHE_LIFETIME);
$this->cache->save(serialize($channels), $cacheKey, [\Magento\Framework\App\Config::CACHE_TAG], self::CACHE_LIFETIME);

return $channels;
}
Expand Down
13 changes: 12 additions & 1 deletion Model/ConstraintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct(Session $session)
$this->checkoutSession = $session;
}

public function validate(array $constraints): bool
public function validate(array $constraints, string $browser): bool
{
foreach ($constraints as $constraint) {
switch ($constraint['type']) {
Expand All @@ -31,6 +31,12 @@ public function validate(array $constraints): bool
return false;
}

break;
case 'supported':
if (!$this->validateBrowser($constraint['field'], $browser)) {
return false;
}

break;
default:
break;
Expand All @@ -54,4 +60,9 @@ private function validateMaximalTotal(float $maximal): bool
{
return $this->checkoutSession->getQuote()->getBaseGrandTotal() <= $maximal;
}

private function validateBrowser(string $browserSupport, string $browser): bool
{
return !('ApplePaySession' == $browserSupport && 'Safari' != $browser);
}
}
48 changes: 41 additions & 7 deletions Model/MethodListPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tpay\Magento2\Model;

use Laminas\Http\PhpEnvironment\Request;
use Magento\Checkout\Model\Session;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Helper\Data;
Expand All @@ -20,6 +21,9 @@ class MethodListPlugin
/** @var TpayInterface */
protected $paymentMethod;

/** @var Request */
protected $request;

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

Expand Down Expand Up @@ -53,7 +57,8 @@ public function __construct(
Session $checkoutSession,
TransactionApiFacade $transactions,
ConstraintValidator $constraintValidator,
TpayInterface $paymentMethod
TpayInterface $paymentMethod,
Request $request
) {
$this->data = $data;
$this->scopeConfig = $scopeConfig;
Expand All @@ -64,6 +69,7 @@ public function __construct(
$this->transactions = $transactions;
$this->constraintValidator = $constraintValidator;
$this->paymentMethod = $paymentMethod;
$this->request = $request;
}

public function afterGetAvailableMethods(MethodList $compiled, $result)
Expand All @@ -72,12 +78,10 @@ public function afterGetAvailableMethods(MethodList $compiled, $result)
return $result;
}

$onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);
$channelList = $onsiteChannels ? explode(',', $onsiteChannels) : [];
$channels = $this->transactions->channels();
$conutryId = $this->checkoutSession->getQuote()->getBillingAddress()->getCountryId();
$countryId = $this->checkoutSession->getQuote()->getBillingAddress()->getCountryId();
[$channelList, $channels] = $this->getChannels();

if ($conutryId && $this->constraintValidator->isClientCountryValid($this->tpayConfig->isAllowSpecific(), $conutryId, $this->tpayConfig->getSpecificCountry())) {
if ($countryId && $this->constraintValidator->isClientCountryValid($this->tpayConfig->isAllowSpecific(), $countryId, $this->tpayConfig->getSpecificCountry())) {
return [];
}

Expand All @@ -92,10 +96,12 @@ public function afterGetAvailableMethods(MethodList $compiled, $result)
return $result;
}

$browser = $this->getBrowser();

foreach ($channelList as $onsiteChannel) {
$channel = $channels[$onsiteChannel];

if (!empty($channel->constraints) && !$this->constraintValidator->validate($channel->constraints)) {
if (!empty($channel->constraints) && !$this->constraintValidator->validate($channel->constraints, $browser)) {
continue;
}

Expand Down Expand Up @@ -146,4 +152,32 @@ private function filterTransaction(array $result): array
return 'Tpay_Magento2' !== $method->getCode();
});
}

private function getChannels(): array
{
$onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);
$channelList = $onsiteChannels ? explode(',', $onsiteChannels) : [];
$channels = $this->transactions->channels();

$flippedChannels = array_flip(array_keys($channels));
$channelList = array_filter($channelList, function ($value) use ($flippedChannels) {
return isset($flippedChannels[$value]);
});

return [$channelList, $channels];
}

private function getBrowser(): string
{
$userAgent = $this->request->getHeader('User-Agent')->getFieldValue();

if (strpos($userAgent, 'Chrome')) {
return 'Chrome';
}
if (strpos($userAgent, 'Safari')) {
return 'Safari';
}

return 'Other';
}
}
11 changes: 11 additions & 0 deletions Provider/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ConfigurationProvider implements TpayConfigInterface

protected $termsURL = 'https://secure.tpay.com/regulamin.pdf';
protected $termsEnURL = 'https://tpay.com/user/assets/files_for_download/payment-terms-and-conditions.pdf';
protected $regulationsURL = 'https://tpay.com/user/assets/files_for_download/klauzula-informacyjna-platnik.pdf';
protected $regulationsEnURL = 'https://tpay.com/user/assets/files_for_download/information-clause-payer.pdf';

/** @var ScopeConfigInterface */
protected $scopeConfig;
Expand Down Expand Up @@ -87,6 +89,15 @@ public function getTermsURL(): string
return $this->termsEnURL;
}

public function getRegulationsURL(): string
{
if ('pl' == substr($this->localeResolver->getLocale(), 0, 2)) {
return $this->regulationsURL;
}

return $this->regulationsEnURL;
}

public function getOpenApiPassword(): ?string
{
return $this->getConfigData('openapi_settings/open_api_password');
Expand Down
4 changes: 4 additions & 0 deletions i18n/pl_PL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@
"Cards payments On-site settings","Konfiguracja kart płatniczych On-site"
"Cards payments Origin API settings","Konfiguracja kart płatniczych Origin API"
"Additional settings","Dodatkowe ustawienia"
"By paying, you accept ","Płacąc, akceptujesz "
"regulations","regulamin"
"The personal data administrator is Krajowy Integrator Płatności S.A with its registered office in Poznań. ","Administratorem danych osobowych jest Krajowy Integrator Płatności spółka akcyjna z siedzibą w Poznaniu."
"Show full content","Zapoznaj się z pełną treścią"
1 change: 1 addition & 0 deletions view/base/web/css/tpay.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions view/base/web/js/open_render_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ require(['jquery', 'mage/translate'], function ($, $t) {
return true;
}

function isBrowserSupport(channelId) {
if (channelId === 170) { //ApplePay
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
}

return true;
}

function ShowChannelsCombo() {
var str = '',
i,
Expand All @@ -71,6 +79,10 @@ require(['jquery', 'mage/translate'], function ($, $t) {
continue;
}

if (!isBrowserSupport(parseInt(id))) {
continue;
}

tile = getBankTile(id, groupName, logoSrc);

if (inArray(id, others) === false) {
Expand Down Expand Up @@ -132,18 +144,5 @@ require(['jquery', 'mage/translate'], function ($, $t) {
checkBlikInput();
setBlikInputAction();
payButton.addClass('disabled');
tos.on('change', function () {
var input = $('#tpay-channel-input');
if (input.val() > 0 && $('#blik_code').val().length === 0 && tos.is(':checked')) {
payButton.removeClass('disabled');
return;
}

if ($('#blik_code').val().length === 6 && tos.is(':checked')) {
payButton.removeClass('disabled');
return;
}
payButton.addClass('disabled');
});
}
);
8 changes: 0 additions & 8 deletions view/base/web/js/renderSavedCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ require(['jquery', 'mage/translate'], function ($, $t) {
}

$('document').ready(function () {
var tos = $('#card_accept_tos');

renderForm();
tos.on('change', function () {
if (tos.is(':checked')) {
$("#tpaycom_magento2cards_submit").removeClass('disabled');
}
});
});

});
24 changes: 0 additions & 24 deletions view/base/web/js/render_channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,6 @@ require(['jquery', 'mage/translate'], function ($, $t) {
checkBlikInput();
setBlikInputAction();
payButton.addClass('disabled');
tos.on('change', function () {
var input = $('#tpay-channel-input');
if (input.val() > 0 && $('#blik_code').val().length === 0 && tos.is(':checked')) {
payButton.removeClass('disabled');
return;
}

if ($('#blik_code').val().length === 6 && tos.is(':checked')) {
payButton.removeClass('disabled');
return;
}
payButton.addClass('disabled');
});
});

$("#tpaycom_magento2generic_submit", '.payment-method').addClass('disabled');
Expand All @@ -166,16 +153,5 @@ require(['jquery', 'mage/translate'], function ($, $t) {
var submitBtn = $("#tpaycom_magento2generic_submit", parent);
submitBtn.addClass('disabled');
});

$("input[name='accept_tos']").on("click", function () {
var parent = $(this).closest('.payment-method-content');
var submitBtn = $("#tpaycom_magento2generic_submit", parent);

if ($('#generic_accept_tos', parent).is(':checked')) {
submitBtn.removeClass('disabled');
} else {
submitBtn.addClass('disabled');
}
});
}
);
4 changes: 0 additions & 4 deletions view/base/web/js/tpayCards.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ require(['jquery', 'mage/translate'], function ($, $t) {
cvcInput.on(TRIGGER_EVENTS, function () {
validateCvc($(this));
});

tos.on('change', function () {
enablePayment();
});
}

$(document).ready(function () {
Expand Down
Loading

0 comments on commit 2a1ddaf

Please sign in to comment.