all(); if ( ! auth()->guard('customer')->check() && ! Cart::getCart()->hasGuestCheckoutItems() ) { return new JsonResource([ 'redirect' => true, 'data' => route('shop.customer.session.index'), ]); } if (Cart::hasError()) { return new JsonResource([ 'redirect' => true, 'redirect_url' => route('shop.checkout.cart.index'), ]); } Cart::saveAddresses($params); $cart = Cart::getCart(); Cart::collectTotals(); if ($cart->haveStockableItems()) { if (! $rates = Shipping::collectRates()) { return new JsonResource([ 'redirect' => true, 'redirect_url' => route('shop.checkout.cart.index'), ]); } return new JsonResource([ 'redirect' => false, 'data' => $rates, ]); } return new JsonResource([ 'redirect' => false, 'data' => Payment::getSupportedPaymentMethods(), ]); } /** * Store shipping method. * * @return \Illuminate\Http\Response */ public function storeShippingMethod() { $validatedData = $this->validate(request(), [ 'shipping_method' => 'required', ]); if ( Cart::hasError() || ! $validatedData['shipping_method'] || ! Cart::saveShippingMethod($validatedData['shipping_method']) ) { return response()->json([ 'redirect_url' => route('shop.checkout.cart.index'), ], Response::HTTP_FORBIDDEN); } Cart::collectTotals(); return response()->json(Payment::getSupportedPaymentMethods()); } /** * Store payment method. * * @return array */ public function storePaymentMethod() { $validatedData = $this->validate(request(), [ 'payment' => 'required', ]); if ( Cart::hasError() || ! $validatedData['payment'] || ! Cart::savePaymentMethod($validatedData['payment']) ) { return response()->json([ 'redirect_url' => route('shop.checkout.cart.index'), ], Response::HTTP_FORBIDDEN); } Cart::collectTotals(); $cart = Cart::getCart(); return [ 'cart' => new CartResource($cart), ]; } /** * Store order */ public function storeOrder() { if (Cart::hasError()) { return new JsonResource([ 'redirect' => true, 'redirect_url' => route('shop.checkout.cart.index'), ]); } Cart::collectTotals(); try { $this->validateOrder(); } catch (\Exception $e) { return response()->json([ 'message' => $e->getMessage(), ], 500); } $cart = Cart::getCart(); if ($redirectUrl = Payment::getRedirectUrl($cart)) { return new JsonResource([ 'redirect' => true, 'redirect_url' => $redirectUrl, ]); } $data = (new OrderResource($cart))->jsonSerialize(); $order = $this->orderRepository->create($data); Cart::deActivateCart(); session()->flash('order_id', $order->id); return new JsonResource([ 'redirect' => true, 'redirect_url' => route('shop.checkout.onepage.success'), ]); } /** * Validate order before creation. * * @return void|\Exception */ public function validateOrder() { $cart = Cart::getCart(); $minimumOrderAmount = core()->getConfigData('sales.order_settings.minimum_order.minimum_order_amount') ?: 0; if ( auth()->guard('customer')->check() && auth()->guard('customer')->user()->is_suspended ) { throw new \Exception(trans('shop::app.checkout.cart.suspended-account-message')); } if ( auth()->guard('customer')->user() && ! auth()->guard('customer')->user()->status ) { throw new \Exception(trans('shop::app.checkout.cart.inactive-account-message')); } if (! Cart::haveMinimumOrderAmount()) { throw new \Exception(trans('shop::app.checkout.cart.minimum-order-message', ['amount' => core()->currency($minimumOrderAmount)])); } if ($cart->haveStockableItems() && ! $cart->shipping_address) { throw new \Exception(trans('shop::app.checkout.onepage.address.check-shipping-address')); } if (! $cart->billing_address) { throw new \Exception(trans('shop::app.checkout.onepage.address.check-billing-address')); } if ( $cart->haveStockableItems() && ! $cart->selected_shipping_rate ) { throw new \Exception(trans('shop::app.checkout.cart.specify-shipping-method')); } if (! $cart->payment) { throw new \Exception(trans('shop::app.checkout.cart.specify-payment-method')); } if ($cart->giftcard_number) { $this->validateGiftCard($cart); } } /** * Validate gift card before order creation. * * @param \Webkul\Checkout\Models\Cart $cart * @return void * @throws \Exception */ protected function validateGiftCard($cart) { $giftCard = \Longyi\Gift\Models\GiftCards::where('giftcard_number', $cart->giftcard_number)->first(); if (!$giftCard) { throw new \Exception('The gift card does not exist or has been deleted. Please select a new gift card'); } if (core()->convertPrice($giftCard->remaining_giftcard_amount) < $cart->giftcard_amount) { throw new \Exception( sprintf( 'The gift card balance is insufficient (current balance: %s, required payment: %s). Please reselect a payment method', core()->formatPrice($giftCard->remaining_giftcard_amount, $cart->cart_currency_code), core()->formatPrice($cart->giftcard_amount, $cart->cart_currency_code) ) ); } if ($giftCard->expirationdate && $giftCard->expirationdate->isPast()) { throw new \Exception('The gift card has expired. Please choose another payment method'); } } }