<?


### DELETE THE USER'S SESSION:
require_once("/BANGK!/master_config.php");
require_once($master_config_root_path . "shared/functions.shared");
require_once($master_config_root_path . "shared/session.shared");



### CLEAR SESSION - START NEW:
//$session_clear_restart = "YES";
//include($_SESSION['template']['company']['file_path'] . "shared/session_clear.shared");
//logger("got to end of clear session---");




### START SESSION VERIFICATION (ALWAYS LAST B4 REST OF PAGES):
$verified_member = "no";
$verified_business = "no";
$verified_transaction = "no";
$verified_banned = "no";
$verified_acccess = "no";
include($_SESSION['template']['company']['file_path'] . "shared/start_verified_session.shared");
#
#
#
### START REMAINING PAGES:


logger("




#############################################################################################
#############################################################################################
#############################################################################################
## START / NEW SESSION:  [index.gate]
#############################################################################################
#############################################################################################
#############################################################################################



");

/*

#############################################################################################
### REFERENCE:

2011.12.18:


https://gateway.BANGK.com/?
business_account_number=98765a9435d3aa2d25a719767d055421
&unique_session_id=XXXYYYZZZ
&transaction_response_url=https://www.your-company.com/listen_for_BANGK!
&customer_return_url=https://www.your-company.com/customer_redirect_page
&amount=115.75
&currency_code_iso3=USD
&customer_email_address=j.smith@example.com
&test_session_status=10


2012.04.22:

https://gateway.nsdb-development.com/?shopping_cart_number=1f0f0da21020db2ff850e3d894a66067fe5f99b854b162a6230835bdff4fee04&
business_account_number=98765a9435d3aa2d25a719767d055421&
unique_session_id=d7e62d6c6e1e99ff0b48afb9a3f9426882282c20b9367afdf4a1489c13546af2&
transaction_response_url=https://nsdb-development.com/test/test_shoppingcart_button.php?business_account_number=98765a9435d3aa2d25a719767d055421&
unique_session_id=bd4d67c46fdb4e3c592d1e34abe031028c53647fd16cb3d72a3ac98c9da8e517&
customer_return_url=https://nsdb-development.com/test/test_shoppingcart_button.php?business_account_number=98765a9435d3aa2d25a719767d055421&
unique_session_id=bd4d67c46fdb4e3c592d1e34abe031028c53647fd16cb3d72a3ac98c9da8e517&
amount=10&
currency_code_iso3=&
customer_email_address=



*/








### RESET CANCEL COOKIE:
logger3("0. cookie_this_session",$cookie_this_session);
if(strtoupper($cookie_this_session) == "CANCELED") {
	logger3("1. cookie_this_session",$cookie_this_session);
	$tmp_time = time();
	setcookie("cookie_this_session", $tmp_time, 0, "/", "." . $_SESSION['template']['company']['domain'], 0);
	$_SESSION['gateway']['incoming'] = "";	
	logger3("_SESSION['gateway']['incoming']",$_SESSION['gateway']['incoming']);
}


#############################################################################################
### SET INCOMING VARS

logger3("unique_session_id",$_GET['unique_session_id']);


//default
$_SESSION['gateway']['incoming']['business_ip'] = $_SERVER['REMOTE_ADDR'];

// standard payment vars:
if($business_account_number) $_SESSION['gateway']['incoming']['business_account_number'] = trim($_GET['business_account_number']);
if($unique_session_id) $_SESSION['gateway']['incoming']['unique_session_id'] = trim($_GET['unique_session_id']);
if($transaction_response_url) $_SESSION['gateway']['incoming']['transaction_response_url'] = trim($_GET['transaction_response_url']);
if($customer_return_url) $_SESSION['gateway']['incoming']['customer_return_url'] = trim($_GET['customer_return_url']);
if($amount) $_SESSION['gateway']['incoming']['amount'] = trim($amount);
if($currency_code_iso3) $_SESSION['gateway']['incoming']['currency_code_iso3'] = trim($_GET['currency_code_iso3']);

// reTRY™
if($credit_card_number) $_SESSION['gateway']['incoming']['credit_card_number'] = trim($_GET['credit_card_number']);
if($expiration_month) $_SESSION['gateway']['incoming']['expiration_month'] = trim($_GET['expiration_month']);
if($expiration_year) $_SESSION['gateway']['incoming']['expiration_year'] = trim($_GET['expiration_year']);
if($cvv) $_SESSION['gateway']['incoming']['cvv'] = trim($_GET['cvv']);
if($customer_ip) $_SESSION['gateway']['incoming']['customer_ip'] = trim($_GET['customer_ip']);

// optional customer values
if($customer_firstname) $_SESSION['gateway']['incoming']['customer_firstname'] = trim($_GET['customer_firstname']);
if($cutomer_lastname) $_SESSION['gateway']['incoming']['cutomer_lastname'] = trim($_GET['cutomer_lastname']);
if($customer_email_address) $_SESSION['gateway']['incoming']['customer_email_address'] = trim($_GET['customer_email_address']);

// shopping cart:
if($shopping_cart_number) $_SESSION['gateway']['incoming']['shopping_cart_number'] = trim($_GET['shopping_cart_number']);

// testing
if($test_session_status) $_SESSION['gateway']['incoming']['test_session_status'] = trim($_GET['test_session_status']);

// processor:
//if($test_session_status) $_SESSION['gateway']['processor'] = "PAYVISION";


### CHECK IF TRANSACTIO ID IS UNIQUE BEFORE LOGGING TO TRANSLOG
$tmp_sql = "SELECT unique_session_id FROM gateway WHERE unique_session_id = '" . $_SESSION['gateway']['incoming']['unique_session_id'] . "' AND business_account_number = '" . $_SESSION['gateway']['incoming']['business_account_number'] . "';";
$session_id_exsists = getTHECASHIER($tmp_sql);
if($session_id_exsists) {
  echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
  DUPLICATE <i><b>unique_session_id</b></i>. (You have already used that value for a previous transaction.  Each transaction must have a UNIQUE <i>unique_session_id</i>.)<p>
  Please try again...";
  exit;
}

#############################################################################################
### LOG EVENT
$tmp_sql = "INSERT into gateway (
	`gatewayid`, 
	`hash`, 
	`business_account_number`, 
	`customer_email_address`, 
	`unique_session_id`, 
	`amount`, 
	`currency_code_iso3`, 
	`credit_card_number`, 
	`expiration_month`, 
	`expiration_year`, 
	`cvv`, 
	`customer_ip`,
	`business_ip`,
	`session_status`, 
	`test_session_status`, 
	`customer_return_url`, 
	`transaction_response_url`, 
	`shopping_cart_number`,
	`datetime_created`, 
	`datetime_updated`
) VALUES (

	'" . $gatewayid_blank . "',
	'" . hash_me() . "',
	'" . $_SESSION['gateway']['incoming']['business_account_number'] . "',
	'" . $_SESSION['gateway']['incoming']['customer_email_address'] . "',
	'" . $_SESSION['gateway']['incoming']['unique_session_id'] . "',
	'" . $_SESSION['gateway']['incoming']['amount'] . "',
	'" . $_SESSION['gateway']['incoming']['currency_code_iso3'] . "',
	'" . $_SESSION['gateway']['incoming']['credit_card_number'] . "',
	'" . $_SESSION['gateway']['incoming']['expiration_month'] . "',
	'" . $_SESSION['gateway']['incoming']['expiration_year'] . "',
	'" . $_SESSION['gateway']['incoming']['cvv'] . "',
	'" . $_SESSION['gateway']['incoming']['customer_ip'] . "',
	'" . $_SESSION['gateway']['incoming']['business_ip'] . "',
	'" . $_SESSION['gateway']['incoming']['session_status'] . "',
	'" . $_SESSION['gateway']['incoming']['test_session_status'] . "',
	'" . $_SESSION['gateway']['incoming']['customer_return_url'] . "',
	'" . $_SESSION['gateway']['incoming']['transaction_response_url'] . "',
	'" . $_SESSION['gateway']['incoming']['shopping_cart_number'] . "',
	'" . datetime() . "',
	'" . datetime() . "');";
$_SESSION['gateway']['gatewayid'] = $gatewayid = insertTHECASHIER($tmp_sql);
logger("tmp_sql: " . $tmp_sql);
logger("gateway['gatewayid']: " . $_SESSION['gateway']['gatewayid']);	





#############################################################################################
### SET BUSINESS ACCOUNT
logger3("gateway['incoming']['business_account_number']",$_SESSION['gateway']['incoming']['business_account_number']);
$_SESSION['gateway']['business']['account'] = account($_SESSION['gateway']['incoming']['business_account_number']);





#############################################################################################
### CHECK FOR COMMON ERROS:



// valid business?
if(strtoupper($_SESSION['gateway']['business']['account']['accounttype']) != "BUSINESS") {
  echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
  Invalid <i><b>business_account_number</b></i>. (You must provide a valid <i>business_account_number</i> for a registered " . $_SESSION['template']['company']['name'] . " Business Account.)<p>
  Please try again...";
  exit;
}


// banned business?
$tmp_banned = check_banned($_SERVER['REMOTE_ADDR'],$_SESSION['member']['memberid'],$_SESSION['gateway']['business']['account']['accountid']);
//test("tmp_banned",$tmp_banned);
if($tmp_banned['status'] == "Suspended") {
	$_SESSION['SYSMSG'] = "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
	This " . $_SESSION['template']['company']['name'] . " Business Account is currently suspended.<p>
	For more information... please contact " . $_SESSION['gateway']['incoming']['business']['account']['accountname'] . " directly: <a href='mailto:" . $_SESSION['gateway']['incoming']['business']['account']['email_account_address'] . "'>" . $_SESSION['gateway']['incoming']['business']['account']['email_account_address'] . "</a>";
	jump("javascript","https://" . $_SESSION['template']['company']['host_www'] . "/cancel.b!");
	exit;
	
} else if($tmp_banned['status'] == "Limited") {
	$_SESSION['SYSMSG'] = "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
	This " . $_SESSION['template']['company']['name'] . " Business Account is currently \"on pause\" - scheduled to be made available again within 24 hours.<p>
	For more information... please contact " . $_SESSION['gateway']['incoming']['business']['account']['accountname'] . " directly, or try again tomorrow...";
	jump("javascript","https://" . $_SESSION['template']['company']['host_www'] . "/cancel.b!");
	exit;
}

// amount?
if(!$_SESSION['gateway']['incoming']['amount']) {
    echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
    Invalid <b><i><b>amount</b></i></b>. (You may not submit a NULL <i>amount</i> value.)<p>
    Please try again...";
    exit;
}






################################################################################
### SET "TEST" PARAMETERS FOR PAYVISION:

//if($testing) {
//	$_SESSION['gateway']['processor']['credit_card']['card_number'] = $_SESSION['gateway']['incoming']['credit_card_number'] = "4444333322221111"; // "5432673003275469";
//	$_SESSION['gateway']['processor']['cvv'] = "";
//	$_SESSION['gateway']['processor']['credit_card']['expiration_month'] = $_SESSION['gateway']['incoming']['expiration_month'] = "12";
//	$_SESSION['gateway']['processor']['credit_card']['expiration_year'] = $_SESSION['gateway']['incoming']['expiration_year'] = "2012";
//	$_SESSION['gateway']['processor']['amount'] = $_SESSION['gateway']['incoming']['amount'] = "10.00";
//	
//	//$_SESSION['gateway']['retry']['transactionId'] = "4855365";
//	//$_SESSION['gateway']['retry']['transactionId'] = "00105b5a-b91a-4ba8-ae66-d1204adb2cbc";
//}







#############################################################################################
### SET GATEWAY SERVICE TYPE:

if($_SESSION['gateway']['incoming']['shopping_cart_number']) {

	$tmp_sql = "SELECT shoppingcartid FROM shopping_cart WHERE cart_number = '" . $_SESSION['gateway']['incoming']['shopping_cart_number'] . "' AND business_accountid = '" . $_SESSION['gateway']['business']['account']['accountid'] . "';";
	$shoppingcartid = getTHECASHIER($tmp_sql);
	logger("tmp_sql: " . $tmp_sql);
	logger("shoppingcartid: " . $shoppingcartid);
	
	if($shoppingcartid) {
		$_SESSION['gateway']['service'] = "SHOPPING CART";
	} else {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		The <i>shopping_cart_number</i> that you have provided is invalid.<p>
		Please try again...";
		exit;
	}

} else if( isset($_SESSION['gateway']['incoming']['credit_card_number']) && (!$_SESSION['gateway']['incoming']['unique_session_id']) ) {
	/// if the unique session id is set, then that means it's a straight through transaction -- NOT an reTRY.
	$_SESSION['gateway']['service'] = "RETRY";
		
} else {
	$_SESSION['gateway']['service'] = "STANDARD";
}




#############################################################################################
### CHECK TRANSLOG FOR PREVIOUS RETRY AUTHORIZATION

logger("1");

$tmp_sql = "SELECT * FROM translog WHERE business_account_number = '" . $_SESSION['gateway']['incoming']['business_account_number'] . "' AND credit_card_number = '" . $_SESSION['gateway']['incoming']['credit_card_number'] . "' AND amount = '" . $_SESSION['gateway']['incoming']['amount'] . "' AND datetime_created > '" . datetime(time()-120) . "' AND status = 'AUTHORIZED';";

logger("tmp_sql: " . $tmp_sql,1);

$_SESSION['gateway']['retry'] = object2array(readTHECASHIER($tmp_sql));
logger("gateway['retry']> "); logger($_SESSION['gateway']['retry']);






############################################################################################
############################################################################################
### FINAL GATEWAY ACTION:
############################################################################################
############################################################################################



############################################################################################
### FACILITATE STANDARD/FINAL PAYMENT/CAPTURE NOW:

logger("gateway['service']: " . $_SESSION['gateway']['service']);
logger("gateway['retry']['translogid']: " . $_SESSION['gateway']['incoming']['translogid']);

logger3("_SESSION['gateway']['service']",$_SESSION['gateway']['service']);
logger3("_SESSION['gateway']['retry']['status']",$_SESSION['gateway']['service']);

if( 
	(strtoupper($_SESSION['gateway']['service']) != "RETRY") || 
	(strtoupper($_SESSION['gateway']['retry']['status']) == "AUTHORIZED") 
	) {

	
	/// CHECK FOR "STANDARD PAYMENT" ERRORS
		
	//unique_session_id
	if(!$_SESSION['gateway']['incoming']['unique_session_id']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i>unique_session_id</i></b>. (You may not submit a NULL <i>unique_session_id</i> value.)
		<p>Please try again...";
		exit;
	}
	
	//is unique??
	$tmp_sql = "SELECT gatewayid FROM gateway WHERE unique_session_id = '" . $_SESSION['gateway']['incoming']['unique_session_id'] . "' AND business_account_number = '" . $_SESSION['gateway']['incoming']['business_account_number'] . "' AND session_status != 'AUTHORIZED';";
	$tmp_gatewayid = getTHECASHIER($tmp_sql);
	logger("00. tmp_sql: " . $tmp_sql);
	logger("00. tmp_gatewayid: " . $tmp_gatewayid);
	
	if($tmp_gatewayid) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		The <i>unique_session_id</i> that you have provided is not unique (i.e., you have already used this <i>unique_session_id</i> for a previous session).<p>
		Please try again...";
		exit;
	}
	
	
	if(!$_SESSION['gateway']['incoming']['customer_return_url']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i>customer_return_url</i></b>. (You may not submit a NULL <i>customer_return_url</i> value.)
		<p>Please try again...";
		exit;
	}
	
	if(!$_SESSION['gateway']['incoming']['transaction_response_url']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i>transaction_response_url</i></b>. (You may not submit a NULL <i>transaction_response_url</i> value.)
		<p>Please try again...";
		exit;
	}


	#############################################################################################
	### CHECK:  THIS IS ONLY A TEST!!!
	
	if($_SESSION['gateway']['incoming']['test_session_status']) {
	
		if(
		($_SESSION['gateway']['incoming']['test_session_status'] != "10") &&
		($_SESSION['gateway']['incoming']['test_session_status'] != "20") &&
		($_SESSION['gateway']['incoming']['test_session_status'] != "30")
		) {
			echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
			Invalid <b><i>test_session_status</i></b>. (FOR LIVE TRANSACTIONS, please remove the <i>test_session_status</i> parameter from your post string.  Otherwise, valid valkues for <i>test_session_status</i> are 10, 20 or 30.)
			<p>Please try again...";
			exit;
			
		}
		//echo "<b>" . $_SESSION['template']['company']['name'] . " TEST TRANSACTION...</b>";
		//echo "<p>STEP 1:&nbsp;&nbsp;Now posting \"test\" response to <i>transaction_response_url</i> ( <span style='color:#00AA00;'>" . $_SESSION['gateway']['incoming']['transaction_response_url'] . "</span> )...";
		
		### DISPLAY PROCESSING MESSAGE:
		$full_html_headers = "yes";
		$spinner_placement = "inline";
		$generic_processing_message = "<br>
		<br>
		<span style='color:#444444; font-family:verdana,arial; font-size:22px;'><b>Please Hold:</b> Processing TEST Transaction....";
		$css_generic_processing_visual_width = "1100px";
		include($_SESSION['template']['company']['file_path'] . "shared/generic_processing_visual.shared");
		echo $generic_processing_visual_api;
		###############################
		
		jump("javascript","test_session_status_1.gate",3000);
		exit;
	}
	

	
	
	#############################################################################################
	### NOT A TEST -- SEND TO PAYMENT PAGE:
	
	if(stristr($_SESSION['route'],"RETRY")) {
		### DISPLAY PROCESSING MESSAGE:
		$full_html_headers = "yes";
		//$spinner_placement = "inline";
		$generic_processing_message = "<br>
		<span style='color:#444444; font-family:verdana,arial; font-size:22px; line-height:45px;'>" . $_SESSION['gateway']['business']['account']['accountname'] . " is <u>redirecting you to <b>" . $_SESSION['template']['company']['simple_name'] . " reTRY</u></b><sup>&trade;</sup><br>
		...for reTRY&trade; credit card processing...";
		//$css_generic_processing_visual_width = "1100px";
		include($_SESSION['template']['company']['file_path'] . "shared/generic_processing_visual.shared");
		echo $generic_processing_visual_api;
		###############################
		
		$tmp_goto = "https://" . $_SESSION['template']['company']['host_www'] . "/" . $_SESSION['gateway']['business']['account']['email_account_address'];
		logger("user goto: " . $tmp_goto);
		jump("javascript",$tmp_goto,3000);
		exit;
		

	} else {  ## SHOPING CART -- MAKE IT FAST
		
		$tmp_goto = "https://" . $_SESSION['template']['company']['host_www'] . "/" . $_SESSION['gateway']['business']['account']['email_account_address'];
		logger("user goto: " . $tmp_goto);
		jump("javascript",$tmp_goto,0);
		exit;
	}



############################################################################################
### FACILITATE reTRY™ AUTHORIZATION NOW: 

} else {

	/// CHECK FOR "reTRY™ Authorization" ERRORS
	
	if(!$_SESSION['gateway']['incoming']['credit_card_number']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i><b>credit_card_number</b></i></b>. (You may not submit a NULL <i>credit_card_number</i> value.)<p>
		Please try again...";
		exit;
	}
	
	if(!$_SESSION['gateway']['incoming']['expiration_month']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i><b>expiration_month</b></i></b>. (You may not submit a NULL <i>expiration_month</i> value.)<p>
		Please try again...";
		exit;
	}
	
	if(!$_SESSION['gateway']['incoming']['expiration_year']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i><b>expiration_year</b></i></b>. (You may not submit a NULL <i>expiration_year</i> value.)<p>
		Please try again...";
		exit;
	}
	
	if(!$_SESSION['gateway']['incoming']['customer_ip']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i><b>customer_ip</b></i></b>. (You may not submit a NULL <i>customer_ip</i> value.)<p>
		Please try again...";
		exit;
	}

/*
	if(!$_SESSION['gateway']['incoming']['cvv']) {
		echo "<b>" . strtoupper($_SESSION['template']['company']['name']) . " GATEWAY ERROR:</b><p>
		Invalid <b><i><b>cvv</b></i></b>. (You may not submit a NULL <i>cvv</i> value.)<p>
		Please try again...";
		exit;
	}
*/		


	################################################################################
	### SEND TO PROCESSOR FOR "FINAL AUTHORIZATION":
	
	$processor_shared_api_business_account_number = $_SESSION['gateway']['incoming']['business_account_number'];
	$processor_shared_api_service = $_SESSION['gateway']['service'];  // (eg. "RETRY", "SHOPPING CART", "INVOICE", "STANDDARD")
	$processor_shared_api_gateway_name ="PAYVSION+BS";
	$processor_shared_api_gateway_action = "Authorize";  // value specific to the ['retry']['processor'] (i.e., payvision)
	$processor_shared_api_customer_ip = $_SESSION['gateway']['incoming']['customer_ip']; // actual customer ip, if possible. (otherwise using maxmind within the processor.)
	$processor_shared_api_amount = $_SESSION['gateway']['incoming']['amount'];
	$processor_shared_api_credit_card_number = $_SESSION['gateway']['incoming']['credit_card_number'];
	$processor_shared_api_cvv = $_SESSION['gateway']['incoming']['cvv'];
	$processor_shared_api_expiration_month = $_SESSION['gateway']['incoming']['expiration_month'];
	$processor_shared_api_expiration_year = $_SESSION['gateway']['incoming']['expiration_year'];
	$processor_shared_api_transactionId = null; // always blank for authorizations
	$processor_shared_api_transactionGuid = null; // always blank for authorizations
	include($_SESSION['template']['company']['file_path'] . "shared/process_payvision+bs.shared");
	// returns:
	// $_SESSION['gateway']['processor']['response']['raw'] == raw response
	// $_SESSION['gateway']['processor']['response']['array'] == array of response
	// $_SESSION['gateway']['processor']['response']['result'] == "AUTHORIZED" or "DECLINED"
	
	logger("gateway['processor']['response']['array']: "); logger($_SESSION['gateway']['processor']['response']['array']);
	
	
	################################################################################
	### SET RETRY RESULTS:
	$_SESSION['gateway']['incoming']['response'] = $_SESSION['gateway']['processor']['response'];
	
	// DISPLAY RESULTS:
	if($_SESSION['gateway']['incoming']['response']['result'] == "AUTHORIZED") {
		echo "YES";
	} else if($_SESSION['gateway']['incoming']['response']['result'] == "DECLINED") {
		echo "NO";
	}
	
	exit;


}


### END VERRIFIED SESSION
#
#
#
include($_SESSION['template']['company']['file_path'] . "shared/end_verified_session.shared");

?>