<?php
/**
DOCS:
https://api.netcash.co.za/inbound-payments/pay-now/pay-now-ecommerce/

Netcash will always send a response to the Notify URL - this is asynchronous and this is the URL that should be used to write the transaction status to the database.

The Accept and Deline URL's are just synchronous responses to keep the user updated on the status of the transaction and, if there is a time delay from the bank (security), then the Accept URL will not contain important information such as the Trace ID - that will be sent to the Notify URL when ready.
**/
require_once('netcash_common.php');

/**
ACCEPT URL:
===========

Field No:	Field Name:				Type		Description:
---------	----------------------	---------	-----------------------------------------------------------------------------------
1			TransactionAccepted		Boolean		“true” means the transaction was successful
2			CardHolderIpAddr		AN			This is the original IP the request was made from
3			RequestTrace			AN			This is a unique trace id for the transaction. This id can be used to retrieve 
												additional information from the 3rd Party transaction logs
4			Reference				AN			This is the unique reference that you sent Netcash in the original request
5			Extra1					AN			This is the extra field you sent up in the m4 variable when you made the request 
												that we return for you to integrate into your own system.
6			Extra2					AN			This is the extra field you sent up in the m5 variable when you made the request 
												that we return for you to integrate into your own system.
7			Extra3					AN			This is the extra field you sent up in the m6 variable when you made the request 
												that we return for you to integrate into your own system.
8			Amount					N			This is the amount that you sent Netcash in the original request
9			Method					N			1 Credit card
												2 			Bank EFT
												3 			Retail
												4 			Ozow
												5 			MasterPass
												6 			Visa Checkout
												7			Masterpass QR
10			ccToken					AN			Credit Card Vault GUiD
11			ccHolder				AN			Credit Cardholder name
12			ccMasked				AN			Masked Credit Card number
13			ccExpiry				N			Credit Card expiry date MMCCYY

DECLINE URL:
============

Field No:	Field Name:				Type		Description:
---------	----------------------	---------	-----------------------------------------------------------------------------------
1			TransactionAccepted		Boolean		“true” means the transaction was successful
2			CardHolderIpAddr		AN			This is the original IP the request was made from
3			RequestTrace			AN			This is a unique trace id for the transaction. This id can be used to retrieve 
												additional information from the 3rd Party transaction logs
4			Reference				AN			This is the unique reference that you sent Netcash in the original request
5			Extra1					AN			This is the extra field you sent up in the m4 variable when you made the request 
												that we return for you to integrate into your own system.
6			Extra2					AN			This is the extra field you sent up in the m5 variable when you made the request 
												that we return for you to integrate into your own system.
7			Extra3					AN			This is the extra field you sent up in the m6 variable when you made the request 
												that we return for you to integrate into your own system.
8			Amount					N			This is the amount that you sent Netcash in the original request
9			Reason					AN			This is the unpaid reason

REDIRECT URL:
============

Field No:	Field Name:				Type		Description:
---------	----------------------	---------	-----------------------------------------------------------------------------------
1			TransactionAccepted		Boolean		“true” means the transaction was successful
2			CardHolderIpAddr		AN			This is the original IP the request was made from
3			RequestTrace			AN			This is a unique trace id for the transaction. This id can be used to retrieve 
												additional information from the 3rd Party transaction logs
4			Reference				AN			This is the unique reference that you sent Netcash in the original request
5			Extra1					AN			This is the extra field you sent up in the m4 variable when you made the request 
												that we return for you to integrate into your own system.
6			Extra2					AN			This is the extra field you sent up in the m5 variable when you made the request 
												that we return for you to integrate into your own system.
7			Extra3					AN			This is the extra field you sent up in the m6 variable when you made the request 
												that we return for you to integrate into your own system.
8			Amount					N			This is the amount that you sent Netcash in the original request
9			Reason					AN			This is the unpaid reason
10			Method					N			1	Credit card
												2 	Bank EFT
												3 	Retail
												4 	Ozow
												5 	MasterPass
												6 	Visa Checkout
												7	Masterpass QR

URLS:
Accept url:		This is the URL that the browser is sent to if the transaction is successful.
Decline url:	This is the URL that the browser is sent to if the transaction is declined. 
				If a card transaction is cancelled by the card holder during the payment process, the page will redirect to the decline url page.
Notify url:		This is the URL that Netcash will call for transactions like EFT and Retail where the payment is not able to be captured in real time.
				These payments will be picked up when they are made and Netcash will notify your system through this URL.
Redirect url:	This is the URL that the browser is sent to once the complete button is clicked on the EFT and Retail payment screens on Netcash.
				This could/would return the customer to your site while you are waiting for a payment to be made.

**/

/**
    [TransactionAccepted] => false
    [Reason] => Declined
    [CardHolderIpAddr] => 41.13.250.160
    [RequestTrace] => 77459.137796931
    [Reference] => 46:20210608:060238
    [Extra1] => {hash value}
    [Extra2] => 2a2d6c4c1653c86a214a854ef1f9ad60 (Session Id)
    [Extra3] => user_code, base64_Encoded
    [Amount] => 12.34
    [Method] => 1
    [type] => DEPOSITRECEIPT
**/

//Security Check

if ($_POST['RequestTrace'] ?? 0) //if no "RequestTrace" then no transaction, so most likely cancelled.
{
	//initialise with 'Unknown - something went wrong' message:
	$dispIcon	= 'fas fa-exclamation-triangle';
	$dispTitle	= 'Something Went Wrong';
	$dispCap	= 'Your payment could not be confirmed.';

	if ($success)
	{	
		notifyUser($userCode, $PayDescr.' payment successful. Thank you!');
		$dispIcon	= 'fas fa-smile-beam';
		$dispTitle	= 'Thank You!';
		$dispCap	= 'Your payment was successful.';
	}
	else
	{
		notifyUser($userCode, $failMessage);
		$dispIcon	= 'fas fa-frown';
		$dispTitle	= 'Payment Unsuccessful';
		$dispCap	= '<p>Unfortunately your online payment was unsuccessful. Please try again, or use an alternative payment method.
					<br><span class="gym4free">gym4free</span> benefits will not be available if your account is in arrears.<br>
					Please contact Gym Company '.$memberGymName.' for further assistance.</p>';
	}
}
else
{
	$txnok		= 0;
	$dispIcon	= 'fas fa-ban';
	$dispTitle	= 'Transaction Cancelled';
	$dispCap	= '<p>The transaction was not processed.<br>
				<span class="gym4free">gym4free</span> benefits will not be available if your account is in arrears.<br>
				Please contact Gym Company '.$memberGymName.' for further assistance.</p>';
	//SableGeneralFunctions::divertFromPage('home/', $failMessage);
}
$fclass = SableSystem::loggedIn() ? "forms" : "";
?>
<div class="<?= $fclass ?>" 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="<?= $dispIcon ?>" aria-hidden="true"></i>&nbsp;<?= $dispTitle ?></h3>
				<p></p>
			</div>
		</div>
		<div class="row forms-grid">
			<div class="col-md-12">
				<p></p>
				<h2 class="sub"><?= $dispCap ?></h2>
			</div>
		</div>
		<div class="row forms-grid">
			<div class="col-md-12">
				<p></p>
				<h3 class="sub">Payment Details:</h3>
			</div>
		</div>
		<div class="row forms-grid">
			<div class="col-xs-3 col-md-2"><strong>Method:</strong></div>
			<div class="col-xs-8 col-md-10"><?= $payMethod ?></div>
		</div>
		<div class="row forms-grid">
			<div class="col-xs-3 col-md-2"><strong>Description:</strong></div>
			<div class="col-xs-8 col-md-10"><?= $PayDescr ?></div>
		</div>		
		<div class="row forms-grid">
			<div class="col-xs-3 col-md-2"><strong>Amount:</strong></div>
			<div class="col-xs-8 col-md-10"><?= $ZARAmount ?></div>
		</div>
		
		<?php
		/**
		echo('--POST--<br>');
		print_r($_POST);
		echo('<br>');
		print_r('ok: '.$txnok);
		echo('<br>');
		print_r('success: '.$success);
		echo('--DATA--<br>');
		print_r($txnData);
		**/
		if ($success)
		{
			//only allow a redirect if the transaction was successful.
			if ($txnData['redirect'] ?? 0)
			{
				include_once($txnData['file_path']); //will now use $txnData['param_data'] as the fields required for this file
		?>
		<div class="row forms-grid">
			<div class="col-xs-3 col-md-2"><strong>Result:</strong></div>		
			<div class="col-xs-8 col-md-10"><?= $result ?></div>
		</div>
		<?php
			}
		}
		?>
		
		<div class="row forms-grid">
			<div class="col-md-12">
				<p></p>
			</div>
		</div>
		<!--// buttons  -->
		<div class="row forms-grid">
		<?php if (SableSystem::loggedIn()) { ?>
			<div class="col-md-2">
				<input type="button" id="btnCancel" value="Back"
				class="submit" onclick="window.location = '<?= SableSystem::getThemeLinkPath('/dashboard/gym4free/'); ?>'"/>
			</div>
		<?php } else { ?>
			<div class="col-md-2">
				<input type="button" id="btnCancel" value="Login" class="submit" onclick="window.location = '<?= SableSystem::getSableLinkPath('/login/'); ?>'"/>
			</div>
			<div class="col-md-10">
				
				<span class="success">You have been logged out for security reasons. Please login again to continue.</span>
			</div>
			<?php
				}
			?>				
			</div>
		</div>
		<!-- buttons //-->	
	</div>
</div>
<?php
SableDBFunctions::deleteAjaxResponse($_POST['Extra1']); //cleanup
//SableDebug::debugfetch('....END....', LOG_FILE_NAME);