<?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: Monet_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);   
                    
                }
                
                if( ($_POST['amount']>=100) && ($_POST['amount']<=25000) && (trim($UI__Rgst->txnpwd)==trim($_POST['pinno'])) ) 
                { 
                        
						$PAYWAllet =  GetBalance();  
                        $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("select *  from  _pkCc__  where  Uxd=:Uxd ");   
                        $stmt->bindValue(':Uxd', $UI__Rgst->id, PDO::PARAM_STR);
                        $stmt->execute(); 
                        $RstUxd = $stmt->fetch(PDO::FETCH_OBJ);
                        $PAYWAllet =  GetBalance(); 
                        $verify = password_verify($PAYWAllet, $RstUxd->bl1);
                            
                        if($verify)  
                        {     	 
                                
                        $Extcallback = '{"serKy":"NEUIEUBNDDOUIOEIONKLV6897634fnfvBVKV34","mobile":"'.$_POST['mobile'].'","rname_":"'.$_POST['rname_'].'","amount":"'.$_POST['amount'].'","accountNo":"'.$_POST['accountNo'].'","ifsc":"'.$_POST['ifsc'].'","ifsc":"'.$_POST['ifsc'].'"}';  
                        $ch = curl_init('https://www.paykindia.com/ws/customHit.php');                                             
                        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $Extcallback);                                                                  
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                        'x-tera-tenant-region: IN', 
                        'Content-Type: application/json', 
                        'Content-Length: ' . strlen($Extcallback))                                                                       
                        );       																		 
                        $responseDp = curl_exec($ch);   
        				  
        				$stmt=$conn->prepare("insert into UI__MoneY(response,Tdate) values(:calBack,:Tdate)"); 
        				$stmt->bindValue(':calBack', $responseDp , PDO::PARAM_INT);    
        				$stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);     
        				$stmt->execute();     
        				  
        				
                        //$TT = '{"monTbl":"{\"id\":794,\"amount\":\"112\",\"mobile\":\"8308462810\",\"beneficiary_name\":\"Udal umrao rathod \",\"beneficiary_accountno\":\"35723890377\",\"beneficiary_ifsc\":\"SBIN0011151\",\"user_id\":\"369\",\"cust_id\":\"913559\",\"status\":1,\"wallet_update\":\"\",\"payee_txn_id\":\"b029bffa57\",\"user_type\":0,\"url\":\"paykindia.com\",\"orderid\":\"\",\"report_id\":\"\",\"last_update\":\"2023-09-12 09:43:51\",\"client_id\":\"859180\",\"utr\":\"325509637242\"}","rechargeStatus":"Success","payee_txn_id":"b029bffa57","orderid":"","report_id":"xxxx","RechargeFlag":1,"info":"Thanks for doing money transfer with us. Please feel free to contact us for any kind of support.","status":"success"}';
        				
        				$Resp1 = json_decode($responseDp,true);  
                        if($Resp1['rechargeStatus']!='Fail')
                        {     
                            $Resp2 = json_decode($Resp1['monTbl'],true);
        				    extract($Resp2); 
                            
                            $stmt =  $conn->prepare("insert into moneytransfer (amount,mobile,beneficiary_name,beneficiary_accountno,beneficiary_ifsc,user_id,cust_id,status,payee_txn_id,user_type,url,orderid,report_id,last_update,client_id,utr) values(:amount,:mobile,:beneficiary_name,:beneficiary_accountno,:beneficiary_ifsc,:user_id,:cust_id,:status,:payee_txn_id,:user_type,:url,:orderid,:report_id,:last_update,:client_id,:utr)");            
                            $stmt->bindValue(':amount', $_POST['amount'], PDO::PARAM_STR);
                            $stmt->bindValue(':mobile', $mobile, PDO::PARAM_STR);
                            $stmt->bindValue(':beneficiary_name', $beneficiary_name, PDO::PARAM_STR);
                            $stmt->bindValue(':beneficiary_accountno', $beneficiary_accountno, PDO::PARAM_STR);
                            $stmt->bindValue(':beneficiary_ifsc', $beneficiary_ifsc, PDO::PARAM_STR);
                            $stmt->bindValue(':user_id', $user_id, PDO::PARAM_STR);
                            $stmt->bindValue(':cust_id', $_SESSION['userId'], PDO::PARAM_STR);
                            $stmt->bindValue(':status', $status, PDO::PARAM_INT);
                            $stmt->bindValue(':payee_txn_id', $payee_txn_id, PDO::PARAM_STR);
                            $stmt->bindValue(':user_type', 0, PDO::PARAM_INT);
                            $stmt->bindValue(':url', "NA", PDO::PARAM_STR);
                            $stmt->bindValue(':orderid', $orderid, PDO::PARAM_STR);
                            $stmt->bindValue(':report_id', $report_id, PDO::PARAM_STR);
                            $stmt->bindValue(':last_update', $last_update, PDO::PARAM_STR);
                            $stmt->bindValue(':client_id', $client_id, PDO::PARAM_STR);
                            $stmt->bindValue(':utr', $utr, PDO::PARAM_STR);   
                            $stmt->execute();
                                
                            $stmt =  $conn->prepare("delete from recevier_users where senderCustom_id=:senderCustom_id and receiver_ph=:receiver_ph "); 
                            $stmt->bindValue(':senderCustom_id', $_SESSION['userId'], PDO::PARAM_STR);
                            $stmt->bindValue(':receiver_ph', $_POST['mobile'], PDO::PARAM_STR);      
                            $stmt->execute();  
                             
                            $stmt =  $conn->prepare("insert into recevier_users (amount,receiver_name,senderCustom_id,receiver_ph,account,ifsc,register_date) values(:amount,:receiver_name,:senderCustom_id,:receiver_ph,:txt_accountno,:txt_ifsc,:register_date)");
                            $stmt->bindValue(':amount', $_POST['amount'], PDO::PARAM_STR);     
                            $stmt->bindValue(':receiver_name', $_POST['rname_'], PDO::PARAM_STR);       
                            $stmt->bindValue(':senderCustom_id', $_SESSION['userId'], PDO::PARAM_STR);
                            $stmt->bindValue(':receiver_ph', $_POST['mobile'], PDO::PARAM_STR);     
                            $stmt->bindValue(':txt_accountno', $_POST['accountNo'], PDO::PARAM_STR);     
                            $stmt->bindValue(':txt_ifsc', $_POST['ifsc'], PDO::PARAM_STR);     
                            $stmt->bindValue(':register_date', date("d-M-Y"), PDO::PARAM_STR);       
                            $stmt->execute();   
                            
                            ////     
                                
                            $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);    
        
        					
        					$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',  $payee_txn_id , PDO::PARAM_STR); 
                            $stmt->bindValue(':bank_remark', "NA", PDO::PARAM_STR); 
                            $stmt->bindValue(':status', $status , PDO::PARAM_STR); 
                            $stmt->bindValue(':accept', 1 , PDO::PARAM_STR);      
                            $stmt->execute();          
                            LvBal($_SESSION['userId'],$_POST['amount'],2,"Money Tranfered",$beneficiary_name);    
                            
                            
                            $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',   7 , PDO::PARAM_STR);   
                            $stmt->bindValue(':bank',  "Money Transfer" , 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',  $beneficiary_accountno , PDO::PARAM_STR); 
                            $stmt->bindValue(':TopayName',  $beneficiary_accountno  , PDO::PARAM_STR);     
                            $stmt->execute(); 
                            

                            
                            $_SESSION['Regis'] = "Transaction successful";
                            $_SESSION['topSuc'] = 1;
                            
                            
                                
                            $subject = "Money Transfer  On ".$SITE_NAME;  
                            $template_type = 1;
                            $attachment = '';
                            $name = $beneficiary_name;
                            $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 Money 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
                        $_SESSION['Regis'] = "Fail: Error No: 303 Contact Customer Care: 805 050 1222";
                }
                else
                    $_SESSION['Regis'] = "Please check Acc No, IFSC, Your Withdrawal Amount & Try Again";
                    
                header("Location: Monet_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/AmtTranfer.php");
?>