sarga/packages/Webkul/Shop/src/Http/Controllers/OnepageController.php

179 lines
4.5 KiB
PHP
Raw Normal View History

2018-09-13 11:10:45 +00:00
<?php
namespace Webkul\Shop\Http\Controllers;
2018-09-25 05:22:22 +00:00
2018-10-28 13:23:20 +00:00
use Webkul\Shop\Http\Controllers\Controller;
2018-09-13 11:10:45 +00:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Auth;
2018-09-28 13:28:54 +00:00
use Webkul\Checkout\Facades\Cart;
2018-09-20 10:01:51 +00:00
use Webkul\Shipping\Facades\Shipping;
use Webkul\Payment\Facades\Payment;
2018-09-28 13:28:54 +00:00
use Webkul\Checkout\Http\Requests\CustomerAddressForm;
2018-10-04 06:42:06 +00:00
use Webkul\Sales\Repositories\OrderRepository;
2018-09-13 11:10:45 +00:00
/**
* Chekout controller for the customer and guest for placing order
2018-09-13 11:10:45 +00:00
*
2018-09-19 06:58:15 +00:00
* @author Jitendra Singh <jitendra@webkul.com>
2018-09-13 11:10:45 +00:00
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class OnepageController extends Controller
2018-09-13 11:10:45 +00:00
{
2018-10-04 06:42:06 +00:00
/**
* OrderRepository object
*
* @var array
*/
protected $orderRepository;
2018-09-13 11:10:45 +00:00
/**
2018-09-19 06:58:15 +00:00
* Contains route related configuration
2018-09-13 11:10:45 +00:00
*
2018-09-19 06:58:15 +00:00
* @var array
2018-09-13 11:10:45 +00:00
*/
protected $_config;
2018-09-19 06:58:15 +00:00
/**
* Create a new controller instance.
*
2018-10-04 06:42:06 +00:00
* @param Webkul\Attribute\Repositories\OrderRepository $orderRepository
2018-09-19 06:58:15 +00:00
* @return void
*/
2018-10-04 06:42:06 +00:00
public function __construct(OrderRepository $orderRepository)
2018-09-13 11:10:45 +00:00
{
2018-10-04 06:42:06 +00:00
$this->orderRepository = $orderRepository;
2018-09-13 11:10:45 +00:00
$this->_config = request('_config');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
2018-10-05 11:59:43 +00:00
if(Cart::hasError())
2018-09-26 04:21:14 +00:00
return redirect()->route('shop.checkout.cart.index');
2018-10-05 11:59:43 +00:00
return view($this->_config['view'])->with('cart', Cart::getCart());
2018-09-13 11:10:45 +00:00
}
2018-09-20 10:01:51 +00:00
/**
* Saves customer address.
*
2018-09-28 13:28:54 +00:00
* @param \Webkul\Checkout\Http\Requests\CustomerAddressForm $request
2018-09-20 10:01:51 +00:00
* @return \Illuminate\Http\Response
*/
2018-09-25 05:22:22 +00:00
public function saveAddress(CustomerAddressForm $request)
2018-09-20 10:01:51 +00:00
{
2018-10-05 11:59:43 +00:00
if(Cart::hasError() || !Cart::saveCustomerAddress(request()->all()) || !$rates = Shipping::collectRates())
2018-09-26 04:21:14 +00:00
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
2018-09-20 10:01:51 +00:00
2018-10-12 08:22:06 +00:00
Cart::collectTotals();
2018-09-26 04:21:14 +00:00
return response()->json($rates);
2018-09-20 10:01:51 +00:00
}
/**
* Saves shipping method.
*
* @return \Illuminate\Http\Response
*/
public function saveShipping()
{
2018-09-26 10:19:18 +00:00
$shippingMethod = request()->get('shipping_method');
2018-10-05 11:59:43 +00:00
if(Cart::hasError() || !$shippingMethod || !Cart::saveShippingMethod($shippingMethod))
2018-09-26 10:19:18 +00:00
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
Cart::collectTotals();
return response()->json(Payment::getSupportedPaymentMethods());
2018-09-20 10:01:51 +00:00
}
/**
* Saves payment method.
*
* @return \Illuminate\Http\Response
*/
2018-09-25 05:22:22 +00:00
public function savePayment()
2018-09-20 10:01:51 +00:00
{
2018-09-26 10:19:18 +00:00
$payment = request()->get('payment');
2018-10-05 11:59:43 +00:00
if(Cart::hasError() || !$payment || !Cart::savePaymentMethod($payment))
2018-09-26 10:19:18 +00:00
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
$cart = Cart::getCart();
return response()->json([
'jump_to_section' => 'review',
'html' => view('shop::checkout.onepage.review', compact('cart'))->render()
]);
2018-09-20 10:01:51 +00:00
}
2018-10-04 06:42:06 +00:00
/**
* Saves order.
*
* @return \Illuminate\Http\Response
*/
public function saveOrder()
{
2018-10-05 11:59:43 +00:00
if(Cart::hasError())
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
2018-10-04 06:42:06 +00:00
Cart::collectTotals();
$this->validateOrder();
2018-10-05 06:18:58 +00:00
$order = $this->orderRepository->create(Cart::prepareDataForOrder());
Cart::deActivateCart();
2018-10-04 06:42:06 +00:00
session()->flash('order', $order);
return response()->json([
'success' => true
]);
}
/**
* Order success page
*
* @return \Illuminate\Http\Response
*/
public function success()
{
if(!$order = session('order'))
return redirect()->route('shop.checkout.cart.index');
return view($this->_config['view'], compact('order'));
}
/**
* Validate order before creation
*
* @return mixed
*/
public function validateOrder()
{
2018-10-05 06:18:58 +00:00
$cart = Cart::getCart();
if(!$cart->shipping_address) {
throw new \Exception(trans('Please check shipping address.'));
}
if(!$cart->billing_address) {
throw new \Exception(trans('Please check billing address.'));
}
if(!$cart->selected_shipping_rate) {
throw new \Exception(trans('Please specify shipping method.'));
}
if(!$cart->payment) {
throw new \Exception(trans('Please specify payment method.'));
}
2018-10-04 06:42:06 +00:00
}
2018-09-13 11:10:45 +00:00
}