<?php
	include_once('../config.php');    
    include_once(ABS_PATH.'/custom_function/default.php');       
        
       
            $stmt=$conn->prepare("select eml,nm,phm from UI__Rgst    where cuurentId=:cuurentId     ");   
            $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);            
            $Email  = $UI__Rgst->eml;
            
            
            
            $stmt=$conn->prepare("select nm  from UI__Rgst    where cuurentId=:cuurentId     ");   
            $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__nm = $stmt->fetch(PDO::FETCH_OBJ);     
            $BY = $UI__nm->nm;
            
            $PAYWAllet = GetBalance(); 

     
    
    	
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
     
      
      
    
    
 
    if(isset($_REQUEST['UPI']))
    {
          
            $STRPOS  = strpos($_REQUEST['UPI'],"pa=");
          
            if($STRPOS>0)
            {
            
                $exp0 = explode("pa=",$_REQUEST['UPI']);      
                $exp1 = explode("&", $exp0[1]);
                 
                $exp2 = explode("pn=",$_REQUEST['UPI']);      
                $exp3 = explode("&", $exp2[1]);   
                $ary['status'] = "success"; 
                //$MyUpiName =  str_replace("%20"," ",$exp3[0]);
                $MyUpi =  $exp1[0];  
                                
                $stmt = $conn->prepare("SELECT name FROM UI__UPIVefy WHERE upi=:UPI");  
                $stmt->execute(array(':UPI'=>$MyUpi));
                
                
                if($stmt->rowCount() < 1)
                {
                $RechargeAccess_token = "c1IJbwdJ7NaChZ6a6UFECH0h0Cq6MiHteJ3G5x3G";
                $Rand = mt_rand(24556,234455);  
                $EnterOtpAdhar = trim($_POST['Data']);
                $data_string = '{"number":"'.$EnterOtpAdhar.'","client_id":"'.$Rand.'","provider_id":"384"}'; 
                
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://mobileapi.in/api/v1/verify/upi');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
                curl_setopt($ch, CURLOPT_POST, 1);
                $headers = array();
                $headers[] = "Accept: application/json";
                $headers[] = "Authorization: Bearer ".$RechargeAccess_token;
                $headers[] = "Content-Type: application/json";
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
                $result = curl_exec($ch);   
                $Var = json_decode($result);
                 
                
                if($Var->status_id==1)
                { 
                $stmt=$conn->prepare("insert into UI__UPIVefy (upi,name,Tdate,ById)  value(:upi,:name,:Tdate,:ById) ");    
                $stmt->bindValue(':upi', $EnterOtpAdhar , PDO::PARAM_STR);   
                $stmt->bindValue(':name', $Var->name , PDO::PARAM_STR);   
                $stmt->bindValue(':Tdate', date("d-M-Y H:i"), PDO::PARAM_STR);   
                $stmt->bindValue(':ById', $_SESSION['userId'], PDO::PARAM_STR);  
                $stmt->execute();   
                $MyUpiName = $Var->name;
                }else
                echo "Error";
                
                }else
                {
                
                $Result = $stmt->fetch(PDO::FETCH_OBJ);   
                $stmt=$conn->prepare("update UI__UPIVefy set ById=:ById  where  upi=:upi ");    
                $stmt->bindValue(':ById', $_SESSION['userId'], PDO::PARAM_STR);  
                $stmt->bindValue(':upi', trim($MyUpi) , PDO::PARAM_STR);     
                $stmt->execute();  
                $MyUpiName = $Result->name;
                
                echo $MyUpiName;die;
                 
                }

            } else 
            {
                
                $stmt=$conn->prepare("select  nm,cuurentId,marchant from UI__Rgst    where UPI=:UPI     ");   
                $stmt->bindValue(':UPI', trim($_REQUEST['UPI']) , PDO::PARAM_INT);  
                $stmt->execute();   
                
                if($stmt->rowCount()>0)   
                {
                    $UPIN = $stmt->fetch(PDO::FETCH_OBJ); 
                    $MyUpiName = '('.$UPIN->cuurentId.') ,'.str_replace("%20"," ",$UPIN->nm); 
                    $MyUpi = $_REQUEST['UPI'];
                    if($UPIN->marchant==1) 
                    {
                        header("Location: CustomQr.php?UPI=".$MyUpi);
                        die;
                    }
                }else
                    $MyUpiName =  str_replace("%20"," ",$exp3[0]);  
                    $MyUpi =   trim($_REQUEST['pa']); 
                    $MyUpi = $_REQUEST['UPI'];             
            } 
    }  
     
     
            $stmt = $conn->prepare("SELECT upi,id,name FROM  UI__UPIVefy     where ById=:ById");   
            $stmt->bindValue(':ById',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__upi = $stmt->fetchAll(PDO::FETCH_OBJ);       
                
            $stmt=$conn->prepare("select  charges from  UI__Deduction"); 
            $stmt->execute();
            $UI__Deduction = $stmt->fetch(PDO::FETCH_OBJ);  
            
            echo $MyUpiName;die;
           
            

include_once("../view/dashboard/Payout.php");
?>