Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single page payment #150

Open
wants to merge 10 commits into
base: 1.7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^7.3",

"sylius/sylius": "^1.7",
"sylius/sylius": "1.7.*",
"nyholm/append-query-string": "^0.1.1",
"phpseclib/phpseclib": "^2.0"
},
Expand Down
43 changes: 0 additions & 43 deletions src/Controller/CancelPayPalCheckoutPaymentAction.php

This file was deleted.

38 changes: 32 additions & 6 deletions src/Controller/CompletePayPalOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Sylius\PayPalPlugin\Controller;

use Sylius\Component\Core\Model\PaymentInterface;
use Doctrine\Persistence\ObjectManager;
use SM\Factory\FactoryInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\OrderCheckoutTransitions;
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
use Sylius\PayPalPlugin\Provider\PaymentProviderInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -20,28 +24,50 @@ final class CompletePayPalOrderAction
/** @var UrlGeneratorInterface */
private $router;

/** @var PaymentProviderInterface */
private $paymentProvider;

/** @var OrderProviderInterface */
private $orderProvider;

/** @var FactoryInterface */
private $stateMachineFactory;

/** @var ObjectManager */
private $orderManager;

public function __construct(
PaymentStateManagerInterface $paymentStateManager,
UrlGeneratorInterface $router,
OrderProviderInterface $orderProvider
PaymentProviderInterface $paymentProvider,
OrderProviderInterface $orderProvider,
FactoryInterface $stateMachineFactory,
ObjectManager $orderManager
) {
$this->paymentStateManager = $paymentStateManager;
$this->router = $router;
$this->paymentProvider = $paymentProvider;
$this->orderProvider = $orderProvider;
$this->stateMachineFactory = $stateMachineFactory;
$this->orderManager = $orderManager;
}

public function __invoke(Request $request): Response
{
$token = (string) $request->attributes->get('token');
$order = $this->orderProvider->provideOrderByToken($token);
/** @var PaymentInterface $payment */
$payment = $order->getLastPayment(PaymentInterface::STATE_PROCESSING);
$id = (string) $request->query->get('id');
$payment = $this->paymentProvider->getByPayPalOrderId($id);
/** @var OrderInterface $order */
$order = $payment->getOrder();

$this->paymentStateManager->complete($payment);

$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);

$this->orderManager->flush();

$request->getSession()->set('sylius_order_id', $order->getId());

return new JsonResponse([
'orderID' => $payment->getDetails()['paypal_order_id'],
'status' => $payment->getState(),
Expand Down
52 changes: 51 additions & 1 deletion src/Controller/PayPalButtonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

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\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\Processor\LocaleProcessorInterface;
use Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface;
use Sylius\PayPalPlugin\Provider\PayPalConfigurationProviderInterface;
Expand Down Expand Up @@ -43,6 +48,12 @@ final class PayPalButtonsController
/** @var LocaleProcessorInterface */
private $localeProcessor;

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

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

public function __construct(
Environment $twig,
UrlGeneratorInterface $router,
Expand All @@ -51,7 +62,9 @@ public function __construct(
PayPalConfigurationProviderInterface $payPalConfigurationProvider,
OrderRepositoryInterface $orderRepository,
AvailableCountriesProviderInterface $availableCountriesProvider,
LocaleProcessorInterface $localeProcessor
LocaleProcessorInterface $localeProcessor,
CacheAuthorizeClientApiInterface $authorizeClientApi,
IdentityApiInterface $identityApi
) {
$this->twig = $twig;
$this->router = $router;
Expand All @@ -61,6 +74,8 @@ public function __construct(
$this->orderRepository = $orderRepository;
$this->availableCountriesProvider = $availableCountriesProvider;
$this->localeProcessor = $localeProcessor;
$this->authorizeClientApi = $authorizeClientApi;
$this->identityApi = $identityApi;
}

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

public function renderPayPalPaymentAction(Request $request): Response
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a huge fan of the "no verification at all" to who is the owner of the order. Changing the orderId in the request could lead to some issues here. Is it right?

{
$orderId = $request->attributes->getInt('orderId');
/** @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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are so many places where we get data from the gateway config :/ We should definitely extract some service for that (but, of course, not in this PR 🖖)

/** @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' => $this->localeProcessor->process((string) $order->getLocaleCode()),
'merchant_id' => $gatewayConfig->getConfig()['merchant_id'],
'order_id' => $order->getId(),
'partner_attribution_id' => $partnerAttributionId,
]));
}
}
82 changes: 0 additions & 82 deletions src/Controller/PayWithPayPalFormAction.php

This file was deleted.

16 changes: 3 additions & 13 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@
<argument type="service" id="sylius.order_processing.order_payment_processor.checkout" />
</service>

<service id="Sylius\PayPalPlugin\Controller\CancelPayPalCheckoutPaymentAction">
<argument type="service" id="Sylius\PayPalPlugin\Provider\PaymentProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
</service>

<service id="Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction">
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
<argument type="service" id="router" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\PaymentProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Api\CompleteOrderApiInterface" />
<argument type="service" id="sm.factory" />
<argument type="service" id="sylius.manager.order" />
</service>

<service id="Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromPaymentPageAction" public="true">
Expand Down Expand Up @@ -84,12 +80,6 @@
<argument type="service" id="sylius.repository.order" />
<argument type="service" id="Sylius\PayPalPlugin\Provider\AvailableCountriesProviderInterface" />
<argument type="service" id="Sylius\PayPalPlugin\Processor\LocaleProcessorInterface" />
</service>

<service id="Sylius\PayPalPlugin\Controller\PayWithPayPalFormAction">
<argument type="service" id="twig" />
<argument type="service" id="sylius.repository.payment" />
<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>
Expand Down
16 changes: 2 additions & 14 deletions src/Resources/config/shop_routing.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
sylius_paypal_plugin_pay_with_paypal_form:
path: /pay-with-paypal/{id}
methods: [GET]
defaults:
_controller: Sylius\PayPalPlugin\Controller\PayWithPayPalFormAction

sylius_paypal_plugin_create_paypal_order:
path: /create-pay-pal-order/{token}
path: /create-pay-pal-order/{id}
methods: [POST]
defaults:
_controller: Sylius\PayPalPlugin\Controller\CreatePayPalOrderAction
Expand All @@ -17,7 +11,7 @@ sylius_paypal_plugin_create_paypal_order_from_cart:
_controller: Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromCartAction

sylius_paypal_plugin_complete_paypal_order:
path: /complete-pay-pal-order/{token}
path: /complete-pay-pal-order
methods: [POST]
defaults:
_controller: Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction
Expand Down Expand Up @@ -116,12 +110,6 @@ sylius_paypal_plugin_cancel_payment:
defaults:
_controller: Sylius\PayPalPlugin\Controller\CancelPayPalPaymentAction

sylius_paypal_plugin_cancel_checkout_payment:
path: /cancel-pay-pal-checkout-payment
methods: [POST]
defaults:
_controller: Sylius\PayPalPlugin\Controller\CancelPayPalCheckoutPaymentAction

sylius_paypal_plugin_payment_error:
path: /pay-pal-payment-error
methods: [POST]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% if order.lastPayment.method.gatewayConfig.factoryName == 'sylius.pay_pal' and order.lastPayment.state == 'cart' %}
{{ render(controller('Sylius\\PayPalPlugin\\Controller\\PayPalButtonsController:renderPayPalPaymentAction', {'orderId': order.id})) }}
{% else %}
<button type="submit" class="ui huge primary fluid icon labeled button" {{ sylius_test_html_attribute('confirmation-button') }}>
<i class="check icon"></i> {{ 'sylius.ui.place_order'|trans }}
</button>
{% endif %}
Loading