Skip to content

Commit

Permalink
render payment buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
SirDomin committed Oct 23, 2020
1 parent d1c54bc commit 72a23ed
Show file tree
Hide file tree
Showing 5 changed files with 428 additions and 388 deletions.
55 changes: 54 additions & 1 deletion src/Controller/PayPalButtonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

namespace Sylius\PayPalPlugin\Controller;

use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\Payment;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface;
use Sylius\PayPalPlugin\Api\IdentityApiInterface;
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
use Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -39,14 +45,22 @@ final class PayPalButtonsController
/** @var AvailableCountriesProviderInterface */
private $availableCountriesProvider;

/** @var CacheAuthorizeClientApiInterface */
private $authorizeClientApi;

/** @var IdentityApiInterface */
private $identityApi;

public function __construct(
Environment $twig,
UrlGeneratorInterface $router,
ChannelContextInterface $channelContext,
LocaleContextInterface $localeContext,
PayPalConfigurationProviderInterface $payPalConfigurationProvider,
OrderRepositoryInterface $orderRepository,
AvailableCountriesProviderInterface $availableCountriesProvider
AvailableCountriesProviderInterface $availableCountriesProvider,
CacheAuthorizeClientApiInterface $authorizeClientApi,
IdentityApiInterface $identityApi
) {
$this->twig = $twig;
$this->router = $router;
Expand All @@ -55,6 +69,8 @@ public function __construct(
$this->payPalConfigurationProvider = $payPalConfigurationProvider;
$this->orderRepository = $orderRepository;
$this->availableCountriesProvider = $availableCountriesProvider;
$this->authorizeClientApi = $authorizeClientApi;
$this->identityApi = $identityApi;
}

public function renderProductPageButtonsAction(Request $request): Response
Expand Down Expand Up @@ -129,4 +145,41 @@ public function renderPaymentPageButtonsAction(Request $request): Response
return new Response('');
}
}

public function renderPayPalPaymentAction(Request $request): Response
{
$orderId = $request->attributes->getInt('orderId');
/** @var ChannelInterface $channel */
$channel = $this->channelContext->getChannel();
/** @var OrderInterface $order */
$order = $this->orderRepository->find($orderId);
/** @var PaymentInterface $payment */
$payment = $order->getLastPayment();
/** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $payment->getMethod();
/** @var GatewayConfigInterface $gatewayConfig */
$gatewayConfig = $paymentMethod->getGatewayConfig();
/** @var string $clientId */
$clientId = $gatewayConfig->getConfig()['client_id'];
/** @var string $partnerAttributionId */
$partnerAttributionId = $gatewayConfig->getConfig()['partner_attribution_id'];

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

$token = $this->authorizeClientApi->authorize($paymentMethod);
$clientToken = $this->identityApi->generateToken($token);

return new Response($this->twig->render('@SyliusPayPalPlugin/payWithPaypal.html.twig', [
'available_countries' => $this->availableCountriesProvider->provide(),
'billing_address' => $order->getBillingAddress(),
'client_id' => $clientId,
'client_token' => $clientToken,
'currency' => $order->getCurrencyCode(),
'locale' => $request->getLocale(),
'merchant_id' => $gatewayConfig->getConfig()['merchant_id'],
'order_token' => 'test',
'partner_attribution_id' => $partnerAttributionId,
]));
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/SyliusPayPalExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function load(array $config, ContainerBuilder $container): void
$container->setParameter('sylius.paypal.logging.increased', $config['logging']['increased']);

if ($config['sandbox']) {
$container->setParameter('sylius.pay_pal.facilitator_url', 'https://paypal.sylius.com');
$container->setParameter('sylius.pay_pal.facilitator_url', 'https://sylius.local:8001');
$container->setParameter('sylius.pay_pal.api_base_url', 'https://api.sandbox.paypal.com/');
$container->setParameter('sylius.pay_pal.reports_sftp_host', 'reports.sandbox.paypal.com');
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<argument type="service" id="Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface" />
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\IdentityApiInterface" />
</service>

<service id="Sylius\PayPalPlugin\Controller\PayWithPayPalFormAction">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends '@SyliusShop/Checkout/layout.html.twig' %}

{% form_theme form '@SyliusShop/Form/theme.html.twig' %}

{% block content %}
{{ sylius_template_event(['sylius.shop.checkout.complete.steps', 'sylius.shop.checkout.steps'], _context|merge({'active': 'complete', 'orderTotal': order.total})) }}

<div class="ui padded segment">
{% include '@SyliusShop/Checkout/Complete/_header.html.twig' %}

{{ sylius_template_event('sylius.shop.checkout.complete.after_content_header', {'order': order}) }}

{% include '@SyliusShop/_flashes.html.twig' %}

{{ form_start(form, {'action': path('sylius_shop_checkout_complete'), 'attr': {'class': 'ui loadable form', 'novalidate': 'novalidate'}}) }}
{{ form_errors(form) }}
<input type="hidden" name="_method" value="PUT" />

{{ sylius_template_event('sylius.shop.checkout.complete.summary', _context) }}

<div class="ui hidden divider"></div>
{% include '@SyliusShop/Checkout/Complete/_form.html.twig' %}

{{ sylius_template_event('sylius.shop.checkout.complete.before_navigation', {'order': order}) }}

{% if order.lastPayment.method.gatewayConfig.factoryName == 'sylius.pay_pal' %}
{{ render(controller('Sylius\\PayPalPlugin\\Controller\\PayPalButtonsController:renderPayPalPaymentAction', {'orderId': order.id})) }}
{% else %}
{% include '@SyliusShop/Checkout/Complete/_navigation.html.twig' %}
{% endif %}

{{ form_row(form._token) }}
{{ form_end(form, {'render_rest': false}) }}
</div>
{% endblock %}
Loading

0 comments on commit 72a23ed

Please sign in to comment.