diff --git a/src/Controller/CompletePayPalOrderAction.php b/src/Controller/CompletePayPalOrderAction.php index e7a24936..54147f15 100644 --- a/src/Controller/CompletePayPalOrderAction.php +++ b/src/Controller/CompletePayPalOrderAction.php @@ -4,7 +4,10 @@ namespace Sylius\PayPalPlugin\Controller; +use Doctrine\Persistence\ObjectManager; +use SM\Factory\FactoryInterface; use Sylius\Component\Core\Model\PaymentInterface; +use Sylius\Component\Core\OrderCheckoutTransitions; use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface; use Sylius\PayPalPlugin\Provider\OrderProviderInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -23,25 +26,42 @@ final class CompletePayPalOrderAction /** @var OrderProviderInterface */ private $orderProvider; + /** @var FactoryInterface */ + private $stateMachineFactory; + + /** @var ObjectManager */ + private $orderManager; + public function __construct( PaymentStateManagerInterface $paymentStateManager, UrlGeneratorInterface $router, - OrderProviderInterface $orderProvider + OrderProviderInterface $orderProvider, + FactoryInterface $stateMachineFactory, + ObjectManager $orderManager ) { $this->paymentStateManager = $paymentStateManager; $this->router = $router; $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); + $id = (int) $request->attributes->get('id'); + $order = $this->orderProvider->provideOrderById($id); + /** @var PaymentInterface $payment */ $payment = $order->getLastPayment(PaymentInterface::STATE_PROCESSING); - $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/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index da8643a0..57437154 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -29,8 +29,8 @@ - - + + diff --git a/src/Resources/config/shop_routing.yaml b/src/Resources/config/shop_routing.yaml index 50201335..d481e395 100644 --- a/src/Resources/config/shop_routing.yaml +++ b/src/Resources/config/shop_routing.yaml @@ -11,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/{id} methods: [POST] defaults: _controller: Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction diff --git a/src/Resources/views/payWithPaypal.html.twig b/src/Resources/views/payWithPaypal.html.twig index 108fe7cb..735fbefe 100644 --- a/src/Resources/views/payWithPaypal.html.twig +++ b/src/Resources/views/payWithPaypal.html.twig @@ -182,7 +182,7 @@