Skip to content

Commit

Permalink
Enable disabled PayPal payment method with button
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Sep 10, 2020
1 parent 6743ead commit 5264cae
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 8 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])
;
}
}
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');
}
}
15 changes: 9 additions & 6 deletions src/Form/Extension/PaymentMethodTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@
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\CheckboxType;
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)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event): void {
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
/** @var PaymentMethodInterface $data */
$data = $event->getData();
$form = $event->getForm();

if ($data->getGatewayConfig()->getFactoryName() === 'sylius.pay_pal') {
$form->add('enabled', CheckboxType::class, [
/** @var GatewayConfigInterface $gatewayConfig */
$gatewayConfig = $data->getGatewayConfig();
if ($gatewayConfig->getFactoryName() === 'sylius.pay_pal') {
$form->add('enabled', HiddenType::class, [
'required' => false,
'disabled' => true,
'label' => 'sylius.form.payment_method.enabled',
'data' => $data->isEnabled(),
]);
}
});
Expand Down
4 changes: 2 additions & 2 deletions src/Onboarding/Processor/BasicOnboardingProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function process(
'partner_attribution_id' => $response['partner_attribution_id'],
]);

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

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
9 changes: 9 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,14 @@
>
<argument type="service" id="sylius.manager.payment" />
</service>

<service
id="Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface"
class="Sylius\PayPalPlugin\Enabler\PayPalPaymentMethodEnabler"
>
<argument type="service" id="sylius.http_client" />
<argument>%sylius.paypal.facilitator_url%</argument>
<argument type="service" id="sylius.manager.payment_method" />
</service>
</services>
</container>
5 changes: 5 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<argument type="service" id="sylius.repository.payment_method" />
</service>

<service id="Sylius\PayPalPlugin\Controller\EnableSellerAction" public="true">
<argument type="service" id="sylius.repository.payment_method" />
<argument type="service" id="Sylius\PayPalPlugin\Enabler\PaymentMethodEnablerInterface" />
</service>

<service id="Sylius\PayPalPlugin\Controller\CreatePayPalOrderAction">
<argument type="service" id="payum" />
<argument type="service" id="sylius.repository.order" />
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/flashes.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ sylius:
pay_pal:
order_cancelled: 'PayPal order has been cancelled'
payment_cancelled: 'PayPal payment has been cancelled'
payment_enabled: 'PayPal payment has just been enabled.'
payment_not_enabled: 'PayPal payment could not be enabled. Try again later.'
something_went_wrong: 'Something went wrong. Please, try again.'
5 changes: 5 additions & 0 deletions src/Resources/views/Grid/enableSeller.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% if data.gatewayConfig.factoryName == 'sylius.pay_pal' and not data.enabled %}
<form action="{{ path('sylius_paypal_plugin_admin_enable_seller', {'id': data.id}) }}" method="post">
<button type="submit" class="ui button purple icon labeled"><i class="icon checkmark"></i> {{ 'sylius.ui.enable'|trans }}</button>
</form>
{% endif %}

0 comments on commit 5264cae

Please sign in to comment.