<?php
	include_once('../config.php');      
	include_once(ABS_PATH.'/custom_function/default.php'); 
    if(!isset($_SESSION['userId']))
    { 
         header("Location: ../");
         die;
    }   
    
    
                    
                    
                    
                    
                       
                    function encrypt($text, $encKey) 
                    {
                        $IV = substr(md5(uniqid('', true)),16);
                        $algorithm = 'aes-256-cbc';
                        $encrypted = openssl_encrypt($text, $algorithm, $encKey, 0, $IV);
                        return $IV.$encrypted;
                    } 
                       
                       
                    function decrypt($encryptedTxt, $decKey) 
                    {
                        $algorithm = 'aes-256-cbc';
                        if (strpos($encryptedTxt, ' ') !== false) {
                            $encryptedTxt = str_replace(' ', '+', $encryptedTxt);
                        }
                        $IV = substr($encryptedTxt, 0, 16);
                        $encrypted = substr($encryptedTxt, 16);
                        $decrypted = openssl_decrypt($encrypted, $algorithm, $decKey, 0, $IV);
                        return $decrypted;
                    } 
                      
                      
                     
                    

                    
              
                if(isset($_REQUEST['clearAll'])) 
                {
                    unset($_SESSION['TaccountNo']);
                    unset($_SESSION['Tifsc']);
                    unset($_SESSION['Tname']);
                    unset($_SESSION['OtpLogin']); 
                    unset($_SESSION['OtpLoginEror']);  
                    header("Location: AddMAccount.php");
                    die; 
                }
                          
                if(isset($_POST['AddOtp'])) 
                { 
                    if($_POST['enterOtp']==$_SESSION['OtpLogin'])
                    {    
                        $stmt =  $conn->prepare("insert into UI__AddAccount (custId,accno,ifsc,Tdate,accName) values(:custId,:accno,:ifsc,:Tdate,:accName)");            
                        $stmt->bindValue(':custId', $_SESSION['userId'] , PDO::PARAM_STR);
                        $stmt->bindValue(':accno', $_SESSION['TaccountNo'], PDO::PARAM_STR);
                        $stmt->bindValue(':ifsc', $_SESSION['Tifsc'], PDO::PARAM_STR);
                        $stmt->bindValue(':Tdate',date("d-M-Y"), PDO::PARAM_STR); 
                        $stmt->bindValue(':accName',$_SESSION['Tname'], PDO::PARAM_STR); 
                        $stmt->execute();     
                        unset($_SESSION['TaccountNo']);
                        unset($_SESSION['Tifsc']);
                        unset($_SESSION['Tname']);
                        unset($_SESSION['OtpLogin']); 
                        unset($_SESSION['OtpLoginEror']);  
                        $_SESSION['Regis'] = "Account has been added"; 
                    }else
                        $_SESSION['OtpLoginEror']    = "Enter Correct OTP";
                    
                    header("Location: AddMAccount.php");
                    die;   
                }
                          
                          
        if(isset($_POST['submit'])) 
        {     
            
            
                    
                    $body=[ 'affiliate_id'=>9, 'api_key'=>'aYb7f7O3tvVM3jwpN7yw', 'checksum'=>'92bbd7beb51533fdba675716187fd64bd2b89753f0f598e7628b31a842bb196bcde0969cc0ec81aad67a00c79b02dd925746cdaaf05ee22c1683d0bd6b93a303'];
                     
                    $encryptedBody = json_encode(['query' => encrypt(json_encode($body),'LMug5su8XCAvO0aIYkWGcH1sjumpbwNl')]);
                     
                      
                    $curl = curl_init(); 
                    curl_setopt_array($curl, array(
                      CURLOPT_URL => 'https://hqvt6tbkl5.execute-api.ap-south-1.amazonaws.com/stag/api/v1/services/token',
                      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 => $encryptedBody,
                      CURLOPT_HTTPHEADER => array(
                        'affiliate_id: 9',
                        'Content-Type: application/json'
                      ),
                    ));
                     
                    $response = curl_exec($curl); 
                    curl_close($curl);
                    
                    $decryptedText = decrypt(json_decode($response), 'RoJs6aIPfani91y99SqrdBF4HIuFNxqH');
                    $jsonDecode = json_decode($decryptedText);
                    
                  
                      				   
                    $body=['affiliate_id'=>9, 'api_key'=>'aYb7f7O3tvVM3jwpN7yw', 'merchant_id'=>30661,'channelCode'=>2,'checksum'=>'d7dddb4376c617853ff80baad96103601d9557141a1b7c32b8a204c7b166569ef257ae05f31740961607249c5ce4e2cb3cf9b30aaae3a09ab4dc6117e331a5dc']; 
                    $encryptedBody = json_encode(['query' => encrypt(json_encode($body),'LMug5su8XCAvO0aIYkWGcH1sjumpbwNl')]); 
                    
                    $curl = curl_init(); 
                    curl_setopt_array($curl, array(
                    CURLOPT_URL => 'https://hqvt6tbkl5.execute-api.ap-south-1.amazonaws.com/stag/api/v1/services/sessiontoken',
                    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 => $encryptedBody,
                    CURLOPT_HTTPHEADER => array(
                    'affiliate_id: 9',
					'token: '.$jsonDecode->data->token,
                    'Content-Type: application/json'
                    ),
                    ));
                    
                    $response = curl_exec($curl); 
                    curl_close($curl);   
                     
                    $decryptedText = decrypt(json_decode($response), 'RoJs6aIPfani91y99SqrdBF4HIuFNxqH');
                    $jsonDecode = json_decode($decryptedText); 
                    $SessionToken =  $jsonDecode->data->sessionToken;
                      
                    
                    $Bname  = $_POST['rname_'];
                    $accountNo  = $_POST['accountNo'];
                    $ifsc  = $_POST['ifsc'];
                    $mobile  = $_POST['mobile'];
                    
                    
                    $body=['affiliate_id'=>9, 'merchant_id'=>30661,'uic'=>'976281144816809','sessionRQ'=>'8b9e71cabaf838629e0ceba71d25add8','beneficiary_name'=>"$Bname",'account_number'=>"$accountNo",'ifsc_code'=>"$ifsc",'ben_mobile_number'=>"$mobile","relationship"=>"wife"];  
                    $encryptedBody = json_encode(['query' => encrypt(json_encode($body),'LMug5su8XCAvO0aIYkWGcH1sjumpbwNl')]); 
                      
                    
                    $curl = curl_init(); 
                    curl_setopt_array($curl, array(
                    CURLOPT_URL => 'https://hqvt6tbkl5.execute-api.ap-south-1.amazonaws.com/stag/api/v1/channels/dmt/addbeneficiary',
                    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 => $encryptedBody,
                    CURLOPT_HTTPHEADER => array(
                    'affiliate_id: 9',
					'session_token: '.$SessionToken,
                    'Content-Type: application/json'
                    ),
                    ));
                    
                    $response = curl_exec($curl); 
                    curl_close($curl);      
                    $decryptedText = decrypt(json_decode($response), 'RoJs6aIPfani91y99SqrdBF4HIuFNxqH');
                    $jsonDecode = json_decode($decryptedText);
                    
                    //{"status":200,"message":" Success","respcode":1000, "data": {"order_id" : "MAWEB12121212","ma_bene_verification_id":12121," verification_message":"success"," bank_bene_name":"TEST"," bene_name" : "Swarup Verify", "bank_account_number" : "59001211072"," ben_mobile_number": "7678775543", "ifsc_code":" ABHY0065004" }} 
                    
                      
                    if($jsonDecode->status=="200")
                    {  
                    $stmt=$conn->prepare("insert into   UI__AirpYBeneficiary (ma_beneficiaries_id,Tdate,custId,accNo,ifsc,accName) values(:ma_beneficiaries_id,:Tdate,:custId,:accNo,:ifsc,:accName)   ");   
                    $stmt->bindValue(':ma_beneficiaries_id', $jsonDecode->data->ma_beneficiaries_id , 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(':accNo',  $accountNo , PDO::PARAM_STR); 
                    $stmt->bindValue(':ifsc',  $ifsc , PDO::PARAM_STR); 
                    $stmt->bindValue(':accName',  $_POST['rname_'] , PDO::PARAM_STR); 
                    
                    
                    $stmt->execute(); 
                    $_SESSION['Regis'] = "Beneficiary Added!!";  
                    }else
                        $_SESSION['Regis'] = "Error In adding Beneficiary or Its Already Added";  
                        
                die;        
                        
                header("Location: AddMAccount.php");
                die; 
        }
        
include_once("../view/dashboard/AddMAccountmcopy.php");
?>