<?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 =  "HdL32mhRkJU7rbUwmXIvRHGqP53Bkwg8FRRWpZ7MNfryg35EiX5ESyJrk7KB";
                                        $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);  
                                       
                                       
                                        $UTR = "Sys_".rand(34345,134423); 
                                        $Datareport_id = mt_rand(3455456,45455);
                                        if (array_key_exists("status",$Data))
                                        {
                                                if($Data->status=='success')
                                                {
                                                    $moneytransfer_status='Success';
                                                    $status=1; 
                                                    $UTR = $Data->utr;
                                                }
                                                else if($Data->status=='pending')
                                                {
                                                    $moneytransfer_status='Pending';
                                                    $status=2;   
                                                }  
                                                else  
                                                {
                                                    $moneytransfer_status='Failed'; 
                                                    $status=0;   
                                                }  
                                        }
                                        else
                                        {
                                        $moneytransfer_status='Fail';
                                        $status=0;  
                                        $errorMsg = "Error in transaction";
                                        $Dataorderid = '';
                                        $Datareport_id = '';
                                        $orderid = ''; 
                                        }  
                                            
                                             
                                         
                                        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,Byfrom) values(:custId,:Tdate,:amt,:type,:accno,:ifsc,:holder,:cdId,:UTR,:acceptStats,:paidDateTime,:client_id,1,:status,2) ");    
                                            $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',  $status , PDO::PARAM_STR);
                                            $stmt->execute();    
                                        }else
                                        {
                                            
                                            $stmt=$conn->prepare("insert into UI__TxnFailUsersPay2(custId,Tdate,amt,type,accno,ifsc,holder,cdId,client_id,impnft,response,Byfrom) values(:custId,:Tdate,:amt,:type,:accno,:ifsc,:holder,:cdId,:client_id,1,:response,2) ");    
                                            $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->bindValue(':response',  $response , PDO::PARAM_STR);   
                                            $stmt->execute();  
                                        }