From 71a75e19e4028a2c45df07e651d03d645de8d42c Mon Sep 17 00:00:00 2001 From: SirDomin Date: Sat, 28 Nov 2020 17:15:59 +0100 Subject: [PATCH 1/4] api-platform support --- src/ApiPlatform/PayPalPayment.php | 81 +++++++++++++++++++++++++++++++ src/Resources/config/services.xml | 6 +++ 2 files changed, 87 insertions(+) create mode 100644 src/ApiPlatform/PayPalPayment.php diff --git a/src/ApiPlatform/PayPalPayment.php b/src/ApiPlatform/PayPalPayment.php new file mode 100644 index 00000000..602d9989 --- /dev/null +++ b/src/ApiPlatform/PayPalPayment.php @@ -0,0 +1,81 @@ +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(), + ]; + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 9ee48ceb..4ae44293 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -26,6 +26,12 @@ class="Sylius\PayPalPlugin\Generator\PayPalAuthAssertionGenerator" /> + + + + + + From f7e3cfb5d7ebed9da75a66cbd14d70b3aae8776b Mon Sep 17 00:00:00 2001 From: SirDomin Date: Wed, 2 Dec 2020 22:20:52 +0100 Subject: [PATCH 2/4] pr-fix + spec --- spec/ApiPlatform/PayPalPaymentSpec.php | 110 +++++++++++++++++++++++++ src/ApiPlatform/PayPalPayment.php | 16 ++-- 2 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 spec/ApiPlatform/PayPalPaymentSpec.php diff --git a/spec/ApiPlatform/PayPalPaymentSpec.php b/spec/ApiPlatform/PayPalPaymentSpec.php new file mode 100644 index 00000000..6aaf6ce7 --- /dev/null +++ b/spec/ApiPlatform/PayPalPaymentSpec.php @@ -0,0 +1,110 @@ +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'], + ] + ); + } +} diff --git a/src/ApiPlatform/PayPalPayment.php b/src/ApiPlatform/PayPalPayment.php index 602d9989..8f6965bf 100644 --- a/src/ApiPlatform/PayPalPayment.php +++ b/src/ApiPlatform/PayPalPayment.php @@ -59,15 +59,15 @@ public function provideConfiguration(PaymentInterface $payment): array return [ 'clientId' => $gatewayConfig->getConfig()['client_id'], 'completePayPalOrderFromPaymentPageUrl' => $this->router->generate( - 'sylius_paypal_plugin_complete_paypal_order', - ['token' => $order->getTokenValue()], - UrlGeneratorInterface::ABSOLUTE_URL - ), + '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 - ), + '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(), From 9f5fef74ceec0be5efff85d54a13a2e060e1bbb2 Mon Sep 17 00:00:00 2001 From: SirDomin Date: Thu, 3 Dec 2020 09:06:52 +0100 Subject: [PATCH 3/4] tag changed --- src/Resources/config/services.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 4ae44293..2f9d51f6 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -29,7 +29,7 @@ - + From a0c1577d6a3c80b88851d6947c3949c19bbf14b4 Mon Sep 17 00:00:00 2001 From: SirDomin Date: Thu, 3 Dec 2020 13:42:35 +0100 Subject: [PATCH 4/4] logic explained --- src/ApiPlatform/PayPalPayment.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ApiPlatform/PayPalPayment.php b/src/ApiPlatform/PayPalPayment.php index 8f6965bf..011d78a8 100644 --- a/src/ApiPlatform/PayPalPayment.php +++ b/src/ApiPlatform/PayPalPayment.php @@ -21,7 +21,11 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\RouterInterface; -//TODO: once we implement ApiPlatformPayment interface in core, implement it here +/** + * @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 */