-
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.
Enable disabled PayPal payment method with button
- Loading branch information
Showing
13 changed files
with
264 additions
and
8 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
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
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
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
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,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 %} |