<?php
/*
+--------------------------------------------------------------------------
|   CubeCart 4
|   ========================================
|	CubeCart is a Trade Mark of Devellion Limited
|   Copyright Devellion Limited 2005 - 2006. All rights reserved.
|   Devellion Limited,
|   5 Bridge Street,
|   Bishops Stortford,
|   HERTFORDSHIRE.
|   CM23 2JU
|   UNITED KINGDOM
|   http://www.devellion.com
|	UK Private Limited Company No. 5323904
|   ========================================
|   Web: http://www.cubecart.com
|   Date: Tuesday, 17th October 2006
|   Email: sales (at) cubecart (dot) com
|	License Type: CubeCart is NOT Open Source Software and Limitations Apply 
|   Licence Info: http://www.cubecart.com/v4-software-license
+--------------------------------------------------------------------------
|	form.inc.php
|   ========================================
|	PayPal Direct Payment Gateway
+--------------------------------------------------------------------------
*/
if (!defined('CC_INI_SET')) die("Access Denied");
if(detectSSL() == FALSE) die ("<strong>Critical Error:</strong> This page can ONLY be viewed under SSL!");

// Get Direct Payment module vars
$module = fetchDbConfig("PayPal_Pro");

if ($_GET['process']) {
	
	$gatewayName = sprintf("PayPal Website Payments Pro (%s)", $module['paymentAction']);
	
	if ($_POST['which_field']=="T") {
		$_POST['state'] = $_POST['county'];
	} elseif ($_POST['which_field']=="S") {
		$_POST['state'] = $_POST['county_sel'];
	}
	
	$countryIso = getCountryFormat($_POST['country'],"id","iso");
	if($countryIso == "US" || $countryIso == "CA") {
		$ISOstate = $db->select("SELECT `abbrev` FROM  `".$glob['dbprefix']."CubeCart_iso_counties` WHERE `name` = ".$db->mySQLSafe($_POST['state'])." AND `countryId` = ".$db->mySQLSafe($_POST['country']).";");	
	}
	$processState = ($ISOstate==true) ? $ISOstate[0]['abbrev'] : trim($_POST['state']);
	
	$transData['customer_id']	= $orderSum['customer_id'];
	$transData['order_id']		= $orderSum['cart_order_id'];
	$transData['amount']		= $orderSum['prod_total'];
	$transData['gateway']		= $gatewayName;
	$transData['extra']			= 'C';	
	
	##
	
	$cardNo			= trim($_POST['cardNumber']);
	$issueNo		= trim($_POST['issueNo']);
	$expireDate		= trim($_POST['expirationYear']).str_pad(trim($_POST['expirationMonth']), 2, '0', STR_PAD_LEFT);
	$expireFormat	= 6;
	$scReqd			= true;
	$securityCode	= trim($_POST['cvc2']);
	
	if ($config['defaultCurrency']== 'GBP') {
		$issueDate		= trim($_POST['issueYear']).str_pad(trim($_POST['issueMonth']), 2, '0', STR_PAD_LEFT);
		$issueFormat	= 6;
	} else {
		$issueDate		= false;
		$issueFormat	= false;
	}
	
	require("classes".CC_DS."validate".CC_DS."validateCard.php");
	
	$card = new validateCard();
	$card = $card->check($cardNo, $issueNo, $issueDate, $issueFormat, $expireDate, $expireFormat, $scReqd, $securityCode);

	if ($module['validation'] && $card['response'] == 'FAIL') {
		$errorMsg['customer']	= '';
		
		foreach ($card['error'] as $val) {
			$errorMsg['customer'] .= $val.'<br />';
		}
		
		$transData['trans_id']	= '';
		$transData['status']	= 'FAIL';
		$transData['notes']		= $errorMsg['customer'];
		
	} else {
	
		$extraNotes = '';
		$ppGate = substr($module['mode'],0,2);
		include("modules".CC_DS."altCheckout".CC_DS."PayPal_Pro".CC_DS."wpp-".$ppGate.CC_DS."DoDirectPaymentReceipt.php");
		
		if ($ack == 'SUCCESS') {
			
			// Sale = Funds taken immediately otherwise they may be taken later
			$paymentResult		= ($module['paymentAction'] == "Sale") ? 3 : 2; // 3 is payment complete, 2 is processing
			$confirmationScreen	= ($module['paymentAction'] == "Sale") ? 2 : 3; // Just to confuse matter Al you plonker!! 2 = money taken 3 = money not yet taken (Authorized)
			
			$order->orderStatus($paymentResult, $orderSum['cart_order_id']);
			
			$jumpTo = 'index.php?_g=co&_a=confirmed&s='.$confirmationScreen;
			
			$transData['trans_id']	= $resArray['TRANSACTIONID'];
			$transData['status']	= 'SUCCESS';
			$transData['notes']		= "Successful transaction via Credit Card. ".$extraNotes;
		## Fraud Management Filter Triggered
		} elseif ($ack == 'SuccessWithWarning' && $resArray['PAYMENTSTATUS'] == 'Pending' && $resArray['L_ERRORCODE0'] == 11610) {
			$transData['gateway'] = "PayPal Website Payments Pro (FMF Review)";
			$transData['status']	= 'PENDING';
			$transData['notes']		= "Error code: ".$resArray['L_ERRORCODE0'].". ".urldecode($resArray['L_SHORTMESSAGE0']);
			$jumpTo = 'index.php?_g=co&_a=confirmed&s=3';
		
		} else {
			$transData['trans_id']	= '';
			$transData['status']	= 'FAIL';
			$transData['notes']		= "<strong>Error displayed to customer:</strong> ".$errorMsg['customer']."<br /><strong>Error:</strong> ".$errorMsg['admin']."<br /><strong>PayPal Response:</strong> ".$errorMsg['paypal'];
		}
	
	}
	$order->storeTrans($transData);
	if (isset($jumpTo) && !empty($jumpTo)) {
		httpredir($jumpTo);
	}
}


$formTemplate = new XTemplate ("modules".CC_DS."altCheckout".CC_DS."PayPal_Pro".CC_DS."form.tpl", '', null, 'main', true, true);

if (isset($errorMsg)) {
	
	$formTemplate->assign("LANG_ERROR",$errorMsg['customer']);
	$formTemplate->parse("form.error");

}

$formTemplate->assign("VAL_AMOUNT_DUE",sprintf($lang['gateway']['amount_due'],priceformat($orderSum['prod_total'],true)));

$formTemplate->assign("VAL_FIRST_NAME",$cc_session->ccUserData['firstName']);
$formTemplate->assign("VAL_LAST_NAME",$cc_session->ccUserData['lastName']);
$formTemplate->assign("VAL_EMAIL_ADDRESS",$orderSum['email']);
$formTemplate->assign("VAL_ADD_1",$orderSum['add_1']);
$formTemplate->assign("VAL_ADD_2",$orderSum['add_2']);
$formTemplate->assign("VAL_CITY",$orderSum['town']);

$formTemplate->assign("LANG_STATE",$lang['gateway']['state']);

$jsScript = jsGeoLocationExtended("country", "county_sel", $lang['cart']['na'],"divCountySelect","divCountyText","county","which_field");
	
	
	if(isset($_POST['country'])) {
		$countryId = $_POST['country'];
	} else {
		$countryId = $cc_session->ccUserData['country'];
	}
	if(!isset($_POST['state'])) {
		$_POST['state'] = $cc_session->ccUserData['county'];
	}
 
	$counties = $db->select("SELECT name FROM  ".$glob['dbprefix']."CubeCart_iso_counties WHERE countryId = ".$db->mySQLSafe($countryId));
	
	$formTemplate->assign("VAL_DEL_COUNTY",sanitizeVar($_POST['state']));
	
	if (is_array($counties)) {
		$formTemplate->assign("VAL_COUNTY_SEL_STYLE", "style='display:block;'");
		$formTemplate->assign("VAL_COUNTY_TXT_STYLE", "style='display:none;'");
		$formTemplate->assign("VAL_COUNTY_WHICH_FIELD", "S");
	} else {
		$formTemplate->assign("VAL_COUNTY_SEL_STYLE", "style='display:none;'");
		$formTemplate->assign("VAL_COUNTY_TXT_STYLE", "style='display:block;'");
		$formTemplate->assign("VAL_COUNTY_WHICH_FIELD", "T");
	}
	$formTemplate->assign("JS_COUNTY_OPTIONS", '<script type="text/javascript">'.$jsScript."</script>");

	for($i=0; $i<count($counties); $i++) {

		if (strtolower($counties[$i]['name']) == strtolower($_POST['state'])) {
			$formTemplate->assign("COUNTY_SELECTED","selected='selected'");
		} else {
			$formTemplate->assign("COUNTY_SELECTED","");
		}

		$formTemplate->assign("VAL_DEL_COUNTY_ID",$counties[$i]['name']);

		$countyName = $counties[$i]['name'];
		
		if (strlen($countyName)>20) {
			$countyName = substr($countyName,0,20)."&hellip;";
		}

		$formTemplate->assign("VAL_DEL_COUNTY_NAME",$countyName);
		$formTemplate->parse("form.county_opts");
	}
	
	$countries = $db->select("SELECT id, printable_name FROM ".$glob['dbprefix']."CubeCart_iso_countries ORDER BY printable_name");
	
	for ($i=0; $i<count($countries); $i++) {
		
		if($countryId==$countries[$i]['id']) {
			$formTemplate->assign("COUNTRY_SELECTED","selected='selected'");
		} else {
			$formTemplate->assign("COUNTRY_SELECTED","");
		}
		
		$formTemplate->assign("VAL_COUNTRY_ID",$countries[$i]['id']);

		$countryName = "";
		$countryName = $countries[$i]['printable_name'];

		if (strlen($countryName)>20) {
			$countryName = substr($countryName,0,20)."&hellip;";
		}

		$formTemplate->assign("VAL_COUNTRY_NAME",$countryName);
		$formTemplate->parse("form.country_opts");
	
	} 
	
	//$formTemplate->assign("VAL_COUNTRY",$cc_session->ccUserData['country']);

$formTemplate->assign("VAL_POST_CODE",$orderSum['postcode']);
$formTemplate->assign("VAL_CART_ORDER_ID",$basket['cart_order_id']);
$formTemplate->assign("VAL_ORDER_TOTAL",$basket['grandTotal']);
$formTemplate->assign("VAL_ITEM_TOTAL",$basket['subTotal']);
$formTemplate->assign("VAL_TAX_TOTAL",$basket['tax']);
$formTemplate->assign("VAL_SHIPPING_TOTAL",$basket['shipCost']);


$currency = $db->select("SELECT currency FROM ".$glob['dbprefix']."CubeCart_sessions WHERE sessId = ".$db->mySQLSafe($GLOBALS[CC_SESSION_NAME]));

if($currency == TRUE && $currency[0]['currency'] != ''){
	$formTemplate->assign("VAL_CURRENCY_ID", $currency[0]['currency']);
} else {
	$formTemplate->assign("VAL_CURRENCY_ID", $config['defaultCurrency']);
}

if ($config['defaultCurrency'] == 'USD' && $module['mode']=='US' || $module['mode']=='USDPO') {
	
	$cards = array(
		"Visa" => "Visa", 
		"MasterCard" => "MasterCard",
		"Discover" => "Discover", 
		"Amex" => "American Express"
	);

} elseif($config['defaultCurrency'] == 'CAD' && $module['mode']=='US') {

	$cards = array(
		"Visa" => "Visa", 
		"MasterCard" => "MasterCard",
	);

} elseif($config['defaultCurrency'] == 'GBP' && $module['mode']=='US') {

	$cards = array(
		"Visa" => "Visa", 
		"MasterCard" => "MasterCard", 
		"Switch" => "Switch / Maestro",
		"Solo" => "Solo",
	);

## Legacy PayFlow Code 
} elseif($module['mode']=="UK") {
	
	$cards = array(
		"0" => "Visa",
		"8" => "Visa Electron",
		"8" => "Delta", 
		"1" => "MasterCard", 
		"9" => "Maestro / Switch", 
		"S" => "Solo"
	);
}

foreach($cards as $key => $value) {

	$formTemplate->assign("VAL_CARD_TYPE",$key);
	
	if($key == $_POST['cardType']){
		$formTemplate->assign("CARD_SELECTED","selected='selected'");
	} else {
		$formTemplate->assign("CARD_SELECTED","");
	}
	$formTemplate->assign("VAL_CARD_NAME",$value);
	
	$formTemplate->parse("form.repeat_cards");

}

for($i=1;$i<=12;$i++) {
	
	$val = sprintf('%02d',$i);
	
	if($val == $_POST['expirationMonth']){
		$formTemplate->assign("EXPIRE_MONTHS_SELECTED","selected='selected'");
	} elseif($val == date("j")) {
		$formTemplate->assign("EXPIRE_MONTHS_SELECTED","selected='selected'");
	} else {
		$formTemplate->assign("EXPIRE_MONTHS_SELECTED","");
	}
	$formTemplate->assign("VAL_EXPIRE_MONTH",$val);
	
	$formTemplate->parse("form.expiration_months");

}

$thisYear = date("Y");
$maxYear = $thisYear + 10;
$selectedYear = isset($_POST['expirationYear']) ? $_POST['expirationYear'] : ($thisYear+2);

for($i=$thisYear;$i<=$maxYear;$i++) {
	
	if($i == $selectedYear){
		$formTemplate->assign("EXPIRE_YEARS_SELECTED","selected='selected'");
	} else {
		$formTemplate->assign("EXPIRE_YEARS_SELECTED","");
	}
	$formTemplate->assign("VAL_EXPIRE_YEAR",$i);
	
	$formTemplate->parse("form.expiration_years");

}

$formTemplate->assign("LANG_CC_INFO_TITLE",$lang['gateway']['cc_info_title']);
$formTemplate->assign("LANG_FIRST_NAME",$lang['gateway']['first_name']); 
$formTemplate->assign("LANG_LAST_NAME",$lang['gateway']['last_name']); 
$formTemplate->assign("LANG_CARD_TYPE",$lang['gateway']['card_type']);
$formTemplate->assign("LANG_CARD_NUMBER",$lang['gateway']['card_number']);
$formTemplate->assign("LANG_EXPIRES",$lang['gateway']['lang_expires']);
$formTemplate->assign("LANG_MMYYYY",$lang['gateway']['mmyyyy']);
$formTemplate->assign("LANG_SECURITY_CODE",$lang['gateway']['security_code']);
$formTemplate->assign("LANG_CUST_INFO_TITLE",$lang['gateway']['customer_info']);
$formTemplate->assign("LANG_EMAIL",$lang['gateway']['email']);
$formTemplate->assign("LANG_ADDRESS",$lang['gateway']['address']);
$formTemplate->assign("LANG_CITY",$lang['gateway']['city']);
$formTemplate->assign("LANG_ZIPCODE",$lang['gateway']['zipcode']);
$formTemplate->assign("LANG_COUNTRY",$lang['gateway']['country']);
$formTemplate->assign("LANG_OPTIONAL",$lang['gateway']['optional']);

if($config['defaultCurrency'] == 'GBP'){

	for($i=1;$i<=12;$i++) {
	
		$val = sprintf('%02d',$i);
		
		if($val == $_POST['issueMonth']){
			$formTemplate->assign("ISSUE_MONTHS_SELECTED","selected='selected'");
		} else {
			$formTemplate->assign("ISSUE_MONTHS_SELECTED","");
		}
		$formTemplate->assign("VAL_ISSUE_MONTH",$val);
		
		$formTemplate->parse("form.issue_info.issue_months");

	}

	$minYear = $thisYear - 10;
	
	for($i = $thisYear; $i>=$minYear; $i--) {
		
		if($i == $_POST['issueYear']){
			$formTemplate->assign("ISSUE_YEARS_SELECTED","selected='selected'");
		} else {
			$formTemplate->assign("ISSUE_YEARS_SELECTED","");
		}
		$formTemplate->assign("VAL_ISSUE_YEAR",$i);
		
		$formTemplate->parse("form.issue_info.issue_years");

	}

	
	$formTemplate->assign("LANG_ISSUE_DATE", $lang['gateway']['issue_date']);
	$formTemplate->assign("LANG_ISSUE_NO", $lang['gateway']['issue_number']);
	$formTemplate->parse("form.issue_info");
}

$formTemplate->assign("VAL_GATEWAY","PayPal Website Payments Pro (".$module['paymentAction'].")");

$formTemplate->parse("form");
$formTemplate = $formTemplate->text("form");
?>
