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

347 lines
9.3 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-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;
2019-10-18 13:13:38 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
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
*
* @author Jitendra Singh <jitendra@webkul.com> @jitendra-webkul
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
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
{
/**
2019-12-21 10:30:22 +00:00
* OrderRepository object
*
2019-12-21 10:30:22 +00:00
* @var array
*/
2019-12-21 10:30:22 +00:00
protected $orderRepository;
2019-10-18 13:13:38 +00:00
/**
* customerRepository instance object
*/
protected $customerRepository;
2018-09-19 06:58:15 +00:00
/**
* Create a new controller instance.
*
2019-12-21 10:30:22 +00:00
* @param \Webkul\Attribute\Repositories\OrderRepository $orderRepository
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
2018-09-19 06:58:15 +00:00
* @return void
*/
public function __construct(
OrderRepository $orderRepository,
2019-10-18 13:13:38 +00:00
CustomerRepository $customerRepository
)
2018-09-13 11:10:45 +00:00
{
$this->orderRepository = $orderRepository;
2019-10-18 13:13:38 +00:00
$this->customerRepository = $customerRepository;
2020-02-06 11:48:51 +00:00
parent::__construct();
2018-09-13 11:10:45 +00:00
}
/**
* Display a listing of the resource.
*
2019-10-15 13:35:46 +00:00
* @return \Illuminate\View\View
2018-09-13 11:10:45 +00:00
*/
public function index()
{
2020-01-17 13:42:47 +00:00
if (! auth()->guard('customer')->check() && ! core()->getConfigData('catalog.products.guest-checkout.allow-guest-checkout')) {
return redirect()->route('customer.session.index');
}
2019-01-15 11:54:41 +00:00
if (Cart::hasError())
2018-09-26 04:21:14 +00:00
return redirect()->route('shop.checkout.cart.index');
2019-06-28 14:18:52 +00:00
$cart = Cart::getCart();
2020-01-17 13:42:47 +00:00
if (! auth()->guard('customer')->check() && $cart->hasDownloadableItems()) {
return redirect()->route('customer.session.index');
}
if (! auth()->guard('customer')->check() && ! $cart->hasGuestCheckoutItems()) {
2019-06-28 14:18:52 +00:00
return redirect()->route('customer.session.index');
2020-01-17 13:42:47 +00:00
}
2019-06-28 14:18:52 +00:00
Cart::collectTotals();
2019-06-28 14:18:52 +00:00
return view($this->_config['view'], compact('cart'));
2018-09-13 11:10:45 +00:00
}
2019-05-30 13:02:01 +00:00
/**
* Return order short summary
*
2019-10-15 13:35:46 +00:00
* @return \Illuminate\View\View
2019-05-30 13:02:01 +00:00
*/
public function summary()
{
$cart = Cart::getCart();
return response()->json([
'html' => view('shop::checkout.total.summary', compact('cart'))->render()
]);
}
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
{
2019-03-12 06:29:00 +00:00
$data = request()->all();
2020-02-20 07:43:02 +00:00
if (! auth()->guard('customer')->check() && ! Cart::getCart()->hasGuestCheckoutItems()) {
2020-02-20 07:00:00 +00:00
return response()->json(['redirect_url' => route('customer.session.index')], 403);
}
2019-03-12 06:29:00 +00:00
$data['billing']['address1'] = implode(PHP_EOL, array_filter($data['billing']['address1']));
$data['shipping']['address1'] = implode(PHP_EOL, array_filter($data['shipping']['address1']));
2019-06-28 14:18:52 +00:00
if (Cart::hasError() || ! Cart::saveCustomerAddress($data)) {
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
} else {
$cart = Cart::getCart();
Cart::collectTotals();
2019-06-28 14:18:52 +00:00
if ($cart->haveStockableItems()) {
2020-02-20 07:05:51 +00:00
if (! $rates = Shipping::collectRates()) {
2019-06-28 14:18:52 +00:00
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
2020-02-20 07:05:51 +00:00
} else {
2019-06-28 14:18:52 +00:00
return response()->json($rates);
2020-02-20 07:05:51 +00:00
}
2019-06-28 14:18:52 +00:00
} else {
return response()->json(Payment::getSupportedPaymentMethods());
}
}
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');
2020-02-20 07:05:51 +00:00
if (Cart::hasError() || !$shippingMethod || !Cart::saveShippingMethod($shippingMethod)) {
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
2020-02-20 07:05:51 +00:00
}
2018-09-26 10:19:18 +00:00
Cart::collectTotals();
return response()->json(Payment::getSupportedPaymentMethods());
2018-09-20 10:01:51 +00:00
}
/**
* Saves payment method.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\Http\JsonResponse
2018-09-20 10:01:51 +00:00
*/
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');
2020-02-20 07:05:51 +00:00
if (Cart::hasError() || ! $payment || ! Cart::savePaymentMethod($payment)) {
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
2020-02-20 07:05:51 +00:00
}
Cart::collectTotals();
2019-06-28 14:18:52 +00:00
2018-09-26 10:19:18 +00:00
$cart = Cart::getCart();
return response()->json([
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
'jump_to_section' => 'review',
2020-02-20 07:05:51 +00:00
'html' => view('shop::checkout.onepage.review', compact('cart'))->render()
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
]);
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()
{
2020-02-20 07:05:51 +00:00
if (Cart::hasError()) {
2018-10-05 11:59:43 +00:00
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
2020-02-20 07:05:51 +00:00
}
2018-10-05 11:59:43 +00:00
2018-10-04 06:42:06 +00:00
Cart::collectTotals();
$this->validateOrder();
2018-11-16 10:11:08 +00:00
$cart = Cart::getCart();
2019-01-15 11:54:41 +00:00
if ($redirectUrl = Payment::getRedirectUrl($cart)) {
2018-11-16 10:11:08 +00:00
return response()->json([
2020-02-20 07:05:51 +00:00
'success' => true,
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
'redirect_url' => $redirectUrl
]);
2018-11-16 10:11:08 +00:00
}
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([
Cart rule inside promotions, various improvements and fixes. (#964) * Some syntax issue fixes * Adding discount things inside promotions in admin * routes updated for creating discount rules * fixed customer profile redirect parameter issue * added cart and catalog rule routes to admin section * migrations for discount almost to be finished, final revision left * migrations for discounts complete * catalog rule form underway * catalog rule * Made some dynamic form fields in catalog rule create form * made some changes for custom validations used in ProductForm request class * working on prepopulating the values fields in conditions * need another revisit on discount rules tables and then prepare for actions * removed discounts table * catalog rule form changed to muliti attribute set for single condition * new icons added for promotion and customer note. new action for customer added to take notes on the customers * catalog and cart rule designs stable, now moving towards validations on client side and backend * catalog rules migrations added * catalog rule models added with contracts and proxies * fixed customer group bug in customer registration controller * fixed customer registration bug due to last fix * fixed product card image not found issue * catalog rule translations added * Added migrations for cart rules and catalog rule products their contracts and remaining necessary files related to them. * making cart attributes for cart rules * Added more fields for cart rules * working on conditions for cart rule form * minor changes in migrations related to price rules * currency and locale switcher now only available for a channel on storefront when more than on locale and currency are assigned to that channel * part of conditions on the cart rule form added * daily sync * cart rate migrations updated * Added select and multi select attributes options fetching with ajax on catalog rule form * changed some migrations and data being populated at runtime inside catalog rule form * catalog rule create complete, migrations changes, translations added * catalog rule edit form complete * catalog rule form complete, now moving towards catalog rule products * added delete functionality for catalog rules * added cart rule preferences for coupon codes * cart rule submission problem due to repository issue * Cart rule form and migrations complete * Models and Repositories updated for cart rule usage * base sync with master * designing process to get suitable discount rules * cart rule form completed * added helpers in cart and discount to apply rules on cart at checkout or not * cart rule coupon implementation in progress for discount coupon in shop checkout pages * cart rules working * added coupon box on checkout screen * removed the conditions empty bug * Nearing to completion of coupon based rules * made some changes in cart rule coupon application on checkout related to new designs * some bug fixes * calculation for automatic cart rules complete * non couponable cart rules implemented, now moving on to binding them on frontend * some conditions improvements in couponable cart rules * some bug fixes * removed some bugs from summary blade for cart rule * added the table for cart rule cart for managing rules with cart and removed various bug fixes * some bug fixes * Removed bugs and added coupon based cart rule removal functionality * some bug fixes * cart rule labels refactoring in midway * Cart rule labels bug fixed * removed margin classes from shop to UI * Refactoring cart rule on front end * added checks * Discount rule implemented. * cart rule bug fixes * provision to remove the couponable and non couponable rule had been added * Cart rule frontend work done * altered some frontend variables on onepage checkout * Altered cart rules to some extent
2019-06-10 07:49:05 +00:00
'success' => true,
]);
2018-10-04 06:42:06 +00:00
}
/**
* Order success page
*
* @return \Illuminate\Http\Response
*/
public function success()
{
2020-02-20 07:05:51 +00:00
if (! $order = session('order')) {
2018-10-04 06:42:06 +00:00
return redirect()->route('shop.checkout.cart.index');
2020-02-20 07:05:51 +00:00
}
2018-10-04 06:42:06 +00:00
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();
2020-02-20 07:05:51 +00:00
if ($cart->haveStockableItems() && ! $cart->shipping_address) {
2018-10-05 06:18:58 +00:00
throw new \Exception(trans('Please check shipping address.'));
2020-02-20 07:05:51 +00:00
}
2018-10-05 06:18:58 +00:00
2020-02-20 07:05:51 +00:00
if (! $cart->billing_address) {
2018-10-05 06:18:58 +00:00
throw new \Exception(trans('Please check billing address.'));
2020-02-20 07:05:51 +00:00
}
2018-10-05 06:18:58 +00:00
2020-02-20 07:05:51 +00:00
if ($cart->haveStockableItems() && ! $cart->selected_shipping_rate) {
2018-10-05 06:18:58 +00:00
throw new \Exception(trans('Please specify shipping method.'));
2020-02-20 07:05:51 +00:00
}
2018-10-05 06:18:58 +00:00
2020-02-20 07:05:51 +00:00
if (! $cart->payment) {
2018-10-05 06:18:58 +00:00
throw new \Exception(trans('Please specify payment method.'));
2020-02-20 07:05:51 +00:00
}
2019-12-21 10:30:22 +00:00
}
/**
* Check Customer is exist or not
*
* @return Response
*/
public function checkExistCustomer()
{
$customer = $this->customerRepository->findOneWhere([
'email' => request()->email
]);
2020-02-20 07:05:51 +00:00
if (! is_null($customer)) {
2019-12-21 10:30:22 +00:00
return 'true';
2020-02-20 07:05:51 +00:00
}
2019-12-21 10:30:22 +00:00
return 'false';
}
/**
* Login for checkout
*
* @return Response
*/
public function loginForCheckout()
{
$this->validate(request(), [
'email' => 'required|email'
]);
2020-02-20 07:05:51 +00:00
if (! auth()->guard('customer')->attempt(request(['email', 'password']))) {
2019-12-21 10:30:22 +00:00
return response()->json(['error' => trans('shop::app.customer.login-form.invalid-creds')]);
2020-02-20 07:05:51 +00:00
}
2019-12-21 10:30:22 +00:00
Cart::mergeCart();
return response()->json(['success' => 'Login successfully']);
2018-10-04 06:42:06 +00:00
}
/**
* To apply couponable rule requested
*
* @return JSON
*/
public function applyCoupon()
{
$this->validate(request(), [
'code' => 'string|required'
]);
$code = request()->input('code');
$result = $this->coupon->apply($code);
if ($result) {
Cart::collectTotals();
return response()->json([
'success' => true,
'message' => trans('shop::app.checkout.total.coupon-applied'),
2020-02-20 07:05:51 +00:00
'result' => $result
], 200);
} else {
return response()->json([
'success' => false,
'message' => trans('shop::app.checkout.total.cannot-apply-coupon'),
2020-02-20 07:05:51 +00:00
'result' => null
], 422);
}
return $result;
}
/**
* Initiates the removal of couponable cart rule
*
* @return Void
*/
public function removeCoupon()
{
$result = $this->coupon->remove();
if ($result) {
Cart::collectTotals();
return response()->json([
'success' => true,
'message' => trans('admin::app.promotion.status.coupon-removed'),
2020-02-20 07:05:51 +00:00
'data' => [
'grand_total' => core()->currency(Cart::getCart()->grand_total)
]
], 200);
} else {
return response()->json([
'success' => false,
'message' => trans('admin::app.promotion.status.coupon-remove-failed'),
2020-02-20 07:05:51 +00:00
'data' => null
], 422);
}
}
2018-09-13 11:10:45 +00:00
}