Skip to content

Commit

Permalink
bug #172 Adding new payment method fix ([email protected], Zales0…
Browse files Browse the repository at this point in the history
…123)

This PR was merged into the 1.0 branch.

Discussion
----------



Commits
-------

2fd8004 Adding new payment method fix
22521e7 Add Behat scenarios reproducing the bug
  • Loading branch information
Zales0123 authored Dec 3, 2020
2 parents f1b8091 + 22521e7 commit 1834076
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 27 additions & 3 deletions spec/Listener/PayPalPaymentMethodListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -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);
Expand All @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/Listener/PayPalPaymentMethodListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');

Expand All @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions tests/Behat/Context/Admin/ManagingPaymentMethodsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 1834076

Please sign in to comment.