flutter – i am getting response code 500 from Api response but the data is storing in db

I am developing an flutter eCommerce app in which I used laravel API. every part of the API is working but the checkout page throwing 500 responses and the order is still saving. If it is a server-side problem then how other parts of the api are working perfectly like, login, product fetching, banners…

Please help.

api link. – grocery.liveup.xyz/api/checkout

please help

Flutter code.

    void checkOut(String paymentMethod, String paymentStatus, String wallet, String paymentid, String paymentGateway){
        var http = Client();
        http.post(checkoutUri,body: {
          'cart_id':'${makeOrderData.cart_id}',
          'payment_method':'$paymentMethod',
          'payment_status':'$paymentStatus',
          'wallet':'$wallet',
          'payment_id':'$paymentid',
          'payment_gateway':'$paymentGateway',
        }).then((value){
          print('status - ${value.statusCode}');
          Toast.show(value.statusCode.toString(), context,duration: Toast.LENGTH_SHORT,gravity: Toast.CENTER);
          print('payment - ${value.body}');
          if(value.statusCode == 200){
            MakeOrderBean orderBean = MakeOrderBean.fromJson(jsonDecode(value.body));
            if('${orderBean.status}' == '1'){
              cartCountProvider.hitCartCounter(0);
              Navigator.pushNamed(context, PageRoutes.confirmOrder);
              Toast.show(orderBean.message, context,duration: Toast.LENGTH_SHORT,gravity: Toast.CENTER);
            }else if('${orderBean.status}' == '2'){
              cartCountProvider.hitCartCounter(0);
              Navigator.pushNamed(context, PageRoutes.confirmOrder);
              Toast.show(orderBean.message, context,duration: Toast.LENGTH_SHORT,gravity: Toast.CENTER);
            }else{
              Toast.show(orderBean.message, context,duration: Toast.LENGTH_SHORT,gravity: Toast.CENTER);
            }
          }else{
            // Toast.show('Something went wrong!', context,duration: Toast.LENGTH_SHORT,gravity: Toast.CENTER);
          }
          setState(() {
            isLoading = false;
          });
        }).catchError((e){
          setState(() {
            isLoading = false;
          });

Api function:

 public function checkout(Request $request)
    { 
        $cart_id=$request->cart_id;
        $payment_method= $request->payment_method;
        $payment_status = $request->payment_status;
        $wallet = $request->wallet;
          $payment_id = $request->payment_id;
            $payment_gateway = $request->payment_gateway;
        $orderr = DB::table('orders')
           ->where('cart_id', $cart_id)
           ->first(); 
           
         $cart =  DB::table('orders')
           ->where('cart_id', $cart_id)
           ->first();   
        $store_id = $cart->store_id;  
         $getD = DB::table('store')
                         ->where('store_id', $store_id)
                        ->first();
                        
       $store_n = $getD->store_name; 
        $user_id= $orderr->user_id;   
        $delivery_date = $orderr->delivery_date;
        $time_slot= $orderr->time_slot;
        
        $var= DB::table('store_orders')
           ->where('order_cart_id', $cart_id)
           ->get();
        $price2 = $orderr->rem_price;
        $ph = DB::table('users')
                  ->select('user_name','user_phone','wallet')
                  ->where('user_id',$user_id)
                  ->first();
        $user_phone = $ph->user_phone;  
        $user_name = $ph->user_name;
        foreach ($var as $h){
        $varient_id = $h->varient_id;
        $p = DB::table('store_orders')
           ->where('order_cart_id',$cart_id)
           ->where('varient_id',$varient_id)
           ->first();
        $price = $p->price;   
        $order_qty = $h->qty;
        $unit() = $p->unit;
        $qty()= $p->quantity;
        $p_name() = $p->product_name."(".$p->quantity.$p->unit.")*".$order_qty;
        $prod_name = implode(',',$p_name);
        }
         $charge = 0;
         $prii = $price2;
        if ($payment_method == 'COD' || $payment_method =='cod'){
             $walletamt = 0;    
            
             $payment_status="COD";
            if($wallet == 'yes' || $wallet == 'Yes' || $wallet == 'YES'){
             if($ph->wallet >= $prii){
                $rem_amount = 0; 
                $walletamt = $prii; 
                $rem_wallet = $ph->wallet-$prii;
                $walupdate = DB::table('users')
                           ->where('user_id',$user_id)
                           ->update(('wallet'=>$rem_wallet));
                $payment_status="success";           
                $payment_method = "wallet";   
                $payment_id = $cart_id;
                $payment_gateway = "wallet";
             }
             else{
                
                $rem_amount= $prii - $ph->wallet;
                $walletamt = $ph->wallet;
                $rem_wallet = 0;
                $walupdate = DB::table('users')
                           ->where('user_id',$user_id)
                           ->update(('wallet'=>$rem_wallet));
             }
         }
         else{
             $rem_amount=  $prii;
             $walletamt= 0;
         }
       
          $oo = DB::table('orders')
           ->where('cart_id',$cart_id)
            ->update((
            'paid_by_wallet'=>$walletamt,
            'rem_price'=>$rem_amount,
            'payment_status'=>$payment_status,
            'payment_method'=>$payment_method
            )); 
             
            $sms = DB::table('notificationby')
                       ->select('sms')
                       ->where('user_id',$user_id)
                       ->first();
            $sms_status = $sms->sms;
            
                if($sms_status == 1){
                    $orderplacedmsg = $this->ordersuccessfull($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_phone);
                    
            
                }
                
                
                
                
                
                      /////send mail
            $email = DB::table('notificationby')
                   ->select('email','app')
                   ->where('user_id',$user_id)
                   ->first();
             $q = DB::table('users')
                              ->select('user_email','user_name','device_id')
                              ->where('user_id',$user_id)
                              ->first();
            $user_email = $q->user_email;             
            $device_id = $q->device_id;     
            $user_name = $q->user_name;       
            $email_status = $email->email;       
            if($email_status == 1){
                   
                    $codorderplaced = $this->codorderplacedMail($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name);
                    
                    
               }
               
            
      
               
                 ///////send notification to User//////
             if($email->app ==1){
                 
                  $codorderplaced = $this->codorderplacedinapp($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name,$user_id,$device_id);
                    
                     
                 
             }  
                $orderr1 = DB::table('orders')
                       ->where('cart_id', $cart_id)
                       ->first();   
           
                ///////send notification to store//////
                $getD = DB::table('store')
                         ->where('store_id', $store_id)
                        ->first();
                        
                $store_n = $getD->store_name; 
             if($getD){
               
                   $store_phone = $getD->phone_number;
                   $store_email = $getD->email;
                     $orderplacedmsgstore = $this->ordersuccessfullstore($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$store_phone);
                     $codorderplacedstore = $this->codorderplacedMailstore($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name, $store_n,$store_email);
                     
                      $codorderplacedstore = $this->codorderplacedinappstore($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name,$store_n,$store_id);
                 }
         $admin = DB::table('admin')
                ->first();
          $admin_email = $admin->admin_email;
          $admin_name = $admin->admin_name;
             $codorderplacedadmin = $this->codorderplacedMailadmin($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name,$store_n,$admin_email,$admin_name); 
               
                               
            $delete = DB::table('store_orders')
                           ->where('store_approval',$user_id)
                           ->where('order_cart_id', 'incart')
                           ->delete();
            $message = array('status'=>'1', 'message'=>'Order Placed successfully', 'data'=>$orderr1 );
            return $message;   
        }
       
        else{
        $walletamt = 0;    
        $prii = $price2 + $charge;
        if($request->wallet == 'yes' || $request->wallet == 'Yes' || $request->wallet == 'YES'){
             if($ph->wallet >= $prii){
                $rem_amount = 0; 
                $walletamt = $prii; 
                $rem_wallet = $ph->wallet - $prii;
                $walupdate = DB::table('users')
                           ->where('user_id',$user_id)
                           ->update(('wallet'=>$rem_wallet));
                $payment_status="success";           
                $payment_method = "wallet";
                $payment_id =$cart_id;
                $payment_gateway = "wallet";
                
             }
             else{
                 
                $rem_amount=  $prii-$ph->wallet;
                $walletamt = $ph->wallet;
                $rem_wallet =0;
                $walupdate = DB::table('users')
                           ->where('user_id',$user_id)
                           ->update(('wallet'=>$rem_wallet));
             }
         }
          else{
              $rem_amount=  $prii;
              $walletamt = 0;
          }
        if($payment_status=='success'){
            
          
            $oo = DB::table('orders')
           ->where('cart_id',$cart_id)
            ->update((
            'paid_by_wallet'=>$walletamt,
            'rem_price'=>$rem_amount,
            'payment_method'=>$payment_method,
            'payment_status'=>'success'
            )); 
            
         $payments = DB::table('cart_payments')
            ->insert((
            'cart_id'=>$cart_id,
            'amount'=>$rem_amount,
            'payment_gateway'=>$payment_gateway,
            'payment_id'=>$payment_id,
            'created_at'=>Carbon::now(),
            'updated_at'=>Carbon::now()
            )); 
            
            $sms = DB::table('notificationby')
                       ->select('sms')
                       ->where('user_id',$user_id)
                       ->first();
            $sms_status = $sms->sms;
                if($sms_status == 1){
                    
                /////send sms/////    
                $codorderplaced = $this->ordersuccessfull($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_phone);
                }
                      /////send mail
            $email = DB::table('notificationby')
                   ->select('email','app')
                   ->where('user_id',$user_id)
                   ->first();
            $email_status = $email->email;
             $q = DB::table('users')
                  ->select('user_email','user_name')
                  ->where('user_id',$user_id)
                  ->first();
            $user_email = $q->user_email;     
            $user_name = $q->user_name;
            if($email_status == 1){
                   
                     ///sending mails//    
                    $orderplaced = $this->orderplacedMail($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name);
                 
               }
            if($email->app == 1){
                 ///////send notification to User//////
                 $codorderplaced = $this->codorderplacedinapp($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name,$user_id);
                
                 
             } 
            $orderr1 = DB::table('orders')
           ->where('cart_id', $cart_id)
           ->first();
           
              ///////send notification to store//////
                $getD = DB::table('store')
                         ->where('store_id', $store_id)
                        ->first();
                        
                $store_n = $getD->store_name;   
                
                
               
                                
            $delete = DB::table('store_orders')
                           ->where('store_approval',$user_id)
                           ->where('order_cart_id', 'incart')
                           ->delete();
    
    
             if($getD){
               
                   $store_phone = $getD->phone_number;
                   $store_email = $getD->email;
                     $orderplacedmsgstore = $this->ordersuccessfullstore($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$store_phone);
                     $codorderplacedstore = $this->codorderplacedMailstore($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name, $store_n,$store_email);
                     
                      $codorderplacedstore = $this->codorderplacedinappstore($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name,$store_n,$store_id);
                 }
         $admin = DB::table('admin')
                ->first();
          $admin_email = $admin->admin_email;
          $admin_name = $admin->admin_name;
             $codorderplacedadmin = $this->codorderplacedMailadmin($cart_id,$prod_name,$price2,$delivery_date,$time_slot,$user_email,$user_name,$store_n,$admin_email,$admin_name); 
            $message = array('status'=>'2', 'message'=>'Order Placed successfully', 'data'=>$orderr1 );
            return $message; 
         }
         else{
              $oo = DB::table('orders')
           ->where('cart_id',$cart_id)
            ->update((
            'paid_by_wallet'=>0,
            'rem_price'=>$rem_amount,
            'payment_method'=>NULL,
            'payment_status'=>'failed'
            ));  
            $message = array('status'=>'0', 'message'=>'Payment Failed');
            return $message;
         }
      }
    }