Skip to content

Commit

Permalink
Merge pull request #192 from Invertus/SL-244/performance-issues
Browse files Browse the repository at this point in the history
SL-244 performance issues in checkout
  • Loading branch information
MarijusCoding authored Oct 1, 2024
2 parents 17fdb72 + 08b2b1e commit 4524b9e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 33 deletions.
18 changes: 16 additions & 2 deletions saferpayofficial.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function hookPaymentOptions($params)
/** @var Invertus\SaferPay\Service\SaferPayCartService $assertService */
$cartService = $this->getService(\Invertus\SaferPay\Service\SaferPayCartService::class);
if (!$cartService->isCurrencyAvailable($params['cart'])) {
return;
return [];
}

/** @var \Invertus\SaferPay\Provider\PaymentTypeProvider $paymentTypeProvider */
Expand All @@ -206,15 +206,29 @@ public function hookPaymentOptions($params)
\Invertus\SaferPay\Service\PaymentRestrictionValidation::class
);

$logosEnabled = $paymentRepository->getAllActiveLogosNames();
$logosEnabled = array_column($logosEnabled, 'name');

$activePaymentMethods = $paymentRepository->getActivePaymentMethodsNames();
$activePaymentMethods = array_column($activePaymentMethods, 'name');


foreach ($paymentMethods as $paymentMethod) {
$paymentMethod['paymentMethod'] = str_replace(' ', '', $paymentMethod['paymentMethod']);

if(!in_array($paymentMethod['paymentMethod'], $activePaymentMethods)) {
continue;
}

if (!in_array($this->context->currency->iso_code, $paymentMethods[$paymentMethod['paymentMethod']]['currencies'])) {
continue;
}

if (!$paymentRestrictionValidation->isPaymentMethodValid($paymentMethod['paymentMethod'])) {
continue;
}

$imageUrl = ($paymentRepository->isLogoEnabledByName($paymentMethod['paymentMethod']))
$imageUrl = (in_array($paymentMethod['paymentMethod'], $logosEnabled))
? $paymentMethod['logoUrl'] : '';

$isCreditCard = in_array(
Expand Down
40 changes: 39 additions & 1 deletion src/Repository/SaferPayPaymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,52 @@ public function isLogoEnabledByName($paymentName)
return Db::getInstance()->getValue($query);
}

public function getAllActiveLogosNames()
{
$query = new DbQuery();
$query->select('name');
$query->from('saferpay_logo');
$query->where('active = "1"');

$result = Db::getInstance()->executeS($query);

if (!$result) {
return [];
}

return $result;
}

public function getActivePaymentMethods()
{
$query = new DbQuery();
$query->select('*');
$query->from('saferpay_payment');
$query->where('active = "1"');

return Db::getInstance()->executeS($query);
$result = Db::getInstance()->executeS($query);

if (!$result) {
return [];
}

return $result;
}

public function getActivePaymentMethodsNames()
{
$query = new DbQuery();
$query->select('name');
$query->from('saferpay_payment');
$query->where('active = "1"');

$result = Db::getInstance()->executeS($query);

if (!$result) {
return [];
}

return $result;
}

public function truncateTable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,10 @@ public function __construct(
*/
public function isValid($paymentName)
{
if (!$this->paymentRepository->isActiveByName($paymentName)) {
return false;
}

if (!$this->isCountrySupportedByPaymentName($paymentName)) {
return false;
}

if (!$this->isCurrencySupportedByPaymentName($paymentName)) {
return false;
}

return true;
}

Expand Down Expand Up @@ -135,26 +127,4 @@ private function isCountrySupportedByPaymentName($paymentName)

return $isCountryInList || $isAllCountries;
}

/**
* @param string $paymentName
*
* @return bool
*/
private function isCurrencySupportedByPaymentName($paymentName)
{
$enabledCurrencies = $this->getEnabledCurrenciesByPaymentName($paymentName);

if (in_array('0', $enabledCurrencies)) {
$enabledCurrencies = [];
$currencyOptions = $this->obtainPaymentMethods->obtainPaymentMethods()[$paymentName]['currencies'];
foreach ($currencyOptions as $isoCode) {
$enabledCurrencies[$isoCode] = $isoCode;
}

return in_array($this->legacyContext->getCurrencyIsoCode(), $enabledCurrencies);
}

return in_array($this->legacyContext->getCurrencyId(), $enabledCurrencies);
}
}

0 comments on commit 4524b9e

Please sign in to comment.