<?php 	
                                            
                                      
                                        $stmt =  $conn->prepare("select phm from  UI__Rgst  where  cuurentId=:cuurentId     ");   
    									$stmt->bindValue(':cuurentId', $_POST['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', $_POST['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);    
									
                                        $Remail =  "dailypay2024@gmail.com";
                                        $Rd =  "gp6ZiH";  
                                        $access_token = '{"email":"'.$Remail.'","password":"'.$Rd.'"}'; 
                                        $ch = curl_init('https://erp.pay2all.in/api/token'); 
                                        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); 
                                        $json_response = json_decode($response);    
                                        $RechargeAccess_token = $json_response->token;  
                                        $beneficiary_ifsc = $_POST['ifsc'];
                                         
                                           
                                        $client_id = mt_rand(100000, 999999);
                                        $amount=round($_POST['amount']-$deduction);
                                        $beneficiary_accountno = $account= $_POST['accountNo'];
                                        $beneficiary_name =  $AccDetails->accName;  
                                        
                                        if($beneficiary_accountno=="6445319288")
                                        $beneficiary_name = "AARTI KACHARE";       
												    
												    
                                        $data_string = '{"mobile_number": "'.$phm->phm.'","amount": "'.$amount.'","beneficiary_name": "'.$beneficiary_name.'","account_number": "'.$beneficiary_accountno.'","ifsc": "'.$beneficiary_ifsc.'","client_id": "'.$client_id.'","provider_id": "160","wallet_id":"0"}';        
                                          
                                         
                                        $ch = curl_init();
                                        curl_setopt($ch, CURLOPT_URL, 'https://erp.pay2all.in/api/v1/payout/bank_transfer');
                                        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);
                                        $res = curl_exec($ch); 
                                        
                                       
                                        $stmt=$conn->prepare("insert into UI__MoneY(Uid,response,Tdate) values(:Uid,:calBack,:Tdate)"); 
                                        $stmt->bindValue(':Uid',  $_POST['userid'] , PDO::PARAM_STR);    
                                        $stmt->bindValue(':calBack', $res , PDO::PARAM_INT);    
                                        $stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);     
                                        $stmt->execute();  
                                        $Data = json_decode($res); 
                                        
                                
                                       
                                        if($Data->utr)
                                        $UTR = $Data->utr;
                                        else
                                        $UTR = "XXXXX";  
                                        
                                        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 = "XXXXXX";
                                        }  
                                        
                                        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 = '';    
                                        }  
                                         
                                        if( ($status==1) || ($status==2) ) 
                                        {
                                            $Datareport_id = mt_rand(3455456,45455);
                                        }else
                                            $Datareport_id = mt_rand(3455456,45455);
                                            
                                      if(!empty($Data->report_id))
                                            $report_id = $Data->report_id;
                                        else
                                            $report_id = "NA";     
                                            
                                        if($status!=0) 
                                        {      
                                            
                                            if(!empty($Data->utr))
                                                $UTR = $Data->utr;  
                                           
                                            $stmt=$conn->prepare("insert into UI__TxnFailUsers(status,report_id,custId,Tdate,amt,type,accno,ifsc,holder,cdId,UTR,acceptStats,paidDateTime,impnft,Byfrom) values(:status,:report_id,:custId,:Tdate,:amt,:type,:accno,:ifsc,:holder,:cdId,:UTR,:acceptStats,:paidDateTime,1,1) ");    
                                            $stmt->bindValue(':status',  $status, PDO::PARAM_STR);
                                            $stmt->bindValue(':report_id',  $report_id, PDO::PARAM_STR);
                                            $stmt->bindValue(':custId',  $_POST['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->execute();  
                                            
                                        }else
                                        {  
                                            $stmt=$conn->prepare("insert into UI__TxnFailUsers(report_id,custId,Tdate,amt,type,accno,ifsc,holder,cdId,impnft,Byfrom) values(:report_id,:custId,:Tdate,:amt,:type,:accno,:ifsc,:holder,:cdId,1,1) ");    
                                            $stmt->bindValue(':report_id',  $report_id , PDO::PARAM_STR);
                                            $stmt->bindValue(':custId',  $_POST['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->execute();  
                                            
                                            
                                            $beneficiary_name = $_POST['rname_'];
                                            $mob = "9067815172";
                                            $iD = $_POST['userid'];
                                            $msg = urlencode('Dear '.$iD.' your withdrawal amount of Rs. '.$Amt.' has been CREDITED by Bank Transfer to '.$beneficiary_name.', DAILYPAY.'); 
                                            $URL  = 'http://mobicomm.dove-sms.com//submitsms.jsp?user=DailyPAY&key=d92eabc025XX&mobile=+91'.$mob.'&message='.$msg.'&senderid=DAYPAY&accusage=1&entityid=1201169824017740929&tempid=1207169900458310637';
                                            
                                            $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);

                                        }