| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- namespace Webkul\Shop\Http\Controllers\API;
- use Illuminate\Http\Resources\Json\JsonResource;
- use Illuminate\Http\Response;
- use Webkul\Checkout\Facades\Cart;
- use Webkul\Customer\Repositories\CustomerRepository;
- use Webkul\Payment\Facades\Payment;
- use Webkul\Sales\Repositories\OrderRepository;
- use Webkul\Sales\Transformers\OrderResource;
- use Webkul\Shipping\Facades\Shipping;
- use Webkul\Shop\Http\Requests\CartAddressRequest;
- use Webkul\Shop\Http\Resources\CartResource;
- class OnepageController extends APIController
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct(
- protected OrderRepository $orderRepository,
- protected CustomerRepository $customerRepository
- ) {}
- /**
- * Return cart summary.
- */
- public function summary(): JsonResource
- {
- $cart = Cart::getCart();
- return new CartResource($cart);
- }
- /**
- * Store address.
- */
- public function storeAddress(CartAddressRequest $cartAddressRequest): JsonResource
- {
- $params = $cartAddressRequest->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');
- }
- }
- }
|