Skip to content

Commit e91f27e

Browse files
committed
Handle order completion after checkout properly
1 parent 3d8b66c commit e91f27e

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/Controller/CompletePayPalOrderAction.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Sylius\PayPalPlugin\Controller;
66

7+
use Doctrine\Persistence\ObjectManager;
8+
use SM\Factory\FactoryInterface;
79
use Sylius\Component\Core\Model\PaymentInterface;
10+
use Sylius\Component\Core\OrderCheckoutTransitions;
811
use Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface;
912
use Sylius\PayPalPlugin\Provider\OrderProviderInterface;
1013
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -23,25 +26,42 @@ final class CompletePayPalOrderAction
2326
/** @var OrderProviderInterface */
2427
private $orderProvider;
2528

29+
/** @var FactoryInterface */
30+
private $stateMachineFactory;
31+
32+
/** @var ObjectManager */
33+
private $orderManager;
34+
2635
public function __construct(
2736
PaymentStateManagerInterface $paymentStateManager,
2837
UrlGeneratorInterface $router,
29-
OrderProviderInterface $orderProvider
38+
OrderProviderInterface $orderProvider,
39+
FactoryInterface $stateMachineFactory,
40+
ObjectManager $orderManager
3041
) {
3142
$this->paymentStateManager = $paymentStateManager;
3243
$this->router = $router;
3344
$this->orderProvider = $orderProvider;
45+
$this->stateMachineFactory = $stateMachineFactory;
46+
$this->orderManager = $orderManager;
3447
}
3548

3649
public function __invoke(Request $request): Response
3750
{
38-
$token = (string) $request->attributes->get('token');
39-
$order = $this->orderProvider->provideOrderByToken($token);
51+
$id = (int) $request->attributes->get('id');
52+
$order = $this->orderProvider->provideOrderById($id);
53+
4054
/** @var PaymentInterface $payment */
4155
$payment = $order->getLastPayment(PaymentInterface::STATE_PROCESSING);
42-
4356
$this->paymentStateManager->complete($payment);
4457

58+
$stateMachine = $this->stateMachineFactory->get($order, OrderCheckoutTransitions::GRAPH);
59+
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_COMPLETE);
60+
61+
$this->orderManager->flush();
62+
63+
$request->getSession()->set('sylius_order_id', $order->getId());
64+
4565
return new JsonResponse([
4666
'orderID' => $payment->getDetails()['paypal_order_id'],
4767
'status' => $payment->getState(),

src/Resources/config/services/controller.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
3030
<argument type="service" id="router" />
3131
<argument type="service" id="Sylius\PayPalPlugin\Provider\OrderProviderInterface" />
32-
<argument type="service" id="Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface" />
33-
<argument type="service" id="Sylius\PayPalPlugin\Api\CompleteOrderApiInterface" />
32+
<argument type="service" id="sm.factory" />
33+
<argument type="service" id="sylius.manager.order" />
3434
</service>
3535

3636
<service id="Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromPaymentPageAction" public="true">

src/Resources/config/shop_routing.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ sylius_paypal_plugin_create_paypal_order_from_cart:
1111
_controller: Sylius\PayPalPlugin\Controller\CreatePayPalOrderFromCartAction
1212

1313
sylius_paypal_plugin_complete_paypal_order:
14-
path: /complete-pay-pal-order/{token}
14+
path: /complete-pay-pal-order/{id}
1515
methods: [POST]
1616
defaults:
1717
_controller: Sylius\PayPalPlugin\Controller\CompletePayPalOrderAction

src/Resources/views/payWithPaypal.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
<script src="https://www.paypal.com/sdk/js?components=hosted-fields,buttons,funding-eligibility&locale={{ locale }}&currency={{ currency }}&vault=false&client-id={{ client_id }}&merchant-id={{ merchant_id }}&intent=capture" data-partner-attribution-id="{{ partner_attribution_id }}" data-enable-3ds data-client-token="{{ client_token }}"></script>
183183
<script>
184184
let createPayPalOrderUrl = "{{ path('sylius_paypal_plugin_create_paypal_order_from_payment_page', { 'id': order_id }) }}";
185-
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order_from_payment_page', { 'id': order_id }) }}"
185+
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order', { 'id': order_id }) }}"
186186
let errorPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_payment_error') }}";
187187
let availableCountries = {{ available_countries|json_encode|raw }};
188188
let cancelPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_cancel_payment') }}";

0 commit comments

Comments
 (0)