<?  logger3("



################################################################################
################################################################################
### NEW FILE:  [/BANGK!/shared/process_ecorepay.shared]  .................................


");
################################################################################
################################################################################
################################################################################




################################################################################################
### GET & SET API VARS:
$_SESSION['gateway']['processor']['service'] = $processor_shared_api_service;  // ("RETRY" or "STANDARD")
$_SESSION['gateway']['processor']['business_account_number'] = $processor_shared_api_business_account_number;
$_SESSION['gateway']['processor']['gateway_name'] = "PMD";  // $processor_shared_api_gateway_name;

if($processor_shared_api_gateway_action) {  // ("Payment", "Authorize", "Capture", "Void", "Refund") -- specific to processor (i.e., PAYVISION)
	$_SESSION['gateway']['processor']['gateway_action'] = $processor_shared_api_gateway_action;
} else {
	$_SESSION['gateway']['processor']['gateway_action'] = "Payment"; // default / straight-through
}

$_SESSION['gateway']['processor']['customer_ip'] = $processor_shared_api_customer_ip;
$_SESSION['gateway']['processor']['amount'] = $processor_shared_api_amount;

$_SESSION['gateway']['processor']['credit_card']['name_on_card'] = $processor_shared_api_name_on_card;
$_SESSION['gateway']['processor']['credit_card']['brand'] = $processor_shared_api_card_brand;
$_SESSION['gateway']['processor']['credit_card']['card_number'] = $processor_shared_api_credit_card_number;
$_SESSION['gateway']['processor']['credit_card']['cvv'] = $processor_shared_api_cvv;
$_SESSION['gateway']['processor']['credit_card']['expiration_month'] = $processor_shared_api_expiration_month;
$_SESSION['gateway']['processor']['credit_card']['expiration_year'] = $processor_shared_api_expiration_year;

$_SESSION['gateway']['processor']['transactionId'] = $processor_shared_api_transactionId;
$_SESSION['gateway']['processor']['transactionGuid'] = $processor_shared_api_transactionGuid;
$_SESSION['gateway']['processor']['emailaddress'] = $processor_shared_api_emailaddress;

//$_SESSION['gateway']['processor']['credit_card']['card_number'] = $processor_shared_api_credit_card_number;


### maxmind specific
# if($_SESSION['authorization']['phone']['country_code']==1) {
# 	$processor_shared_api_customer_telephone = substr($_SESSION['authorization']['phone']['number'],0.6);
# }

$_SESSION['gateway']['processor']['billing_city'] = $processor_shared_api_billing_city;
$_SESSION['gateway']['processor']['billing_region'] =  $processor_shared_api_billing_region;
$_SESSION['gateway']['processor']['billing_postal_code'] = $processor_shared_api_billing_postal_code;
$_SESSION['gateway']['processor']['billing_country'] = $processor_shared_api_billing_country;
$_SESSION['gateway']['processor']['customer_telephone'] = $processor_shared_api_customer_telephone;

/*
$_SESSION['gateway']['processor'][''] = "";
*/

### SET COMMON VARS:
$_SESSION['gateway']['processor']['geobytes'] = geobytes($processor_shared_api_customer_ip);
$_SESSION['gateway']['processor']['card_details'] = card_details($_SESSION['gateway']['processor']['credit_card']['card_number']);
$_SESSION['gateway']['processor']['business_account'] = account($_SESSION['gateway']['processor']['business_account_number']);

logger3("gateway['processor']['business_account_number']: ",$_SESSION['gateway']['processor']['business_account_number']);
logger3("gateway['processor']['business_account']: ",$_SESSION['gateway']['processor']['business_account']);





###







################################################################################################
### SET PREFERENCES:




################################################################################################
### SET MAXMIND PARAMETERS (GET CORRECT BIN COUNTRY)

### PREFERENCES:
//USERID: 55335
# $maxmind_url = "https://minfraud2.maxmind.com/app/ccv2r";
# $maxmind_license_key = "TuG3uxB3yvj4";
# $maxmind_requested_type = "premium";
# $maxmind_txn_type = "creditcard";
# 
# ### PARAMETERS:
# $_SESSION['gateway']['processor']['maxmind']['parameters'] = "";
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "i=" . $_SESSION['gateway']['processor']['customer_ip'];
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&city=" . $_SESSION['gateway']['processor']['billing_city'];
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&region=" . $_SESSION['gateway']['processor']['billing_region'];
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&postal=" . $_SESSION['gateway']['processor']['billing_postal_code'];
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&country=" . $_SESSION['gateway']['processor']['billing_country'];
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&custPhone=" . $_SESSION['gateway']['processor']['customer_telephone'];
# 
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&license_key=" . $maxmind_license_key;
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&requested_type=" . $maxmind_requested_type;
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&txn_type=" . $maxmind_txn_type;
# $_SESSION['gateway']['processor']['maxmind']['parameters'] .= "&bin=" . $_SESSION['gateway']['processor']['card_details']['bin'];
# 
# $_SESSION['gateway']['processor']['maxmind']['url'] = $maxmind_url;
# 
# $_SESSION['gateway']['processor']['maxmind']['results']['raw'] = processorCURLER($_SESSION['gateway']['processor']['maxmind']['url'],$_SESSION['gateway']['processor']['maxmind']['parameters']);
# 
# 
# 
# 
# 
# 
# 
# 
# logger3("gateway['processor']['maxmind']['url']: ",$_SESSION['gateway']['processor']['maxmind']['url']);
# logger3("gateway['processor']['maxmind']['parameters']: ",$_SESSION['gateway']['processor']['maxmind']['parameters']);
# logger3("gateway['processor']['maxmind']['results']['raw']: ",$_SESSION['gateway']['processor']['maxmind']['results']['raw']);
# 
# $_SESSION['gateway']['processor']['maxmind']['results']['explode'] = explode(";",$_SESSION['gateway']['processor']['maxmind']['results']['raw']);
# 
# $_SESSION['gateway']['processor']['maxmind']['binCountry'] = explode("=",$_SESSION['gateway']['processor']['maxmind']['results']['explode'][7]);
# 
# if($_SESSION['gateway']['processor']['maxmind']['binCountry'][1] != "") {
# 	
# 	$_SESSION['gateway']['processor']['country_ison'] = geo_data($_SESSION['gateway']['processor']['maxmind']['binCountry'][1],"iso2","numcode");
# } else {
# 	$_SESSION['gateway']['processor']['country_ison'] = $_SESSION['gateway']['processor']['geobytes']['ison'];
# }
# 
# if(strlen($_SESSION['gateway']['processor']['billing_country']) == 3) {
# 	$tmp_billing_country_iso2 = geo_data($_SESSION['gateway']['processor']['billing_country'],"iso3","iso2");
# } else {
# 	$tmp_billing_country_iso2 = $_SESSION['gateway']['processor']['billing_country'];
# }
# 
# $_SESSION['maxmind'] = maxmind($creditcard_bin=$_SESSION['gateway']['processor']['card_details']['bin'],$customer_ip=$_SESSION['gateway']['processor']['customer_ip'],$billing_city=$_SESSION['gateway']['processor']['billing_city'],$billing_region=$_SESSION['gateway']['processor']['billing_region'],$billing_postal_code=$_SESSION['gateway']['processor']['billing_postal_code'],$billing_country=$tmp_billing_country_iso2,$customer_telephone="");
# 
# 
# 
#
#
####################################################################################
####################################################################################





####################################################################################
####################################################################################
#
#
#  START: PMD INTERFACE




logger3("
##########################################################
### SEND TO PMD FOR PROCESSING:


",$x);


### LIVE:
//$tmp_GSP_gateway_url = "https://gwgsp.globalsecureprocessing.com/webservice/server.php?wsdl"; // LIVE
//$tmp_GSP_gateway_url = "https://gwsys1.globalsecureprocessing.com/webservice/server.php?wsdl"; // DEMO
//$tmp_GSP_gateway_url = $master_config_GSP_gateway;
//logger3("tmp_GSP_gateway_url",$tmp_GSP_gateway_url);

$merchant_id = '62';
logger3("merchant_id",$merchant_id);

$merchant_token = 'MID-62f8cot8gp93';
logger3("merchant_token",$merchant_token);

$traceID = '123445';
logger3("traceID",$traceID);

$hash = md5($merchant_id.$merchant_token.$traceID); 
logger3("hash",$hash);

$amount = $_SESSION['gateway']['processor']['amount'];
logger3("amount",$amount);

$currency = 'USD';
logger3("currency",$currency);

$customer_firstname = $_SESSION['payme']['customer']['firstname'];
logger3("customer_firstname",$customer_firstname);

$customer_lastname = $_SESSION['payme']['customer']['lastname'];
logger3("customer_lastname",$customer_lastname);


# if(!empty($_SESSION['gateway']['processor']['emailaddress'])) {
# 	$customer_email = $_SESSION['gateway']['processor']['emailaddress'];
# } else {
# 	$customer_email = hash_me()."_PMD@bangk.com";;
# }

$customer_email =  $_SESSION['gateway']['processor']['emailaddress'];
logger3("customer_email",$customer_email);

# if($_SESSION['authorization']['phone']['calling_code']) {
# 	$customer_phone = $_SESSION['authorization']['phone']['calling_code'] . $_SESSION['authorization']['phone']['number'];
# } else {
# 	$customer_phone = $_SESSION['payme']['customer']['voice_authorization']['calling_code'] . $_SESSION['payme']['customer']['voice_authorization']['number'];
# }
# 
$customer_phone = $_SESSION['gateway']['processor']['customer_telephone'];
logger3("000. customer_phone",$customer_phone);

/// PMD passes through info
//$customer_ip = $_SERVER['REMOTE_ADDR']; // current at processing

$customer_ip = $processor_shared_api_customer_ip;
logger3("customer_ip",$customer_ip);

$billing_street = $_SESSION['payme']['customer']['credit_card']['billing_street_1'] . "; " . $_SESSION['payme']['customer']['credit_card']['billing_street_2'];
logger3("billing_street",$billing_street);

if(!empty($_SESSION['payme']['customer']['credit_card']['billing_postalcode'])) {
	$billing_postalcode = $_SESSION['payme']['customer']['credit_card']['billing_postalcode'];
} else {
	$billing_postalcode = "000000";
}

logger3("billing_postalcode",$billing_postalcode);

if(!empty($_SESSION['payme']['customer']['credit_card']['billing_city'])) {
	$billing_city = $_SESSION['payme']['customer']['credit_card']['billing_city'];
} else {
	$billing_city = "City";
}
logger3("billing_city",$billing_city);

if(!empty($_SESSION['payme']['customer']['credit_card']['billing_state'])) {
	$billing_state = $_SESSION['payme']['customer']['credit_card']['billing_state'];
} else {
	$billing_state = "State";
}
logger3("billing_state",$billing_state);

if(!empty($_SESSION['payme']['customer']['credit_card']['billing_country'])) {
	$billing_country = $_SESSION['payme']['customer']['credit_card']['billing_country'];
} else {
	$billing_country = geo_data($_SESSION['authorization']['phone']['calling_code'],"Calling_code","iso2");
}

$billing_country = minimizeCOUNTRY($billing_country);
if($billing_country == "US") {
    $billing_state = minimizeSTATE($billing_state);        
}

logger3("billing_country",$billing_country);

$cc_number = $_SESSION['payme']['customer']['credit_card']['number'];
logger3("cc_number",$cc_number);

$cc_type = strtolower($_SESSION['payme']['customer']['credit_card']['brand']);
logger3("cc_type",$cc_type);

$cc_cardholder = $_SESSION['payme']['customer']['credit_card']['name_on_card'];
logger3("cc_cardholder",$cc_cardholder);

$cc_ex_month = $_SESSION['payme']['customer']['credit_card']['expiration_month'];
logger3("cc_ex_month",$cc_ex_month);

$cc_ex_year = $_SESSION['payme']['customer']['credit_card']['expiration_year'];
if(strlen($cc_ex_year) == 4) {
	$split_year = str_split($cc_ex_year);
	$cc_ex_year = $split_year[2] . $split_year[3];
}
logger3("cc_ex_year",$cc_ex_year);

$cc_ccv = $_SESSION['payme']['customer']['credit_card']['cvv'];
logger3("cc_ccv",$cc_ccv);

include($master_config_root_path . "shared/process_PMD_creditcard.shared");

// Get the Response
logger3("**** Response ****",$x);
logger3("TransactionID = " , $tmp_result['TransactionID']);
//logger3("Traceid = " , $tmp_result['trace_id']);
//logger3("Transaction amount = " , $tmp_result['transaction_amount']); 
//logger3("Transaction currency = " , $tmp_result['transaction_currency']);
logger3("Transaction Result = " , $tmp_result['result']);
logger3("Transaction Result Code = " , $tmp_result['ResponseCode']); 
logger3("Transaction Result Text = " , $tmp_result['StatusDescription']);



### SET SOME VARS:
$_SESSION['transaction']['PMD']['raw'] = $tmp_result;
logger3("000. _SESSION['transaction']['PMD']['raw']",$_SESSION['transaction']['PMD']['raw']);


#  END: PMD INTERFACE
#
#
####################################################################################
####################################################################################





####################################################################################
####################################################################################
### SET/UPDATE $_SESSION['transaction'][] SESSION VARS:
####################################################################################
####################################################################################









/// response
if(
($_SESSION['transaction']['PMD']['raw']['success'] == "1") // 1 = sucess @ PMD
) {
	$_SESSION['transaction']['response'] = "APPROVED";	
} else {
	$_SESSION['transaction']['response'] = "FAILED";
}

/// message
$_SESSION['transaction']['message'] = $_SESSION['transaction']['PMD']['raw']['message'];




logger3("transaction['response']: ",$_SESSION['transaction']['response']);
logger3("transaction['message']: ",$_SESSION['transaction']['message']);







####################################################################################
####################################################################################
### SET/UPDATE $_SESSION['gateway'][] SESSION VARS:
####################################################################################
####################################################################################


####################################################################################
/// link results:
$_SESSION['gateway']['processor']['response']['array'] =  $_SESSION['transaction']['PMD']['raw'];
$_SESSION['gateway']['processor']['response']['raw'] = $_SESSION['transaction']['PMD']['raw'];
####################################################################################




### set final result value
if(
($_SESSION['transaction']['PMD']['raw']['success'] == "1") // 1 = sucess @ PMD
) {
	if(stristr($_SESSION['gateway']['processor']['gateway_action'],"Authorize")) {
		$_SESSION['gateway']['processor']['response']['result'] = "AUTHORIZED";
	
	} else if(stristr($_SESSION['gateway']['processor']['gateway_action'],"Capture")) {
		$_SESSION['gateway']['processor']['response']['result'] = "CAPTURED";
	
	
	### STANDARD CREDIT CARD PAYMENT
	} else if(stristr($_SESSION['gateway']['processor']['gateway_action'],"Payment")) {
		$_SESSION['gateway']['processor']['response']['result'] = "APPROVED";
	
	} else {
		$_SESSION['gateway']['processor']['response']['result'] = NULL;
	}
	
} else {
	$_SESSION['gateway']['processor']['response']['result'] = "DECLINED";
}


logger3("_SESSION['gateway']['processor']['response']['result']",$_SESSION['gateway']['processor']['response']['result']);

### LOG TO DB:


$translog_translogid = "";
$translog_platform = $_SESSION['template']['company']['host_www'];
$translog_gatewayid = $_SESSION['gateway']['gatewayid'];
$translog_hash = make_hash("SHA1");
$translog_service = $_SESSION['gateway']['processor']['service'];
$translog_action = $_SESSION['gateway']['processor']['gateway_action'];
$translog_business_accountid = $_SESSION['payme']['business']['account']['accountid'];
$translog_business_account_number = $_SESSION['payme']['business']['account']['hash'];
$translog_memberid = $_SESSION['member']['memberid'];
$translog_accountid = $_SESSION['account']['accountid'];
$translog_acquirer = $_SESSION['gateway']['processor']['gateway_name'];
$translog_amount = currency_format($_SESSION['gateway']['processor']['amount'],null);

$translog_type = $_SESSION['gateway']['processor']['credit_card']['brand'];
$translog_name = $_SESSION['gateway']['processor']['credit_card']['name_on_card'];
$translog_number = $_SESSION['payme']['customer']['credit_card']['card_number'];
$translog_credit_card_number = $_SESSION['payme']['customer']['credit_card']['card_number'];
$translog_expdate = "";

$translog_expiration_month = $_SESSION['gateway']['processor']['credit_card']['expiration_month'];
$translog_expiration_year = $_SESSION['gateway']['processor']['credit_card']['expiration_year'];

$translog_streetaddress1 = $_SESSION['payme']['customer']['credit_card']['billing_street_1'];
$translog_streetaddress2 = $_SESSION['payme']['customer']['credit_card']['billing_street_2'];
$translog_city = $_SESSION['payme']['customer']['credit_card']['billing_city'];
$translog_region = $_SESSION['payme']['customer']['credit_card']['billing_state'];
$translog_country = $_SESSION['payme']['customer']['credit_card']['billing_country'];
$translog_postalcode = $_SESSION['payme']['customer']['credit_card']['billing_postalcode'];
$translog_phone = $_SESSION['gateway']['processor']['customer_telephone'];
$translog_cvv2 = $_SESSION['payme']['customer']['credit_card']['cvv'];
$translog_customer_emailaddress = $customer_email;

$translog_iswipe = "";
$translog_ordernumber = "";

$translog_response_raw = $_SESSION['transaction']['PMD']['raw']['ResponseCode'];
$translog_response = $_SESSION['transaction']['response'];
$translog_result = $_SESSION['transaction']['PMD']['raw']['result'];
$translog_status =  $_SESSION['transaction']['PMD']['raw']['StatusCode'];
$translog_message = $_SESSION['transaction']['PMD']['raw']['message'];
$translog_transactionid_from_gateway = $_SESSION['transaction']['PMD']['raw']['TransactionID'];

$translog_maxmind_response = addslashes($_SESSION['gateway']['processor']['maxmind']['results']['raw']);
$translog_maxmind_result = $_SESSION['gateway']['processor']['country_ison'];
$translog_payvision_transactionId = "";
$translog_payvision_transactionGuid = "";

$translog_error = "";
$translog_avs = "";
$translog_code = "";
$translog_note = "";
$translog_datetime_created = datetime();
$translog_datetime_updated = datetime();
$translog_UID = "";
$translog_ip = $processor_shared_api_customer_ip;

$tmp_sql = "INSERT INTO translog 
	(`translogid`,
	`platform`,
	`gatewayid`,
	`hash`,
	`service`,
	`action`,
	`business_accountid`,
	`business_account_number`,
	`memberid`, 
	`accountid`, 
	`acquirer`, 
	`amount`, 
	`type`, 
	`name`, 
	`number`,
	`credit_card_number`,
	`expdate`, 
	`expiration_month`,
	`expiration_year`,
	`streetaddress1`, 
	`streetaddress2`, 
	`city`, 
	`region`, 
	`country`, 
	`postalcode`, 
	`phone`, 
	`cvv2`, 
	`customer_emailaddress`,
	`iswipe`, 
	`ordernumber`,
	`response_raw`, 
	`response`, 
	`result`,
	`status`,
	`message`,
	`transactionid_from_gateway`,
	`maxmind_response`,
	`maxmind_result`,
	`payvision_transactionId`,
	`payvision_transactionGuid`,
	`error`, 
	`avs`, 
	`code`, 
	`note`, 
	`datetime_created`, 
	`datetime_updated`, 
	`UID`,
	`ip`) 
VALUES 
	('" . $translog_translogid . "', 
	'" . $translog_platform . "',
	'" . $translog_gatewayid . "',
	'" . $translog_hash . "', 
	'" . $translog_service . "', 
	'" . $translog_action . "', 
	'" . $translog_business_accountid . "', 
	'" . $translog_business_account_number . "',
	'" . $translog_memberid . "', 
	'" . $translog_accountid . "', 
	'" . $translog_acquirer . "', 
	'" . $translog_amount . "', 
	'" . $translog_type . "', 
	'" . $translog_name . "', 
	'" . $translog_number . "', 
	'" . $translog_credit_card_number . "',
	'" . $translog_expdate . "', 
	'" . $translog_expiration_month . "',
	'" . $translog_expiration_year . "',
	'" . $translog_streetaddress1 . "', 
	'" . $translog_streetaddress2 . "', 
	'" . $translog_city . "', 
	'" . $translog_region . "', 
	'" . $translog_country . "', 
	'" . $translog_postalcode . "', 
	'" . $translog_phone . "', 
	'" . $translog_cvv2 . "', 
	'" . $translog_customer_emailaddress . "', 
	'" . $translog_iswipe . "', 
	'" . $translog_ordernumber . "', 
	'" . $translog_response_raw . "', 
	'" . $translog_response . "', 
	'" . $translog_result . "',
	'" . $translog_status . "',
	'" . $translog_message . "', 
	'" . $translog_transactionid_from_gateway . "', 
	'" . $translog_maxmind_response . "',
	'" . $translog_maxmind_result . "',
	'" . $translog_payvision_transactionId . "',
	'" . $translog_payvision_transactionGuid . "',
	'" . $translog_error . "', 
	'" . $translog_avs . "', 
	'" . $translog_code . "', 
	'" . $translog_note . "', 
	'" . $translog_datetime_created . "', 
	'" . $translog_datetime_updated . "', 
	'" . $translog_UID . "',
	'" . $translog_ip . "') ;";

$translogid = $_SESSION['gateway']['processor']['translogid'] = insertTHECASHIER($tmp_sql);
logger3("tmp_sql",$tmp_sql);
logger3("gateway['processor']['translogid']: ",$_SESSION['gateway']['processor']['translogid']);


/// update gateway record
if($_SESSION['gateway']['gatewayid']) {

	$tmp_sql = "UPDATE gateway SET session_status = 'AUTHORIZED', datetime_updated = '" . $translog_datetime_updated . "' WHERE gatewayid = '" . $_SESSION['gateway']['gatewayid'] . "';";
	updateTHECASHIER($tmp_sql);
}


?>