From 0a9b1489d035f65b9c87aec59a5a2da5d8c3df9f Mon Sep 17 00:00:00 2001 From: arti0090 Date: Thu, 31 Oct 2024 12:18:17 +0100 Subject: [PATCH] Make changes to take into account current channel --- config/config/sylius_fixtures.php | 1 - config/services/tpay.php | 2 +- .../FindByChannelWithGatewayConfigTrait.php | 30 ++++ ...ChannelWithGatewayConfigTraitInterface.php | 13 ++ .../Provider/ValidTpayChannelListProvider.php | 39 +++-- .../Application/config/packages/_sylius.yaml | 6 + .../Repository/PaymentMethodRepository.php | 13 ++ .../PaymentMethodRepositoryInterface.php | 11 ++ .../ValidTpayChannelListProviderTest.php | 145 ++++++++---------- 9 files changed, 159 insertions(+), 101 deletions(-) create mode 100644 src/Repository/FindByChannelWithGatewayConfigTrait.php create mode 100644 src/Repository/FindByChannelWithGatewayConfigTraitInterface.php create mode 100644 tests/Application/src/Repository/PaymentMethodRepository.php create mode 100644 tests/Application/src/Repository/PaymentMethodRepositoryInterface.php diff --git a/config/config/sylius_fixtures.php b/config/config/sylius_fixtures.php index 8aa6e48e..6764e2c7 100644 --- a/config/config/sylius_fixtures.php +++ b/config/config/sylius_fixtures.php @@ -4,7 +4,6 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; -use CommerceWeavers\SyliusTpayPlugin\Tpay\PayGroup; use CommerceWeavers\SyliusTpayPlugin\Tpay\PaymentType; use Symfony\Config\SyliusFixturesConfig; diff --git a/config/services/tpay.php b/config/services/tpay.php index eb45d817..09f57274 100644 --- a/config/services/tpay.php +++ b/config/services/tpay.php @@ -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') diff --git a/src/Repository/FindByChannelWithGatewayConfigTrait.php b/src/Repository/FindByChannelWithGatewayConfigTrait.php new file mode 100644 index 00000000..3cb04509 --- /dev/null +++ b/src/Repository/FindByChannelWithGatewayConfigTrait.php @@ -0,0 +1,30 @@ +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() + ; + } +} diff --git a/src/Repository/FindByChannelWithGatewayConfigTraitInterface.php b/src/Repository/FindByChannelWithGatewayConfigTraitInterface.php new file mode 100644 index 00000000..7ca1f239 --- /dev/null +++ b/src/Repository/FindByChannelWithGatewayConfigTraitInterface.php @@ -0,0 +1,13 @@ +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(); @@ -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']; diff --git a/tests/Application/config/packages/_sylius.yaml b/tests/Application/config/packages/_sylius.yaml index ce960b37..655cf95b 100644 --- a/tests/Application/config/packages/_sylius.yaml +++ b/tests/Application/config/packages/_sylius.yaml @@ -23,3 +23,9 @@ sylius_order: order: classes: model: App\Entity\Order + +sylius_payment: + resources: + payment_method: + classes: + repository: App\Repository\PaymentMethodRepository diff --git a/tests/Application/src/Repository/PaymentMethodRepository.php b/tests/Application/src/Repository/PaymentMethodRepository.php new file mode 100644 index 00000000..f62cd2c5 --- /dev/null +++ b/tests/Application/src/Repository/PaymentMethodRepository.php @@ -0,0 +1,13 @@ +availableTpayApiBankListProvider = $this->prophesize(AvailableTpayChannelListProviderInterface::class); - $this->gatewayMethodRepository = $this->prophesize(RepositoryInterface::class); - $this->paymentMethodRepository = $this->prophesize(RepositoryInterface::class); + $this->paymentMethodRepository = $this->prophesize(PaymentMethodRepositoryInterface::class); + $this->channelContext = $this->prophesize(ChannelContextInterface::class); $this->cypher = $this->prophesize(CypherInterface::class); } public function test_it_throws_exception_if_there_is_no_tpay_based_payment(): void { $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('There is no gateway config of Tpay type available'); + $this->expectExceptionMessage('There is no payment method of Tpay type available'); $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - $this->gatewayMethodRepository - ->findBy(['gatewayName' => 'tpay']) - ->willReturn([]) - ; + $channel = $this->prophesize(ChannelInterface::class); + $this->channelContext->getChannel()->willReturn($channel->reveal()); + + $this->paymentMethodRepository->findByChannelAndGatewayConfigNameWithGatewayConfig( + $channel, + 'tpay' + )->willReturn([]); $this->createTestSubject()->provide(); } @@ -106,18 +111,26 @@ public function test_it_throws_exception_if_there_is_no_gateway_config_that_is_p $this->expectException(UnableToGetBankListException::class); $this->expectExceptionMessage('Bank list cannot be retrieved if there is no payment method with PayByLink type configured'); - $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - - $notPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); - $notPblGatewayConfigConfig = [ + $notTpayBasedPaymentMethod = $this->prophesize(PaymentMethodInterface::class); + $notTpayBasedGatewayConfig = $this->prophesize(GatewayConfigInterface::class); + $notTpayBasedGatewayConfigConfig = [ 'type' => 'visa_mobile', ]; - $notPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); - $notPblGatewayConfig->getConfig()->willReturn($notPblGatewayConfigConfig); + $notTpayBasedPaymentMethod->getGatewayConfig()->willReturn($notTpayBasedGatewayConfig); + + $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - $this->gatewayMethodRepository - ->findBy(['gatewayName' => 'tpay']) - ->willReturn([$notPblGatewayConfig->reveal()]) + $channel = $this->prophesize(ChannelInterface::class); + $this->channelContext->getChannel()->willReturn($channel->reveal()); + + $notTpayBasedGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); + $notTpayBasedGatewayConfig->getConfig()->willReturn($notTpayBasedGatewayConfigConfig); + + $this->paymentMethodRepository + ->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay') + ->willReturn([ + $notTpayBasedPaymentMethod + ]) ; $this->createTestSubject()->provide(); @@ -125,18 +138,26 @@ public function test_it_throws_exception_if_there_is_no_gateway_config_that_is_p public function test_it_returns_all_available_payments_if_only_tpay_payment_method_is_pbl(): void { + $tpayBasedPaymentMethod = $this->prophesize(PaymentMethodInterface::class); $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); $tpayPblGatewayConfigConfig = [ 'type' => 'pay_by_link', ]; - $tpayPblGatewayConfig->decrypt($this->cypher)->shouldNotBeCalled(); + $tpayBasedPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig); + + $tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - $this->gatewayMethodRepository - ->findBy(['gatewayName' => 'tpay']) - ->willReturn([$tpayPblGatewayConfig->reveal()]) + $channel = $this->prophesize(ChannelInterface::class); + $this->channelContext->getChannel()->willReturn($channel->reveal()); + + $this->paymentMethodRepository + ->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay') + ->willReturn([ + $tpayBasedPaymentMethod->reveal(), + ]) ; $result = $this->createTestSubject()->provide(); @@ -146,32 +167,37 @@ public function test_it_returns_all_available_payments_if_only_tpay_payment_meth public function test_it_returns_valid_payments_according_to_available_tpay_payment_methods(): void { + $tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); $tpayPblGatewayConfigConfig = [ 'type' => 'pay_by_link', ]; + $tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig); $tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); + $anotherTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); $anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); $anotherTpayPblGatewayConfigConfig = [ 'type' => 'visa_mobile', ]; + $anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig); $anotherTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig); $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - $this->gatewayMethodRepository - ->findBy(['gatewayName' => 'tpay']) + $channel = $this->prophesize(ChannelInterface::class); + $this->channelContext->getChannel()->willReturn($channel->reveal()); + + $this->paymentMethodRepository + ->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay') ->willReturn([ - $tpayPblGatewayConfig->reveal(), - $anotherTpayPblGatewayConfig->reveal(), + $tpayPblPaymentMethod->reveal(), + $anotherTpayPblPaymentMethod->reveal(), ]) ; - $this->paymentMethodRepository->findBy(['enabled' => false])->willReturn([]); - $result = $this->createTestSubject()->provide(); $expected = self::BANK_LIST; @@ -182,29 +208,34 @@ public function test_it_returns_valid_payments_according_to_available_tpay_payme public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type(): void { + $tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); $tpayPblGatewayConfigConfig = [ 'type' => 'pay_by_link', ]; + $tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfig); $tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); + $anotherTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); $anotherTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); $anotherTpayPblGatewayConfigConfig = [ 'i_have_no_type' => 'i_should_still_work', ]; + $anotherTpayPblPaymentMethod->getGatewayConfig()->willReturn($anotherTpayPblGatewayConfig); $anotherTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); $anotherTpayPblGatewayConfig->getConfig()->willReturn($anotherTpayPblGatewayConfigConfig); $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - $this->paymentMethodRepository->findBy(['enabled' => false])->willReturn([]); + $channel = $this->prophesize(ChannelInterface::class); + $this->channelContext->getChannel()->willReturn($channel->reveal()); - $this->gatewayMethodRepository - ->findBy(['gatewayName' => 'tpay']) + $this->paymentMethodRepository + ->findByChannelAndGatewayConfigNameWithGatewayConfig($channel, 'tpay') ->willReturn([ - $tpayPblGatewayConfig->reveal(), - $anotherTpayPblGatewayConfig->reveal(), + $tpayPblPaymentMethod->reveal(), + $anotherTpayPblPaymentMethod->reveal(), ]) ; @@ -213,56 +244,12 @@ public function test_it_returns_valid_payments_even_if_gateway_config_lacks_type $this->assertSame(self::BANK_LIST, $result); } - public function test_it_returns_valid_payments_even_without_disabled_payments(): void - { - $tpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); - $tpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); - $tpayPblGatewayConfigConfig = [ - 'type' => 'pay_by_link', - ]; - $tpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); - $tpayPblGatewayConfig->getConfig()->willReturn($tpayPblGatewayConfigConfig); - $tpayPblPaymentMethod->getGatewayConfig()->willReturn($tpayPblGatewayConfigConfig); - - $disabledTpayPblPaymentMethod = $this->prophesize(PaymentMethodInterface::class); - $disabledTpayPblGatewayConfig = $this->prophesize(GatewayConfigInterface::class); - $disabledTpayPblGatewayConfigConfig = [ - 'type' => 'visa_mobile', - ]; - - $disabledTpayPblGatewayConfig->decrypt($this->cypher)->shouldBeCalled(); - $disabledTpayPblGatewayConfig->getConfig()->willReturn($disabledTpayPblGatewayConfigConfig); - $disabledTpayPblPaymentMethod->getGatewayConfig()->willReturn($disabledTpayPblGatewayConfigConfig); - $disabledTpayPblPaymentMethod->getId()->willReturn('6'); - - $this->availableTpayApiBankListProvider->provide()->willReturn(self::BANK_LIST); - - $this->paymentMethodRepository->findBy(['enabled' => false])->willReturn([ - $disabledTpayPblPaymentMethod->reveal(), - ]); - - $this->gatewayMethodRepository - ->findBy(['gatewayName' => 'tpay']) - ->willReturn([ - $tpayPblGatewayConfig->reveal(), - $disabledTpayPblGatewayConfig->reveal(), - ]) - ; - - $result = $this->createTestSubject()->provide(); - - $expected = self::BANK_LIST; - unset($expected['6']); - - $this->assertSame($expected, $result); - } - private function createTestSubject(): ValidTpayChannelListProvider { return new ValidTpayChannelListProvider( $this->availableTpayApiBankListProvider->reveal(), - $this->gatewayMethodRepository->reveal(), $this->paymentMethodRepository->reveal(), + $this->channelContext->reveal(), $this->cypher->reveal(), ); }