<?php
	include_once('../config.php');      
    include_once(ABS_PATH.'/custom_function/default.php'); 
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
                      
    if(isset($_POST['submit'])) 
    {              
         
					$PAYWAllet = GetBalance();  
                    if($PAYWAllet<$_POST['amount'])
                    {
                        $_SESSION['Regis'] = "Transaction Fail Due To Low Balance";
                        header("Location: Wllat_transf.php");
                        die; 
                    }else
                    {
                        $stmt=$conn->prepare("select txnpwd from UI__Rgst  where  cuurentId=:cuurentId    ");   
                        $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                        $stmt->execute(); 
                        $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);   
                        
                    }
                
                
                $evar = 0; 
                $stmt=$conn->prepare("select id from   UI__PanVeri where custId=:cuurentId");    
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
                $stmt->execute(); 
                $PanVer = $stmt->rowCount();
                
                
                $stmt=$conn->prepare("select id from   UI__AdharVeri where custId=:cuurentId");    
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
                $stmt->execute(); 
                $AdharVer = $stmt->rowCount();
                
                if( ($PanVer>0) &&  ($AdharVer>0))
                    $evar = 1;
                    
                
                $stmt=$conn->prepare("select id,amt from  UI__DChstr  where  Rid=:cuurentId   and typ=1  ");   
                $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                $stmt->execute(); 
                $UI__TopupFetch = $stmt->fetchAll(PDO::FETCH_OBJ);
                
                $pdata = 0;    
                foreach($UI__TopupFetch as $fcheck)
                    $pdata +=$fcheck->amt;
                    
                if($pdata<10000)   
                    $evar=1;   
                     
                    
                if(($_POST['amount']>=10) && ($_POST['amount']<=25000)  && (trim($UI__Rgst->txnpwd)==trim($_POST['pinno']))  && ($evar>0) ) 
                { 
                 
                    $txnid = mt_rand(234553,343553);
                    $amount= $_POST['amount'].".00";
                    $apisecret = "yZe6SQavyVPe4kVfMjebHnbfKTNPyFz98PJvKT6qgeRR4sNr"; 
                    $api_user_id= "z3dz5LVEYBw34AUXAcaUr7V8jNATpPcfbJpMfZbd48tJNzNA";
                    $concat = $amount."|".$api_user_id."|".$txnid."|".$apisecret; 
                    $hash = hash("sha512",$concat);    
                    
                    $mobile = $_POST['mobile']; 
                    $Operator = $_POST['Operator'];
                    $url = 'https://api.komparify.com/api/v2/recharge?search_type=voucher&number='.$mobile.'&operator='.$Operator.'&txnid='.$txnid.'&securehash='.$hash.'&decimal_value='.$amount.'&method=GET&api_user_id=z3dz5LVEYBw34AUXAcaUr7V8jNATpPcfbJpMfZbd48tJNzNA';   
                       
                    $crl = curl_init();
                    curl_setopt($crl, CURLOPT_URL, $url);
                    curl_setopt($crl, CURLOPT_FRESH_CONNECT, true);
                    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true); 
                    $response = curl_exec($crl);   
                    curl_close($crl);
    
                    //    $Res = '{ "error" : "false", "order": "true", "sid": "9762811448", "operator": "PayTM Wallet", "amount": "12", "decimal_amount" : "12.0", "type": "Voucher", "rechargetype": "R", "orderid": "17809-38277-02596", "txnid": "332750", "status":"Transaction In Progress" ,"deducted_amount" : 12.1416 ,"remaining_balance" : 1795.7168 }';
                    $Data = (array)json_decode($response);
    
                    if($Data['error']=='false') 
                    {
                        $stmt =  $conn->prepare("insert into UI__WalletTransferData (custId,TxnId,ph,status,orderId,Tdate) values(:custId,:TxnId,:ph,:status,:orderId,:Tdate)");            
                        $stmt->bindValue(':custId', $_SESSION['userId'], PDO::PARAM_STR);
                        $stmt->bindValue(':TxnId', $Data['txnid'], PDO::PARAM_STR);
                        $stmt->bindValue(':ph', $Data['sid'], PDO::PARAM_STR);
                        $stmt->bindValue(':status', $Data['status'], PDO::PARAM_STR);
                        $stmt->bindValue(':orderId', $Data['orderid'], PDO::PARAM_STR); 
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i"), 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', $_POST['amount'] , PDO::PARAM_STR);
                        $stmt->bindValue(':orgAmt', $_POST['amount'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':order_id',  $Data['orderid'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':bank_remark', "PA", PDO::PARAM_STR); 
                        $stmt->bindValue(':status', "NA" , PDO::PARAM_STR); 
                        $stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
                        $stmt->execute();            
                        LvBal($_SESSION['userId'],$_POST['amount'],2,"Wallet Transfer",$Data['sid']);    
                        
                        $stmt=$conn->prepare("select * from UI__Rgst  where  cuurentId=:cuurentId  ");   
                        $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                        $stmt->execute();
                        $UI__Rgst = $stmt->fetch(PDO::FETCH_OBJ);  
                        
                        
                        $stmt=$conn->prepare("insert into UI__Receipt(type,bank,mobile,email,amountPaid,Tid,status,Tdate,custId,cdId,Topay,TopayName) values(:type,:bank,:mobile,:email,:amountPaid,:Tid,:status,:Tdate,:custId,:cdId,:Topay,:TopayName)"); 
                        $stmt->bindValue(':type',   10 , PDO::PARAM_STR);   
                        $stmt->bindValue(':bank',  "Wallet Recharge" , PDO::PARAM_STR);   
                        $stmt->bindValue(':mobile', $UI__Rgst->phm, PDO::PARAM_STR);
                        $stmt->bindValue(':email', $UI__Rgst->eml , PDO::PARAM_STR); 
                        $stmt->bindValue(':amountPaid', $_POST['amount']   , PDO::PARAM_STR); 
                        $stmt->bindValue(':Tid', mt_rand(1234552,23555), PDO::PARAM_STR); 
                        $stmt->bindValue(':status', 1 , PDO::PARAM_STR); 
                        $stmt->bindValue(':Tdate', date("d-M-Y H:i") , PDO::PARAM_STR);      
                        $stmt->bindValue(':custId', $_SESSION['userId']  , PDO::PARAM_STR);  
                        $stmt->bindValue(':cdId', $_SESSION['LastIntId']  , PDO::PARAM_STR); 
                        $stmt->bindValue(':Topay',  $Data['sid'] , PDO::PARAM_STR); 
                        $stmt->bindValue(':TopayName',  $Data['sid']  , PDO::PARAM_STR);     
                        $stmt->execute(); 
                        
                        $_SESSION['Regis'] = "Transaction successful";
                        $_SESSION['topSuc'] = 1; 
                        
                        $stmt=$conn->prepare("select eml from UI__Rgst  where  cuurentId=:cuurentId    ");   
                        $stmt->bindValue(':cuurentId',  $_SESSION['userId'] , PDO::PARAM_STR);  
                        $stmt->execute(); 
                        $EMAIL = $stmt->fetch(PDO::FETCH_OBJ);    
    
    
                        $subject = "Wallet Transfer  On ".$SITE_NAME;  
                        $template_type = 1;
                        $attachment = '';
                        $name = $Data['sid'];
                        $Amt = $_POST['amount'];
                        
                        $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 Wallet Transfer 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->eml,$subject,$message,$template_type,$attachment); 
                        
                        
                    }else
                        $_SESSION['Regis'] = "Transaction Fail";  
                }
                else if($evar==0)
                    $_SESSION['Regis'] = "Please complete your E-KYC & Try Again"; 
                else
                    $_SESSION['Regis'] = "Amount should be between 10 and 25000"; 
                header("Location: Wllat_transf.php");
                die; 
    }
    
    $stmt=$conn->prepare("select * from  UI__Rgst where cuurentId=:cuurentId");    
    $stmt->bindValue(':cuurentId',  $_SESSION['userId'], PDO::PARAM_STR);     
    $stmt->execute(); 
    $Result = $stmt->fetch(PDO::FETCH_OBJ);   
    $PAYWAllet = GetBalance();
	

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