Skip to content

Commit

Permalink
Make changes to take into account current channel
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 authored and lchrusciel committed Oct 31, 2024
1 parent 94e7f39 commit f1a5168
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 101 deletions.
1 change: 0 additions & 1 deletion config/config/sylius_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Symfony\Config\SyliusFixturesConfig;

Expand Down
2 changes: 1 addition & 1 deletion config/services/tpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@
$services->set('commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list', ValidTpayChannelListProvider::class)
->args([
service('commerce_weavers_sylius_tpay.tpay.provider.available_tpay_api_bank_list'),
service('sylius.repository.gateway_config'),
service('sylius.repository.payment_method'),
service('sylius.context.channel'),
service('payum.dynamic_gateways.cypher')
])
->alias(AvailableTpayChannelListProviderInterface::class, 'commerce_weavers_sylius_tpay.tpay.provider.validated_tpay_api_bank_list')
Expand Down
30 changes: 30 additions & 0 deletions src/Repository/FindByChannelWithGatewayConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Repository;

use Doctrine\ORM\EntityRepository;
use Sylius\Component\Channel\Model\ChannelInterface;

/**
* @mixin EntityRepository
*/
trait FindByChannelWithGatewayConfigTrait
{
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, string $gatewayConfigName): array
{
return $this->createQueryBuilder('o')
->addSelect('gatewayConfig')
->leftJoin('o.gatewayConfig', 'gatewayConfig')
->where('o.enabled = :enabled')
->andWhere(':channel MEMBER OF pm.channels')
->andWhere('gatewayConfig.gatewayName = :gatewayName')
->setParameter('enabled', true)
->setParameter('channel', $channel)
->setParameter('gatewayName', $gatewayConfigName)
->getQuery()
->getResult()
;
}
}
13 changes: 13 additions & 0 deletions src/Repository/FindByChannelWithGatewayConfigTraitInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Repository;

use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface as BasePaymentMethodRepositoryInterface;

interface FindByChannelWithGatewayConfigTraitInterface extends BasePaymentMethodRepositoryInterface
{
public function findByChannelWithGatewayConfig(ChannelInterface $channel): array;
}
39 changes: 19 additions & 20 deletions src/Tpay/Provider/ValidTpayChannelListProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

namespace CommerceWeavers\SyliusTpayPlugin\Tpay\Provider;

use App\Repository\PaymentMethodRepositoryInterface;
use CommerceWeavers\SyliusTpayPlugin\Model\GatewayConfigInterface;
use CommerceWeavers\SyliusTpayPlugin\Payum\Exception\UnableToGetBankListException;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup;
use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType;
use Payum\Core\Security\CypherInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Webmozart\Assert\Assert;

final class ValidTpayChannelListProvider implements ValidTpayChannelListProviderInterface
{
public function __construct(
private readonly AvailableTpayChannelListProviderInterface $availableTpayApiBankListProvider,
private readonly RepositoryInterface $gatewayRepository,
private readonly RepositoryInterface $paymentMethodRepository,
private readonly PaymentMethodRepositoryInterface $paymentMethodRepository,
private readonly ChannelContextInterface $channelContext,
private readonly CypherInterface $cypher,
) {
}
Expand All @@ -26,21 +28,24 @@ public function provide(): array
{
$availableChannels = $this->availableTpayApiBankListProvider->provide();

/** @var GatewayConfigInterface[] $tpayGatewayConfigs */
$tpayGatewayConfigs = $this->gatewayRepository->findBy(['gatewayName' => 'tpay']);
/** @var PaymentMethodInterface[] $paymentMethods */
$paymentMethods = $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig(
$this->channelContext->getChannel(),
'tpay',
);

Assert::notEmpty($tpayGatewayConfigs, 'There is no gateway config of Tpay type available');

if (count($tpayGatewayConfigs) === 1 &&
$tpayGatewayConfigs[0]->getConfig()['type'] === PaymentType::PAY_BY_LINK
) {
return $availableChannels;
}
Assert::notEmpty($paymentMethods, 'There is no payment method of Tpay type available');

$paymentMethodsToRemoveByGroupId = [];
$hasPblPaymentAvailable = false;
foreach ($tpayGatewayConfigs as $tpayGatewayConfig) {
// cached doctrine values are encrypted hence need for decrypt
foreach ($paymentMethods as $paymentMethod) {
/** @var GatewayConfigInterface|null $tpayGatewayConfig */
$tpayGatewayConfig = $paymentMethod->getGatewayConfig();

if (null === $tpayGatewayConfig) {
continue;
}

$tpayGatewayConfig->decrypt($this->cypher);
$config = $tpayGatewayConfig->getConfig();

Expand Down Expand Up @@ -68,12 +73,6 @@ public function provide(): array
);
}

$disabledPaymentMethods = $this->paymentMethodRepository->findBy(['enabled' => false]);

foreach ($disabledPaymentMethods as $disabledPaymentMethod) {
$paymentMethodsToRemoveByGroupId[] = $disabledPaymentMethod->getId();
}

return array_filter($availableChannels, static function (array $channel) use ($paymentMethodsToRemoveByGroupId): bool {
$groupId = (int) $channel['groups'][0]['id'];

Expand Down
6 changes: 6 additions & 0 deletions tests/Application/config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ sylius_order:
order:
classes:
model: App\Entity\Order

sylius_payment:
resources:
payment_method:
classes:
repository: App\Repository\PaymentMethodRepository
13 changes: 13 additions & 0 deletions tests/Application/src/Repository/PaymentMethodRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Repository;

use CommerceWeavers\SyliusTpayPlugin\Repository\FindByChannelWithGatewayConfigTrait;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\PaymentMethodRepository as BasePaymentMethodRepository;

final class PaymentMethodRepository extends BasePaymentMethodRepository implements PaymentMethodRepositoryInterface
{
use FindByChannelWithGatewayConfigTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Repository;

use Sylius\Component\Channel\Model\ChannelInterface;
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface as BasePaymentMethodRepositoryInterface;

interface PaymentMethodRepositoryInterface extends BasePaymentMethodRepositoryInterface
{
public function findByChannelAndGatewayConfigNameWithGatewayConfig(ChannelInterface $channel, string $gatewayConfigName): array;
}
Loading

0 comments on commit f1a5168

Please sign in to comment.