diff --git a/composer.json b/composer.json
index e391935c..26f9f806 100644
--- a/composer.json
+++ b/composer.json
@@ -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"
},
diff --git a/src/Controller/CancelPayPalCheckoutPaymentAction.php b/src/Controller/CancelPayPalCheckoutPaymentAction.php
deleted file mode 100644
index 523db726..00000000
--- a/src/Controller/CancelPayPalCheckoutPaymentAction.php
+++ /dev/null
@@ -1,43 +0,0 @@
-paymentProvider = $paymentProvider;
- $this->paymentStateManager = $paymentStateManager;
- }
-
- public function __invoke(Request $request): Response
- {
- $content = (array) json_decode((string) $request->getContent(false), true);
-
- $payment = $this->paymentProvider->getByPayPalOrderId((string) $content['payPalOrderId']);
-
- /** @var FlashBagInterface $flashBag */
- $flashBag = $request->getSession()->getBag('flashes');
- $flashBag->add('error', 'sylius.pay_pal.something_went_wrong');
-
- $this->paymentStateManager->cancel($payment);
-
- return new Response('', Response::HTTP_NO_CONTENT);
- }
-}
diff --git a/src/Controller/CompletePayPalOrderAction.php b/src/Controller/CompletePayPalOrderAction.php
index e7a24936..fa18fefc 100644
--- a/src/Controller/CompletePayPalOrderAction.php
+++ b/src/Controller/CompletePayPalOrderAction.php
@@ -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;
@@ -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(),
diff --git a/src/Controller/PayPalButtonsController.php b/src/Controller/PayPalButtonsController.php
index f2a577ce..8a5caf79 100644
--- a/src/Controller/PayPalButtonsController.php
+++ b/src/Controller/PayPalButtonsController.php
@@ -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;
@@ -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,
@@ -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;
@@ -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
@@ -135,4 +150,39 @@ public function renderPaymentPageButtonsAction(Request $request): Response
return new Response('');
}
}
+
+ public function renderPayPalPaymentAction(Request $request): Response
+ {
+ $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();
+ /** @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,
+ ]));
+ }
}
diff --git a/src/Controller/PayWithPayPalFormAction.php b/src/Controller/PayWithPayPalFormAction.php
deleted file mode 100644
index 8e2ace2c..00000000
--- a/src/Controller/PayWithPayPalFormAction.php
+++ /dev/null
@@ -1,82 +0,0 @@
-twig = $twig;
- $this->paymentRepository = $paymentRepository;
- $this->countriesProvider = $countriesProvider;
- $this->authorizeClientApi = $authorizeClientApi;
- $this->identityApi = $identityApi;
- }
-
- public function __invoke(Request $request): Response
- {
- /** @var PaymentInterface $payment */
- $payment = $this->paymentRepository->find($request->attributes->get('id'));
- /** @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->countriesProvider->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' => $order->getTokenValue(),
- 'partner_attribution_id' => $partnerAttributionId,
- ]));
- }
-}
diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml
index 1127c00e..aee9aba6 100644
--- a/src/Resources/config/services/controller.xml
+++ b/src/Resources/config/services/controller.xml
@@ -25,17 +25,13 @@