diff --git a/Controller/PayPalApiController.php b/Controller/PayPalApiController.php index 55c9801..580ce4c 100644 --- a/Controller/PayPalApiController.php +++ b/Controller/PayPalApiController.php @@ -9,10 +9,15 @@ use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Attribute\Route; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Thelia\Controller\Front\BaseFrontController; +use Thelia\Core\Event\Order\OrderEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Log\Tlog; +use Thelia\Model\Base\OrderStatusQuery; use Thelia\Model\CurrencyQuery; use Thelia\Model\OrderQuery; +use Thelia\Model\OrderStatus; #[Route("/paypal/api", name: "paypal_api_")] class PayPalApiController extends BaseFrontController @@ -94,7 +99,7 @@ public function createOrder(Request $request, PayPalApiService $payPalApiService } #[Route("/capture", name: "capture", methods: "POST")] - public function captureOrder(Request $request, PayPalApiService $payPalApiService) + public function captureOrder(Request $request, PayPalApiService $payPalApiService, EventDispatcherInterface $dispatcher) { try { $data = json_decode($request->getContent(), true); @@ -107,6 +112,12 @@ public function captureOrder(Request $request, PayPalApiService $payPalApiServic ); $responseContent = $response->getContent(); + $status = json_decode($responseContent, true)['status']; + if ("COMPLETED" === $status){ + $event = new OrderEvent($paypalOrder->getOrder()); + $event->setStatus(OrderStatusQuery::create()->filterByCode(OrderStatus::CODE_PAID)->findOne()?->getId()); + $dispatcher->dispatch($event, TheliaEvents::ORDER_UPDATE_STATUS); + } return new JsonResponse($responseContent); } catch (\Exception $exception) { Tlog::getInstance()->error($exception->getMessage()); diff --git a/templates/frontOffice/default/paypal/paypal-payment.html b/templates/frontOffice/default/paypal/paypal-payment.html index ba0f1f0..d200492 100644 --- a/templates/frontOffice/default/paypal/paypal-payment.html +++ b/templates/frontOffice/default/paypal/paypal-payment.html @@ -147,14 +147,46 @@

{intl l="PayPal - Sélection de paiement"}

} } + async function capturePaypalOrder(orderId) + { + try { + const response = await fetch("/paypal/api/capture", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + order_id: orderId + }), + }); + + const order = await response.json(); + + if (!response.ok) { + console.error('Error:', order.error.message); + return null; + } + + const parsedOrder = JSON.parse(order); + + if (parsedOrder.status === "COMPLETED") { + window.location.href = `/order/placed/{$order_id}`; + } else { + window.location.href = `/order-failed`; + } + + } catch (error) { + console.error('Error:', error); + return null; + } + } + paypal.Buttons({ createOrder: function(data, actions) { - return createPaypalOrder(orderId) + return createPaypalOrder(orderId); }, onApprove: function (data, actions) { - console.log('test Paypal') - console.log(data.orderID) - window.location.href = `/order/placed/{$order_id}`; + capturePaypalOrder(orderId); } }).render('#paypal-button-container');