-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #89 Check if permissions are granted correctly (Zales0123)
This PR was merged into the 1.0-dev branch. Discussion ---------- Should be merge after https://github.com/Sylius/PayPalFacilitator/pull/22 The disabled PayPal payment method is created if permissions are not granted during onboarding. Then it's possible to check them with the button (therefore enabling a payment method). <img width="1154" alt="Zrzut ekranu 2020-09-9 o 15 42 42" src="https://user-images.githubusercontent.com/6212718/92606840-b51f0c00-f2b3-11ea-8ae1-75e6289701f8.png"> <img width="1162" alt="Zrzut ekranu 2020-09-9 o 15 43 08" src="https://user-images.githubusercontent.com/6212718/92606850-b6e8cf80-f2b3-11ea-8f38-e4ec660b98ac.png"> <img width="1155" alt="Zrzut ekranu 2020-09-9 o 15 43 57" src="https://user-images.githubusercontent.com/6212718/92606851-b7816600-f2b3-11ea-85f8-0093185adea8.png"> Commits ------- 53ef2aa Create not enabled payment method if permissions are not granted 6743ead Disable "enabled" field on PayPal payment method 5264cae Enable disabled PayPal payment method with button
- Loading branch information
Showing
14 changed files
with
366 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\PayPalPlugin\Enabler; | ||
|
||
use Doctrine\Persistence\ObjectManager; | ||
use GuzzleHttp\Client; | ||
use Payum\Core\Model\GatewayConfigInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface; | ||
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException; | ||
|
||
final class PayPalPaymentMethodEnablerSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
Client $client, | ||
ObjectManager $paymentMethodManager | ||
): void { | ||
$this->beConstructedWith($client, 'http://base-url.com', $paymentMethodManager); | ||
} | ||
|
||
function it_implements_payment_method_enabler_interface(): void | ||
{ | ||
$this->shouldImplement(PaymentMethodEnablerInterface::class); | ||
} | ||
|
||
function it_enables_payment_method_if_it_has_proper_credentials_set( | ||
Client $client, | ||
ObjectManager $paymentMethodManager, | ||
PaymentMethodInterface $paymentMethod, | ||
GatewayConfigInterface $gatewayConfig, | ||
ResponseInterface $response, | ||
StreamInterface $body | ||
): void { | ||
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); | ||
$gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123']); | ||
|
||
$client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response); | ||
$response->getBody()->willReturn($body); | ||
$body->getContents()->willReturn('{ "permissionsGranted": true }'); | ||
|
||
$paymentMethod->setEnabled(true)->shouldBeCalled(); | ||
$paymentMethodManager->flush()->shouldBeCalled(); | ||
|
||
$this->enable($paymentMethod); | ||
} | ||
|
||
function it_throws_exception_if_payment_method_credentials_are_not_granted( | ||
Client $client, | ||
ObjectManager $paymentMethodManager, | ||
PaymentMethodInterface $paymentMethod, | ||
GatewayConfigInterface $gatewayConfig, | ||
ResponseInterface $response, | ||
StreamInterface $body | ||
): void { | ||
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig); | ||
$gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123']); | ||
|
||
$client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response); | ||
$response->getBody()->willReturn($body); | ||
$body->getContents()->willReturn('{ "permissionsGranted": false }'); | ||
|
||
$paymentMethod->setEnabled(true)->shouldNotBeCalled(); | ||
$paymentMethodManager->flush()->shouldNotBeCalled(); | ||
|
||
$this | ||
->shouldThrow(PaymentMethodCouldNotBeEnabledException::class) | ||
->during('enable', [$paymentMethod]) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Controller; | ||
|
||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; | ||
use Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface; | ||
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; | ||
|
||
final class EnableSellerAction | ||
{ | ||
/** @var PaymentMethodRepositoryInterface */ | ||
private $paymentMethodRepository; | ||
|
||
/** @var PaymentMethodEnablerInterface */ | ||
private $paymentMethodEnabler; | ||
|
||
public function __construct( | ||
PaymentMethodRepositoryInterface $paymentMethodRepository, | ||
PaymentMethodEnablerInterface $paymentMethodEnabler | ||
) { | ||
$this->paymentMethodRepository = $paymentMethodRepository; | ||
$this->paymentMethodEnabler = $paymentMethodEnabler; | ||
} | ||
|
||
public function __invoke(Request $request): Response | ||
{ | ||
/** @var PaymentMethodInterface $paymentMethod */ | ||
$paymentMethod = $this->paymentMethodRepository->find($request->attributes->getInt('id')); | ||
/** @var FlashBagInterface $flashBag */ | ||
$flashBag = $request->getSession()->getBag('flashes'); | ||
|
||
try { | ||
$this->paymentMethodEnabler->enable($paymentMethod); | ||
} catch (PaymentMethodCouldNotBeEnabledException $exception) { | ||
$flashBag->add('error', 'sylius.pay_pal.payment_not_enabled'); | ||
|
||
return new RedirectResponse($request->headers->get('referer')); | ||
} | ||
|
||
$flashBag->add('success', 'sylius.pay_pal.payment_enabled'); | ||
|
||
return new RedirectResponse($request->headers->get('referer')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Enabler; | ||
|
||
use Doctrine\Persistence\ObjectManager; | ||
use GuzzleHttp\Client; | ||
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException; | ||
|
||
final class PayPalPaymentMethodEnabler implements PaymentMethodEnablerInterface | ||
{ | ||
/** @var Client */ | ||
private $client; | ||
|
||
/** @var string */ | ||
private $baseUrl; | ||
|
||
/** @var ObjectManager */ | ||
private $paymentMethodManager; | ||
|
||
public function __construct(Client $client, string $baseUrl, ObjectManager $paymentMethodManager) | ||
{ | ||
$this->client = $client; | ||
$this->baseUrl = $baseUrl; | ||
$this->paymentMethodManager = $paymentMethodManager; | ||
} | ||
|
||
public function enable(PaymentMethodInterface $paymentMethod): void | ||
{ | ||
/** @var GatewayConfigInterface $gatewayConfig */ | ||
$gatewayConfig = $paymentMethod->getGatewayConfig(); | ||
$config = $gatewayConfig->getConfig(); | ||
|
||
$response = $this->client->request( | ||
'GET', | ||
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id']) | ||
); | ||
|
||
$content = (array) json_decode($response->getBody()->getContents(), true); | ||
if (!((bool) $content['permissionsGranted'])) { | ||
throw new PaymentMethodCouldNotBeEnabledException(); | ||
} | ||
|
||
$paymentMethod->setEnabled(true); | ||
$this->paymentMethodManager->flush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Enabler; | ||
|
||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException; | ||
|
||
interface PaymentMethodEnablerInterface | ||
{ | ||
/** | ||
* @throws PaymentMethodCouldNotBeEnabledException | ||
*/ | ||
public function enable(PaymentMethodInterface $paymentMethod): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Exception; | ||
|
||
final class PaymentMethodCouldNotBeEnabledException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('PayPal payment method could not be enabled'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Form\Extension; | ||
|
||
use Sylius\Bundle\PaymentBundle\Form\Type\PaymentMethodType; | ||
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Symfony\Component\Form\AbstractTypeExtension; | ||
use Symfony\Component\Form\Extension\Core\Type\HiddenType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
final class PaymentMethodTypeExtension extends AbstractTypeExtension | ||
{ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { | ||
/** @var PaymentMethodInterface $data */ | ||
$data = $event->getData(); | ||
$form = $event->getForm(); | ||
|
||
/** @var GatewayConfigInterface $gatewayConfig */ | ||
$gatewayConfig = $data->getGatewayConfig(); | ||
if ($gatewayConfig->getFactoryName() === 'sylius.pay_pal') { | ||
$form->add('enabled', HiddenType::class, [ | ||
'required' => false, | ||
'label' => 'sylius.form.payment_method.enabled', | ||
'data' => $data->isEnabled(), | ||
]); | ||
} | ||
}); | ||
} | ||
|
||
public function getExtendedTypes(): iterable | ||
{ | ||
return [PaymentMethodType::class]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.