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 new file mode 100644 index 00000000..011d78a8 --- /dev/null +++ b/src/ApiPlatform/PayPalPayment.php @@ -0,0 +1,85 @@ +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..2f9d51f6 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -26,6 +26,12 @@ class="Sylius\PayPalPlugin\Generator\PayPalAuthAssertionGenerator" /> + + + + + +