-
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.
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
- Loading branch information
Showing
3 changed files
with
201 additions
and
0 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,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'], | ||
] | ||
); | ||
} | ||
} |
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,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(), | ||
]; | ||
} | ||
} |
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