<?php

//https://getway.dreamludo.site/payment/widthdaw.php?user_token=e8d2a2f1ac98d41d3b7422fd11ab98fa&note=&order_id=7678006786&amount=10&Ac_or_upi=858947589765&type=UPI&name=sa&ifsc=879



if(isset($_POST['user_token']) or isset($_POST['note']) or isset($_POST['order_id']) or isset($_POST['amount']) or isset($_POST['Ac_or_upi']) or isset($_POST['type']) or isset($_POST['name'])){
    
        
 $name=$_POST['name'];
 $type=$_POST['type'];
  $Ac_or_upi=$_POST['Ac_or_upi'];
$ifsc=$_POST['ifsc'];
$order_id=$_POST['order_id'];
$amount=$_POST['amount'];
$user_token=$_POST['user_token'];
$note=$_POST['note'];
$t=time();
 $tnx_id=rand(100000,999999999999999).$t;
$nuller="";
    $sizelim=25;
    if(strlen($user_token)>100 or strlen($note)>$sizelim or strlen($order_id)>$sizelim or strlen($ifsc)>$sizelim or strlen($Ac_or_upi)>$sizelim or strlen($type)>$sizelim or strlen($name)>$sizelim){
           echo '{
  "status": "ERR",
  "msg": "Parameter Size Too Long"
}';
    }else{
         if(trim($user_token)=="" or trim($order_id)=="" or trim($Ac_or_upi)=="" or trim($type)=="" or trim($name)==""){
                  echo '{
  "status": "ERR",
  "msg": "Parameter null passed2 "
}';
             
         }else{
      
      $conn = mysqli_connect("localhost", "nuahkbzh_upi", "nuahkbzh_upi", "nuahkbzh_upi");

  $sql = "SELECT * FROM `users` where user_token='$user_token'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
         $current=$row['wbal'];
     }
$server = $_SERVER["SERVER_NAME"];
      
    
    
$query = "SELECT * FROM `reports` where order_id ='$order_id' ";
$query_run = mysqli_query($conn, $query);



  if ($query_run->num_rows > 0) {
   
      echo '{
  "status": "ERR",
  "msg": "Order_id Already Used"
}';
        
        
    }else{
        if($type=="UPI" or $type=="BANK"){
        if($amount>=10){
            if($current>=$amount){
            $quertyed="UPDATE `users` SET `wbal` = wbal-'$amount' WHERE `users`.`user_token` = '$user_token'";
 $query2 = "INSERT INTO `reports` (`id`, `transactionId`, `status`, `order_id`, `ifsc`, `Accountnop`, `type`, `amount`, `user_token`, `UTR`, `description`, `date`, `Accountname`, `user_name`, `transactionNote`, `user_id`) VALUES (NULL, '$tnx_id', 'Pending', '$order_id', '$ifsc', '$Ac_or_upi', '$type', '$amount', '$user_token', '$tnx_id', 'ewdwsxdsd', '$t', '$name', '$name', '$note', '')";   
        $results = $conn->query($query2);   
          $results = $conn->query($quertyed); 
          if($results){
               echo '{
  "status": "SUCCESS",
  "msg": "Order Succesfully"
}';
          }
          
        
            }else{
                 echo '{
  "status": "ERR",
  "msg": "Wallat Amount is low"
}';
            }
        }else{
              echo '{
  "status": "ERR",
  "msg": "Amount Greater Than  10"
}';
        }
    }else{
          echo '{
  "status": "ERR",
  "msg": "Wrong Type Parameter"
}';
    }
    }
}else{
     echo '{
  "status": "ERR",
  "msg": "Inviled Access Tocken"
}';
    
    
}
        
    }
    
    
    }
    
    
    
    
    
  
}else{
  echo '{
  "status": "ERR",
  "msg": "Parameter null passed"
}';
    
}








?>