<?php

### PLATFORM SESSION & FUNCTIONS:
require_once("/BANGK!/master_config.php");
require_once($master_config_root_path . "shared/functions.shared");
require_once($master_config_root_path . "shared/session.shared");
#######################################################################################


################################################################################
### API:
	/// $_SESSION['payme']['gateway_to_use']
	/// 
	/// $_SESSION['payme']['currency']['base']['total']
	///
	/// $_SESSION['payme']['customer']['credit_card']['number']
	/// $_SESSION['payme']['customer']['credit_card']['expiration_month']
	/// $_SESSION['payme']['customer']['credit_card']['expiration_year']
	/// $_SESSION['payme']['customer']['credit_card']['cvv']
	/// 
	/// $_SESSION['payme']['customer']['credit_card']['billing_city']
	/// $_SESSION['payme']['customer']['credit_card']['billing_state']
	/// $_SESSION['payme']['customer']['credit_card']['billing_postalcode']
	/// $_SESSION['payme']['customer']['credit_card']['billing_country']
	/// 
	/// $_SESSION['authorization']['phone']['number']
	/// $_SESSION['authorization']['phone']['calling_code']
################################################################################





################################################################################
################################################################################
### PROCESS TRANSACTION (SEND TO PROCESSOR)


# if(strlen($_SESSION['payme']['customer']['credit_card']['billing_country']) == 3) {
# 	$tmp_billing_country_iso2 = geo_data($_SESSION['payme']['customer']['credit_card']['billing_country'],"iso3","iso2");
# } else {
# 	$tmp_billing_country_iso2 = $_SESSION['payme']['customer']['credit_card']['billing_country'];
# }

if(!empty($_SESSION['session']['geo']['iso2'])) {
	$tmp_billing_country_iso2 = $_SESSION['session']['geo']['iso2'];
} else {
	$tmp_billing_country_iso2 = geo_data($_SESSION['authorization']['phone']['country_iso3'],"iso3","iso2");
}

$tmp_card_details = card_details($_SESSION['payme']['customer']['credit_card']['number']);

### GET MAXMIND RESULTS:
$_SESSION['maxmind'] = maxmind($creditcard_bin=$tmp_card_details['bin'],$customer_ip=$_SERVER['REMOTE_ADDR'],$billing_city=$_SESSION['payme']['customer']['credit_card']['billing_city'],$billing_region=$_SESSION['payme']['customer']['credit_card']['billing_state'],$billing_postal_code=$_SESSION['payme']['customer']['credit_card']['billing_postalcode'],$billing_country=$tmp_billing_country_iso2,$customer_telephone="");





/// TESTING
//logger3("payme['customer']['credit_card']['number']",$_SESSION['payme']['customer']['credit_card']);
if($testing) unset($_SESSION['transaction']['response']);
logger3("_SESSION['transaction']['response']",$_SESSION['transaction']['response']);



### MASTER_CONFIG_MAXMIND
if(
($master_config_maxmind_status == "OFF") ||
(empty($master_config_maxmind_status))
) {
	$_SESSION['maxmind']['country_match'] = "YES";
}

logger3("master_config_maxmind_status",$master_config_maxmind_status);
logger3("_SESSION['maxmind']",$_SESSION['maxmind']);
logger3("_SESSION['authorization']",$_SESSION['authorization']);

### GATEWAY:
/* (stristr($_SESSION['maxmind']['country_match'],"YES")) && */

### GET PHONE ISO2
$tmp_authorization_country_code_iso2 = geo_data($_SESSION['authorization']['phone']['calling_code'],"calling_code","iso2");
logger3("tmp_authorization_country_code_iso2",$tmp_authorization_country_code_iso2);


/// UNSET FOR LIVE:
logger3("_SESSION['transaction']['response']",$_SESSION['transaction']['response']);

//$_SESSION['transaction']['response'] = "";

if(
(empty($_SESSION['transaction']['response'])) &&


(

/* CALLING CODE == 1 */
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "US") ) ||

( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "CA") && ($_SESSION['maxmind']['bin_country'] == "CA") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "CA") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "CA") && ($_SESSION['maxmind']['bin_country'] == "US") ) ||

( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "DO") && ($_SESSION['maxmind']['bin_country'] == "DO") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "DO") && ($_SESSION['maxmind']['bin_country'] == "US") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "DO") ) ||

( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "PR") && ($_SESSION['maxmind']['bin_country'] == "PR") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "PR") && ($_SESSION['maxmind']['bin_country'] == "VI") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "PR") && ($_SESSION['maxmind']['bin_country'] == "US") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "PR") ) ||

( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "VI") && ($_SESSION['maxmind']['bin_country'] == "PR") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "VI") && ($_SESSION['maxmind']['bin_country'] == "US") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "VI") && ($_SESSION['maxmind']['bin_country'] == "VI") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "1") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "VI") ) ||


/* CALLING CODE == 44 (GB) */
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "GB") && ($_SESSION['maxmind']['bin_country'] == "GB") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "GS") && ($_SESSION['maxmind']['bin_country'] == "GS") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "GS") && ($_SESSION['maxmind']['bin_country'] == "GB") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "GB") && ($_SESSION['maxmind']['bin_country'] == "IE") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "IE") && ($_SESSION['maxmind']['bin_country'] == "IE") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "JE") && ($_SESSION['maxmind']['bin_country'] == "GB") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "44") && ($_SESSION['maxmind']['customer_country'] == "GB") && ($_SESSION['maxmind']['bin_country'] == "JE") ) ||

/* CALLING CODE == 353 (IRELAND) */
( ($_SESSION['authorization']['phone']['calling_code'] == "353") && ($_SESSION['maxmind']['customer_country'] == "GB") && ($_SESSION['maxmind']['bin_country'] == "GB") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "353") && ($_SESSION['maxmind']['customer_country'] == "IE") && ($_SESSION['maxmind']['bin_country'] == "IE") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "353") && ($_SESSION['maxmind']['customer_country'] == "GB") && ($_SESSION['maxmind']['bin_country'] == "IE") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "353") && ($_SESSION['maxmind']['customer_country'] == "IE") && ($_SESSION['maxmind']['bin_country'] == "GB") ) ||

/* CALLING CODE == 61 (AUSTRALIA) */
( ($_SESSION['authorization']['phone']['calling_code'] == "61") && ($_SESSION['maxmind']['customer_country'] == "GB") && ($_SESSION['maxmind']['bin_country'] == "AU") ) ||

/* THAILAND */
( ($_SESSION['authorization']['phone']['calling_code'] == "66") && ($_SESSION['maxmind']['bin_country'] == "US" || $_SESSION['maxmind']['bin_country'] == "AU" || $_SESSION['maxmind']['bin_country'] == "TH") ) ||


/* CALLING CODE == 41 (CH) */
( ($_SESSION['authorization']['phone']['calling_code'] == "41") && ($_SESSION['maxmind']['customer_country'] == "CH") && ($_SESSION['maxmind']['bin_country'] == "FR") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "41") && ($_SESSION['maxmind']['customer_country'] == "CH") && ($_SESSION['maxmind']['bin_country'] == "DE") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "41") && ($_SESSION['maxmind']['customer_country'] == "CH") && ($_SESSION['maxmind']['bin_country'] == "IT") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "41") && ($_SESSION['maxmind']['customer_country'] == "CH") && ($_SESSION['maxmind']['bin_country'] == "GB") ) ||
( ($_SESSION['authorization']['phone']['calling_code'] == "41") && ($_SESSION['maxmind']['customer_country'] == "CH") && ($_SESSION['maxmind']['bin_country'] == "CR") ) ||

/* CALLING CODE == 93 (Afg) */
( ($_SESSION['authorization']['phone']['calling_code'] == "93") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "US") ) ||


/* CALLING CODE == 60 (SG) */
( ($_SESSION['authorization']['phone']['calling_code'] == "60") && ($_SESSION['maxmind']['customer_country'] == "SG") && ($_SESSION['maxmind']['bin_country'] == "MY") ) ||



/* CALLING CODE == 880 (BD) */
//( ($_SESSION['authorization']['phone']['calling_code'] == "880") && ($_SESSION['maxmind']['customer_country'] == "US") && ($_SESSION['maxmind']['bin_country'] == "AU") ) ||
//( ($_SESSION['authorization']['phone']['calling_code'] == "880") && ($_SESSION['maxmind']['customer_country'] == "AU") && ($_SESSION['maxmind']['bin_country'] == "AU") ) ||



/* EVERYTHING MATCHED */
( ($_SESSION['maxmind']['customer_country'] == $_SESSION['maxmind']['bin_country']) && ($_SESSION['maxmind']['customer_country'] == $tmp_authorization_country_code_iso2) ) ||


/* ADD IP-BASED */
(stristr($_SERVER['REMOTE_ADDR'],"127.0.0.1")) ||
(stristr($_SERVER['REMOTE_ADDR'],"92.251.55")) ||
($_SERVER['REMOTE_ADDR'] == "0.0.0.1") /* finished */

) &&


/*
(!stristr($_SESSION['maxmind']['customer_country'],"IN")) && 
(!stristr($_SESSION['authorization']['phone']['calling_code'],"91")) &&

(!stristr($_SESSION['maxmind']['customer_country'],"PK")) && 
(!stristr($_SESSION['authorization']['phone']['calling_code'],"92")) &&
*/













(stristr($_SESSION['authorization']['type'],"CREDITCARD"))
) {
	$processor_shared_api_service = "STANDARD";
	$processor_shared_api_business_account_number = "";
	$processor_shared_api_gateway_name = $_SESSION['payme']['gateway_to_use'];
	$processor_shared_api_gateway_action = "PAYMENT";
	$processor_shared_api_customer_ip = $_SERVER['REMOTE_ADDR']; //$_SESSION['geodata']['ip']; // current @ login; $_SERVER['REMOTE_ADDR']; // current at live
	
	$processor_shared_api_last_4_ssn = $_SESSION['payme']['customer']['last_4_ssn'];

	$processor_shared_api_amount = $_SESSION['payme']['currency']['base']['total'];
	
	$processor_shared_api_credit_card_number = $_SESSION['payme']['customer']['credit_card']['card_number'];
	$processor_shared_api_expiration_month = $_SESSION['payme']['customer']['credit_card']['expiration_month'];
	$processor_shared_api_expiration_year = $_SESSION['payme']['customer']['credit_card']['expiration_year'];
	$processor_shared_api_cvv = $_SESSION['payme']['customer']['credit_card']['cvv'];
	
	$processor_shared_api_name_on_card = $_SESSION['payme']['customer']['credit_card']['name_on_card'];
	$processor_shared_api_card_brand = $_SESSION['payme']['customer']['credit_card']['brand'];
	
	$processor_shared_api_billing_city = $_SESSION['payme']['customer']['credit_card']['billing_city'];
	$processor_shared_api_billing_region = $_SESSION['payme']['customer']['credit_card']['billing_state'];
	$processor_shared_api_billing_postal_code = $_SESSION['payme']['customer']['credit_card']['billing_postalcode'];
	$processor_shared_api_billing_country = $_SESSION['payme']['customer']['credit_card']['billing_country'];
	$processor_shared_api_customer_telephone = $_SESSION['authorization']['phone']['calling_code']."/".$_SESSION['authorization']['phone']['number'];
	$processor_shared_api_emailaddress = $_SESSION['account']['emailaddress'];
	$processor_shared_api_DOB = str_replace(".","",$_SESSION['payme']['customer']['birth_date']);
	


	### START GATEWAY MANAGER: ##########################################################################
	### MASTER CONFIG OVERRIDE -- OD THIS FIRST:
	logger3("master_config_testing_credit_card_transaction",$master_config_testing_credit_card_transaction);
	//logger3("master_config_testing_credit_card_transaction",$master_config_testing_credit_card_transaction);
	
	if( // forces approvals for testing
	(empty($master_config_testing_credit_card_transaction)) ||
	(stristr($master_config_testing_credit_card_transaction,"OFF"))
	) {
		//if(!empty($master_config_force_gateway_cc)) $_SESSION['payme']['gateway_to_use'] = $master_config_force_gateway_cc;
		/// force ECP
		logger3("_SESSION['payme']['gateway_to_use']",$_SESSION['payme']['gateway_to_use']);
		//exit;
		
		
		if(stristr($_SESSION['payme']['gateway_to_use'],"ECP")) {
		
			
		
			include($_SESSION['template']['company']['file_path'] . "shared/process_ecorepay.shared");
		
			/// RETRY:
			### if(
			### (!empty($master_config_retry_gateway)) &&
			### (empty($master_config_force_gateway_cc)) &&
			### (!stristr($_SESSION['transaction']['response'],"APPROVED")) &&
			### ($processor_shared_api_credit_card_number[0] != "5")
			### ) {	
			### 	//include($_SESSION['template']['company']['file_path'] . "shared/process_payvision+bs.shared");
			### 	$payment_gateway_to_use = $master_config_retry_gateway;
			### 	include($_SESSION['template']['company']['file_path'] . "shared/". $payment_gateway_to_use.".shared");
			### }
			
		} else {
		
			// default primary
			//include($_SESSION['template']['company']['file_path'] . "shared/process_payvision+bs.shared");
			
			// default backup/secondary:
			// if($_SESSION['transaction']['response'] != "APPROVED") {	
			// 	include($_SESSION['template']['company']['file_path'] . "shared/process_ecorepay.shared");
			// }
			
			$payment_gateway_to_use = $_SESSION['payme']['gateway_to_use'];
			include($_SESSION['template']['company']['file_path'] . "shared/process_". $payment_gateway_to_use .".shared");
			
			
			
		}
	}
	### END GATEWY MANAGER ##########################################################################
	
	//if($gsp_communication_status == "FAILED") {
		
	$_SESSION['gateway']['processor']['translogid'] = $translogid; // from processing gateway
	$_SESSION['transaction']['deposit']['translogid'] = $translogid; // from processing gateway
	
	
	logger3("_SESSION['gateway']['processor']['translogid']",$_SESSION['gateway']['processor']['translogid']);
	///
	logger3("transaction['process_payvision']['results']…",$_SESSION['transaction']['process_payvision']['results']);
	logger3("transaction['process_payvision']['gateway']['code']: ",$_SESSION['transaction']['process_payvision']['gateway']['code']);
	logger3("transaction['process_payvision']['gateway']['result']: ",$_SESSION['transaction']['process_payvision']['gateway']['result']);
	logger3("transaction['process_payvision']['gateway']['message']: ",$_SESSION['transaction']['process_payvision']['gateway']['message']);
	
	logger3("transaction['process_payvision']['bank']['code']: ",$_SESSION['transaction']['process_payvision']['bank']['code']);
	logger3("transaction['process_payvision']['bank']['result']: " .$_SESSION['transaction']['process_payvision']['bank']['result']);
	logger3("transaction['process_payvision']['bank']['message']: ",$_SESSION['transaction']['process_payvision']['bank']['message']);
	logger3("transaction['process_payvision']['bank']['transaction_id']: ",$_SESSION['transaction']['process_payvision']['bank']['transaction_id']);
} else {
	$maxmind_rejection = TRUE;
}






################################################################################
################################################################################
### LOGGER


logger3("## MEMBER:",$member);
logger3("### ACCOUNT:",$account);

logger3("### SESSION:",$session);
logger3("### TRANSACTION:",$transaction);
logger3("### PAYME:",$payme);


logger3("transaction['response']: ",$_SESSION['transaction']['response']); // set in process_payvision.shared
logger3("transaction[action]: ",$_SESSION['transaction']['action']);
logger3("authorization[status]: ",$_SESSION['authorization']['status']);
logger3("transaction[status]: ",$_SESSION['transaction']['status']);

# logger3("0. transaction['payvision_bs_results'][Cdc][CdcEntry][0][Items][CdcEntryItem][0][Value]: " , $_SESSION['transaction']['payvision_bs_results'][Cdc][CdcEntry][0][Items][CdcEntryItem][0][Value]);





################################################################################
################################################################################
### APPROVED !!!

//logger3("1. payme['status']",$_SESSION['payme']['status']);

/// LIVE DEMO!!!
logger3("payme['customer']['credit_card']['number']",$_SESSION['payme']['customer']['credit_card']['number']);
logger3("payme['customer']['credit_card']['cvv']",$_SESSION['payme']['customer']['credit_card']['cvv']);
logger3("payme['customer']['credit_card']['expiration_month']",$_SESSION['payme']['customer']['credit_card']['expiration_month']);
logger3("payme['customer']['credit_card']['expiration_year']",$_SESSION['payme']['customer']['credit_card']['expiration_year']);


	
# if(
#     (
#     ($_SESSION['payme']['customer']['credit_card']['number'] == "4000000000000000")
#     /* || ($_SESSION['payme']['customer']['credit_card']['number'] == "4444333322221111") */
#     ) &&
#     ($_SESSION['payme']['customer']['credit_card']['cvv'] == "000")
#     ) {
#     $_SESSION['transaction']['response'] = "APPROVED";
#     $_SESSION['transaction']['results'] = "APPROVED";
#     $_SESSION['transaction']['action'] = "process.deposit.creditcard";
#     //$_SESSION['transaction']['action'] = "process.outgoing";
# }



/// REPEAT TRANSACTIONS -- FOR TESTING///
if($_SESSION['testing']) $_SESSION['transaction']['status'] = "ACTIVE";


### MASTER CONFIG OVERRIDE -- OD THIS FIRST:
if(
(stristr($master_config_testing_credit_card_transaction,"APPROVED"))
) {
	$_SESSION['transaction']['response'] = "APPROVED";
    $_SESSION['transaction']['results'] = "APPROVED";
    $_SESSION['transaction']['action'] = "process.deposit.creditcard";

} else if(
(stristr($master_config_testing_credit_card_transaction,"DECLINED"))
) {
	$_SESSION['transaction']['response'] = "DECLINED";
	$_SESSION['transaction']['results'] = "DECLINED";
	

### REACT TO MAXMIND RESULTS
} else if($maxmind_rejection == TRUE) {

	$_SESSION['transaction']['response'] = "DECLINED";
	$_SESSION['transaction']['results'] = "DECLINED";
	$_SESSION['transaction']['message'] = "Unable to Authenticate Cardholder Identity";


### STANDARD PAYMENT APPROVED
} else if(
(stristr($_SESSION['gateway']['processor']['response']['result'],"APPROVED"))
) {
	$_SESSION['transaction']['results'] = "APPROVED";


### STANDARD PAYMENT DECLINED
} else {

	$_SESSION['transaction']['results'] = "DECLINED";
	$_SESSION['transaction']['message'] = "Due to heightened security, <u>e-commerce transactions are being blocked by your bank</u>. &nbsp; To complete this payment, please call your bank (note: your bank's Customer Support telephone number is located <u>on the back of your credit card</u>), and instruct your bank to \"ALLOW this transaction\". &nbsp; Then... please RETURN to ".$_SESSION['template']['company']['name'].", and authorize a new payment to complete this purchase. &nbsp; (<i><b>Alternatively</b></i>, please use another credit card to complete this transaction now.)";
	
}

logger3("SET: _SESSION['transaction']['results']",$_SESSION['transaction']['results']);
logger3("_SESSION['transaction']['response']",$_SESSION['transaction']['response']);
logger3("_SESSION['transaction']['status']",$_SESSION['transaction']['status']);



logger3("_SESSION['authorization']['type']",$_SESSION['authorization']['type']);
logger3("_SESSION['session']['geo']['iso3']",$_SESSION['session']['geo']['iso3']);
logger3("SESSION['authorization']['phone']['country']",$_SESSION['authorization']['phone']['country']);


?>