From 2fd8004cdf6cf941378f38a6c6f232e1dc9aa23c Mon Sep 17 00:00:00 2001 From: "i.belikov" Date: Wed, 2 Dec 2020 18:16:17 +0200 Subject: [PATCH 1/2] Adding new payment method fix --- .../PayPalPaymentMethodListenerSpec.php | 30 +++++++++++++++++-- src/Listener/PayPalPaymentMethodListener.php | 13 ++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/spec/Listener/PayPalPaymentMethodListenerSpec.php b/spec/Listener/PayPalPaymentMethodListenerSpec.php index 810d8fd9..d32ca09f 100644 --- a/spec/Listener/PayPalPaymentMethodListenerSpec.php +++ b/spec/Listener/PayPalPaymentMethodListenerSpec.php @@ -4,6 +4,7 @@ namespace spec\Sylius\PayPalPlugin\Listener; +use Payum\Core\Model\GatewayConfigInterface; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; @@ -35,9 +36,12 @@ function it_initiates_onboarding_when_creating_a_supported_payment_method( OnboardingInitiatorInterface $onboardingInitiator, PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider, ResourceControllerEvent $event, - PaymentMethodInterface $paymentMethod + PaymentMethodInterface $paymentMethod, + GatewayConfigInterface $gatewayConfig ): void { $event->getSubject()->willReturn($paymentMethod); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + $gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal'); $payPalPaymentMethodProvider->provide()->willThrow(PayPalPaymentMethodNotFoundException::class); $onboardingInitiator->supports($paymentMethod)->willReturn(true); @@ -66,10 +70,13 @@ function it_redirects_with_error_if_the_pay_pal_payment_method_already_exists( UrlGeneratorInterface $urlGenerator, FlashBagInterface $flashBag, ResourceControllerEvent $event, - PaymentMethodInterface $paymentMethod + PaymentMethodInterface $paymentMethod, + GatewayConfigInterface $gatewayConfig ): void { $event->getSubject()->willReturn($paymentMethod); $payPalPaymentMethodProvider->provide()->willReturn($paymentMethod); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + $gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal'); $flashBag->add('error', 'sylius.pay_pal.more_than_one_seller_not_allowed')->shouldBeCalled(); @@ -87,9 +94,12 @@ function it_does_nothing_when_creating_an_unsupported_payment_method( OnboardingInitiatorInterface $onboardingInitiator, PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider, ResourceControllerEvent $event, - PaymentMethodInterface $paymentMethod + PaymentMethodInterface $paymentMethod, + GatewayConfigInterface $gatewayConfig ): void { $event->getSubject()->willReturn($paymentMethod); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + $gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal'); $payPalPaymentMethodProvider->provide()->willThrow(PayPalPaymentMethodNotFoundException::class); $onboardingInitiator->supports($paymentMethod)->willReturn(false); @@ -98,4 +108,18 @@ function it_does_nothing_when_creating_an_unsupported_payment_method( $this->initializeCreate($event); } + + function it_does_nothing_if_payment_method_is_not_pay_pal( + ResourceControllerEvent $event, + PaymentMethodInterface $paymentMethod, + GatewayConfigInterface $gatewayConfig + ): void { + $event->getSubject()->willReturn($paymentMethod); + $paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); + $gatewayConfig->getFactoryName()->willReturn('offline'); + + $event->setResponse(Argument::any())->shouldNotBeCalled(); + + $this->initializeCreate($event); + } } diff --git a/src/Listener/PayPalPaymentMethodListener.php b/src/Listener/PayPalPaymentMethodListener.php index 2b17b69d..6f3b0c9d 100644 --- a/src/Listener/PayPalPaymentMethodListener.php +++ b/src/Listener/PayPalPaymentMethodListener.php @@ -4,6 +4,7 @@ namespace Sylius\PayPalPlugin\Listener; +use Payum\Core\Model\GatewayConfigInterface; use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent; use Sylius\Component\Core\Model\PaymentMethodInterface; use Sylius\PayPalPlugin\Exception\PayPalPaymentMethodNotFoundException; @@ -46,6 +47,10 @@ public function initializeCreate(ResourceControllerEvent $event): void /** @var PaymentMethodInterface $paymentMethod */ Assert::isInstanceOf($paymentMethod, PaymentMethodInterface::class); + if (!$this->isNewPaymentMethodPayPal($paymentMethod)) { + return; + } + if ($this->isTherePayPalPaymentMethod()) { $this->flashBag->add('error', 'sylius.pay_pal.more_than_one_seller_not_allowed'); @@ -61,6 +66,14 @@ public function initializeCreate(ResourceControllerEvent $event): void $event->setResponse(new RedirectResponse($this->onboardingInitiator->initiate($paymentMethod))); } + private function isNewPaymentMethodPayPal(PaymentMethodInterface $paymentMethod): bool + { + /** @var GatewayConfigInterface $gatewayConfig */ + $gatewayConfig = $paymentMethod->getGatewayConfig(); + + return $gatewayConfig->getFactoryName() === 'sylius.pay_pal'; + } + private function isTherePayPalPaymentMethod(): bool { try { From 22521e7b6cbbf684a67ba922f5cbb0debd6a22e5 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Thu, 3 Dec 2020 13:29:49 +0100 Subject: [PATCH 2/2] Add Behat scenarios reproducing the bug --- ...board_more_than_one_pay_pal_seller.feature | 5 +++++ .../Admin/ManagingPaymentMethodsContext.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/features/trying_to_onboard_more_than_one_pay_pal_seller.feature b/features/trying_to_onboard_more_than_one_pay_pal_seller.feature index c40967c7..a7fe86a2 100644 --- a/features/trying_to_onboard_more_than_one_pay_pal_seller.feature +++ b/features/trying_to_onboard_more_than_one_pay_pal_seller.feature @@ -13,3 +13,8 @@ Feature: Trying to onboard more than one PayPal seller Scenario: Trying to onboard second PayPal seller When I try to create a new payment method with "PayPal" gateway factory Then I should be notified that I cannot onboard more than one PayPal seller + + @ui + Scenario: Being able to create different payment methods + When I want to create a new offline payment method + Then I should not be notified that I cannot onboard more than one PayPal seller diff --git a/tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php b/tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php index c32e4cd5..73bdce83 100644 --- a/tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php +++ b/tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php @@ -5,6 +5,8 @@ namespace Tests\Sylius\PayPalPlugin\Behat\Context\Admin; use Behat\Behat\Context\Context; +use Behat\Mink\Exception\ElementNotFoundException; +use Sylius\Behat\Exception\NotificationExpectationMismatchException; use Sylius\Behat\NotificationType; use Sylius\Behat\Page\Admin\Crud\IndexPageInterface; use Sylius\Behat\Page\Admin\PaymentMethod\CreatePageInterface; @@ -67,4 +69,21 @@ public function iShouldBeNotifiedThatICannotOnboardMoreThanOnePayPalSeller(): vo NotificationType::failure() ); } + + /** + * @Then I should not be notified that I cannot onboard more than one PayPal seller + */ + public function iShouldNotBeNotifiedThatICannotOnboardMoreThanOnePayPalSeller(): void + { + try { + $this->notificationChecker->checkNotification( + 'You cannot onboard more than one PayPal seller!', + NotificationType::failure() + ); + } catch (NotificationExpectationMismatchException|ElementNotFoundException $exception) { + return; + } + + throw new \DomainException('Step should fail'); + } }