Skip to content

Commit 972fbb2

Browse files
authored
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
2 parents 7b593e9 + 5264cae commit 972fbb2

File tree

14 files changed

+366
-2
lines changed

14 files changed

+366
-2
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace spec\Sylius\PayPalPlugin\Enabler;
15+
16+
use Doctrine\Persistence\ObjectManager;
17+
use GuzzleHttp\Client;
18+
use Payum\Core\Model\GatewayConfigInterface;
19+
use PhpSpec\ObjectBehavior;
20+
use Psr\Http\Message\ResponseInterface;
21+
use Psr\Http\Message\StreamInterface;
22+
use Sylius\Component\Core\Model\PaymentMethodInterface;
23+
use Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface;
24+
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException;
25+
26+
final class PayPalPaymentMethodEnablerSpec extends ObjectBehavior
27+
{
28+
function let(
29+
Client $client,
30+
ObjectManager $paymentMethodManager
31+
): void {
32+
$this->beConstructedWith($client, 'http://base-url.com', $paymentMethodManager);
33+
}
34+
35+
function it_implements_payment_method_enabler_interface(): void
36+
{
37+
$this->shouldImplement(PaymentMethodEnablerInterface::class);
38+
}
39+
40+
function it_enables_payment_method_if_it_has_proper_credentials_set(
41+
Client $client,
42+
ObjectManager $paymentMethodManager,
43+
PaymentMethodInterface $paymentMethod,
44+
GatewayConfigInterface $gatewayConfig,
45+
ResponseInterface $response,
46+
StreamInterface $body
47+
): void {
48+
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
49+
$gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123']);
50+
51+
$client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response);
52+
$response->getBody()->willReturn($body);
53+
$body->getContents()->willReturn('{ "permissionsGranted": true }');
54+
55+
$paymentMethod->setEnabled(true)->shouldBeCalled();
56+
$paymentMethodManager->flush()->shouldBeCalled();
57+
58+
$this->enable($paymentMethod);
59+
}
60+
61+
function it_throws_exception_if_payment_method_credentials_are_not_granted(
62+
Client $client,
63+
ObjectManager $paymentMethodManager,
64+
PaymentMethodInterface $paymentMethod,
65+
GatewayConfigInterface $gatewayConfig,
66+
ResponseInterface $response,
67+
StreamInterface $body
68+
): void {
69+
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
70+
$gatewayConfig->getConfig()->willReturn(['merchant_id' => '123123']);
71+
72+
$client->request('GET', 'http://base-url.com/seller-permissions/check/123123')->willReturn($response);
73+
$response->getBody()->willReturn($body);
74+
$body->getContents()->willReturn('{ "permissionsGranted": false }');
75+
76+
$paymentMethod->setEnabled(true)->shouldNotBeCalled();
77+
$paymentMethodManager->flush()->shouldNotBeCalled();
78+
79+
$this
80+
->shouldThrow(PaymentMethodCouldNotBeEnabledException::class)
81+
->during('enable', [$paymentMethod])
82+
;
83+
}
84+
}

spec/Onboarding/Processor/BasicOnboardingProcessorSpec.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,66 @@ function it_processes_onboarding_for_supported_payment_method_and_request(
8181
$this->process($paymentMethod, $request)->shouldReturn($paymentMethod);
8282
}
8383

84+
function it_processes_onboarding_for_supported_payment_method_with_not_granted_permissions_and_request(
85+
ClientInterface $httpClient,
86+
ResponseInterface $response,
87+
StreamInterface $body,
88+
GatewayConfigInterface $gatewayConfig,
89+
PaymentMethodInterface $paymentMethod,
90+
Request $request
91+
): void {
92+
$gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal');
93+
$gatewayConfig->getConfig()->willReturn(
94+
[
95+
'client_id' => 'CLIENT-ID',
96+
'client_secret' => 'CLIENT-SECRET',
97+
'sylius_merchant_id' => 'SYLIUS-MERCHANT-ID',
98+
'merchant_id' => 'MERCHANT-ID',
99+
]
100+
);
101+
102+
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
103+
104+
$request->query = new ParameterBag(['onboarding_id' => 'ONBOARDING-ID', 'permissionsGranted' => false]);
105+
106+
$httpClient
107+
->request(
108+
'GET',
109+
'https://paypal.facilitator.com/partner-referrals/check/ONBOARDING-ID',
110+
[
111+
'headers' => [
112+
'Content-Type' => 'application/json',
113+
'Accept' => 'application/json',
114+
],
115+
]
116+
)
117+
->willReturn($response)
118+
;
119+
120+
$response->getBody()->willReturn($body);
121+
$body->getContents()->willReturn(
122+
'{"client_id":"CLIENT-ID",
123+
"client_secret":"CLIENT-SECRET",
124+
"sylius_merchant_id":"SYLIUS-MERCHANT-ID",
125+
"merchant_id":"MERCHANT-ID",
126+
"partner_attribution_id":"ATTRIBUTION-ID"}'
127+
);
128+
129+
$paymentMethod->setEnabled(false)->shouldBeCalled();
130+
$gatewayConfig->setConfig(
131+
[
132+
'client_id' => 'CLIENT-ID',
133+
'client_secret' => 'CLIENT-SECRET',
134+
'onboarding_id' => 'ONBOARDING-ID',
135+
'sylius_merchant_id' => 'SYLIUS-MERCHANT-ID',
136+
'merchant_id' => 'MERCHANT-ID',
137+
'partner_attribution_id' => 'ATTRIBUTION-ID',
138+
]
139+
)->shouldBeCalled();
140+
141+
$this->process($paymentMethod, $request)->shouldReturn($paymentMethod);
142+
}
143+
84144
function it_throws_an_exception_when_trying_to_process_onboarding_for_unsupported_payment_method_or_request(
85145
PaymentMethodInterface $paymentMethod,
86146
Request $request
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\PayPalPlugin\Controller;
6+
7+
use Sylius\Component\Core\Model\PaymentMethodInterface;
8+
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
9+
use Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface;
10+
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException;
11+
use Symfony\Component\HttpFoundation\RedirectResponse;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
15+
16+
final class EnableSellerAction
17+
{
18+
/** @var PaymentMethodRepositoryInterface */
19+
private $paymentMethodRepository;
20+
21+
/** @var PaymentMethodEnablerInterface */
22+
private $paymentMethodEnabler;
23+
24+
public function __construct(
25+
PaymentMethodRepositoryInterface $paymentMethodRepository,
26+
PaymentMethodEnablerInterface $paymentMethodEnabler
27+
) {
28+
$this->paymentMethodRepository = $paymentMethodRepository;
29+
$this->paymentMethodEnabler = $paymentMethodEnabler;
30+
}
31+
32+
public function __invoke(Request $request): Response
33+
{
34+
/** @var PaymentMethodInterface $paymentMethod */
35+
$paymentMethod = $this->paymentMethodRepository->find($request->attributes->getInt('id'));
36+
/** @var FlashBagInterface $flashBag */
37+
$flashBag = $request->getSession()->getBag('flashes');
38+
39+
try {
40+
$this->paymentMethodEnabler->enable($paymentMethod);
41+
} catch (PaymentMethodCouldNotBeEnabledException $exception) {
42+
$flashBag->add('error', 'sylius.pay_pal.payment_not_enabled');
43+
44+
return new RedirectResponse($request->headers->get('referer'));
45+
}
46+
47+
$flashBag->add('success', 'sylius.pay_pal.payment_enabled');
48+
49+
return new RedirectResponse($request->headers->get('referer'));
50+
}
51+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\PayPalPlugin\Enabler;
15+
16+
use Doctrine\Persistence\ObjectManager;
17+
use GuzzleHttp\Client;
18+
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
19+
use Sylius\Component\Core\Model\PaymentMethodInterface;
20+
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException;
21+
22+
final class PayPalPaymentMethodEnabler implements PaymentMethodEnablerInterface
23+
{
24+
/** @var Client */
25+
private $client;
26+
27+
/** @var string */
28+
private $baseUrl;
29+
30+
/** @var ObjectManager */
31+
private $paymentMethodManager;
32+
33+
public function __construct(Client $client, string $baseUrl, ObjectManager $paymentMethodManager)
34+
{
35+
$this->client = $client;
36+
$this->baseUrl = $baseUrl;
37+
$this->paymentMethodManager = $paymentMethodManager;
38+
}
39+
40+
public function enable(PaymentMethodInterface $paymentMethod): void
41+
{
42+
/** @var GatewayConfigInterface $gatewayConfig */
43+
$gatewayConfig = $paymentMethod->getGatewayConfig();
44+
$config = $gatewayConfig->getConfig();
45+
46+
$response = $this->client->request(
47+
'GET',
48+
sprintf('%s/seller-permissions/check/%s', $this->baseUrl, (string) $config['merchant_id'])
49+
);
50+
51+
$content = (array) json_decode($response->getBody()->getContents(), true);
52+
if (!((bool) $content['permissionsGranted'])) {
53+
throw new PaymentMethodCouldNotBeEnabledException();
54+
}
55+
56+
$paymentMethod->setEnabled(true);
57+
$this->paymentMethodManager->flush();
58+
}
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\PayPalPlugin\Enabler;
6+
7+
use Sylius\Component\Core\Model\PaymentMethodInterface;
8+
use Sylius\PayPalPlugin\Exception\PaymentMethodCouldNotBeEnabledException;
9+
10+
interface PaymentMethodEnablerInterface
11+
{
12+
/**
13+
* @throws PaymentMethodCouldNotBeEnabledException
14+
*/
15+
public function enable(PaymentMethodInterface $paymentMethod): void;
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\PayPalPlugin\Exception;
6+
7+
final class PaymentMethodCouldNotBeEnabledException extends \Exception
8+
{
9+
public function __construct()
10+
{
11+
parent::__construct('PayPal payment method could not be enabled');
12+
}
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sylius\PayPalPlugin\Form\Extension;
6+
7+
use Sylius\Bundle\PaymentBundle\Form\Type\PaymentMethodType;
8+
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
9+
use Sylius\Component\Core\Model\PaymentMethodInterface;
10+
use Symfony\Component\Form\AbstractTypeExtension;
11+
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
12+
use Symfony\Component\Form\FormBuilderInterface;
13+
use Symfony\Component\Form\FormEvent;
14+
use Symfony\Component\Form\FormEvents;
15+
16+
final class PaymentMethodTypeExtension extends AbstractTypeExtension
17+
{
18+
public function buildForm(FormBuilderInterface $builder, array $options): void
19+
{
20+
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
21+
/** @var PaymentMethodInterface $data */
22+
$data = $event->getData();
23+
$form = $event->getForm();
24+
25+
/** @var GatewayConfigInterface $gatewayConfig */
26+
$gatewayConfig = $data->getGatewayConfig();
27+
if ($gatewayConfig->getFactoryName() === 'sylius.pay_pal') {
28+
$form->add('enabled', HiddenType::class, [
29+
'required' => false,
30+
'label' => 'sylius.form.payment_method.enabled',
31+
'data' => $data->isEnabled(),
32+
]);
33+
}
34+
});
35+
}
36+
37+
public function getExtendedTypes(): iterable
38+
{
39+
return [PaymentMethodType::class];
40+
}
41+
}

src/Onboarding/Processor/BasicOnboardingProcessor.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public function process(
3838
/** @var GatewayConfig $gatewayConfig */
3939
Assert::notNull($gatewayConfig);
4040

41+
$onboardingId = (string) $request->query->get('onboarding_id');
4142
$checkPartnerReferralsResponse = $this->httpClient->request(
4243
'GET',
43-
sprintf('%s/partner-referrals/check/%s', $this->url, (string) $request->query->get('onboarding_id')),
44+
sprintf('%s/partner-referrals/check/%s', $this->url, $onboardingId),
4445
[
4546
'headers' => [
4647
'Content-Type' => 'application/json',
@@ -61,10 +62,15 @@ public function process(
6162
'client_secret' => $response['client_secret'],
6263
'merchant_id' => $response['merchant_id'],
6364
'sylius_merchant_id' => $response['sylius_merchant_id'],
64-
'onboarding_id' => $request->query->get('onboarding_id'),
65+
'onboarding_id' => $onboardingId,
6566
'partner_attribution_id' => $response['partner_attribution_id'],
6667
]);
6768

69+
$permissionsGranted = (bool) $request->query->get('permissionsGranted', true);
70+
if (!$permissionsGranted) {
71+
$paymentMethod->setEnabled(false);
72+
}
73+
6874
return $paymentMethod;
6975
}
7076

src/Resources/config/admin_routing.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ sylius_paypal_plugin_admin_download_payouts_report:
55
methods: [GET]
66
defaults:
77
_controller: Sylius\PayPalPlugin\Controller\DownloadPayoutsReportAction
8+
9+
sylius_paypal_plugin_admin_enable_seller:
10+
path: /enable-seller/{id}
11+
methods: [POST]
12+
defaults:
13+
_controller: Sylius\PayPalPlugin\Controller\EnableSellerAction

src/Resources/config/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ sylius_grid:
3030
action:
3131
download_report: "@SyliusPayPalPlugin/Grid/downloadReport.html.twig"
3232
enable_pay_pal: "@SyliusPayPalPlugin/Grid/enablePayPal.html.twig"
33+
enable_seller: "@SyliusPayPalPlugin/Grid/enableSeller.html.twig"
3334
grids:
3435
sylius_admin_payment_method:
3536
actions:
3637
item:
38+
enable_seller:
39+
type: enable_seller
3740
download_report:
3841
type: download_report
3942
label: sylius.pay_pal.report

0 commit comments

Comments
 (0)