Skip to content

Commit

Permalink
Constraints validation
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Jan 15, 2024
1 parent 9bc7c60 commit 89e85d5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
9 changes: 6 additions & 3 deletions Model/ApiFacade/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public function makeRefund(InfoInterface $payment, float $amount): array
public function channels(): array
{
$result = $this->transactions()->getChannels();
$channels = [];

return array_map(function (array $channel) {
return new Channel(
foreach ($result['channels'] ?? [] as $channel) {
$channels[$channel['id']] = new Channel(
$channel['id'],
$channel['name'],
$channel['fullName'],
Expand All @@ -48,7 +49,9 @@ public function channels(): array
$channel['groups'],
$channel['constraints']
);
}, $result['channels'] ?? []);
}

return $channels;
}

private function handleDataStructure(array $data): array
Expand Down
2 changes: 1 addition & 1 deletion Model/ApiFacade/Transaction/TransactionApiFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function channels(): array
}

$channels = array_filter($this->openApi->channels(), function (Channel $channel) {
return true === $channel->available && true === empty($channel->constraints);
return true === $channel->available;
});

$this->cache->save(serialize($channels), self::CHANNELS_CACHE_KEY, [], self::CACHE_LIFETIME);
Expand Down
52 changes: 52 additions & 0 deletions Model/ConstraintValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace tpaycom\magento2basic\Model;

use Magento\Checkout\Model\Session;

class ConstraintValidator
{
/** @var Session */
private $checkoutSession;

public function __construct(Session $session)
{
$this->checkoutSession = $session;
}

public function validate(array $constraints): bool
{
foreach ($constraints as $constraint) {
switch ($constraint['type']) {
case 'min':
if (!$this->validateMinimalTotal((float) $constraint['value'])) {
return false;
}

break;
case 'max':
if (!$this->validateMaximalTotal((float) $constraint['value'])) {
return false;
}

break;
default:
break;
}
}

return true;
}

private function validateMinimalTotal(float $minimal): bool
{
return $this->checkoutSession->getQuote()->getGrandTotal() > $minimal;
}

private function validateMaximalTotal(float $maximal): bool
{
return $this->checkoutSession->getQuote()->getGrandTotal() < $maximal;
}
}
39 changes: 32 additions & 7 deletions Model/MethodListPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use tpaycom\magento2basic\Api\TpayInterface;
use tpaycom\magento2basic\Model\ApiFacade\Transaction\TransactionApiFacade;
use tpaycom\magento2basic\Model\Config\Source\OnsiteChannels;

class MethodListPlugin
{
private const CONFIG_PATH = 'payment/tpaycom_magento2basic/onsite_channels';
private const CONFIG_PATH = 'payment/tpaycom_magento2basic/openapi_settings/onsite_channels';

/** @var Data */
private $data;
Expand All @@ -34,31 +35,55 @@ class MethodListPlugin
/** @var Session */
private $checkoutSession;

public function __construct(Data $data, ScopeConfigInterface $scopeConfig, OnsiteChannels $onsiteChannels, StoreManagerInterface $storeManager, Tpay $tpay, Session $checkoutSession)
{
/** @var TransactionApiFacade */
private $transactions;

/** @var ConstraintValidator */
private $constraintValidator;

public function __construct(
Data $data,
ScopeConfigInterface $scopeConfig,
OnsiteChannels $onsiteChannels,
StoreManagerInterface $storeManager,
Tpay $tpay,
Session $checkoutSession,
TransactionApiFacade $transactions,
ConstraintValidator $constraintValidator
) {
$this->data = $data;
$this->scopeConfig = $scopeConfig;
$this->onsiteChannels = $onsiteChannels;
$this->storeManager = $storeManager;
$this->tpay = $tpay;
$this->checkoutSession = $checkoutSession;
$this->transactions = $transactions;
$this->constraintValidator = $constraintValidator;
}

public function afterGetAvailableMethods(MethodList $compiled, $result)
{
$onsiteChannels = $this->scopeConfig->getValue(self::CONFIG_PATH, ScopeInterface::SCOPE_STORE);
$channels = $onsiteChannels ? explode(',', $onsiteChannels) : [];
$channelList = $onsiteChannels ? explode(',', $onsiteChannels) : [];
$channels = $this->transactions->channels();

if (!$this->tpay->isCartValid((float) $this->checkoutSession->getQuote()->getGrandTotal())) {
if (!$this->tpay->isCartValid($this->checkoutSession->getQuote()->getGrandTotal())) {
return $result;
}

$result[] = $this->getMethodInstance('tpay.com - Płatność kartą', 'tpaycom_magento2basic_cards');
$result = $this->filterResult($result);

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

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

$title = $this->onsiteChannels->getLabelFromValue($onsiteChannel);
$result[] = $this->getMethodInstance(
$this->onsiteChannels->getLabelFromValue($onsiteChannel),
$title,
"generic-{$onsiteChannel}"
);
}
Expand Down

0 comments on commit 89e85d5

Please sign in to comment.