Skip to content

Commit

Permalink
feature #170 API Platform support (SirDomin)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.0-dev branch.

Discussion
----------

By using plugin class we can access its data by request from ApiPlatform within Sylius api platform payment integration.
Some refactor is needed inside PayPalPlugin.

Commits
-------

71a75e1 api-platform support
f7e3cfb pr-fix + spec
9f5fef7 tag changed
a0c1577 logic explained
  • Loading branch information
Zales0123 authored Dec 3, 2020
2 parents b334b3b + a0c1577 commit be62241
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
110 changes: 110 additions & 0 deletions spec/ApiPlatform/PayPalPaymentSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?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\ApiPlatform;

use Payum\Core\Model\GatewayConfigInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

final class PayPalPaymentSpec extends ObjectBehavior
{
function let(RouterInterface $router, AvailableCountriesProviderInterface $availableCountriesProvider): void
{
$this->beConstructedWith($router, $availableCountriesProvider);
}

function it_supports_paypal_payment_method(
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig
): void {
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);

$gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal');

$this->supports($paymentMethod)->shouldReturn(true);
}

function it_provides_proper_paypal_configuration(
PaymentInterface $payment,
PaymentMethodInterface $paymentMethod,
OrderInterface $order,
GatewayConfigInterface $gatewayConfig,
AvailableCountriesProviderInterface $availableCountriesProvider,
RouterInterface $router
): void {
$payment->getMethod()->willReturn($paymentMethod);

$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
$gatewayConfig->getConfig()->willReturn(
[
'client_id' => 'CLIENT-ID',
'partner_attribution_id' => 'PARTNER-ATTRIBUTION-ID',
]
);

$payment->getOrder()->willReturn($order);

$order->getId()->willReturn(20);
$order->getLocaleCode()->willReturn('en_US');
$order->getCurrencyCode()->willReturn('USD');
$order->getTokenValue()->willReturn('TOKEN');

$availableCountriesProvider->provide()->willReturn(['PL', 'US']);

$router->generate(
'sylius_paypal_plugin_complete_paypal_order',
['token' => 'TOKEN'],
UrlGeneratorInterface::ABSOLUTE_URL
)->willReturn('https://path-to-complete/TOKEN');

$router->generate(
'sylius_paypal_plugin_create_paypal_order',
['token' => 'TOKEN'],
UrlGeneratorInterface::ABSOLUTE_URL
)->willReturn('https://path-to-create/TOKEN');

$router->generate(
'sylius_paypal_plugin_cancel_payment',
[],
UrlGeneratorInterface::ABSOLUTE_URL
)->willReturn('https://path-to-cancel');

$router->generate(
'sylius_paypal_plugin_payment_error',
[],
UrlGeneratorInterface::ABSOLUTE_URL
)->willReturn('https://path-to-error');

$this->provideConfiguration($payment)->shouldReturn(
[
'clientId' => 'CLIENT-ID',
'completePayPalOrderFromPaymentPageUrl' => 'https://path-to-complete/TOKEN',
'createPayPalOrderFromPaymentPageUrl' => 'https://path-to-create/TOKEN',
'cancelPayPalPaymentUrl' => 'https://path-to-cancel',
'partnerAttributionId' => 'PARTNER-ATTRIBUTION-ID',
'locale' => 'en_US',
'orderId' => 20,
'currency' => 'USD',
'orderToken' => 'TOKEN',
'errorPayPalPaymentUrl' => 'https://path-to-error',
'available_countries' => ['PL', 'US'],
]
);
}
}
85 changes: 85 additions & 0 deletions src/ApiPlatform/PayPalPayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\ApiPlatform;

use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;

/**
* @experimental
* This part is used to wire payment method handlers with Api Platform based on (https://github.com/Sylius/Sylius/pull/12107)
* For now its dead code for itself, once we tag new Sylius version with new changes this code will be used.
*/
final class PayPalPayment
{
/** @var RouterInterface */
private $router;

/** @var AvailableCountriesProviderInterface */
private $availableCountriesProvider;

public function __construct(RouterInterface $router, AvailableCountriesProviderInterface $availableCountriesProvider)
{
$this->router = $router;
$this->availableCountriesProvider = $availableCountriesProvider;
}

public function supports(PaymentMethodInterface $paymentMethod): bool
{
/** @var GatewayConfigInterface $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();

return $gatewayConfig->getFactoryName() === 'sylius.pay_pal';
}

//TODO: use provider here and in Buttons controller
public function provideConfiguration(PaymentInterface $payment): array
{
/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $payment->getMethod();

/** @var OrderInterface $order */
$order = $payment->getOrder();

/** @var GatewayConfigInterface $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();

return [
'clientId' => $gatewayConfig->getConfig()['client_id'],
'completePayPalOrderFromPaymentPageUrl' => $this->router->generate(
'sylius_paypal_plugin_complete_paypal_order',
['token' => $order->getTokenValue()],
UrlGeneratorInterface::ABSOLUTE_URL
),
'createPayPalOrderFromPaymentPageUrl' => $this->router->generate(
'sylius_paypal_plugin_create_paypal_order',
['token' => $order->getTokenValue()],
UrlGeneratorInterface::ABSOLUTE_URL
),
'cancelPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_cancel_payment', [], UrlGeneratorInterface::ABSOLUTE_URL),
'partnerAttributionId' => $gatewayConfig->getConfig()['partner_attribution_id'],
'locale' => $order->getLocaleCode(),
'orderId' => $order->getId(),
'currency' => $order->getCurrencyCode(),
'orderToken' => $order->getTokenValue(),
'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error', [], UrlGeneratorInterface::ABSOLUTE_URL),
'available_countries' => $this->availableCountriesProvider->provide(),
];
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
class="Sylius\PayPalPlugin\Generator\PayPalAuthAssertionGenerator"
/>

<service id="Sylius\PayPalPlugin\ApiPlatform\PayPalPayment">
<argument type="service" id="router" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface" />
<tag name="sylius.api.payment_method_handler" />
</service>

<service id="Sylius\PayPalPlugin\Listener\PayPalPaymentMethodListener">
<argument type="service" id="Sylius\PayPalPlugin\Onboarding\Initiator\OnboardingInitiatorInterface" />
<argument type="service" id="router" />
Expand Down

0 comments on commit be62241

Please sign in to comment.