Skip to content

Commit be62241

Browse files
authored
feature #170 API Platform support (SirDomin)
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
2 parents b334b3b + a0c1577 commit be62241

File tree

3 files changed

+201
-0
lines changed

3 files changed

+201
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\ApiPlatform;
15+
16+
use Payum\Core\Model\GatewayConfigInterface;
17+
use PhpSpec\ObjectBehavior;
18+
use Sylius\Component\Core\Model\OrderInterface;
19+
use Sylius\Component\Core\Model\PaymentInterface;
20+
use Sylius\Component\Core\Model\PaymentMethodInterface;
21+
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
22+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23+
use Symfony\Component\Routing\RouterInterface;
24+
25+
final class PayPalPaymentSpec extends ObjectBehavior
26+
{
27+
function let(RouterInterface $router, AvailableCountriesProviderInterface $availableCountriesProvider): void
28+
{
29+
$this->beConstructedWith($router, $availableCountriesProvider);
30+
}
31+
32+
function it_supports_paypal_payment_method(
33+
PaymentMethodInterface $paymentMethod,
34+
GatewayConfigInterface $gatewayConfig
35+
): void {
36+
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
37+
38+
$gatewayConfig->getFactoryName()->willReturn('sylius.pay_pal');
39+
40+
$this->supports($paymentMethod)->shouldReturn(true);
41+
}
42+
43+
function it_provides_proper_paypal_configuration(
44+
PaymentInterface $payment,
45+
PaymentMethodInterface $paymentMethod,
46+
OrderInterface $order,
47+
GatewayConfigInterface $gatewayConfig,
48+
AvailableCountriesProviderInterface $availableCountriesProvider,
49+
RouterInterface $router
50+
): void {
51+
$payment->getMethod()->willReturn($paymentMethod);
52+
53+
$paymentMethod->getGatewayConfig()->willReturn($gatewayConfig);
54+
$gatewayConfig->getConfig()->willReturn(
55+
[
56+
'client_id' => 'CLIENT-ID',
57+
'partner_attribution_id' => 'PARTNER-ATTRIBUTION-ID',
58+
]
59+
);
60+
61+
$payment->getOrder()->willReturn($order);
62+
63+
$order->getId()->willReturn(20);
64+
$order->getLocaleCode()->willReturn('en_US');
65+
$order->getCurrencyCode()->willReturn('USD');
66+
$order->getTokenValue()->willReturn('TOKEN');
67+
68+
$availableCountriesProvider->provide()->willReturn(['PL', 'US']);
69+
70+
$router->generate(
71+
'sylius_paypal_plugin_complete_paypal_order',
72+
['token' => 'TOKEN'],
73+
UrlGeneratorInterface::ABSOLUTE_URL
74+
)->willReturn('https://path-to-complete/TOKEN');
75+
76+
$router->generate(
77+
'sylius_paypal_plugin_create_paypal_order',
78+
['token' => 'TOKEN'],
79+
UrlGeneratorInterface::ABSOLUTE_URL
80+
)->willReturn('https://path-to-create/TOKEN');
81+
82+
$router->generate(
83+
'sylius_paypal_plugin_cancel_payment',
84+
[],
85+
UrlGeneratorInterface::ABSOLUTE_URL
86+
)->willReturn('https://path-to-cancel');
87+
88+
$router->generate(
89+
'sylius_paypal_plugin_payment_error',
90+
[],
91+
UrlGeneratorInterface::ABSOLUTE_URL
92+
)->willReturn('https://path-to-error');
93+
94+
$this->provideConfiguration($payment)->shouldReturn(
95+
[
96+
'clientId' => 'CLIENT-ID',
97+
'completePayPalOrderFromPaymentPageUrl' => 'https://path-to-complete/TOKEN',
98+
'createPayPalOrderFromPaymentPageUrl' => 'https://path-to-create/TOKEN',
99+
'cancelPayPalPaymentUrl' => 'https://path-to-cancel',
100+
'partnerAttributionId' => 'PARTNER-ATTRIBUTION-ID',
101+
'locale' => 'en_US',
102+
'orderId' => 20,
103+
'currency' => 'USD',
104+
'orderToken' => 'TOKEN',
105+
'errorPayPalPaymentUrl' => 'https://path-to-error',
106+
'available_countries' => ['PL', 'US'],
107+
]
108+
);
109+
}
110+
}

src/ApiPlatform/PayPalPayment.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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\ApiPlatform;
15+
16+
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
17+
use Sylius\Component\Core\Model\OrderInterface;
18+
use Sylius\Component\Core\Model\PaymentInterface;
19+
use Sylius\Component\Core\Model\PaymentMethodInterface;
20+
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
21+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22+
use Symfony\Component\Routing\RouterInterface;
23+
24+
/**
25+
* @experimental
26+
* This part is used to wire payment method handlers with Api Platform based on (https://github.com/Sylius/Sylius/pull/12107)
27+
* For now its dead code for itself, once we tag new Sylius version with new changes this code will be used.
28+
*/
29+
final class PayPalPayment
30+
{
31+
/** @var RouterInterface */
32+
private $router;
33+
34+
/** @var AvailableCountriesProviderInterface */
35+
private $availableCountriesProvider;
36+
37+
public function __construct(RouterInterface $router, AvailableCountriesProviderInterface $availableCountriesProvider)
38+
{
39+
$this->router = $router;
40+
$this->availableCountriesProvider = $availableCountriesProvider;
41+
}
42+
43+
public function supports(PaymentMethodInterface $paymentMethod): bool
44+
{
45+
/** @var GatewayConfigInterface $gatewayConfig */
46+
$gatewayConfig = $paymentMethod->getGatewayConfig();
47+
48+
return $gatewayConfig->getFactoryName() === 'sylius.pay_pal';
49+
}
50+
51+
//TODO: use provider here and in Buttons controller
52+
public function provideConfiguration(PaymentInterface $payment): array
53+
{
54+
/** @var PaymentMethodInterface $paymentMethod */
55+
$paymentMethod = $payment->getMethod();
56+
57+
/** @var OrderInterface $order */
58+
$order = $payment->getOrder();
59+
60+
/** @var GatewayConfigInterface $gatewayConfig */
61+
$gatewayConfig = $paymentMethod->getGatewayConfig();
62+
63+
return [
64+
'clientId' => $gatewayConfig->getConfig()['client_id'],
65+
'completePayPalOrderFromPaymentPageUrl' => $this->router->generate(
66+
'sylius_paypal_plugin_complete_paypal_order',
67+
['token' => $order->getTokenValue()],
68+
UrlGeneratorInterface::ABSOLUTE_URL
69+
),
70+
'createPayPalOrderFromPaymentPageUrl' => $this->router->generate(
71+
'sylius_paypal_plugin_create_paypal_order',
72+
['token' => $order->getTokenValue()],
73+
UrlGeneratorInterface::ABSOLUTE_URL
74+
),
75+
'cancelPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_cancel_payment', [], UrlGeneratorInterface::ABSOLUTE_URL),
76+
'partnerAttributionId' => $gatewayConfig->getConfig()['partner_attribution_id'],
77+
'locale' => $order->getLocaleCode(),
78+
'orderId' => $order->getId(),
79+
'currency' => $order->getCurrencyCode(),
80+
'orderToken' => $order->getTokenValue(),
81+
'errorPayPalPaymentUrl' => $this->router->generate('sylius_paypal_plugin_payment_error', [], UrlGeneratorInterface::ABSOLUTE_URL),
82+
'available_countries' => $this->availableCountriesProvider->provide(),
83+
];
84+
}
85+
}

src/Resources/config/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
class="Sylius\PayPalPlugin\Generator\PayPalAuthAssertionGenerator"
2727
/>
2828

29+
<service id="Sylius\PayPalPlugin\ApiPlatform\PayPalPayment">
30+
<argument type="service" id="router" />
31+
<argument type="service" id="Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface" />
32+
<tag name="sylius.api.payment_method_handler" />
33+
</service>
34+
2935
<service id="Sylius\PayPalPlugin\Listener\PayPalPaymentMethodListener">
3036
<argument type="service" id="Sylius\PayPalPlugin\Onboarding\Initiator\OnboardingInitiatorInterface" />
3137
<argument type="service" id="router" />

0 commit comments

Comments
 (0)