Skip to content

Commit

Permalink
Handle order completion after checkout properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Zales0123 committed Nov 12, 2020
1 parent d9b7589 commit 956e1ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
28 changes: 24 additions & 4 deletions src/Controller/CompletePayPalOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<argument type="service" id="Sylius\PayPalPlugin\Manager\PaymentStateManagerInterface" />
<argument type="service" id="router" />
<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
2 changes: 1 addition & 1 deletion src/Resources/config/shop_routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/payWithPaypal.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<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>
<script>
let createPayPalOrderUrl = "{{ path('sylius_paypal_plugin_create_paypal_order_from_payment_page', { 'id': order_id }) }}";
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order_from_payment_page', { 'id': order_id }) }}"
let completePayPalOrderUrl = "{{ path('sylius_paypal_plugin_complete_paypal_order', { 'id': order_id }) }}"
let errorPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_payment_error') }}";
let availableCountries = {{ available_countries|json_encode|raw }};
let cancelPayPalPaymentUrl = "{{ path('sylius_paypal_plugin_cancel_payment') }}";
Expand Down

0 comments on commit 956e1ce

Please sign in to comment.