
PK 
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator;
use Response;
use Redirect;
use Session;
use Auth;
use App\library\Shopcart;
use Carbon\Carbon;
use App\Mail\OrderShipped;
use Illuminate\Support\Facades\Mail;
use App\UserAddress;
use App\Wishlist;
use App\Promocode;
use App\Promocodeuse;
use App\Product;
use App\Order;
use App\Cart;
class CheckoutControllerV1 extends Controller
{
protected $shopcart;
private $carts = [];
private $cartTotal = 0;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:web');
//$this->shopcart = $Shopcart;
//$this->getCartlist();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (view()->exists('frontend.carts.checkout')) {
$cart = $this->getCartContant();
if ($cart->status == false) {
return redirect()->route('cart.index');
}
$user_id = Auth::user()->id;
$cartTotal = $cartSubTotal = $discountat = 0;
$carts = ($cart->cart_contant) ? json_decode($cart->cart_contant, true) : [];
$cartSubTotal = $cartTotal = $cart->cart_total;
$orderShipCharge = ($cartTotal <= 500) ? 75 : 0;
$discountPercent = 0;
$afterDiscountCartTotal = $cartTotal - ($cartTotal * ($discountPercent / 100));
$cartSubTotal = ($cartSubTotal+$orderShipCharge);
$checkWalletAmount = self::checkWalletAmount($user_id);
$wallet_amount = ($checkWalletAmount->status) ? $checkWalletAmount->data->wallet_amount : 0;
//##p($carts); exit;
$addresses = UserAddress::Where('user_id', $user_id)->orderBy('id', 'desc')->get();
return view('frontend.carts.checkout', compact('carts', 'cartTotal', 'discountat', 'orderShipCharge','cartSubTotal', 'discountPercent', 'afterDiscountCartTotal', 'wallet_amount', 'addresses'));
}
//return back()->with('message', 'Opps view not found');
}
/**
* Apply promocode of the resource.
*
* @return \Illuminate\Http\Response
*/
public function aplayToPromocode(Request $request)
{
$rules = [
'couponcode' => 'required',
'cartamount' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json(['status' => false, 'errors' => $validator->getMessageBag()->toArray()]);
} else {
$promo_code = $request->couponcode;
$cartAmount = $request->cartamount;
$data = $this->checkPromocode($promo_code, $cartAmount);
return response()->json($data);
}
}
/**
* Apply promocode of the resource.
*
* @return \Illuminate\Http\Response
*/
public function cartCheckout(Request $request)
{
if (!empty($request->iswallet) && $request->iswallet == true) {
$response = $this->checkoutCart($isWallet = true, $request->payshipaddress, $request->couponcode, $paymode = null, $txnid = null);
if ($response->status == true) {
//p($response);
$user_id = Auth::user()->id;
$order_id = $response->data->order_id;
$order_txnid = $response->data->order_txnid;
$order_paid_amount = $response->data->order_paid_amount;
$wallet_balance_amount = $response->data->wallet_balance_amount;
$order_paymode = $response->data->order_paymode;
self::checkWalletAmount($user_id, $order_paid_amount, $order_id, $order_txnid, $wallet_status = '2', $walletAction = 'debit');
}
} else {
$response = $this->checkoutCart($isWallet = false, $request->payshipaddress, $request->couponcode, $paymode = null, $txnid = null);
}
//p($response);
return response()->json($response);
//p($request->all()); exit;
//exit;
/* $resCode=["status"=>false,"message"=>"Invalid Submission !"];
if (!empty($request->payshipaddress)) {
$user_id = Auth::user()->id;
$promo_id=$promo_code='';
$cartAmount=$cartSubTotal=$promo_amount=$cartDiscountAmount=$shippingCharge=$card_amount=0;
$cart =$this->getCartContant();
$cartAmount = $cartDiscountAmount = $cart->cart_total;
if (!empty($request->couponcode)) {
$request->couponcode;
$promo = (object)$this->checkPromocode($request->couponcode);
if($promo->status==true){
$promo_id = $promo->data['couponid'];
$promo_code = $promo->data['couponame'];
$promo_amount=$promo->data['couponamount'];
$cartDiscountAmount=$promo->data['cartdiscountamount'];
};
}
p($promo);
$cart_contant= $cart->cart_contant;
##### START SAVE DATA ######
$order = new Order();
$order->user_id = $user_id;
$order->order_contant = $cart_contant;
$order->order_subtotal = $cartAmount;
$order->order_ship_charge = $shippingCharge;
$order->order_promo_id = $promo_id;
$order->order_promo_code = $promo_code;
$order->order_promo_amount = $promo_amount;
$order->order_discount = '';
$order->order_card_amount = $card_amount;
$order->order_total_amount = $cartDiscountAmount;
$order->order_paymode = 1;
$order->order_source = 1;
$order->order_paystatus = 1;
$order->order_txnid = 'sa'.rand();
$order->order_status = '1';
$order->isactive = 1;
$order->order_courier_service = '';
$order->order_tracking_id = '';
$order->order_tracking_url = '';
$order->order_billname = '';
$order->order_contactno = '';
$order->order_email = '';
$order->order_address = '';
$order->order_pincode = '';
$order->order_city = '';
$order->order_state = '';
$order->order_country = '';
$order->isdelete = 1;
$order->created_at = Carbon::now();
$order->updated_at = Carbon::now();
$order->visitor =ipAddress();
$order->save();
p($request->All());
} else {
$resCode=["status"=>false,"message"=>"Please select shipping address !"];
} */
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function cartCheckoutCcAvenue(Request $request)
{
/*$data = $request->all();
$newArray = ['order_id' => uniqid(), 'user_id' => Auth::user()->id,
'billing_name' => '',
'billing_address' => '',
'billing_city' => '',
'billing_state' => '',
'billing_zip' => '',
'billing_country' => '',
'billing_tel' => '',
'billing_email' => '',
];
$ccavanue = array_merge($data, $newArray);
//dd($ccavanue);
return view('frontend.carts.ccavanue', compact('ccavanue'));*/
$user_id = Auth::user()->id;
$data = $request->all();
$addresses = \App\UserAddress::Where('user_id', $user_id)->where('id', $data['payshipaddress'])->first();
$cart = $this->getCartContant();
$carts = ($cart->cart_contant) ? $cart->cart_contant : [];
$codcouponid =(empty($data[ 'codcouponid'])) ? 0 : $data[ 'codcouponid'];
$codcouponname =(empty($data[ 'codcouponname'])) ? '' : $data[ 'codcouponname'];
$codcouponamt = (empty( $data['codcouponamt']))?0: $data['codcouponamt'];
$order_cart_total = $data['cartamount'];
$order_total_amount = $data['finalcartamount'];
$order_ship_charge = ($data['cartamount'] <= 500) ? 75 : 0;
$order_card_amount = $data['cartdiscountamount'];
//$order_total_amount += $order_ship_charge;
$order_subtotal = $order_total_amount;
// if($user_id==801){
// $order_total_amount=1;
// }
$nthOrderId = \App\OrderPaymentLoger::max('id') + 1;
//$user_id . '/' . time() . '/' . $nthOrderId
$orderPayLoger = [
'user_id' => $user_id, 'order_contant' => $carts, 'order_cart_total' => $order_cart_total, 'order_subtotal' => $order_subtotal, 'order_ship_charge' => $order_ship_charge,
'order_promo_id' => $codcouponid, 'order_promo_code' => $codcouponname, 'order_promo_amount' => $codcouponamt,
'order_discount' => '', 'order_card_amount' =>$order_card_amount, 'order_total_amount' => round($order_total_amount,2),
//'order_paid_amount'=>0,'order_paymode'=>'','order_source'=>'',
//'order_paystatus'=>'','order_txnid'=>'','order_status'=>'',
'order_txnid' => $nthOrderId,
'order_billname' => $addresses->shipname, 'order_contactno' => $addresses->shipcontactno, 'order_email' => $addresses->shipemailid, 'order_address' => $addresses->shipaddress,
'order_pincode' => $addresses->shippincode, 'order_city' => $addresses->shipcity, 'order_state' => $addresses->shipstate,
'order_country' => $addresses->billcountry, 'isdelete' => 1,
'visitor' => ipAddress()
];
$OrderPaymentLoger = \App\OrderPaymentLoger::create($orderPayLoger);
$order_id = $OrderPaymentLoger->id;
/*$newArray = [
'order_id' => time().'|'.$order_id,
'user_id' => $user_id,
'billing_name' => $addresses->shipname,
'billing_address' => $addresses->shipaddress,
'billing_city' => $addresses->shipcity,
'billing_state' => $addresses->shipstate,
'billing_zip' => $addresses->shippincode,
'billing_country' => $addresses->billcountry,
'billing_tel' => $addresses->shipcontactno,
'billing_email' => $addresses->shipemailid,
];*/
$ccavanue = $orderPayLoger; //array_merge($data, $newArray);
return view('frontend.carts.ccavanue', compact('ccavanue', 'order_id'));
}
public function ccavRequestHandler(Request $request)
{
$data = $request->all();
unset($data['_token']);
$merchant_data = '';
$working_key = '8BB7C42ADB5397536860ACCAA99370AA'; //Shared by CCAVENUES
$access_code = 'AVGK03IB72CC06KGCC'; //Shared by CCAVENUES
foreach ($data as $key => $value) {
$merchant_data .= $key . '=' . $value . '&';
}
$encrypted_data = encryptCcAvenue($merchant_data, $working_key); // Method for encrypting the data.
return view('frontend.carts.ccavanuePayOnline', compact('encrypted_data', 'access_code', 'data'));
//dump($working_key,$access_code,$merchant_data,$encrypted_data,$data);
}
public function payonline(Request $request)
{
dd(request()->all());
//print_r($request->all()); exit;
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
private function getCartContant()
{
if (Auth::guard('web')->check()) {
$user_id = Auth::user()->id;
$cartTotal = $cartId = 0;
$cart = Cart::where('user_id', $user_id)->select('id', 'user_id', 'cart_contant')->first();
if ($cart) {
$cartId = $cart->id;
$cart_contant = $cart->cart_contant;
$carts = ($cart_contant) ? json_decode($cart_contant, true) : [];
if (count($carts) > 0) {
foreach ($carts as $key => $cartval) {
$cartTotal += round($cartval['product_sale_price'] * $cartval['quantity']);
}
return (object)['status' => true, 'cart_id' => $cartId, 'cart_total' => $cartTotal, 'cart_contant' => $cart_contant];
}
}
}
return (object)['status' => false, 'cart_id' => $cartId, 'cart_total' => 0, 'cart_contant' => ''];
}
private function checkPromocode($promocode = null, $cartamount = null)
{
$promo_code = (!empty($promocode)) ? $promocode : 0;
$cartAmount = 0;//(int)(!empty($cartamount))?$cartamount:0;
if (!empty($promo_code)) {
//$user_id = Auth::user()->id;
$cart = $this->getCartContant();
$cartAmount = $cart->cart_total;
$promo = Promocode::where('promo_code', $promo_code)->where('isactive', 1)->where('isdelete', 1)->first();
//->select('id','promo_name','promo_code','promo_ntime','promo_used','promo_type','promomin_price','promomax_price','promofrom_date','promoto_date')->get()
if ($promo) {
$user_id = Auth::user()->id;
//##Check Current user used or not current promocode
$Promocodeuse = Promocodeuse::where('user_id', $user_id)->where('promo_code', $promo_code)->where('isactive', 1)->first();
if ($Promocodeuse) {
return ['status' => false, 'message' => 'Opps! You have allready used Promocode!'];
}
//##find out availabe or not
if ($promo->promo_ntime >= $promo->promo_used) {
//##find out promocode expired or not
$currentTime = Carbon::now()->timestamp;
$promofrom_date = Carbon::parse($promo->promofrom_date)->timestamp;
$promoto_date = Carbon::parse($promo->promoto_date)->timestamp;
if ($currentTime >= $promofrom_date && $currentTime <= $promoto_date) {
//##find out discount amount available or not based on cartAmount
//if($cartAmount >= $promo->promomin_price && $promo->promomax_price <= $cartAmount ){
$promomin_price = $promo->promomin_price;
if ($cartAmount >= $promo->promomin_price) {
//##get promotype 1:%discount;2:flatdiscount
$promoAmount = $promo->promo_amount;
$discountAmount = ($promo->promo_type == 1) ? ($cartAmount * ($promoAmount / 100)) : $promoAmount;
$cartDiscountAmount = round($cartAmount - $discountAmount);
return ['status' => true, 'message' => 'Promocode Applied !', 'data' => ['couponid' => $promo->id, 'couponame' => $promo->promo_code, 'couponamount' => $discountAmount, 'cartdiscountamount' => $cartDiscountAmount, 'cartamount' => $cartAmount]];
} else {
return ['status' => false, 'message' => "minimum $promomin_price cart amout for apply this coupon code!"];
}
} else {
return ['status' => false, 'message' => 'Opps! Promocode has been expired !'];
}
} else {
return ['status' => false, 'message' => 'Opps! Promocode allready used !'];
}
//$trialExpires = Carbon::now()->addDays(30);
//echo $lengthOfAd = $end_date->diffInDays($now);
//->toDateString();
}
//setcookie('promocode',0);
}
return ["status" => false, "message" => "Invalid Promo code !"];
}
private function checkoutCart($isWallet = true, $payshipaddress = null, $promocode = null, $paymode = null, $txnid = null)
{
$resCode = ["status" => false, "message" => "Invalid Submission !"];
if (!empty($payshipaddress)) {
$user_id = Auth::user()->id;
$cart = $this->getCartContant();
if ($cart->status == true) {
$promo_code = '';
$cartAmount = $cartSubTotal = $promo_amount = $cartDiscountAmount = $shippingCharge = $card_amount = $promo_id = $wallet_balance_amount = 0;
$txnId = (!empty($txnid)) ? $txnid : $user_id . '-' . time();
$order_paymode = (!empty($paymode)) ? $paymode : 1;
$order_paymode = 'cod';
$order_paystatus = 'unpaid';
$order_paid_amount = $wallet_paid_amount = 0;
$cartAmount = $cartDiscountAmount = $cart->cart_total;
$cart_contant = $cart->cart_contant;
$cart_id = $cart->cart_id;
if (!empty($promocode)) {
$promo = (object)$this->checkPromocode($promocode, $cartAmount);
if ($promo->status == true) {
$promo_id = $promo->data['couponid'];
$promo_code = $promo->data['couponame'];
$promo_amount = $promo->data['couponamount'];
$cartDiscountAmount = $promo->data['cartdiscountamount'];
};
}
if ($isWallet == true) {
$checkWalletAmount = self::checkWalletAmount($user_id);
$wallet_amount = ($checkWalletAmount->status) ? $checkWalletAmount->data->wallet_amount : 0;
if ($cartDiscountAmount > $wallet_amount) {
$resStatus = ["status" => false, "message" => "Opps We don't prooced your order with wallet amount becoz your wallet amount is to less !"];
return arrayToObject($resStatus);
exit;
}
$order_paymode = 'wallet';
$order_paystatus = 'paid';
$order_paid_amount = $cartDiscountAmount;
$wallet_balance_amount = ($wallet_amount - $order_paid_amount);
}
$address = UserAddress::where('user_id', $user_id)->where('id', $payshipaddress)->select('shipname', 'shipaddress', 'shipemailid', 'shippincode', 'shipcontactno', 'shipstate', 'shipcity', 'shipcountry')->first();
##### START SAVE DATA ######
$order = new Order();
$order->user_id = $user_id;
$order->order_contant = $cart_contant;
$order->order_subtotal = $cartAmount;
$order->order_ship_charge = $shippingCharge;
$order->order_promo_id = $promo_id;
$order->order_promo_code = $promo_code;
$order->order_promo_amount = $promo_amount;
$order->order_discount = '';
$order->order_card_amount = $card_amount;
$order->order_total_amount = $cartDiscountAmount;
$order->order_paid_amount = $order_paid_amount;
$order->order_paymode = $order_paymode;
$order->order_source = 'web';
$order->order_paystatus = $order_paystatus;
$order->order_txnid = $txnId;
$order->order_status = 'placed';
$order->isactive = 1;
$order->order_courier_service = '';
$order->order_tracking_id = '';
$order->order_tracking_url = '';
$order->order_billname = $address->shipname;
$order->order_contactno = $address->shipcontactno;
$order->order_email = $address->shipemailid;
$order->order_address = $address->shipaddress;
$order->order_pincode = $address->shippincode;
$order->order_city = $address->shipcity;
$order->order_state = $address->shipstate;
$order->order_country = $address->shipcountry;
$order->isdelete = 1;
$order->created_at = Carbon::now();
$order->updated_at = Carbon::now();
$order->visitor = ipAddress();
if ($order->save()) {
$order_id = $order->id;
$cartContent = json_decode($order->order_contant, true);
foreach ($cartContent as $key => $cart) {
$product = Product::find($cart['product_id']);
if ($product->id) {
$product->product_sold_qty += $cart['quantity'];
if ($product->save()) {
}
}
$orderline = new \App\Orderline();
$orderline->user_id = $user_id;
$orderline->order_id = $order_id;
$orderline->orderCartId = $cart['cartId'];
$orderline->orderProductId = $cart['product_id'];
$orderline->orderProductName = $cart['product_name'];
$orderline->orderProductSalePrice = $cart['product_sale_price'];
$orderline->orderEachQuantity = $cart['quantity'];
$orderline->orderEachTotal = $cart['total'];
$orderline->orderCategoryId = $cart['component']['category_id'];
$orderline->orderSizeId = $cart['component']['size_id'];
$orderline->ordeColorId = $cart['component']['color_id'];
$orderline->orderProductSlug = $cart['component']['product_slug'];
$orderline->orderIsdiscount = ($cart['option']['isdiscount'] == 1) ? 2 : 1;
$orderline->orderProductPrice = $cart['option']['product_price'];
$orderline->orderProductImage = $cart['images']['product_image'];
$orderline->isdelete = 1;
$orderline->created_at = Carbon::now();
$orderline->updated_at = Carbon::now();
$orderline->visitor = ipAddress();
$orderline->save();
}
if (!empty($promocode && $promo->status == true)) {
$promocode = Promocode::where('promo_code', $promo_code)->where('id', $promo_id)->first();
$promocode->promo_used = ($promocode->promo_used + 1);
$promocode->save();
$promocodeuse = new Promocodeuse();
$promocodeuse->user_id = $user_id;
$promocodeuse->promo_id = $promocode->id;
$promocodeuse->promo_name = $promocode->promo_name;
$promocodeuse->promo_code = $promocode->promo_code;
$promocodeuse->promo_type = $promocode->promo_type;
$promocodeuse->promo_amount = $promocode->promo_amount;
$promocodeuse->visitor = ipAddress();
$promocodeuse->save();
}
Cart::where('id', $cart_id)->where('user_id', $user_id)->update(['cart_contant' => '[]', 'updated_at' => Carbon::now()]);
$shopcart = new Shopcart();
$shopcart->clearCart();
Mail::to($address->shipemailid)
->bcc('info@evami.in')
->bcc('indusinfotek.vinay@gmail.com')
->send(new OrderShipped($order, $orderline));
$resStatus = ["status" => true, "message" => "Order placed successfully !", 'data' => ['order_id' => $order_id, 'order_txnid' => $txnId, 'order_paid_amount' => $order_paid_amount, 'wallet_balance_amount' => $wallet_balance_amount, 'order_paymode' => $order_paymode]];
} else {
$resStatus = ["status" => false, "message" => "due to some technical isse we missed to place your order Please contact to our customer support!"];
}
} else {
$resStatus = ["status" => false, "message" => "Your cart is Empty !"];
}
} else {
$resStatus = ["status" => false, "message" => "Please select shipping address !"];
}
return arrayToObject($resStatus);
}
private function checkWalletAmount($user_id = null, $order_paid_amount = null, $order_id = '', $order_txnid = '', $wallet_status = '', $walletAction = 'check')
{
if (empty($user_id)) {
return ['status' => false, 'message' => 'Please provide customer id !'];
}
$status = ['status' => false, 'message' => 'Some thing went wrong !'];
$walletCreditAmount = $walletDebitAmount = $walletBalanceAmount = 0;
switch ($walletAction) {
case 'debit':
if ($user_id && $order_paid_amount) {
$wallets = \App\Wallet::select('wallet_credit_amount', 'wallet_debit_amount', 'wallet_balance_amount')->active()->isdelete()->where('user_id', $user_id)->where('isapproved', '2')->orderBy('id', 'desc')->first();
/* if($wallets->count()>0) {
foreach ($wallets as $wkey => $wvalue) {
$walletCreditAmount +=$wvalue->wallet_credit_amount;
$walletDebitAmount +=$wvalue->wallet_debit_amount;
}
$orderTotalPaidAmount = $order_paid_amount;
$walletBalanceAmount = ($walletCreditAmount - $walletDebitAmount);
$walletBalanceAmount = ($walletBalanceAmount-$orderTotalPaidAmount);
} else {
$orderTotalPaidAmount = $order_paid_amount;
$walletBalanceAmount = ($walletCreditAmount - $walletDebitAmount);
$walletBalanceAmount = ($walletBalanceAmount-$orderTotalPaidAmount);
} */
$walletBalanceAmount = $wallets->wallet_balance_amount - $order_paid_amount;
$wallet_status = (!empty($wallet_status)) ? $wallet_status : 2;
$wallet = new \App\Wallet;
$wallet->user_id = $user_id;
$wallet->wallet_credit_amount = 0;
$wallet->wallet_debit_amount = $order_paid_amount;
$wallet->wallet_balance_amount = $walletBalanceAmount;
$wallet->wallet_txnid = $order_txnid;
$wallet->wallet_refrenceid = $order_id;
$wallet->wallet_status = $wallet_status;
$wallet->isactive = 1;
$wallet->isapproved = 2;
$wallet->isdelete = 1;
$wallet->addedby = 0;
$wallet->created_at = Carbon::now();
$wallet->updated_at = Carbon::now();
$wallet->visitor = ipAddress();
if ($wallet->save()) {
$status = ['status' => true, 'message' => "wallet updated successfully !", 'data' => []];
} else {
$status = ['status' => false, 'message' => "wallet don't updated successfully !"];
// mail to admin inform to wallet updation has been faild update to react
}
}
break;
case 'check':
default:
$wallets = \App\Wallet::select('wallet_credit_amount', 'wallet_debit_amount', 'wallet_balance_amount')->active()->isdelete()->where('user_id', $user_id)->where('isapproved', '2')->get();
$status = ['status' => false, 'message' => 'in your wallet 0 balance amount!'];
if ($wallets->count() > 0) {
foreach ($wallets as $wkey => $wvalue) {
$walletCreditAmount += $wvalue->wallet_credit_amount;
$walletDebitAmount += $wvalue->wallet_debit_amount;
}
$walletBalanceAmount = ($walletCreditAmount - $walletDebitAmount);
if ($walletBalanceAmount > 0) {
$status = ['status' => true, 'message' => "in your wallet $walletBalanceAmount balance amount!", 'data' => ['wallet_amount' => $walletBalanceAmount]];
}
}
break;
}
return arrayToObject($status);
}
private function checkoutCart_org($payshipaddress = null, $promocode = null, $paymode = null, $txnid = null)
{
$resCode = ["status" => false, "message" => "Invalid Submission !"];
if (!empty($payshipaddress)) {
$user_id = Auth::user()->id;
$cart = $this->getCartContant();
if ($cart->status == true) {
$promo_code = '';
$cartAmount = $cartSubTotal = $promo_amount = $cartDiscountAmount = $shippingCharge = $card_amount = $promo_id = 0;
$txnId = (!empty($txnid)) ? $txnid : $user_id . '-' . time();
$paymode = (!empty($paymode)) ? $paymode : 1;
$cartAmount = $cartDiscountAmount = $cart->cart_total;
$cart_contant = $cart->cart_contant;
$cart_id = $cart->cart_id;
if (!empty($promocode)) {
$promo = (object)$this->checkPromocode($promocode, $cartAmount);
if ($promo->status == true) {
$promo_id = $promo->data['couponid'];
$promo_code = $promo->data['couponame'];
$promo_amount = $promo->data['couponamount'];
$cartDiscountAmount = $promo->data['cartdiscountamount'];
};
}
$address = UserAddress::where('user_id', $user_id)->where('id', $payshipaddress)->select('shipname', 'shipaddress', 'shipemailid', 'shippincode', 'shipcontactno', 'shipstate', 'shipcity', 'shipcountry')->first();
##### START SAVE DATA ######
$order = new Order();
$order->user_id = $user_id;
$order->order_contant = $cart_contant;
$order->order_subtotal = $cartAmount;
$order->order_ship_charge = $shippingCharge;
$order->order_promo_id = $promo_id;
$order->order_promo_code = $promo_code;
$order->order_promo_amount = $promo_amount;
$order->order_discount = '';
$order->order_card_amount = $card_amount;
$order->order_total_amount = $cartDiscountAmount;
$order->order_paymode = 1;
$order->order_source = 1;
$order->order_paystatus = 1;
$order->order_txnid = $txnId;
$order->order_status = '1';
$order->isactive = 1;
$order->order_courier_service = '';
$order->order_tracking_id = '';
$order->order_tracking_url = '';
$order->order_billname = $address->shipname;
$order->order_contactno = $address->shipcontactno;
$order->order_email = $address->shipemailid;
$order->order_address = $address->shipaddress;
$order->order_pincode = $address->shippincode;
$order->order_city = $address->shipcity;
$order->order_state = $address->shipstate;
$order->order_country = $address->shipcountry;
$order->isdelete = 1;
$order->created_at = Carbon::now();
$order->updated_at = Carbon::now();
$order->visitor = ipAddress();
if ($order->save()) {
$order_id = $order->id;
$cartContent = json_decode($order->order_contant, true);
foreach ($cartContent as $key => $cart) {
$product = Product::find($cart['product_id']);
if ($product->id) {
$product->product_sold_qty += $cart['quantity'];
if ($product->save()) {
}
}
}
if (!empty($promocode && $promo->status == true)) {
$promocode = Promocode::where('promo_code', $promo_code)->where('id', $promo_id)->first();
$promocode->promo_used = ($promocode->promo_used + 1);
$promocode->save();
$promocodeuse = new Promocodeuse();
$promocodeuse->user_id = $user_id;
$promocodeuse->promo_id = $promocode->id;
$promocodeuse->promo_name = $promocode->promo_name;
$promocodeuse->promo_code = $promocode->promo_code;
$promocodeuse->promo_type = $promocode->promo_type;
$promocodeuse->promo_amount = $promocode->promo_amount;
$promocodeuse->visitor = ipAddress();
$promocodeuse->save();
}
Cart::where('id', $cart_id)->where('user_id', $user_id)->update(['cart_contant' => '[]', 'updated_at' => Carbon::now()]);
$shopcart = new Shopcart();
$shopcart->clearCart();
$resCode = ["status" => true, "message" => "Order shiped successfully !", 'data' => ['order_id' => $order_id]];
} else {
$resCode = ["status" => false, "message" => "due to some technical isse we missed to place your order Please contact to our customer support!"];
}
} else {
$resCode = ["status" => false, "message" => "Your cart is Empty !"];
}
} else {
$resCode = ["status" => false, "message" => "Please select shipping address !"];
}
return $resCode;
}
}


PK 99