<?php
	include_once('../config.php');   
    include_once(ABS_PATH.'/custom_function/default.php');       
          
          
            
        
                 $stmt=$conn->prepare("select *  from UI__WithDraw    where userid =:sId   ");   
                $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
                $stmt->execute(); 
                $UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 
                
                foreach($UI__WithDraw as $mttt) 
                {   
                    
                    if($mttt->accept==2) 
                    {
                        $URL  = "https://api.bharatpays.in/api/upi_payout/check_status?token=2d62cd8f089f626687bbb4dd064f58f0&order_id=".$mttt->order_id;
                        
                        $data_string = ""; 
                        $ch = curl_init($URL); 
                        //$ch = curl_init("https://api.bharatpays.in/api/va/create_account?token=ea2c6d7268cbbd22d479f661c45935de&name=DailyPay&primary_contact=DailyPay&contact_type=Customer&email_id=vsn.national10@gmail.com&mobile_number=9970099701"); 
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                    
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                        'x-tera-tenant-region: IN', 
                        'Content-Type: application/json',
                        'Content-Length: '.strlen($data_string))                                                                      
                        );      
                        $response = curl_exec($ch);    
                        $Res = json_decode($response);
                        
                        if($Res->data->status=='FAILED')
                        {      
                            $stmt=$conn->prepare("update  UI__WithDraw set accept = 0 where order_id=:order_id and userid=:userid and status='FAILED' ");   
                            $stmt->bindValue(':order_id', $mttt->order_id, PDO::PARAM_STR);  
                            $stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_STR);  
                            $stmt->execute(); 
                            
                            $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept)"); 
                            $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                            $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                            $stmt->bindValue(':amt', $mttt->amt, PDO::PARAM_STR);
                            $stmt->bindValue(':orgAmt', $mttt->orgAmt , PDO::PARAM_STR); 
                            $stmt->bindValue(':order_id', "NA" , PDO::PARAM_STR); 
                            $stmt->bindValue(':bank_remark', "NA" , PDO::PARAM_STR); 
                            $stmt->bindValue(':status', "REFUNDED" , PDO::PARAM_STR); 
                            $stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
                            $stmt->execute();    
                        }else
                        {
                            $stmt=$conn->prepare("update  UI__WithDraw set accept = 1 where order_id=:order_id and userid=:userid and status='SUCCESSED' ");   
                            $stmt->bindValue(':order_id', $mttt->order_id, PDO::PARAM_STR);  
                            $stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_STR);  
                            $stmt->execute();  
                        }   
                    }
                } 
                
        
        $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 *  from UI__DailyROIGene    where custId=:sId     ");   
        $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
        $stmt->execute(); 
        $XUI__DailyROIGene = $stmt->fetchAll(PDO::FETCH_OBJ);  
        
        $Tamnt = 0;
        foreach($XUI__DailyROIGene as $xx)
        {
            $Tamnt +=$xx->amount;
        }      
        
        
         $stmt=$conn->prepare("select *  from UI__Bns    where Rid =:sId   ");   
            $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
            $stmt->execute(); 
            $UI__Bns = $stmt->fetchAll(PDO::FETCH_OBJ); 
            
             $LevelI = 0;
             foreach($UI__Bns as $dddm)
             {
                 $LevelI +=$dddm->Tamt;
             }
             
     
                $stmt=$conn->prepare("select Amt   from UI__Tpm where TopUpId=:refId   ");   
                $stmt->bindValue(':refId',  $_SESSION['userId'] , PDO::PARAM_INT);  
                $stmt->execute(); 
                $MdirectB = $stmt->fetchAll(PDO::FETCH_OBJ);  
                
                $directIncome = 0;
                foreach($MdirectB as $dmm)
                    $directIncome += $dmm->Amt;                
            
            
        
        
        $stmt=$conn->prepare("select *  from UI__WithDraw    where userid=:sId and (accept=1 ||accept=2)  ");   
        $stmt->bindValue(':sId',  $_SESSION['userId'] , PDO::PARAM_INT);  
        $stmt->execute(); 
        $UI__WithDraw = $stmt->fetchAll(PDO::FETCH_OBJ); 
        
        
        $totalWithdrwa = 0;
        foreach($UI__WithDraw as $tl)
        {
        $totalWithdrwa +=$tl->amt;
        }
        
        
        
        
        $stmt=$conn->prepare("select availableFund from UI__Fundtransfer  where  userid=:userid  order by id desc  ");   
        $stmt->bindValue(':userid',  $_SESSION['userId'] , PDO::PARAM_INT);  
        $stmt->execute(); 
        $UI__Fundtransfer = $stmt->fetch(PDO::FETCH_OBJ); 
        
         
            
    $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 =  ($Tamnt+$LevelI+$directIncome+$UI__Fundtransfer->availableFund)-($totalWithdrwa);        
    	
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
    
    if(isset($_POST['submit'])) 
    {  
           
                
                
                if(($PAYWAllet)>=$_POST['AMMT'])
                {    
                 
                 
                $rec_mob  = $UI__Rgst->phm;
                $rec_amt  = $_POST['AMMT'];
                $rec_ref = rand(23234,4546);
                $vpa = $_POST['UPI'];
                $EMAIL = $UI__Rgst->eml;
                $nm = explode(" ",$UI__Rgst->nm);
                $URL  = "https://api.bharatpays.in/api/upi_payout/create_payout?token=2d62cd8f089f626687bbb4dd064f58f0&vpa=".$vpa."&recepient_name=".$nm[0]."&email_id=".$EMAIL."&mobile_no=".$rec_mob."&amount=".$rec_amt."&reference_id=".$rec_ref;
                   
                $data_string = ""; 
				$ch = curl_init($URL);
				//$ch = curl_init('https://api.bharatpays.in/api/upi_payout/check_status?token=ea2c6d7268cbbd22d479f661c45935de&order_id=849952882220');
				//$ch = curl_init("https://api.bharatpays.in/api/va/create_account?token=ea2c6d7268cbbd22d479f661c45935de&name=DailyPay&primary_contact=DailyPay&contact_type=Customer&email_id=vsn.national10@gmail.com&mobile_number=9970099701"); 
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                    
				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
				curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
				'x-tera-tenant-region: IN', 
				'Content-Type: application/json',
				'Content-Length: '.strlen($data_string))                                                                      
				);      
				$response =   curl_exec($ch);    
				$Res1 = explode("</div>",$response);   
				$Res = json_decode(end($Res1));
				 
                $stmt=$conn->prepare("insert into UI__PayoutResponse(response,Tdate,UPI) values(:response,:Tdate,:UPI)"); 
                $stmt->bindValue(':response',   $response , PDO::PARAM_STR);   
                $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);    
                $stmt->bindValue(':UPI',  $vpa, PDO::PARAM_STR);    
                $stmt->execute();   
                 
                $accept = 0; 
				if($Res->success==1) 
				{
				     if($Res->data->status=='PENDING') 
				     {
				         $accept = 2; 
				         $_SESSION['Regis'] = "Payment Pending!!"; 
				     }
				    else 
				      { 
			            $accept = 1;
			            $_SESSION['Regis'] = "Payment  Success";     
				      } 
				    
				if($Res->data->bank_remark=='')
				    $Bremakr = "NA";
				else
				    $Bremakr = $Res->data->bank_remark;
				    
                $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt,order_id,bank_remark,status,accept) values(:userid,:Tdate,:amt,:orgAmt,:order_id,:bank_remark,:status,:accept)"); 
                $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);   
                $stmt->bindValue(':amt', $_POST['AMMT'] , PDO::PARAM_STR);
                $stmt->bindValue(':orgAmt', $_POST['UPI'] , PDO::PARAM_STR); 
                $stmt->bindValue(':order_id', $Res->data->order_id , PDO::PARAM_STR); 
                $stmt->bindValue(':bank_remark', $Bremakr, PDO::PARAM_STR); 
                $stmt->bindValue(':status', $Res->data->status , PDO::PARAM_STR); 
                $stmt->bindValue(':accept', $accept , PDO::PARAM_STR);      
                $stmt->execute();      
                $_SESSION['Regis'] = "Payment Successful!!";
                $_SESSION['topSuc'] = 1;
                
                $subject = "Payout Successful  On ".$SITE_NAME;  
                $template_type = 1;
                $attachment = '';
                $name = $_POST['UPI'];  
                $Amt = $_POST['AMMT'];
                
                $message="
                <tr>
                <td align='left' valign='top'>
                <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for payout with us. Your Details are as follows </h5> 
                <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> To: $name , Amount Rs : $Amt </h5>
                
                </td>
                </tr>";     
                SendMail($Email,$subject,$message,$template_type,$attachment);         
               
               
				}else {  
				    $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt) values(:userid,:Tdate,:amt,:orgAmt)"); 
                    $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                    $stmt->bindValue(':Tdate',  date("d-M-y H:i") , PDO::PARAM_STR);   
                    $stmt->bindValue(':amt', $_POST['AMMT'] , PDO::PARAM_INT);
                    $stmt->bindValue(':orgAmt', $_POST['UPI'] , PDO::PARAM_INT); 
                    $stmt->execute();       
                    $_SESSION['Regis'] = "Payment Failed"; 
                    
                    $subject = "Payout Faild  On ".$SITE_NAME;  
                    $template_type = 1;
                    $attachment = '';
                    $name = $_POST['UPI'];  
                    $Amt = $_POST['AMMT'];
                    
                    $message="
                    <tr>
                    <td align='left' valign='top'>
                    <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                    <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for payout with us. Your Details are as follows </h5> 
                    <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> To: $name , Amount Rs : $Amt </h5>
                    
                    </td>
                    </tr>";     
                    SendMail($Email,$subject,$message,$template_type,$attachment);
                    
				} 
                header("Location: Payout.php"); 
                die;
                }
                else 
                {    
                    $stmt=$conn->prepare("insert into UI__WithDraw(userid,Tdate,amt,orgAmt) values(:userid,:Tdate,:amt,:orgAmt)"); 
                    $stmt->bindValue(':userid',   $_SESSION['userId']  , PDO::PARAM_STR);   
                    $stmt->bindValue(':Tdate',  date("d-M-y H:i") , PDO::PARAM_STR);   
                    $stmt->bindValue(':amt', $_POST['AMMT'] , PDO::PARAM_INT);
                    $stmt->bindValue(':orgAmt', $_POST['UPI'] , PDO::PARAM_INT); 
                    $stmt->execute();       
                    
                    $subject = "Payout Faild  On ".$SITE_NAME;  
                    $template_type = 1;
                    $attachment = '';
                    $name = $_POST['UPI'];  
                    $Amt = $_POST['AMMT'];
                    
                    $message="
                    <tr>
                    <td align='left' valign='top'>
                    <h4 style='font-weight:bold; padding:2px 2px; margin:3px; text-align:left; color:#002368;'>Dear user,</h4>
                    <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'>Thanks for payout with us. Your Details are as follows </h5> 
                    <h5 style='font-weight:bold; padding:2px 2px; margin:0px; text-align:center; color:#002368;'> To: $name , Amount Rs : $Amt </h5>
                    
                    </td>
                    </tr>";     
                    SendMail($Email,$subject,$message,$template_type,$attachment);          
                    $_SESSION['Regis'] = "Payment Failed"; 
                    header("Location: Payout.php");
                die; 
                }
    }       
    
          
            
        
  
    if(isset($_REQUEST['UPI']))
    {
             
            $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]; 
    }
    
    
    
include_once("../view/dashboard/test.php");
?>