<?php
require_once(__DIR__."/../includes/sysinit.php");
//if session user is not set, exit with 'not authorised':
if (!($_SESSION['user']['user_code'] ?? 0)) SableGeneralFunctions::divertFromPage();

$hash		= $_SESSION['caller']['args'][1];
$userData	= SableDBFunctions::getAjaxResponse($hash, false);

$refData = explode(':', $userData['reference']);
$payTs = DateTime::createFromFormat('Y-m-d H-i-s', $refData[1].' '.$refData[2]);
$payIp = SableGeneralFunctions::getRealIpAddr();

$actionLink = 'https://paynow.netcash.co.za/site/paynow.aspx'; //SABLE_DEMO_SYSTEM ? '' : 'https://paynow.netcash.co.za/site/paynow.aspx';

/**
$sql = "insert ignore into netcash_payment 
		(np_ip, np_user, np_date, np_amount, np_reference) 
		values 
		('{$payIp}', {$userData['user_code']}, '{$payTs->format('Y-m-d H:i:s')}', {$userData['amount']}, '{$userData['reference']}')";
**/

$sql = "select np_id from netcash_payment where np_reference = '{$userData['reference']}'";
$npid= $sableDB->getValue($sql);

if (!$npid)
{
	$sql = "insert into netcash_payment 
			(np_ip, np_user, np_date, np_amount, np_reference) 
			values 
			('{$payIp}', {$userData['user_code']}, '{$payTs->format('Y-m-d H:i:s')}', {$userData['amount']}, '{$userData['reference']}')";

	$res = $sableDB->updateQuery($sql);
}
else
{
	$res = 1; //ok
}

$serviceKey = SABLE_DEMO_SYSTEM ? "zzzzz" : "xxxxx";

if ($res)
{
?>
<div class="forms" style="min-height: 15px !important;">
	<div class="container forms-grids">
		<div class="row forms-grid">
			<div class="col-md-12">
				<h3 class="title"><i class="fa fa-credit-card" aria-hidden="true"></i>&nbsp;<span id="main-title">Make Payment</span></h3>
				<p></p>
			</div>
		</div>	
	</div>
</div>

<noscript><h4 class="sub">Please enable JavaScript in your browser and then refresh this page. This payment cannot be processed without JavaScript enabled.</h4></noscript>

<form name="form" id="x1" method="POST" action="<?php echo($actionLink);?>" target="_top"> <!---Form POST **--->
	<input type="hidden" name="M1" value="<?= $serviceKey ?>">	<!--- YOUR PAY NOW SERVICE KEY GOES IN HERE --->
	<input type="hidden" name="M2" value="zzzzz">	<!--- SOFTWARE VENDOR KEY GOES IN HERE --->
	<input type="hidden" name="p2" value="<?= $userData['reference']; ?>">			<!---Unique ID for this / each transaction--->
	<input type="hidden" name="p3" value="<?= $userData['description']; ?>">		<!---Description of goods being purchased--->
	<input type="hidden" name="p4" value="<?= $userData['amount']; ?>">				<!---Amount to be settled / paid--->
	<input type="hidden" name="Budget" value="N">									<!---Compulsory should be Y--->
	<input type="hidden" name="m4" value="<?= $hash; ?>">							<!---This is an extra field ***--->
	<input type="hidden" name="m5" value="<?= session_id(); ?>">					<!---This is an extra field ***--->
	<input type="hidden" name="m6" value="<?= SableSystem::SableEncode($_SESSION['user']['user_code']); ?>">										<!---This is an extra field ***--->
	<?php
	/*
	<input type="hidden" name="m4" value="Extra 1">									<!---This is an extra field ***--->
	<input type="hidden" name="m5" value="Extra 2">									<!---This is an extra field ***--->
	<input type="hidden" name="m6" value="Extra 3">									<!---This is an extra field ***--->
	*/
	?>
	<input type="hidden" name="m9" value="<?= $userData['user_email']; ?>">		<!---Card holders email address--->
	<input type="hidden" name="m11" value="<?= $userData['user_cell']; ?>">		<!---Card holders mobile number--->
</form>

<script>
function resetForm()
{
	$("#ajaxDiv").hide();
	$(".body").css("cursor", "default");
	$("#main-title").html('Connection timed out. Please refresh to try again.');
}

$(document).ready
(
	function(e)
	{
		//$("#main-title").html('Connecting to NetCash...');
		$("#main-title").html('Opening payment gateway...');
		$("#ajaxDiv").show();
		$(".body").css("cursor", "wait");
		$('#x1').submit();
		//console.log('Form will be submitted automatically');
		setTimeout(resetForm, 15000);
	}
);
</script>
<?php
}
else
{
?>
<div class="forms" style="min-height: 15px !important;">
	<div class="container forms-grids">
		<div class="row forms-grid">
			<div class="col-md-12">
				<h3 class="title"><i class="fa fa-circle-exclamation" aria-hidden="true"></i>&nbsp;<span id="main-title">Payment Error</span></h3>
				<p></p>
			</div>
		</div>
		<div class="row forms-grid">
			<div class="col-md-12">
				<span>An error occurred. <a href="/?/home/">Please click here to revert and try again.</a></span>
			</div>
		</div>			
	</div>
</div>
<?php
}
