middleware([ RedirectIfCartIsEmpty::class, CheckCartStock::class, CheckCouponUsageLimit::class, ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { Gateway::all(); return view('public.checkout.create', [ 'cart' => Cart::instance(), 'countries' => Country::supported(), 'gateways' => Gateway::all(), 'defaultAddress' => auth()->user()->defaultAddress ?? new DefaultAddress, 'addresses' => $this->getAddresses(), 'termsPageURL' => Page::urlForPage(setting('storefront_terms_page')), ]); } /** * Get addresses for the logged in user. * * @return \Illuminate\Support\Collection */ private function getAddresses() { if (auth()->guest()) { return collect(); } return auth()->user()->addresses->keyBy('id'); } /** * Store a newly created resource in storage. * * @param \Modules\Order\Http\Requests\StoreOrderRequest $request * @param \Modules\User\Services\CustomerService $customerService * @param \Modules\Checkout\Services\OrderService $orderService * @return \Illuminate\Http\Response */ public function store(StoreOrderRequest $request, CustomerService $customerService, OrderService $orderService) { if (auth()->guest() && $request->create_an_account) { $customerService->register($request)->login(); } $order = $orderService->create($request); $gateway = Gateway::get($request->payment_method); try { $response = $gateway->purchase($order, $request); } catch (Exception $e) { $orderService->delete($order); return response()->json([ 'message' => $e->getMessage(), ], 403); } return response()->json($response); } }