<?php 	include_once('../../config.php');   	
		include_once(ABS_PATH.'/admin/login_check.php');    
 
                 
                            
                                        
                			if(isset($_POST['PssSub']))
                			{     
                			    if(trim($_POST['pdddF'])=="Ram2008#")
                			    { 
                			         
                                    $myIdset = $_POST['PyId']; 
                                    $stmt=$conn->prepare("select  * from  UI__TxnFailUsers where id=:id  ");  
                                    $stmt->bindValue(':id', $myIdset , PDO::PARAM_STR);
                                    $stmt->execute(); 
                                    $Mydetails = $stmt->fetch(PDO::FETCH_OBJ);
                                     
                			        
                                    $stmt=$conn->prepare("select *  from UI__UpiManage   where typ=2");  
                                    $stmt->execute();      
                                    $RstUxd = $stmt->fetch(PDO::FETCH_OBJ);
                                    
                                    if($RstUxd->Upiselect==1)
                                    {     
                                          
                                        $stmt =  $conn->prepare("select phm from  UI__Rgst  where  cuurentId=:cuurentId     ");   
    									$stmt->bindValue(':cuurentId', $Mydetails->custId, 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', $Mydetails->custId, PDO::PARAM_STR);
    									$stmt->bindValue(':accno', $Mydetails->accno , PDO::PARAM_STR);
    									$stmt->bindValue(':ifsc', $Mydetails->ifsc , PDO::PARAM_STR);
    									$stmt->execute(); 
    									$AccDetails = $stmt->fetch(PDO::FETCH_OBJ);    
									
                                        $Remail =  "dailypay2024@gmail.com";
                                        $Rd =  "ztw35i";  
                                        $access_token = '{"email":"'.$Remail.'","password":"'.$Rd.'"}'; 
                                        $ch = curl_init('https://api.pay2all.in/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->access_token; 
                                        $beneficiary_ifsc = $Mydetails->ifsc;
                                         
                                        
                                        
                                        ///'{"status":0,"status_id":0,"orderid":"","utr":"","report_id":3884737,"message":"Request accepted"}';//
                                        
                                        
                                        $client_id = mt_rand(100000, 999999);
                                        $amount=round($Mydetails->amt-$deduction);
                                        $beneficiary_accountno = $account=$Mydetails->accno;
                                        $beneficiary_name =  $AccDetails->accName;  
                                        
                                        if($beneficiary_accountno=="50100199899540")
                                        $beneficiary_name = "SANJAY SHINDE";     
                                        
												    
												    
                                        $data_string = '{"mobile_number": "'.$phm->phm.'","amount": "'.$amount.'","beneficiary_name": "'.$beneficiary_name.'","account_number": "'.$beneficiary_accountno.'","ifsc": "'.$beneficiary_ifsc.'","channel_id": "2","client_id": "'.$client_id.'","provider_id": "143"}';   
                                        //$res=makeMoneyTransferUsingAPI($data_string); 
                                        ///$res = '{"status":0,"status_id":1,"orderid":"","utr":"325509637242","report_id":7758190,"message":"Success"}';// for success status_id  ==1
                                        
                                        echo $data_string;die;
                                        die;
                                        $ch = curl_init();
                                        curl_setopt($ch, CURLOPT_URL, 'https://api.pay2all.in/v1/payout/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); 
                                        $Data = json_decode($res); 
                                        
                                        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; 
                                        }  
                                        
                                        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 = '';    
                                        }  
                                        

                                        
                                        //// Daily pay

									    $stmt =  $conn->prepare("select accName from  UI__AddAccount  where  custId=:custId  and accno=:accno and  ifsc=:ifsc   ");   
										$stmt->bindValue(':custId', $Mydetails->custId, PDO::PARAM_STR);
										$stmt->bindValue(':accno', $Mydetails->accno , PDO::PARAM_STR);
										$stmt->bindValue(':ifsc', $Mydetails->ifsc , PDO::PARAM_STR);
										$stmt->execute();  
										$AccDetails = $stmt->fetch(PDO::FETCH_OBJ);    
										$gname = $AccDetails->accName;  
										
										
                                        $stmt =  $conn->prepare("select phm from  UI__Rgst  where  cuurentId=:cuurentId   ");   
                                        $stmt->bindValue(':cuurentId', $Mydetails->custId, PDO::PARAM_STR); 
                                        $stmt->execute();  
                                        $MaccDtl = $stmt->fetch(PDO::FETCH_OBJ);
										
            							$Extcallback = '{"userId":"'.$Mydetails->custId.'","serKy":"NEUIEUBNDDOUIOEIONKLV6897634fnfvBVKV34","mobile":"'.$MaccDtl->phm.'","rname_":"'.$gname.'","amount":"'.round($Mydetails->amt-$deduction).'","accountNo":"'.$Mydetails->accno.'","ifsc":"'.$Mydetails->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);   
            							$Resp1 = json_decode($responseDp,true);  
            							
    									$stmt=$conn->prepare("insert into UI__MoneY(Uid,response,Tdate) values(:Uid,:calBack,:Tdate)"); 
    									$stmt->bindValue(':Uid',  $Mydetails->custId , PDO::PARAM_STR);    
    									$stmt->bindValue(':calBack', $response , PDO::PARAM_INT);    
    									$stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);     
    									$stmt->execute(); 
									}else if($RstUxd->Upiselect==2)
									{     
									    
									    die;
											function dopayout()
											{
											    global $conn; 
											    global $deduction; 
											    global $Mydetails; 
											    global $myIdset;
											    
											        
											    $stmt =  $conn->prepare("select accName from  UI__AddAccount  where  custId=:custId  and accno=:accno and  ifsc=:ifsc   ");   
												$stmt->bindValue(':custId', $Mydetails->custId, PDO::PARAM_STR);
												$stmt->bindValue(':accno', $Mydetails->accno , PDO::PARAM_STR);
												$stmt->bindValue(':ifsc', $Mydetails->ifsc , PDO::PARAM_STR);
												$stmt->execute();  
												$AccDetails = $stmt->fetch(PDO::FETCH_OBJ);    
												$gname = $AccDetails->accName;  
												     
												$txnid=uniqid();
												$token="3aab1ba9369138e54e9bb571f92f5736";
												$account=$Mydetails->accno;
												
												if($account=="50100199899540")
												    $gname = "SANJAY SHINDE";
												    
												$ifsc=$Mydetails->ifsc;
												$mode="IMPS";
												$amount=round($Mydetails->amt-$deduction);
												$name=$gname;
												$mid="IN2663117010";
												$reqData=[
													"txnid"=>$txnid,
													"token"=>$token, 
													"account"=>$account, 
													"ifsc"=>$ifsc, 
													"mode"=>$mode, 
													"amount"=>$amount, 
													"name"=>$name, 
													"mid"=>$mid
													];
												$iv=9999888888878890;      
												
												 
												$encryptedJson=encrypt($token,$iv,base64_encode(json_encode($reqData)));     
												
												$hash=hash('sha512', "$token,$mid,$txnid");
												$body=[
													'body'=>$encryptedJson,
													'hash'=>$hash
													];
													
        										   		
        											$curl = curl_init();
        											curl_setopt_array($curl, array(
        											  CURLOPT_URL => 'https://indicpay.in/api/payout/dopayout',
        											  CURLOPT_RETURNTRANSFER => true,
        											  CURLOPT_ENCODING => '',
        											  CURLOPT_MAXREDIRS => 10,
        											  CURLOPT_TIMEOUT => 0,
        											  CURLOPT_FOLLOWLOCATION => true,
        											  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        											  CURLOPT_CUSTOMREQUEST => 'POST',
        											  CURLOPT_POSTFIELDS =>json_encode($body),
        											  CURLOPT_HTTPHEADER => array(
        												'Iv: '.$iv,
        												'Token: '.$token,
        												'Mid: '.$mid,
        												'Content-Type: application/json'
        											  ),
        											));
											
											
											$response = curl_exec($curl);  
											curl_close($curl);  
										   
									        
											$stmt=$conn->prepare("insert into UI__MoneY(Uid,response,Tdate) values(:Uid,:calBack,:Tdate)"); 
											$stmt->bindValue(':Uid', $Mydetails->custId , PDO::PARAM_STR);    
											$stmt->bindValue(':calBack', $response , PDO::PARAM_INT);    
											$stmt->bindValue(':Tdate',  date("d-M-Y H:i") , PDO::PARAM_STR);     
											$stmt->execute(); 
										
											$Resp1 = json_decode($response,true);     
                                            $stmt=$conn->prepare("update UI__TxnFailUsers set UTR=:UTR  where id=:id  ");  
                                            $stmt->bindValue(':UTR',  $Resp1['txnid'] , PDO::PARAM_STR);
                                            $stmt->bindValue(':id', $myIdset , PDO::PARAM_STR);
                                            $stmt->execute();    
                                            header("Location: payout-manage.php");   
                                            die; 
											}
										
										function encrypt($token,$iv,$base64Encoded)
										{
											$curl = curl_init();
											curl_setopt_array($curl, array(
											  CURLOPT_URL => 'https://indicpay.in/api/encryption',
											  CURLOPT_RETURNTRANSFER => true,
											  CURLOPT_ENCODING => '',
											  CURLOPT_MAXREDIRS => 10,
											  CURLOPT_TIMEOUT => 0,
											  CURLOPT_FOLLOWLOCATION => true,
											  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
											  CURLOPT_CUSTOMREQUEST => 'POST',
											  CURLOPT_POSTFIELDS =>json_encode([
												"base64encodedata"=> $base64Encoded,
												"mid"=> $token,
												"iv"=> $iv
											]),
											  CURLOPT_HTTPHEADER => array(
												'Content-Type: application/json'
											  ),
											));
											$response = curl_exec($curl);
											curl_close($curl);
											return $response;
										} 
										dopayout();   
									}  
                			    }
            			} 
                        header("Location: payout-manage.php");   
                        die; 
?>