<?php 	
    include_once('../config.php');                                    
                                   
                                         
                                        $stmt =  $conn->prepare("select * from  UI__Rgst  where  cuurentId=:cuurentId     ");   
    									$stmt->bindValue(':cuurentId', $_SESSION['userId'] , PDO::PARAM_STR); 
    									$stmt->execute();  
    									$phm = $stmt->fetch(PDO::FETCH_OBJ);   
    								 
									          
    								    $stmt =  $conn->prepare("select accName from  UI__AddAccount  where  custId=:custId  and accno=:accno and  ifsc=:ifsc   ");   
										$stmt->bindValue(':custId', $_SESSION['userId'] , PDO::PARAM_STR);
										$stmt->bindValue(':accno', $_POST['accountNo'] , PDO::PARAM_STR);
										$stmt->bindValue(':ifsc', $_POST['ifsc'] , PDO::PARAM_STR);
    									$stmt->execute(); 
    									$AccDetails = $stmt->fetch(PDO::FETCH_OBJ);  
    									
    									// 
									 
										$beneficiary_name =  $AccDetails->accName;   
                                        $Remail =  "RdVFLtRb83jLrxM5mafitjRW6Gp4zbfiwYXfhr6FEg6n3w4RQXfC9pNkhEMd";
                                        $email =  $phm->eml; 
                                        $customer_mobile =  $phm->phm; 
                                        $amount = ($_POST['amount']-$deduction) ; 
                                        $ifsc_code =  $_POST['ifsc'];
                                        $ClientId =  mt_rand(100000, 999999);  
                                        $account_number = $_POST['accountNo'];  
                                        $access_token = '{"api_token":"'.$Remail.'","email":"'.$email.'","beneficiary_name":"'.$beneficiary_name.'","mobile_number":"'.$customer_mobile.'","amount":"'.$amount.'","ifsc_code":"'.$ifsc_code.'","client_id":"'.$ClientId.'","account_number":"'.$account_number.'","channel_id":"2"}';
                                         
                                        
                                        
                                        $ch = curl_init('https://grappay.com/api/payout/v2/transfer-now');                                             
                                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
                                        curl_setopt($ch, CURLOPT_POSTFIELDS, $access_token);                                                                  
                                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
                                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                                        'x-tera-tenant-region: IN', 
                                        'Content-Type: application/json', 
                                        'Content-Length: ' . strlen($access_token))                                                                       
                                        );       																		 
                                        $response = curl_exec($ch);  
                                         
                                        
                                        $stmt=$conn->prepare("insert into UI__MoneY(Uid,response,Tdate,request) values(:Uid,:calBack,:Tdate,:request)"); 
                                        $stmt->bindValue(':Uid',  $_SESSION['userId'] , PDO::PARAM_STR);    
                                        $stmt->bindValue(':calBack', $response.":: GrepPay" , PDO::PARAM_INT);    
                                        $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);     
                                        $stmt->bindValue(':request',  $access_token , PDO::PARAM_STR);     
                                        
                                        
                                        $stmt->execute();  
                                        $Data = json_decode($response);  
                                       
                                       
                                        if($Data->utr)
                                            $UTR = $Data->utr;
                                        else
                                            $UTR = "";  
                                        
                                        if (array_key_exists("status_id",$Data))
                                        {
                                        if(($Data->status_id==0) || ($Data->status_id==1))
                                        {
                                        $moneytransfer_status='Success';
                                        $status=1;
                                        $_SESSION['moneytransfer_success'.$rand_no]=1;
                                        }
                                        else if(($Data->status_id==2) || ($Data->status_id==4))
                                        {
                                        $moneytransfer_status='Fail';
                                        $status=0;   
                                        }
                                        else if($Data->status_id==3)
                                        {
                                        $moneytransfer_status='Pending'; 
                                        $status=2; 
                                        $UTR = "Sys_".rand(34345,134423);
                                        }  
                                        
                                        if(!empty($Data->orderid))
                                        {
                                        $orderid=$Data->orderid;   
                                        $Dataorderid = $Data->orderid;
                                        $Datareport_id = $Data->report_id;
                                        }else 
                                        {
                                        $errorMsg = $Data->message;
                                        $Dataorderid = '';
                                        $Datareport_id = '';
                                        $orderid = ''; 
                                        }
                                        
                                        }else
                                        {
                                        $moneytransfer_status='Fail';
                                        $status=0;  
                                        $errorMsg = "Error in transaction";
                                        $Dataorderid = '';
                                        $Datareport_id = '';
                                        $orderid = '';    
                                        }  
                                         
                                        $status = 0; 
                                        if($Data->status=='pending')  
                                        {
                                              $UTR = "Sys_".rand(34345,134423);
                                              $statusUp = 0;
                                              $status = 1;
                                        }
                                              
                                        if( ($status==1) || ($status==2) ) 
                                        {
                                            $Datareport_id = mt_rand(3455456,45455);
                                        }else
                                            $Datareport_id = mt_rand(3455456,45455);
                                            
                                           
                                            
                                        if($status!=0) 
                                        {     
                                            $stmt=$conn->prepare("insert into UI__TxnFailUsersPay2(custId,Tdate,amt,type,accno,ifsc,holder,cdId,UTR,acceptStats,paidDateTime,client_id,impnft,status) values(:custId,:Tdate,:amt,:type,:accno,:ifsc,:holder,:cdId,:UTR,:acceptStats,:paidDateTime,:client_id,1,:status) ");    
                                            $stmt->bindValue(':custId',  $_SESSION['userId'], PDO::PARAM_STR);
                                            $stmt->bindValue(':Tdate',  date("d-M-Y H:i"), PDO::PARAM_STR);
                                            $stmt->bindValue(':amt',  ($_POST['amount']-$deduction) , PDO::PARAM_STR);
                                            $stmt->bindValue(':type',  1, PDO::PARAM_STR);  
                                            $stmt->bindValue(':accno',  $_POST['accountNo'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':ifsc',  $_POST['ifsc'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':holder',  $_POST['rname_'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':cdId',  $_SESSION['LastIntId'] , PDO::PARAM_STR);
                                            $stmt->bindValue(':UTR',  $UTR , PDO::PARAM_STR);
                                            $stmt->bindValue(':acceptStats',  date("d-M-Y H:i") , PDO::PARAM_STR);
                                            $stmt->bindValue(':paidDateTime',  date("d-M-Y H:i") , PDO::PARAM_STR);
                                            $stmt->bindValue(':client_id',  $ClientId , PDO::PARAM_STR);  
                                            $stmt->bindValue(':status',  $statusUp , PDO::PARAM_STR);
                                            $stmt->execute();    
                                        }else
                                        {
                                            
                                            $stmt=$conn->prepare("insert into UI__TxnFailUsersPay2(custId,Tdate,amt,type,accno,ifsc,holder,cdId,client_id,impnft) values(:custId,:Tdate,:amt,:type,:accno,:ifsc,:holder,:cdId,:client_id,1) ");    
                                            $stmt->bindValue(':custId',  $_SESSION['userId'], PDO::PARAM_STR);
                                            $stmt->bindValue(':Tdate',  date("d-M-Y H:i"), PDO::PARAM_STR);
                                            $stmt->bindValue(':amt',  ($_POST['amount']-$deduction) , PDO::PARAM_STR);
                                            $stmt->bindValue(':type',  1, PDO::PARAM_STR);  
                                            $stmt->bindValue(':accno',  $_POST['accountNo'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':ifsc',  $_POST['ifsc'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':holder',  $_POST['rname_'] , PDO::PARAM_STR);  
                                            $stmt->bindValue(':cdId',  $_SESSION['LastIntId'] , PDO::PARAM_STR); 
                                            $stmt->bindValue(':client_id',  $ClientId , PDO::PARAM_STR);  
                                            $stmt->execute();  
                                        }