Skip to content

Commit

Permalink
feature #89 Check if permissions are granted correctly (Zales0123)
Browse files Browse the repository at this point in the history
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
SirDomin authored Sep 10, 2020
2 parents 7b593e9 + 5264cae commit 972fbb2
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 2 deletions.
84 changes: 84 additions & 0 deletions spec/Enabler/PayPalPaymentMethodEnablerSpec.php
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])
;
}
}
60 changes: 60 additions & 0 deletions spec/Onboarding/Processor/BasicOnboardingProcessorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,66 @@ function it_processes_onboarding_for_supported_payment_method_and_request(
$this->process($paymentMethod, $request)->shouldReturn($paymentMethod);
}

function it_processes_onboarding_for_supported_payment_method_with_not_granted_permissions_and_request(
ClientInterface $httpClient,
ResponseInterface $response,
StreamInterface $body,
GatewayConfigInterface $gatewayConfig,
PaymentMethodInterface $paymentMethod,
Request $request
): void {
$gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal');
$gatewayConfig->getConfig()->willReturn(
[
'client_id' => 'CLIENT-ID',
'client_secret' => 'CLIENT-SECRET',
'sylius_merchant_id' => 'SYLIUS-MERCHANT-ID',
'merchant_id' => 'MERCHANT-ID',
]
);

$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);

$request->query = new ParameterBag(['onboarding_id' => 'ONBOARDING-ID', 'permissionsGranted' => false]);

$httpClient
->request(
'GET',
'https://paypal.facilitator.com/partner-referrals/check/ONBOARDING-ID',
[
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
],
]
)
->willReturn($response)
;

$response->getBody()->willReturn($body);
$body->getContents()->willReturn(
'{"client_id":"CLIENT-ID",
"client_secret":"CLIENT-SECRET",
"sylius_merchant_id":"SYLIUS-MERCHANT-ID",
"merchant_id":"MERCHANT-ID",
"partner_attribution_id":"ATTRIBUTION-ID"}'
);

$paymentMethod->setEnabled(false)->shouldBeCalled();
$gatewayConfig->setConfig(
[
'client_id' => 'CLIENT-ID',
'client_secret' => 'CLIENT-SECRET',
'onboarding_id' => 'ONBOARDING-ID',
'sylius_merchant_id' => 'SYLIUS-MERCHANT-ID',
'merchant_id' => 'MERCHANT-ID',
'partner_attribution_id' => 'ATTRIBUTION-ID',
]
)->shouldBeCalled();

$this->process($paymentMethod, $request)->shouldReturn($paymentMethod);
}

function it_throws_an_exception_when_trying_to_process_onboarding_for_unsupported_payment_method_or_request(
PaymentMethodInterface $paymentMethod,
Request $request
Expand Down
51 changes: 51 additions & 0 deletions src/Controller/EnableSellerAction.php
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'));
}
}
59 changes: 59 additions & 0 deletions src/Enabler/PayPalPaymentMethodEnabler.php
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();
}
}
16 changes: 16 additions & 0 deletions src/Enabler/PaymentMethodEnablerInterface.php
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;
}
13 changes: 13 additions & 0 deletions src/Exception/PaymentMethodCouldNotBeEnabledException.php
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');
}
}
41 changes: 41 additions & 0 deletions src/Form/Extension/PaymentMethodTypeExtension.php
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];
}
}
10 changes: 8 additions & 2 deletions src/Onboarding/Processor/BasicOnboardingProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public function process(
/** @var GatewayConfig $gatewayConfig */
Assert::notNull($gatewayConfig);

$onboardingId = (string) $request->query->get('onboarding_id');
$checkPartnerReferralsResponse = $this->httpClient->request(
'GET',
sprintf('%s/partner-referrals/check/%s', $this->url, (string) $request->query->get('onboarding_id')),
sprintf('%s/partner-referrals/check/%s', $this->url, $onboardingId),
[
'headers' => [
'Content-Type' => 'application/json',
Expand All @@ -61,10 +62,15 @@ public function process(
'client_secret' => $response['client_secret'],
'merchant_id' => $response['merchant_id'],
'sylius_merchant_id' => $response['sylius_merchant_id'],
'onboarding_id' => $request->query->get('onboarding_id'),
'onboarding_id' => $onboardingId,
'partner_attribution_id' => $response['partner_attribution_id'],
]);

$permissionsGranted = (bool) $request->query->get('permissionsGranted', true);
if (!$permissionsGranted) {
$paymentMethod->setEnabled(false);
}

return $paymentMethod;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Resources/config/admin_routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ sylius_paypal_plugin_admin_download_payouts_report:
methods: [GET]
defaults:
_controller: Sylius\PayPalPlugin\Controller\DownloadPayoutsReportAction

sylius_paypal_plugin_admin_enable_seller:
path: /enable-seller/{id}
methods: [POST]
defaults:
_controller: Sylius\PayPalPlugin\Controller\EnableSellerAction
3 changes: 3 additions & 0 deletions src/Resources/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ sylius_grid:
action:
download_report: "@SyliusPayPalPlugin/Grid/downloadReport.html.twig"
enable_pay_pal: "@SyliusPayPalPlugin/Grid/enablePayPal.html.twig"
enable_seller: "@SyliusPayPalPlugin/Grid/enableSeller.html.twig"
grids:
sylius_admin_payment_method:
actions:
item:
enable_seller:
type: enable_seller
download_report:
type: download_report
label: sylius.pay_pal.report
Expand Down
Loading

0 comments on commit 972fbb2

Please sign in to comment.