<?php
// include_once $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'config.php';
// include_once $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'api'.DIRECTORY_SEPARATOR.'help2PayAPI.php';

// include_once('config.php');
include_once('../config.php');
include_once('help2PayAPI.php');

//instantiate ModelClassEx
$system = new System();

//instantiate api class
$help2PayAPI = new help2PayAPI();

//error_log paths
// $apiPath 		= dirname(SYSTEM_PATH).DIRECTORY_SEPARATOR.'api';
$apiPath 		= '../api';
$accessLogUrl 	= $apiPath.DIRECTORY_SEPARATOR.'Logs'.DIRECTORY_SEPARATOR.'H2P_Withdrawal'.DIRECTORY_SEPARATOR.'verification'.DIRECTORY_SEPARATOR.'access';
$errorUrl 	    = $apiPath.DIRECTORY_SEPARATOR.'Logs'.DIRECTORY_SEPARATOR.'H2P_Withdrawal'.DIRECTORY_SEPARATOR.'verification'.DIRECTORY_SEPARATOR.'error';
$verifyUrl 	    = $apiPath.DIRECTORY_SEPARATOR.'Logs'.DIRECTORY_SEPARATOR.'H2P_Withdrawal'.DIRECTORY_SEPARATOR.'verification'.DIRECTORY_SEPARATOR.'verify';

//check if directory exists if not, create directory
if(!file_exists($accessLogUrl)) {
    mkdir($accessLogUrl, 0777, true);
}


if(!file_exists($errorUrl)) {
    mkdir($errorUrl, 0777, true);
}

if(!file_exists($verifyUrl)) {
    mkdir($verifyUrl, 0777, true);
}

//log the error details
error_log(date("[Y-m-d H:i:s]")."\r\n".'Accessed' ."\r\n\r\n", 3, $accessLogUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log');
chmod($accessLogUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log',511);

//check get parameters
if(!empty($_GET)){

	//get response parameters
	$uri_response = $_SERVER['REQUEST_URI'];
	$responseArray = explode("?", $uri_response);
	$response = $responseArray[1];

	if(isset($_REQUEST['transId']) && isset($_REQUEST['key'])){
		//get transaction from database
	    $res = $system -> getRowData($system -> accessSelect('SELECT_WITHDRAW_H2P_TEMP', array($_REQUEST['transId'])));

		if(count($res) > 0){

			//get necessary details from result
			$key = $system -> getColumnData($res, 'api_key'); //transaction number
			$key = strtoupper($key);

			if($key==$_REQUEST['key']){
				error_log(date("[Y-m-d H:i:s]")."\r\n".'VERIFY:' ."\r\n"."\r\nTRUE ".$_REQUEST['transId']."  ".$_REQUEST['key']."\r\n\r\n", 3, $verifyUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log');
				chmod($verifyUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log',511);

				echo "true";
			}else{
				error_log(date("[Y-m-d H:i:s]")."\r\n".'ERROR:' ."\r\n"."\r\nFALSE: Key not verified ".$_REQUEST['transId']."  ".$_REQUEST['key']." vs ".$key."\r\n\r\n", 3, $verifyUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log');
				chmod($errorUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log',511);
				echo "false";
			}

		}else{
			//false
			error_log(date("[Y-m-d H:i:s]")."\r\n".'ERROR:' ."\r\n"."\r\nFALSE: Not on database ".$_REQUEST['transId']."  ".$_REQUEST['key']."\r\n\r\n", 3, $verifyUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log');
			chmod($errorUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log',511);
			echo "false";
		}
	}else{
		//log the error
		error_log(date("[Y-m-d H:i:s]")."\r\n".'ERROR:' ."\r\n"."\r\nresponse:\r\n". $response ."\r\n", 3, $errorUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log');
		chmod($errorUrl.DIRECTORY_SEPARATOR.'Log_'.date("Y-m-d").'.log',511);
	}
}
