Viewing file: CartController.php (65.17 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php namespace App\Http\Controllers\Frontend; use App\Http\Controllers\Controller; use App\Models\Setting; use GuzzleHttp\Client; use Illuminate\Http\Request; use App\User; use App\Models\Product; use App\Models\Coupon; use App\Models\Country; use App\Models\Order; use App\Models\OrderProduct; use App\Models\OrderHistory; use App\Models\Shipping; use App\Models\StoreProductOption; use Illuminate\Support\Facades\Log; use Validator; use File; use DB; use Auth; use Carbon; use Stripe\Exception\CardException; use Stripe\StripeClient; use Stripe\Checkout\Session; use Stripe\Exception\ApiErrorException; use Srmklive\PayPal\Services\PayPal as PayPalClient; use Razorpay\Api\Api; use Mollie\Laravel\Facades\Mollie; use Illuminate\Support\Str;
class CartController extends Controller { private $getUser; private $stripe; public function __construct() { $this->middleware(function ($request, $next) { $this->getUser = Auth::guard('customer')->user(); return $next($request); }); $this->stripe = new StripeClient(env('STRIPE_SECRET')); }
//addToCart api public function addToCart(Request $request) { try { $getProduct = Product::with('special:product_id,price,start_date,end_date')->findOrFail($request->product_id); $productBasePrice = $getProduct->price;
//check product exists or not if($getProduct) { //check this product already in cart
//product special $price = $getProduct->price; $special = 0; if($getProduct->special) { $endDate = Carbon\Carbon::createFromFormat('m/d/Y',$getProduct->special->end_date); $startDate = Carbon\Carbon::createFromFormat('m/d/Y',$getProduct->special->start_date); $todayDate = Carbon\Carbon::createFromFormat('m/d/Y', date('m/d/y')); if($startDate->gte($todayDate) && $todayDate->lte($endDate)) { $special = number_format($getProduct->special->price,2); } }
if($special > 0 ){ $productBasePrice = $special; }
//check product has options $findProductOption = StoreProductOption::where('product_id',$request->product_id)->first();
//find option send or not if($findProductOption != null && $request->options == null) { $productData['product'] = Product::select('id','manufacturer_id','image','category_id', 'model','price', 'location', 'quantity','sort_order','status','length','width','height') ->with('productDescription:name,id,product_id,description','category:name,category_id','productManufacturer:id,image') ->where('id',$request->product_id)->first();
$productOptionsData = StoreProductOption::where('product_id',$request->product_id) ->join('product_options','product_options.id','=','store_product_option.option_id') ->join('product_option_description','product_option_description.option_id','=','store_product_option.option_id') ->select('store_product_option.*','product_option_description.name','product_options.type') ->where('product_option_description.language_id',session()->get('currentLanguage')) ->get();
$productOptions = []; $optionName = ''; foreach ($productOptionsData as $key => $value) { $productOptions[$value->type.'-'.$value->name][] = $value; }
$productData['productOptions'] = $productOptions;
return ['status' => 3,'productData' => $productData,'price' => $price,'special' => $special]; } else {
if($request->price > 0) { $productBasePrice =$request->price; }
//get getCustomerCart $getCustomerCart = DB::table('cart')->where('session_id',session()->getId())->where('product_id',$request->product_id) ->where('option',$request->options) ->first();
//if already product in cart then update if($getCustomerCart) {
$qty = $getProduct->quantity + $getCustomerCart->quantity;
// check stock if($qty >= $request->quantity) { $storeOption = null; if($findProductOption) { $storeOption = $request->options; }
//update DB::table('cart')->where('cart_id',$getCustomerCart->cart_id) ->update(['quantity' => $getCustomerCart->quantity + $request->quantity,'option' =>$storeOption,'base_price' => $productBasePrice]);
//increment product quantity Product::where('id',$getCustomerCart->product_id)->update(['quantity' => $qty]);
//decrement product quantity $minusQty = $getCustomerCart->quantity + $request->quantity; Product::where('id',$request->product_id) ->update(['quantity' => $qty - $minusQty]);
$cartCount = DB::table('cart')->where('session_id',session()->getId())->sum('quantity');
return ['status'=> 1,'message'=>'Product Added To Cart!','cartCount' => $cartCount]; } else { return ['status'=> 0,'message'=>'Only ' .$getProduct->quantity. ' products in stock']; } } //insert product to cart else {
$storeOption = null; if($findProductOption) { $storeOption = $request->options; }
// check stock if($getProduct->quantity >= $request->quantity) { DB::table('cart')->insert(['product_id' => $request->product_id,'customer_id' => $this->getUser ? $this->getUser->id : 0,'quantity' => $request->quantity,'date_added'=> date('Y-m-d H:i:s'), 'option' =>$storeOption,'base_price' =>$productBasePrice,'session_id' => session()->getId() ]); //decrement product quantity Product::where('id',$request->product_id)->update(['quantity' => $getProduct->quantity - $request->quantity]); $cartCount = DB::table('cart')->where('session_id',session()->getId())->sum('quantity'); return ['status'=> 1,'message'=>'Product Added To Cart!','cartCount' => $cartCount]; } else { return ['status'=> 0,'message'=>'Only ' .$getProduct->quantity. ' products in stock']; } } }
} else { return ['status'=> 0,'message'=>'Product not found!']; }
} catch (\Exception $e) { return ['status'=> 0,'message'=>'Error']; } }
//get cart public function getCart() {
try {
// get cart products $getCart = DB::table('cart')->where('session_id',session()->getId())->get();
if(count($getCart) > 0 ) { $getProducts = Product::select('product.price','product.id','product.model','product.image','cart.quantity','cart.cart_id','cart.base_price', 'product_description.name','tax_rate.rate','tax_rate.type','tax_rate.name as taxName','tax_rate.status as taxStatus','cart.option') ->join('cart','cart.product_id','=','product.id') ->join('product_description','product_description.product_id','=','product.id') ->leftjoin('tax_rate','tax_rate.id','=','product.tax_rate_id') ->orderBy('cart.date_added','DESC') ->where('cart.session_id',session()->getId()) ->where('product_description.language_id',session()->get('currentLanguage')) ->get();
$cartData = []; $cartTotal = 0.00; $subTotal = []; $taxRates = []; $grandTotal = 0.00; $discount = null; $taxAMT = 0.00; $basePrice = 0.00; $optionSum =0 ; $productOpt = [];
//build cart with with sub total and total foreach ($getProducts as $key => $value) {
$finalPrice = $value->base_price; $basePrice = $value->price; $specialPrice = 0; $optionSum = 0; $productOpt = null; //check options
$decodeOptions = json_decode($value->option); if($decodeOptions != null) {
$option_ID = []; if(isset($decodeOptions->optionColorSelected)) { $option_ID[] = $decodeOptions->optionColorSelected; } if(isset($decodeOptions->optionCheckboxSelected)) { $option_ID[] = $decodeOptions->optionCheckboxSelected; } if(isset($decodeOptions->optionSelectSelected)) { $option_ID[] = $decodeOptions->optionSelectSelected; } if(isset($decodeOptions->optionRadioSelected)) { $option_ID[] = $decodeOptions->optionRadioSelected; }
//get Optoins Price $productOpt = StoreProductOption::whereIn('product_option_id',$option_ID) ->select('store_product_option.label','store_product_option.price','product_option_description.name') ->join('product_options','product_options.id','=','store_product_option.option_id') ->join('product_option_description','product_option_description.option_id','=','product_options.id') ->where('product_option_description.language_id','=',session()->get('currentLanguage')) ->get() ->toArray();
$optionSum = StoreProductOption::whereIn('product_option_id',$option_ID)->sum('price'); if($optionSum > 0) { $finalPrice += (float) $optionSum; } }
$cartTotal += (float) $finalPrice * $value->quantity; $grandTotal += (float) $finalPrice * $value->quantity; $finalPrice = $value->quantity * (float) $finalPrice;
$cartData[] = [ 'cart_id' => $value->cart_id, 'name' => $value->name, 'price' => number_format($value->base_price,2), 'quantity' => $value->quantity, 'image' => $value->image, 'pid' => $value->id, 'totalPrice' => $finalPrice, 'special' => number_format($specialPrice,2), 'taxStatus' => $value->taxStatus, 'taxType' => $value->type, 'rate' => $value->rate, 'taxName' => $value->taxName, 'model' => $value->model, 'options' => $productOpt ];
//check tax applied or not if($value->taxStatus && $value->taxStatus == 1) { if($value->type == 1) { $taxAmount = $finalPrice / 100 * $value->rate; } else { $taxAmount = $finalPrice + $value->rate; } $taxAMT += $taxAmount; }
}
$subTotal[] = ['subTotal' =>$cartTotal]; $taxRates = ['name' => 'Taxes','taxAmount' => $taxAMT ];
//you can use seprate tax arr // if(count($taxRates) > 0) { // // $taxRates = $this->mergeTax($taxRates); // // foreach ($taxRates as $key => $value) { // $grandTotal += $value['taxAmount']; // } // }
$grandTotal += $taxAMT;
//store cart in session session()->put('cart'.session()->getId(),['cartData' => $cartData,'subTotal' =>number_format($cartTotal,2) ,'discount' => $discount,'taxes' => $taxRates,'grandTotal' => $grandTotal,'products' => $getProducts]); session()->save(); $counponHistory = DB::table('coupon_history') ->where('is_valid',1) ->where('session_id',session()->getId()) ->where('order_done',0)->first(); $discountAMT = 0;
if($counponHistory) {
if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $counponHistory->amount; $grandTotal -= $discountAMT; } }
$grandTotal = number_format($grandTotal,2);
$this->buildSeo('Shopping Cart',['Smabmart','Cart'],url()->current(),'');
return view('frontend.cart.shoppingcart',['status'=> 1,'cartData' => $cartData,'subTotal' =>number_format($cartTotal,2) ,'discount' => $discount,'taxes' => $taxRates,'grandTotal' => $grandTotal,'couponData' => $counponHistory,'discountAMT' => $discountAMT]); } else { return view('frontend.cart.shoppingcart',['status'=> 0]); } } catch (\Exception $e) { return redirect()->back()->with('commonError','Error'); } }
//update cart public function updateCart(Request $request) { try { //get cart $getCart = DB::table('cart')->where('cart_id',$request->cart_id)->first(); if($getCart) {
//get product $getProduct = Product::findOrFail($getCart->product_id);
$qty = $getProduct->quantity + $getCart->quantity;
// check stock if($qty >= $request->quantity) {
//update DB::table('cart')->where('cart_id',$getCart->cart_id)->update(['quantity' => $request->quantity]);
//increment product quantity Product::where('id',$getCart->product_id)->update(['quantity' => $qty]);
//decrement product quantity $minusQty = $getCart->quantity + $request->quantity;
Product::where('id',$getCart->product_id) ->update(['quantity' => $qty - $request->quantity]);
//build cart return $this->buildCart($request->all(),'update');
} else { return ['status'=> 0,'message'=>'Only ' .$getProduct->quantity. ' products in stock']; } } else { return ['status'=> 0,'message'=>'Error']; } } catch (\Exception $e) { return ['status'=> 0,'message'=>'Error']; } }
//update cart public function deleteCart(Request $request) {
//get cart $getCart = DB::table('cart')->where('cart_id',$request->cart_id)->first(); if($getCart) { //get product $getProduct = Product::findOrFail($getCart->product_id);
$qty = $getProduct->quantity + $getCart->quantity;
//update DB::table('cart')->where('cart_id',$getCart->cart_id)->delete();
//increment product quantity Product::where('id',$getCart->product_id)->update(['quantity' => $qty]);
//get cart after deleted all items $getCart = DB::table('cart')->where('cart_id',$request->cart_id)->first(); if(!$getCart) { DB::table('coupon_history')->where('session_id',session()->getId())->where('order_done',0)->update(['order_done' => 1]); }
//build cart return $this->buildCart($request->all(),'delete'); } else { return ['status'=> 0,'message'=>'Error']; } }
//build cart public function buildCart($postData,$cartType) {
$sessionCartData = session()->get('cart'.session()->getId()); $getProducts = $sessionCartData['products']; $rmsg= ''; $cartData = []; $cartTotal = 0.00; $subTotal = []; $taxRates = []; $grandTotal = 0.00; $discount = null; $taxAMT = 0.00; $basePrice =0.00; $discountAMT = 0.00; $discountType = 1; $discountPer= 0;
//build cart with with sub total and total foreach ($getProducts as $key => $value) {
$optionSum = 0;
/****************************************************** **********if cart build for update********************** *******************************************************/ if($cartType == 'update') { $quantity = $value->quantity; if($value->cart_id == $postData['cart_id'] ) { $quantity = $postData['quantity']; }
$finalPrice = $value->base_price;
$finalPrice = (int) $quantity * (float) $finalPrice;
$finalPrice = $value->base_price; $basePrice = $value->price; $decodeOptions = json_decode($value->option);
if($decodeOptions != null) { $option_ID = []; if(isset($decodeOptions->optionColorSelected)) { $option_ID[] = $decodeOptions->optionColorSelected; } if(isset($decodeOptions->optionCheckboxSelected)) { $option_ID[] = $decodeOptions->optionCheckboxSelected; } if(isset($decodeOptions->optionSelectSelected)) { $option_ID[] = $decodeOptions->optionSelectSelected; } if(isset($decodeOptions->optionRadioSelected)) { $option_ID[] = $decodeOptions->optionRadioSelected; }
//get Optoins Price $productOpt = StoreProductOption::whereIn('product_option_id',$option_ID) ->select('store_product_option.label','store_product_option.price','product_option_description.name') ->join('product_options','product_options.id','=','store_product_option.option_id') ->join('product_option_description','product_option_description.option_id','=','product_options.id') ->where('product_option_description.language_id','=',session()->get('currentLanguage')) ->get() ->toArray(); $optionSum = StoreProductOption::whereIn('product_option_id',$option_ID)->sum('price'); if($optionSum > 0) { $finalPrice += (float) $optionSum ; } }
$cartTotal += (float) $finalPrice * (int) $quantity; $grandTotal += (float) $finalPrice * (int) $quantity;
// $specialPrice = 0; // if($value->specialPrice) { // if($value->start_date <= date('Y-m-d') && $value->end_date >= date('Y-m-d')) { // $specialPrice = $value->specialPrice; // $finalPrice = $value->specialPrice; // $basePrice = $value->specialPrice; // } // }
// //check options // $decodeOptions = json_decode($value->option); // // if($decodeOptions != null) { // $optionIDArr = [$decodeOptions->optionColorSelected,$decodeOptions->optionSizeSelected,$decodeOptions->optionSelectSelected]; // // //get Optoins Price // $optionSum = StoreProductOption::whereIn('product_option_id',$optionIDArr)->sum('price'); // // if($optionSum > 0) { // $finalPrice += (float) $optionSum; // } // }
$cartData[] = [ 'cart_id' => $value->cart_id, 'name' => $value->name, 'price' => number_format($value->price,2), 'quantity' => $quantity, 'image' => $value->image, 'pid' => $value->id, 'totalPrice' => $finalPrice, 'special' => number_format(0,2), 'taxStatus' => $value->taxStatus, 'taxType' => $value->type, 'rate' => $value->rate, 'taxName' => $value->taxName ];
//check tax applied or not if($value->taxStatus && $value->taxStatus == 1) { if($value->type == 1) { $taxAmount = $finalPrice / 100 * $value->rate; } else { $taxAmount = $finalPrice + $value->rate; } $taxAMT += $taxAmount; // $taxRates[] = ['name' => $value->taxName,'taxAmount' => $taxAmount ]; } $rmsg= 'Cart successfully updated!'; }
/****************************************************** **********if cart build for delete********************** *******************************************************/ else if($cartType == 'delete') { $rmsg= 'Cart Item successfully deleted!'; $quantity = $value->quantity;
if($value->cart_id != $postData['cart_id'] ) { $finalPrice = $value->base_price; $basePrice = $value->price;
//check options // $decodeOptions = json_decode($value->option); // // if($decodeOptions != null) { // $optionIDArr = [$decodeOptions->optionColorSelected,$decodeOptions->optionSizeSelected,$decodeOptions->optionSelectSelected]; // // //get Optoins Price // $optionSum = StoreProductOption::whereIn('product_option_id',$optionIDArr)->sum('price'); // // if($optionSum > 0) { // $finalPrice += (float) $optionSum; // } // }
// $specialPrice = 0; // if($value->specialPrice) { // if($value->start_date <= date('Y-m-d') && $value->end_date >= date('Y-m-d')) { // $specialPrice = $value->specialPrice; // $finalPrice = $value->specialPrice; // $basePrice = $value->specialPrice; // // } // }
$cartTotal += (float) $finalPrice * $quantity; $grandTotal += (float) $finalPrice * $quantity;
$finalPrice = (int) $quantity * (float) $finalPrice;
$cartData[] =[ 'cart_id' => $value->cart_id, 'name' => $value->name, 'price' => number_format($value->price,2), 'quantity' => $quantity, 'image' => $value->image, 'pid' => $value->id, 'totalPrice' => $finalPrice, 'special' => number_format(0,2), 'taxStatus' => $value->taxStatus, 'taxType' => $value->type, 'rate' => $value->rate, 'taxName' => $value->taxName ];
//check tax applied or not if($value->taxStatus && $value->taxStatus == 1) { if($value->type == 1) { $taxAmount = $finalPrice / 100 * $value->rate; } else { $taxAmount = $finalPrice + $value->rate; } $taxAMT += $taxAmount; }
} } }
$subTotal[] = ['subTotal' =>$cartTotal]; $taxRates = ['name' => 'Taxes','taxAmount' => $taxAMT ]; $grandTotal += $taxAMT;
//Again calculate discount $findDiscount = DB::table('coupon_history')->where('session_id',session()->getId())->where('is_valid',1)->where('order_done',0)->first(); if($findDiscount) { $discountType = $findDiscount->coupon_type;
//calculate discount if($findDiscount->coupon_type == 1) { $discountPer = number_format($findDiscount->amount,2); $discountAMT = $grandTotal / 100 * $findDiscount->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $findDiscount->amount; $grandTotal -= $discountAMT; } $discount = ['name' => $findDiscount->coupon_code, 'discountAmt' => number_format( $findDiscount->amount,2), 'type' => $findDiscount->coupon_type, 'discount' =>$findDiscount->amount ]; }
$grandTotal = number_format($grandTotal,2);
$newSessionData = [ 'cartData' => $cartData,'subTotal' =>number_format($cartTotal,2), 'discount' => $discount, 'taxes' => $sessionCartData['taxes'], 'grandTotal' => $grandTotal, 'products' => $sessionCartData['products'] ];
//store cart in session session()->put('cart'.session()->getId(),$newSessionData); session()->save(); $cartCount = DB::table('cart')->where('session_id',session()->getId())->sum('quantity'); return ['status'=> 1,'message' => $rmsg,'discountPer' => $discountPer,'cartData' => $cartData,'cartCount'=>$cartCount,'subTotal' =>number_format($cartTotal,2) ,'discount' => number_format($discountAMT,2),'taxes' => $taxRates,'grandTotal' => $grandTotal,'discountType' => $discountType]; }
//apply coupon public function applyCoupon(Request $request) { try { //check coupon exists $getCoupon = DB::table('coupon')->where('code',$request->couponCode)->first(); $discount = null; $taxRates=[]; $discountAmt = 0.00;
$sessionCartData = session()->get('cart'.session()->getId()); if(count($sessionCartData) > 0) { //find coupon $counponHistory = DB::table('coupon_history')->where('session_id',session()->getId())->where('order_done',0)->first(); $grandTotal = str_replace(",","", $sessionCartData['grandTotal']); if($getCoupon && date('Y-m-d',strtotime($getCoupon->start_date)) <= date('Y-m-d') && date('Y-m-d',strtotime($getCoupon->end_date)) >= date('Y-m-d')) { //calculate discount $discountTxt = ''; if($getCoupon->type == 1) { $discountAmt = $grandTotal / 100 * $getCoupon->discount; $discountTxt = number_format($getCoupon->discount,2).'%'; } else { $discountAmt = $getCoupon->discount; $discountTxt = number_format($getCoupon->discount,2); }
if($sessionCartData['discount'] ) { if( $sessionCartData['discount']['name'] != $request->couponCode.' ('.$discountTxt.')') { $grandTotal = $grandTotal - $discountAmt; $discount = ['name' => 'Discount ('.$discountTxt.')','discountAmt' => number_format($discountAmt,2) ]; } else { $grandTotal = str_replace(',','',$sessionCartData['grandTotal']); $discount = $sessionCartData['discount']; } } else { $grandTotal = $grandTotal - $discountAmt; $discount = ['name' => 'Discount ('.$discountTxt.')','discountAmt' => number_format($discountAmt,2),'type' => $getCoupon->type,'discount' => $getCoupon->discount ]; } if($counponHistory) { //update DB::table('coupon_history')->where('order_done',0)->where('session_id',session()->getId())->update(['coupon_id' => $getCoupon->id, 'coupon_type' => $getCoupon->type,'amount' => $getCoupon->type == 1 ? $getCoupon->discount : $discountAmt,'date_added' => date('Y-m-d'),'is_valid' => true,'coupon_code' => $getCoupon->code]); } else { //insert DB::table('coupon_history')->insert(['coupon_id' => $getCoupon->id,'coupon_type' =>$getCoupon->type ,'session_id' => session()->getId(),'customer_id' =>$this->getUser ? $this->getUser->id : 0,'amount' => $getCoupon->type == 1 ? $getCoupon->discount : $discountAmt,'date_added' => date('Y-m-d'),'is_valid' => true,'coupon_code' => $getCoupon->code]); }
return ['status'=> 1, 'message' =>"Coupon successfully applied!",'discount' => $discount,'discountType' => $getCoupon->type,'discountPer'=>number_format($getCoupon->discount,2),'grandTotal' => number_format($grandTotal,2)]; } else { if($counponHistory) { // if($counponHistory->is_valid){ // $grandTotal += $counponHistory->amount; // } DB::table('coupon_history')->where('order_done',0)->where('session_id',session()->getId())->update(['is_valid' => false]); } return ['status'=> 0,'message'=>'Coupon expired/Invalid!','grandTotal' => number_format($grandTotal,2)]; } } else { if($counponHistory) { // if($counponHistory->is_valid){ // $grandTotal += $counponHistory->amount; // } DB::table('coupon_history')->where('order_done',0)->where('session_id',session()->getId())->update(['is_valid' => false]); } return ['status'=> 2,'message'=>'Invalid coupon code']; }
} catch (\Exception $e) { return ['status'=> 0,'message'=>'Error']; }
}
//get checkout page public function getCheckout(){ try {
$data = []; $data['shippingMethods'] = Shipping::where('status',1)->get(); $data['addresses'] = [];
$addressQuery = DB::table('customer_address') ->join('country','country.id','=','customer_address.country_id') ->select('customer_address.*','country.name as country');
if($this->getUser) { $addressQuery = $addressQuery->where('customer_id',$this->getUser->id); } else { $addressQuery = $addressQuery->where('session_id',session()->getId()); }
$data['addresses'] = $addressQuery->get();
$data['countries'] = Country::where('status','1')->select('id','name','iso_code_3','postcode_required','status')->orderBy('name','ASC')->get(); $sessionCartData = session()->get('cart'.session()->getId()); $data['cartData'] = $sessionCartData;
//check coupon $counponHistory = DB::table('coupon_history') ->where('is_valid',1) ->where('session_id',session()->getId()) ->where('order_done',0) ->first();
$grandTotal = $sessionCartData['grandTotal']; $discountAMT = 0; if($counponHistory) { if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; } else { $discountAMT = $counponHistory->amount; } } $data['discount'] = number_format($discountAMT,2);
$getCart = DB::table('cart')->where('session_id',session()->getId())->get(); if(count($getCart) > 0 ) { $data['orderProducts'] = Product::select('product.price','product.id','product.model','product.image','cart.quantity','cart.cart_id','cart.base_price', 'product_description.name','tax_rate.rate','tax_rate.type','tax_rate.name as taxName','tax_rate.status as taxStatus','cart.option') ->join('cart','cart.product_id','=','product.id') ->join('product_description','product_description.product_id','=','product.id') ->leftjoin('tax_rate','tax_rate.id','=','product.tax_rate_id') ->orderBy('cart.date_added','DESC') ->where('cart.session_id',session()->getId()) ->where('product_description.language_id',session()->get('currentLanguage')) ->get();
}
$this->buildSeo('Checkout',['Smabmart','Checkout'],url()->current(),'');
return view('frontend.cart.checkout',compact('data')); } catch (\Exception $e) { return redirect()->back()->with('commonError','Error'); } }
//when select shipping method public function selectShipping(Request $request){ try { $findShipping = Shipping::findOrFail($request->id);
//get cart data $sessionCartData = session()->get('cart'.session()->getId()); if($sessionCartData ) {
if(array_key_exists('shipping',$sessionCartData)) { $grandTotal = str_replace(",","", $sessionCartData['grandTotal']); $grandTotal = $grandTotal - $sessionCartData['shipping']['charges']; $grandTotal = $grandTotal + $findShipping->shipping_charge; } else{ $grandTotal = str_replace(",","", $sessionCartData['grandTotal']); $grandTotal = $grandTotal + $findShipping->shipping_charge; }
$shipping = [ 'name' =>$findShipping->name, 'charges' => $findShipping->shipping_charge, 'id' => $findShipping->id ];
$newSessionData = [ 'cartData' => $sessionCartData['cartData'], 'subTotal' => $sessionCartData['subTotal'], 'discount' => $sessionCartData['discount'], 'taxes' => $sessionCartData['taxes'], 'grandTotal' => $grandTotal, 'products' => $sessionCartData['products'], 'shipping' => $shipping ];
session()->put('cart'.session()->getId(),$newSessionData); session()->save();
//check coupon $counponHistory = DB::table('coupon_history')->where('is_valid',1)->where('session_id',session()->getId()) ->where('order_done',0)->first(); $discountAMT = 0; if($counponHistory) { if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $counponHistory->amount; $grandTotal -= $discountAMT; } }
return ['status'=> 1,'shipping' => $shipping,'orderSummary' => $sessionCartData['cartData'],'grandTotal' => number_format($grandTotal,2),'couponDiscount'=>number_format($discountAMT,2)]; } else { return ['status'=> 1,'message' => 'Session expired add products again!']; } } catch (\Exception $e) { return ['status'=> 0,'message'=>'Error']; } }
//create session stripe payment public function createStripePaymentIntent(Request $request) {
$sessionCartData = session()->get('cart'.session()->getId()); $getMaxNumber = Order::max('id'); $getAddress = DB::table('customer_address') ->join('country','country.id','=','customer_address.country_id') ->select('customer_address.*','country.name as country','country.iso_code_2') ->where('customer_address.id',$request->address_id) ->first();
$grandTotal = $sessionCartData['grandTotal']; //check coupon $counponHistory = DB::table('coupon_history')->where('is_valid',1)->where('session_id',session()->getId()) ->where('order_done',0)->first(); $discountAMT = 0; if($counponHistory) { if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $counponHistory->amount; $grandTotal -= $discountAMT; } }
$intent = $this->stripe->paymentIntents->create([ "amount" => $grandTotal * 100, "currency" => "usd", 'payment_method_types' => ['card'], 'metadata' => ['integration_check'=>'accept_a_payment'], "description" => config('settingConfig.config_store_name')." Product Purchase Payment", "shipping" => [ "name" => $getAddress->name, "address" => [ "line1" => $getAddress->address_1, "postal_code" => $getAddress->postcode, "city" => $getAddress->city, "country" => $getAddress->iso_code_2, ], ] ]);
$data = array( 'name'=> $this->getUser->firstname, 'email'=> $this->getUser->email, 'amount'=> $grandTotal, 'client_secret'=> $intent->client_secret, ); return $data; }
//place order public function placeOrder(Request $request){ try { // \Illuminate\Support\Facades\DB::beginTransaction();
$sessionCartData = session()->get('cart'.session()->getId()); $getMaxNumber = Order::max('id'); $getAddress = DB::table('customer_address')->whereId($request->address_id)->first(); $getBillingAddress = DB::table('customer_address')->whereId($request->billing_address_id)->first();
$oaymentMethod = ''; $grandTotal = $sessionCartData['grandTotal']; $findShipping = Shipping::find($request->selectedShippingMethod);
if($request->payment_method == 'stripe') { $paymentMethod = 'Credit/Debit Card (Stripe Payment Geteway)'; //verify payment try { $this->stripe->paymentIntents->retrieve( $request->tid, [] ); } catch (\Exception $e) { return redirect()->back()->with('commonError','Payment not verified'); } }
if($request->payment_method == 'razorpay') { $paymentMethod = 'Razorpay Payment Geteway';
$api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET')); $payment = $api->payment->fetch($request->tid); try { $response = $api->payment->fetch($request->tid)->capture(array('amount'=>$payment['amount'])); } catch (\Exception $e) { return redirect()->back()->with('commonError',$e->getMessage()); } } if($request->payment_method == 'paypal') { $paymentMethod = 'Paypal Payment Geteway';
$provider = new PayPalClient; $provider->setApiCredentials(config('paypal')); $paypalToken = $provider->getAccessToken();
$response = $provider->createOrder([ "intent" => "CAPTURE", "application_context" => [ "return_url" => route('paypal.successTransaction'), "cancel_url" => route('paypal.cancelTransaction'), ], "purchase_units" => [ 0 => [ "amount" => [ "currency_code" => "USD", "value" => $grandTotal ] ] ] ]); if (isset($response['id']) && $response['id'] != null) { // redirect to approve href foreach ($response['links'] as $links) { if ($links['rel'] == 'approve') { session()->put('SHIPPING_DATA',$findShipping->name); session()->put("ORDER_DATA",$request->all()); session()->save(); return redirect()->away($links['href']); } } return redirect() ->back() ->with('commonError', 'Something went wrong.'); } else { return redirect() ->back() ->with('commonError', $response['message'] ?? 'Something went wrong.'); } } //mollie payment Geteway if($request->payment_method == 'mollie') { $generateTrx = Str::random(30); $payment = Mollie::api()->payments->create([ "amount" => [ "currency" => "USD", "value" => number_format($grandTotal,2) // You must send the correct number of decimals, thus we enforce the use of strings ], "description" => "Order #".$getMaxNumber, "redirectUrl" => route('mollie.success'), "webhookUrl" => route('webhooks.mollie'), "metadata" => [ "order_id" => $generateTrx, ], ]);
session()->put('TRX_ID',$generateTrx); session()->put('SHIPPING_DATA',$findShipping->name); session()->put("ORDER_DATA",$request->all()); session()->save(); return redirect($payment->getCheckoutUrl(), 303); } if($request->payment_method == 'cod') { $paymentMethod = 'Cash On Delivery'; }
//check coupon $counponHistory = DB::table('coupon_history')->where('is_valid',1)->where('session_id',session()->getId()) ->where('order_done',0)->first(); $discountAMT = 0; $api_dis_type='percent'; if($counponHistory) { if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $counponHistory->amount; $grandTotal -= $discountAMT; $api_dis_type='flat'; } }
//make stripe payment method 2 //$charge = $this->createCharge($request->stripeToken, $grandTotal,$getAddress);
$paymentDone = 0; if (!empty($charge) && $charge['status'] == 'succeeded') { $paymentDone = 1; } else { $paymentDone = 0; }
//build order array $orderArr = [ 'invoice_no' => $getMaxNumber, 'customer_id' => $this->getUser ? $this->getUser->id : 0, 'firstname' =>$this->getUser ? $this->getUser->firstname :$getBillingAddress->name , 'lastname' => $this->getUser ? $this->getUser->lastname : 'guest', 'email' => $this->getUser ? $this->getUser->email : $getBillingAddress->email, 'telephone' =>$this->getUser ? $this->getUser->telephone : $getBillingAddress->mobile, 'order_date'=>date('Y-m-d'), 'shipping_name' => $request->has('delivery_same_billing') ? $getBillingAddress->name : $sessionCartData['shipping']['name'], 'shipping_address_1' => $request->has('delivery_same_billing') ? $getBillingAddress->address_1 : $getAddress->address_1, 'shipping_address_2' => $request->has('delivery_same_billing') ? $getBillingAddress->address_2 : $getAddress->address_2, 'shipping_city' => $request->has('delivery_same_billing') ? $getBillingAddress->city : $getAddress->city, 'shipping_postcode' =>$request->has('delivery_same_billing') ? $getBillingAddress->postcode : $getAddress->postcode, 'shipping_country_id' =>$request->has('delivery_same_billing') ? $getBillingAddress->country_id : $getAddress->country_id, 'billing_name' => $getBillingAddress->name, 'billing_address_1' => $getBillingAddress->address_1, 'billing_address_2' => $getBillingAddress->address_2, 'billing_city' => $getBillingAddress->city, 'billing_postcode' => $getBillingAddress->postcode, 'billing_country_id' => $getBillingAddress->country_id, 'comment' => $request->comment, 'total' =>str_replace(",","", $sessionCartData['subTotal']) , 'order_status_id' => '1', 'tax_amount' => array_key_exists('taxes',$sessionCartData) ? $sessionCartData['taxes'] ? count($sessionCartData['taxes'] ) > 0 ? $sessionCartData['taxes']['taxAmount'] : 0 : 0 : 0, 'discount' => $discountAMT, 'shipping_charge' => $sessionCartData['shipping']['charges'], 'grand_total' => $grandTotal, 'payment_method' => $paymentMethod, 'transaction_id' => $request->tid ? $request->tid : 0, 'shipping_method' => $findShipping->name ];
//create order $storeOrder = Order::create($orderArr); if($storeOrder) { //Store OrderProduct $storeOrderProductArr = [];
foreach ($sessionCartData['cartData'] as $key => $value) { $storeOrderProductArr[] = [ 'order_id' => $storeOrder->id, 'product_id' => $value['pid'], 'name' => $value['name'], 'quantity' => $value['quantity'], 'image' => $value['image'], 'price' => str_replace(",","", $value['price']), 'special' =>str_replace(",","", $value['special']) , 'total' => str_replace(",","", $value['totalPrice']), 'options' => isset($value['options']) ? serialize($value['options']) : null ]; }
OrderProduct::insert($storeOrderProductArr);
//add order history OrderHistory::create([ 'order_id' => $storeOrder->id, 'order_status_id' => '1', 'notif' => 0, 'comment' => 'Initial Order' ]);
//Section Inventory API Create Sales $api_url = Setting::where('key', 'api_url')->first(); $api_token = Setting::where('key', 'api_token')->first(); if ($api_url && isset($api_url->value) && $api_token && isset($api_token->value)) { try{ $client = new Client();
$api_form_data=[ 'client_id' => auth()->user()->id, 'user_id' => auth()->user()->id, 'order_id' => $storeOrder->id, 'GrandTotal' => $grandTotal?$grandTotal:'0', 'tax_rate' => $storeOrder->tax_amount, 'TaxNet' => $storeOrder->tax_amount, 'discount' => $storeOrder->discount, 'discount_type' => $api_dis_type, 'details'=>$sessionCartData['cartData'] ];
$response = $client->post($api_url->value . '/api/create/sale', [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_token->value, ], 'form_params' => $api_form_data ]); $response = $response->getBody()->getContents(); dd($response); }catch(\Exception $ex){ dd($ex->getMessage()); Log::info($ex->getMessage()); } } //END
session()->forget('cart'.session()->getId());
DB::table('cart')->where('session_id',session()->getId())->delete(); DB::table('customer_address')->where('session_id',session()->getId())->delete(); //update coupon $counponHistory = DB::table('coupon_history')->where('session_id',session()->getId())->update(['order_done' => 1]); return redirect('/payment-success'); } else { dd('ok'); return redirect()->back()->with('commonError','Error when order try again later!'); } // } catch (\Exception $e) { dd($e); return redirect()->back()->with('commonError','Error'); } }
//paypal success function public function paypalSuccess(Request $request) {
$provider = new PayPalClient; $provider->setApiCredentials(config('paypal')); $provider->getAccessToken(); $response = $provider->capturePaymentOrder($request['token']); if (isset($response['status']) && $response['status'] == 'COMPLETED') {
/**************************************************** complete order after payment success ******************************************************/
$getSessionRequests = session()->get('ORDER_DATA'); $shippingMethod = session()->get('SHIPPING_DATA');
$paymentMethod = 'Paypal Payment Geteway'; $sessionCartData = session()->get('cart'.session()->getId()); $getMaxNumber = Order::max('id'); $getAddress = DB::table('customer_address')->whereId($getSessionRequests['address_id'])->first(); $getBillingAddress = DB::table('customer_address')->whereId($getSessionRequests['billing_address_id'])->first();
$oaymentMethod = ''; $grandTotal = $sessionCartData['grandTotal'];
//check coupon $counponHistory = DB::table('coupon_history')->where('is_valid',1)->where('session_id',session()->getId()) ->where('order_done',0)->first(); $discountAMT = 0; if($counponHistory) { if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $counponHistory->amount; $grandTotal -= $discountAMT; } }
$paymentDone = 0; if (!empty($charge) && $charge['status'] == 'succeeded') { $paymentDone = 1; } else { $paymentDone = 0; }
//build order array $orderArr = [ 'invoice_no' => $getMaxNumber, 'customer_id' => $this->getUser ? $this->getUser->id : 0, 'firstname' =>$this->getUser ? $this->getUser->firstname :$getBillingAddress->name , 'lastname' => $this->getUser ? $this->getUser->lastname : 'guest', 'email' => $this->getUser ? $this->getUser->email : $getBillingAddress->email, 'telephone' =>$this->getUser ? $this->getUser->telephone : $getBillingAddress->mobile, 'order_date'=>date('Y-m-d'), 'shipping_name' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->name : $sessionCartData['shipping']['name'], 'shipping_address_1' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->address_1 : $getAddress->address_1, 'shipping_address_2' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->address_2 : $getAddress->address_2, 'shipping_city' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->city : $getAddress->city, 'shipping_postcode' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->postcode : $getAddress->postcode, 'shipping_country_id' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->country_id : $getAddress->country_id, 'billing_name' => $getBillingAddress->name, 'billing_address_1' => $getBillingAddress->address_1, 'billing_address_2' => $getBillingAddress->address_2, 'billing_city' => $getBillingAddress->city, 'billing_postcode' => $getBillingAddress->postcode, 'billing_country_id' => $getBillingAddress->country_id, 'comment' => $getSessionRequests['comment'], 'total' =>str_replace(",","", $sessionCartData['subTotal']) , 'order_status_id' => '1', 'tax_amount' => array_key_exists('taxes',$sessionCartData) ? $sessionCartData['taxes'] ? count($sessionCartData['taxes'] ) > 0 ? $sessionCartData['taxes']['taxAmount'] : 0 : 0 : 0, 'discount' => $discountAMT, 'shipping_charge' => $sessionCartData['shipping']['charges'], 'grand_total' => $grandTotal, 'payment_method' => $paymentMethod, 'transaction_id' =>$request->PayerID, 'shipping_method' => $shippingMethod ];
//create order $storeOrder = Order::create($orderArr); if($storeOrder) {
//Store OrderProduct $storeOrderProductArr = []; foreach ($sessionCartData['cartData'] as $key => $value) { $storeOrderProductArr[] = [ 'order_id' => $storeOrder->id, 'product_id' => $value['pid'], 'name' => $value['name'], 'quantity' => $value['quantity'], 'image' => $value['image'], 'price' => str_replace(",","", $value['price']), 'special' =>str_replace(",","", $value['special']) , 'total' => str_replace(",","", $value['totalPrice']), 'options' => $value['options'] ? serialize($value['options']) : null ]; }
OrderProduct::insert($storeOrderProductArr);
//add order history OrderHistory::create([ 'order_id' => $storeOrder->id, 'order_status_id' => '1', 'notif' => 0, 'comment' => 'Initial Order' ]);
session()->forget('cart'.session()->getId()); session()->forget('ORDER_DATA');
DB::table('cart')->where('session_id',session()->getId())->delete(); DB::table('customer_address')->where('session_id',session()->getId())->delete();
//update coupon $counponHistory = DB::table('coupon_history')->where('session_id',session()->getId())->update(['order_done' => 1]); return redirect('/payment-success'); } else { return redirect()->back()->with('commonError','Error when order try again later!'); }
} else { return redirect() ->route('checkout') ->with('commonError', $response['message'] ?? 'Something went wrong.'); } }
//when mollie payment success public function mollieSuccess(Request $request) {
$getTrx = session()->get('TRX_ID'); if($getTrx) { $findMolliePayment = DB::table("molie_payment_tracking")->where('trx_id',$getTrx)->first(); if($findMolliePayment) { if($findMolliePayment->payment_status == 1) { /**************************************************** complete order after payment success ******************************************************/ $getSessionRequests = session()->get('ORDER_DATA'); $shippingMethod = session()->get('SHIPPING_DATA'); $paymentMethod = 'Mollie Payment Geteway'; $sessionCartData = session()->get('cart'.session()->getId()); $getMaxNumber = Order::max('id'); $getAddress = DB::table('customer_address')->whereId($getSessionRequests['address_id'])->first(); $getBillingAddress = DB::table('customer_address')->whereId($getSessionRequests['billing_address_id'])->first(); $oaymentMethod = ''; $grandTotal = $sessionCartData['grandTotal'];
//check coupon $counponHistory = DB::table('coupon_history')->where('is_valid',1)->where('session_id',session()->getId()) ->where('order_done',0)->first(); $discountAMT = 0; if($counponHistory) { if($counponHistory->coupon_type == 1) { $discountAMT = $grandTotal / 100 * $counponHistory->amount; $grandTotal -= $discountAMT; } else { $discountAMT = $counponHistory->amount; $grandTotal -= $discountAMT; } }
$paymentDone = 0; if (!empty($charge) && $charge['status'] == 'succeeded') { $paymentDone = 1; } else { $paymentDone = 0; }
//build order array $orderArr = [ 'invoice_no' => $getMaxNumber, 'customer_id' => $this->getUser ? $this->getUser->id : 0, 'firstname' =>$this->getUser ? $this->getUser->firstname :$getBillingAddress->name , 'lastname' => $this->getUser ? $this->getUser->lastname : 'guest', 'email' => $this->getUser ? $this->getUser->email : $getBillingAddress->email, 'telephone' =>$this->getUser ? $this->getUser->telephone : $getBillingAddress->mobile, 'order_date'=>date('Y-m-d'), 'shipping_name' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->name : $sessionCartData['shipping']['name'], 'shipping_address_1' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->address_1 : $getAddress->address_1, 'shipping_address_2' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->address_2 : $getAddress->address_2, 'shipping_city' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->city : $getAddress->city, 'shipping_postcode' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->postcode : $getAddress->postcode, 'shipping_country_id' => $getSessionRequests['delivery_same_billing'] ? $getBillingAddress->country_id : $getAddress->country_id, 'billing_name' => $getBillingAddress->name, 'billing_address_1' => $getBillingAddress->address_1, 'billing_address_2' => $getBillingAddress->address_2, 'billing_city' => $getBillingAddress->city, 'billing_postcode' => $getBillingAddress->postcode, 'billing_country_id' => $getBillingAddress->country_id, 'comment' => $getSessionRequests['comment'], 'total' =>str_replace(",","", $sessionCartData['subTotal']) , 'order_status_id' => '1', 'tax_amount' => array_key_exists('taxes',$sessionCartData) ? $sessionCartData['taxes'] ? count($sessionCartData['taxes'] ) > 0 ? $sessionCartData['taxes']['taxAmount'] : 0 : 0 : 0, 'discount' => $discountAMT, 'shipping_charge' => $sessionCartData['shipping']['charges'], 'grand_total' => $grandTotal, 'payment_method' => $paymentMethod, 'transaction_id' =>$findMolliePayment->payment_id, 'shipping_method' => $shippingMethod ];
//create order $storeOrder = Order::create($orderArr); if($storeOrder) {
//Store OrderProduct $storeOrderProductArr = []; foreach ($sessionCartData['cartData'] as $key => $value) { $storeOrderProductArr[] = [ 'order_id' => $storeOrder->id, 'product_id' => $value['pid'], 'name' => $value['name'], 'quantity' => $value['quantity'], 'image' => $value['image'], 'price' => str_replace(",","", $value['price']), 'special' =>str_replace(",","", $value['special']) , 'total' => str_replace(",","", $value['totalPrice']), 'options' => $value['options'] ? serialize($value['options']) : null ]; }
OrderProduct::insert($storeOrderProductArr);
//add order history OrderHistory::create([ 'order_id' => $storeOrder->id, 'order_status_id' => '1', 'notif' => 0, 'comment' => 'Initial Order' ]);
session()->forget('cart'.session()->getId()); session()->forget('ORDER_DATA'); session()->forget('TRX_ID');
DB::table('cart')->where('session_id',session()->getId())->delete(); DB::table('customer_address')->where('session_id',session()->getId())->delete();
//update coupon $counponHistory = DB::table('coupon_history')->where('session_id',session()->getId())->update(['order_done' => 1]); return redirect('/payment-success'); } else { return redirect()->back()->with('commonError','Error when order try again later!'); } } else { return redirect() ->route('checkout') ->with('commonError','Payment failed!'); } } else { return redirect() ->route('checkout') ->with('commonError', 'Something went wrong.'); } } else { return redirect() ->route('checkout') ->with('commonError', 'Something went wrong.'); }
}
//mollie payment geteway public function mollieTransaction(Request $request) { $paymentId = $request->input('id');
$payment = Mollie::api()->payments->get($paymentId); $checkExist = null; $getTrx = session()->get('TRX_ID'); if($getTrx) { $checkExist = DB::table("molie_payment_tracking")->where('trx_id',$getTrx)->first(); } $paymentStatus = 0;
if ($payment->isPaid()) { $paymentStatus = 1; }
if($checkExist != null) { DB::table("molie_payment_tracking") ->where('trx_id',$getTrx) ->update(['payment_id' => $paymentId, 'trx_id'=>$payment->metadata->order_id, 'payment_status' => $paymentStatus]); } else { DB::table("molie_payment_tracking")->insert(['payment_id' => $paymentId, 'trx_id'=>$payment->metadata->order_id,'payment_status' => $paymentStatus]); }
}
//paypal failed function public function paypalFailed() { return redirect()->back()->with('commonError','Error when order try again later!'); }
//payment success page public function paymentSuccess() { $this->buildSeo('Payment success',['Smabmart','Payment success'],url()->current(),''); return view('frontend.cart.paymentSuccess'); }
//create stripe charges private function createCharge($tokenId, $grandTotal,$getAddress) { $charge = null; try { $charge = $this->stripe->charges->create([ "amount" => $grandTotal * 100, "currency" => "usd", 'source' => $tokenId, "description" => config('settingConfig.config_store_name')." Product Purchase Payment", "shipping" => [ "name" => $getAddress->name, "address" => [ "line1" => $getAddress->address_1, "postal_code" => $getAddress->postcode, "city" => $getAddress->city, "country" => $getAddress->country_id, ], ] ]); } catch (Exception $e) { $charge['error'] = $e->getMessage(); } return $charge; }
//merge taxes public function mergeTax($taxRates) {
$finalTaxRates = [];
//merge same taxes $newTaxArr = []; foreach($taxRates as $key => $value) { $newTaxArr[$value['name']][] = $value['taxAmount']; }
//final tax arr foreach($newTaxArr as $key => $value) { $finalTaxRates[] = array('name'=>$key,'taxAmount'=>array_sum($value)); }
return $finalTaxRates;
}
}
|