authorize('canReviewBasketSummary', OrderTunnelStep::class); $output = $this->executor->execute( $query, new GetBasketSummaryQueryInput() ); return view('fo.tunnel.basket-summary', [ 'step' => $output->step, 'basket' => $output->basket, ]); } public function basketSummarySubmit( ValidateSummaryRequest $request, InitializeOrderTunnel $action ): RedirectResponse { $this->authorize('canReviewBasketSummary', OrderTunnelStep::class); $this->executor->execute( $action, $request->toInitializeOrderTunnelInput() ); return to_route('fo.sales.pick-time-slot'); } // * -- STEP 2 -- * public function pickTimeSlot(GetPickTimeSlotQuery $query): View { $this->authorize('canPickTimeSlot', OrderTunnelStep::class); $customer = $this->authService->getCustomerAsMandatory(); $output = $this->executor->execute( $query, new GetPickTimeSlotQueryInput($customer->getId()) ); return view('fo.tunnel.pick-time-slot', [ 'step' => $output->step, 'shop' => $output->shop, 'mapUrl' => $output->mapUrl, 'timeSlots' => $output->timeSlots, ]); } public function pickTimeSlotSubmit( PickTimeSlotRequest $request, ApplyPickTimeSlot $action ): RedirectResponse { $this->authorize('canPickTimeSlot', OrderTunnelStep::class); $output = $this->executor->execute( $action, $request->toApplyPickTimeSlotInput() ); return match ($output->status) { ApplyPickTimeSlotStatus::MATCHED => to_route('fo.sales.confirmation'), ApplyPickTimeSlotStatus::UNAVAILABLE => back()->withErrors([ 'timeslot' => trans('Invalid or unavailable time slot'), ])->withInput(), }; } // * -- STEP 3 -- * public function confirmation(GetOrderConfirmationQuery $query): View { $this->authorize('canFinalizeOrder', OrderTunnelStep::class); $output = $this->executor->execute( $query, new GetOrderConfirmationQueryInput() ); return view('fo.tunnel.confirmation', [ 'step' => $output->step, 'basket' => $output->basket, 'builder' => $output->builder, 'shop' => $output->shop, ]); } public function confirmationSubmit( ConfirmOrderRequest $request, PlacePendingOrder $placePendingOrder, ExportOrderToRegate $exportOrderToRegate, ConfirmPlacedOrder $confirmPlacedOrder, FailPendingOrder $failPendingOrder, ): RedirectResponse { $this->authorize('canFinalizeOrder', OrderTunnelStep::class); $placeInput = $request->toPlacePendingOrderInput(); $shopperId = $placeInput->shopperId ?? $this->authService->getCustomerAsMandatory()->getId(); try { return $this->checkoutLock->synchronize( key: "order:checkout:shopper:{$shopperId->value}", callback: function () use ( $placePendingOrder, $placeInput, $exportOrderToRegate, $confirmPlacedOrder, $failPendingOrder, ): RedirectResponse { /** @var PlacePendingOrderOutput $placeOutput */ $placeOutput = $this->executor->execute( $placePendingOrder, $placeInput ); if ($placeOutput->status !== PlacePendingOrderStatus::PENDING_CREATED) { return $this->redirectOnPendingFailure($placeOutput); } $orderId = $placeOutput->orderId ?? throw new \LogicException('Order ID is missing on pending created output.'); $orderReference = $placeOutput->orderReference ?? throw new \LogicException('Order reference is missing on pending created output.'); /** @var ExportOrderToRegateOutput $exportOutput */ $exportOutput = $this->executor->execute( $exportOrderToRegate, new ExportOrderToRegateInput($orderId) ); if ($exportOutput !== ExportOrderToRegateOutput::SENT) { $this->executor->execute($failPendingOrder, new FailPendingOrderInput($orderId)); return to_route('fo.sales.confirmation')->with( 'message', __('An error occurred while validating your order. Please try again later.') ); } /** @var ConfirmPlacedOrderOutput $confirmOutput */ $confirmOutput = $this->executor->execute( $confirmPlacedOrder, new ConfirmPlacedOrderInput($orderId) ); return match ($confirmOutput) { ConfirmPlacedOrderOutput::CONFIRMED => to_route( 'fo.sales.confirmation.show-confirmed', (string) $orderReference ), ConfirmPlacedOrderOutput::FAILED => to_route('fo.sales.confirmation')->with( 'message', __('An error occurred while validating your order. Please try again later.') ), }; }, ttlSeconds: 60, waitSeconds: 0, ); } catch (LockTimeoutException) { return back()->with( 'message', __('Your order is already being processed. Please wait a moment and try again.') ); } } private function redirectOnPendingFailure(PlacePendingOrderOutput $output): RedirectResponse { return match ($output->status) { PlacePendingOrderStatus::PENDING_CREATED => throw new \LogicException( 'Unexpected PENDING_CREATED status in failure handler.' ), PlacePendingOrderStatus::EXPIRED_TIME_SLOT => to_route('fo.sales.pick-time-slot')->withErrors([ 'timeslot' => trans($output->errorMessage ?? ''), ]), PlacePendingOrderStatus::INACTIVE_SHOPPER => throw new CustomerNotEmployedException(), PlacePendingOrderStatus::BASKET_PRICE_CHANGED, PlacePendingOrderStatus::UNORDERABLE_PRODUCT => to_route('fo.sales.summary')->withErrors([ 'basket' => trans($output->errorMessage ?? ''), ]), PlacePendingOrderStatus::ORDER_LIMIT_EXCEEDED, PlacePendingOrderStatus::WEEKLY_LIMIT_EXCEEDED => back()->with('message', $output->errorMessage), PlacePendingOrderStatus::LOCK_TIMEOUT => back()->with( 'message', __('Your order is already being processed. Please wait a moment and try again.') ), }; } // * -- CONFIRMATION -- * public function confirmed( Order $order, GetTunnelOrderConfirmedQuery $query ): View { $this->authorize('viewOwn', $order); $output = $this->executor->execute( $query, new GetTunnelOrderConfirmedQueryInput($order) ); return view('fo.tunnel.confirmed', [ 'step' => $output->step, 'order' => $output->order, 'shop' => $output->shop, 'mapUrl' => $output->mapUrl, ]); } }