<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php"><font color="blue">log in here.</font></a>';
	exit;
	}
 if($_SESSION['roladmin'] ==1){$roladmin=1;}
//require_once ('mysql_connect.php');
$action          = $_GET["a"];
include ('includes/admin-config.php');
	//echo 'Action = ' . $action; (for testing)
//require_once ('config.inc');




if ($action == "update") {

		$query = "UPDATE storePrefs SET ";

		
			If (($_POST['processor_type']=="3")){
				//payflow processor selected - update payflow fields
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", payflow_user='" . $_POST['payflow_user'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " payflow_user='" . $_POST['payflow_user'] . "'";
				}
				
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", payflow_partner='" . $_POST['payflow_partner'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " payflow_partner='" . $_POST['payflow_partner'] . "'";
				}
				
				If (!empty($_POST['payflow_password'])){
					$payflow_pwd = str_replace("*", "", $_POST['payflow_password']);
					if(!empty($payflow_pwd)){
						if(!empty($sv)) {  //sw clause exists, add to it
							$sv .= ", payflow_password='" . $payflow_pwd . "'";
						} else { //sw clause does not exist, create it now
							$sv .= " payflow_password='" . $payflow_pwd . "'";
						}
					}
				}
				
				If (!empty($_POST['payflow_test_mode'])){ //field is populated, need to update data
					if(!empty($sv)) {  //sw clause exists, add to it
						$sv .= ", payflow_test_mode='1'";
					} else { //sw clause does not exist, create it now
						$sv .= " payflow_test_mode='1'";
					}
				} else {
					if(!empty($sv)) {  //sw clause exists, add to it
						$sv .= ", payflow_test_mode='0'";
					} else { //sw clause does not exist, create it now
						$sv .= " payflow_test_mode='0'";
					}
				}
			}
			
			If (($_POST['processor_type']=="4")){
				//payflow processor selected - update payflow fields
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", paypal_pro_username='" . $_POST['paypal_pro_username'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " paypal_pro_username='" . $_POST['paypal_pro_username'] . "'";
				}
				
				
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", paypal_pro_signature='" . $_POST['paypal_pro_signature'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " paypal_pro_signature='" . $_POST['paypal_pro_signature'] . "'";
				}
				
				If (!empty($_POST['paypal_pro_password'])){
					$payflow_pwd = str_replace("*", "", $_POST['paypal_pro_password']);
					if(!empty($payflow_pwd)){
						if(!empty($sv)) {  //sw clause exists, add to it
							$sv .= ", paypal_pro_password='" . $payflow_pwd . "'";
						} else { //sw clause does not exist, create it now
							$sv .= " paypal_pro_password='" . $payflow_pwd . "'";
						}
					}
				}
				
				If (!empty($_POST['payflow_test_mode'])){ //field is populated, need to update data
					if(!empty($sv)) {  //sw clause exists, add to it
						$sv .= ", payflow_test_mode='1'";
					} else { //sw clause does not exist, create it now
						$sv .= " payflow_test_mode='1'";
					}
				} else {
					if(!empty($sv)) {  //sw clause exists, add to it
						$sv .= ", payflow_test_mode='0'";
					} else { //sw clause does not exist, create it now
						$sv .= " payflow_test_mode='0'";
					}
				}
			}
			//update worlpay API keys if used
			If (($_POST['processor_type']=="5")){
				//worldpay processor selected - update payflow fields
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", worldpay_servicekey='" . $_POST['worldpay_servicekey'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " worldpay_servicekey='" . $_POST['worldpay_servicekey'] . "'";
				}
				
				
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", worldpay_clientkey='" . $_POST['worldpay_clientkey'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " worldpay_clientkey='" . $_POST['worldpay_clientkey'] . "'";
				}
				
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", wp_securenet_id='" . $_POST['wp_securenet_id'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " wp_securenet_id='" . $_POST['wp_securenet_id'] . "'";
				}
				
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", wp_securekey='" . $_POST['wp_securekey'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " wp_securekey='" . $_POST['wp_securekey'] . "'";
				}
			}
			If (($_POST['processor_type']=="8")){
				//LinkPOS processor selected - update payflow fields
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", linkpos_company='" . $_POST['linkpos_company'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " linkpos_company='" . $_POST['linkpos_company'] . "'";
				}
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", linkpos_store_name='" . $_POST['linkpos_store_name'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " linkpos_store_name='" . $_POST['linkpos_store_name'] . "'";
				}
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", linkpos_store_pwd='" . $_POST['linkpos_store_pwd'] . "'";
				} else { //sw clause does not exist, create it now
					$sv .= " linkpos_store_pwd='" . $_POST['linkpos_store_pwd'] . "'";
				}
			}
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", salesTax='" . $_POST['salesTax'] . "'";
			} else { //sw clause does not exist, create it now
				$sv .= " salesTax='" . $_POST['salesTax'] . "'";
			}
		
			If (!empty($_POST['chkDeltax'])){ //field is populated, need to update data
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", taxdelivery='1'";
				} else { //sw clause does not exist, create it now
					$sv .= " taxdelivery='1'";
				}
			} else {
				if(!empty($sv)) {  //sw clause exists, add to it
					$sv .= ", taxdelivery='0'";
				} else { //sw clause does not exist, create it now
					$sv .= " taxdelivery='0'";
				}
			}
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", cc_processor='" . $_POST['processor'] . "'";
			} else { //sw clause does not exist, create it now
				$sv .= " cc_processor='" . $_POST['processor'] . "'";
			}
			
			 
			
			


		If (!empty($_POST['chkChecks'])){ //field is populated, need to update data
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", acceptChecks='1'";
			} else { //sw clause does not exist, create it now
				$sv .= " acceptChecks='1'";
			}
		} else {
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", acceptChecks='0'";
			} else { //sw clause does not exist, create it now
				$sv .= " acceptChecks='0'";
			}
		}
		
		If (!empty($_POST['chkCash'])){ //field is populated, need to update data
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", allow_cash_pymt='1'";
			} else { //sw clause does not exist, create it now
				$sv .= " allow_cash_pymt='1'";
			}
		} else {
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", allow_cash_pymt='0'";
			} else { //sw clause does not exist, create it now
				$sv .= " allow_cash_pymt='0'";
			}
		}
		
		If (!empty($_POST['chkMemberID'])){ //field is populated, need to update data
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", allow_id_pymt='1'";
			} else { //sw clause does not exist, create it now
				$sv .= " allow_id_pymt='1'";
			}
		} else {
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", allow_id_pymt='0'";
			} else { //sw clause does not exist, create it now
				$sv .= " allow_id_pymt='0'";
			}
		}
		If (!empty($_POST['paypal_auth'])){ //field is populated, need to update data
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", paypal_auth='1'";
			} else { //sw clause does not exist, create it now
				$sv .= " paypal_auth='1'";
			}
		} else {
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", paypal_auth='0'";
			} else { //sw clause does not exist, create it now
				$sv .= " paypal_auth='0'";
			}
		}
		/*
		If (!empty($_POST['chkTipline'])){ //field is populated, need to update data
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", include_tipline='1'";
			} else { //sw clause does not exist, create it now
				$sv .= " include_tipline='1'";
			}
		} else {
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", include_tipline='0'";
			} else { //sw clause does not exist, create it now
				$sv .= " include_tipline='0'";
			}
		}
		*/
		
		If (($_POST['processor_type']=="2")){
			if(!empty($sv)) {  //sw clause exists, add to it
				$sv .= ", paypalid='" . $_POST['paypalid'] . "'";
			} else { //sw clause does not exist, create it now
				$sv .= " paypalid='" . $_POST['paypalid'] . "'";
			}
		}
		
if ($sv !==""){
	$query .= $sv;

}
//query is created, now execute:
if (!mysql_query($query)){
					echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
			//echo $query .'<br>';	//for testing
				echo "<div align=center><font color=red>Payment preferences updated successfully.</font></div>";
				
			}		


	}	//end of update query

		$query = "SELECT * from storePrefs";


		$rt=mysql_query($query);   
		echo mysql_error();             




echo '<div align="center"><table border=0 cellpadding=0 cellspacing=8>';




//record found - 
echo '<tr><td><form action="' . $_SERVER['PHP_SELF'] . '?a=update&key=' . $nt[recordKey] . '" method="post">';
while($nt=mysql_fetch_array($rt)){
	if($nt[legacy_ordering_ok]=='1'){
		$manual_processing = TRUE;
	} else {
		$manual_processing = FALSE;
	}
	echo '<tr><td valign="top">Accept checks: <td valign="top"><input type="checkbox" name="chkChecks"';
		if(!empty($nt[acceptChecks])){
			echo 'checked=true';
		} 
	echo '><td><img src="images/question.png" title="Check this box to let customers know that you accept checks.">';  

echo '<tr><td valign="top">Accept cash: <td valign="top"><input type="checkbox" name="chkCash"';
		if(!empty($nt[allow_cash_pymt])){
			echo 'checked=true';
		} 
	echo '><td><img src="images/question.png" title="Check this box to allow cash for online orders.">';  
	
	echo '<tr><td valign="top">Accept member ID: <td valign="top"><input type="checkbox" name="chkMemberID"';
		if(!empty($nt[allow_id_pymt])){
			echo 'checked=true';
		} 
	echo '><td><img src="images/question.png" title="Check this box to allow customers to pay using a member ID.">';  

	if(!empty($nt[orderOnline])){	//only show these options if online ordering is activated
		echo '<tr><td valign="top">Credit Card Processor: <td valign="top">
		<select name="processor">
		<option value="0"';
		if($nt[cc_processor]=="0"){echo "selected=selected";}
		echo '>None</option>';
		if($manual_processing == TRUE){	//this is the original, non-PCI compliant pymt method - do not typically allow anymore
			echo'<option value="1"';
			if($nt[cc_processor]=="1"){echo "selected=selected";}
			echo '>Manual</option>';
		}
		echo'<option value="2"';
		if($nt[cc_processor]=="2"){echo "selected=selected";}
		echo '>PayPal Express</option>
		<option value="3"';
		if($nt[cc_processor]=="3"){echo "selected=selected";}
		echo '>PayPal PayFlow</option>
		<option value="4"';
		if($nt[cc_processor]=="4"){echo "selected=selected";}
		echo '>PayPal Payments Pro</option>
		<option value="5"';
		if($nt[cc_processor]=="5"){echo "selected=selected";}
		echo '>WorldPay</option>
		<option value="6"';
		if($nt[cc_processor]=="6"){echo "selected=selected";}
		echo '>Heartland</option>
		<option value="7"';
		if($nt[cc_processor]=="7"){echo "selected=selected";}
		echo '>Authorize.net</option>
		<option value="8"';
		if($nt[cc_processor]=="8"){echo "selected=selected";}
		echo '>LinkPOS</option>
		</select>
		<td><img src="images/question.png" title="Select the method used to handle credit card payments.">';
		if($nt[cc_processor]=="2"){
			echo '<tr><td valign="top">PayPal ID: <input type="hidden" name="processor_type" value="2"><td valign="top"><input type="text" name="paypalid" size="30" value="' . $nt[paypalid] . '"><td><img src="images/question.png" title="Enter your PayPal ID here. This will be the email address you have registered to your PayPal account.">';
			echo '<tr><td valign="top">Use PayPal Auth: <td valign="top"><input type="checkbox" name="paypal_auth"';
			if(!empty($nt[paypal_auth])){
				echo 'checked=true';
			} 
			echo '><td><img src="images/question.png" title="This option places authorizations when customers check out via paypal, instead of collecting final payment. This allows you to add up to 15% tip before capturing.">';  
			echo '<tr><td><td width="65%"><font color="red"><i>If using PayPal, this should be your PayPal email address. If no PayPal ID is entered, customer payment info will be transmitted directly to you with the order, to be processed manually.</i></font>';
		}
		if($nt[cc_processor]=="3"){
			echo '<tr><td valign="top">PayFlow User: <input type="hidden" name="processor_type" value="3"><td valign="top"><input type="text" name="payflow_user" size="30" value="' . $nt[payflow_user] . '"><td><img src="images/question.png" title="Enter your PayFlow User ID here.">';
			echo '<tr><td valign="top">PayFlow Password: <td valign="top"><input type="text" name="payflow_password" size="30" value="********"><td><img src="images/question.png" title="Enter your PayFlow Password here.">';
			echo '<tr><td valign="top">PayFlow Partner: <td valign="top"><input type="text" name="payflow_partner" size="30" value="' . $nt[payflow_partner] . '"><td><img src="images/question.png" title="Enter your PayFlow Partner ID here.">';
			echo '<tr><td valign="top">Test Mode: <td valign="top"><input type="checkbox" name="payflow_test_mode"';
			if(!empty($nt[payflow_test_mode])){
				echo 'checked=true';
			} 
			echo '><td><img src="images/question.png" title="This option puts your card processing in test mode.">';  

		}
		if($nt[cc_processor]=="4"){
			echo '<tr><td valign="top">PayPal Pro Username: <input type="hidden" name="processor_type" value="4"><td valign="top"><input type="text" name="paypal_pro_username" size="30" value="' . $nt[paypal_pro_username] . '"><td><img src="images/question.png" title="Enter your PayPal Pro Username here.">';
			echo '<tr><td valign="top">PayPal Pro Password: <td valign="top"><input type="text" name="paypal_pro_password" size="30" value="********"><td><img src="images/question.png" title="Enter your PayPal Pro Password here.">';
			echo '<tr><td valign="top">PayPal API Signature: <td valign="top"><input type="text" name="paypal_pro_signature" size="30" value="' . $nt[paypal_pro_signature] . '"><td><img src="images/question.png" title="Enter your PayPal API Signature here.">';
			echo '<tr><td valign="top">Test Mode: <td valign="top"><input type="checkbox" name="payflow_test_mode"';
			if(!empty($nt[payflow_test_mode])){
				echo 'checked=true';
			} 
			echo '><td><img src="images/question.png" title="This option puts your card processing in test mode.">';  

		}
		if($nt[cc_processor]=="5"){
			echo '<tr><td valign="top">Worldpay Client key: <input type="hidden" name="processor_type" value="5"><td valign="top"><input type="text" name="worldpay_clientkey" size="30" value="' . $nt[worldpay_clientkey] . '"><td><img src="images/question.png" title="Enter your Worldpay API client key here.">';
			echo '<tr><td valign="top">Worldpay Service key: <td valign="top"><input type="text" name="worldpay_servicekey" size="30" value="' . $nt[worldpay_servicekey] . '"><td><img src="images/question.png" title="Enter your Worldpay API service key here.">';
			echo '<tr><td valign="top">Worldpay SecureNet ID: <td valign="top"><input type="text" name="wp_securenet_id" size="30" value="' . $nt[wp_securenet_id] . '"><td><img src="images/question.png" title="Enter your Worldpay SecureNet ID here.">';
			echo '<tr><td valign="top">Worldpay Security key: <td valign="top"><input type="text" name="wp_securekey" size="30" value="' . $nt[wp_securekey] . '"><td><img src="images/question.png" title="Enter your Worldpay security key here.">';
			$snid = $nt[wp_securenet_id];
			$skey = $nt[wp_securekey];
			if($snid!="" && $skey!=""){
				$authid = "Basic " . base64_encode("$snid:$skey") ; 
				//save $authid to db
				$sql ="update storePrefs set wp_authid = '$authid'";
				mysql_query($sql);
				
			}
			if($authid!=""){
				//if just changed, show new authid
				$display_authid=$authid;
			} else {
				//else show the previous value
				$display_authid=$nt[wp_authid];
			}
			echo "<tr><td valign='top'>Auth ID: <td valign='top'>$display_authid";
			//echo '<tr><td valign="top">Test Mode: <td valign="top"><input type="checkbox" name="payflow_test_mode"';
			//if(!empty($nt[payflow_test_mode])){
			//	echo 'checked=true';
			//} 
			//echo '><td><img src="images/question.png" title="This option puts your card processing in test mode.">';  

		}
		if($nt[cc_processor]=="6"){
			echo '<tr><td valign="top">Heartland API key: <input type="hidden" name="processor_type" value="6"><td valign="top"><input type="text" name="paypal_pro_username" size="30" value="' . $nt[paypal_pro_username] . '"><td><img src="images/question.png" title="Enter your PayPal Pro Username here.">';
			//echo '<tr><td valign="top">PayPal Pro Password: <td valign="top"><input type="text" name="paypal_pro_password" size="30" value="********"><td><img src="images/question.png" title="Enter your PayPal Pro Password here.">';
			//echo '<tr><td valign="top">PayPal API Signature: <td valign="top"><input type="text" name="paypal_pro_signature" size="30" value="' . $nt[paypal_pro_signature] . '"><td><img src="images/question.png" title="Enter your PayPal API Signature here.">';
			//echo '<tr><td valign="top">Test Mode: <td valign="top"><input type="checkbox" name="payflow_test_mode"';
			//if(!empty($nt[payflow_test_mode])){
			//	echo 'checked=true';
			//} 
			//echo '><td><img src="images/question.png" title="This option puts your card processing in test mode.">';  

		}
		if($nt[cc_processor]=="7"){
			echo '<tr><td valign="top">Authorize.net API key: <input type="hidden" name="processor_type" value="7"><td valign="top"><input type="text" name="paypal_pro_username" size="30" value="' . $nt[paypal_pro_username] . '"><td><img src="images/question.png" title="Enter your PayPal Pro Username here.">';
			//echo '<tr><td valign="top">PayPal Pro Password: <td valign="top"><input type="text" name="paypal_pro_password" size="30" value="********"><td><img src="images/question.png" title="Enter your PayPal Pro Password here.">';
			//echo '<tr><td valign="top">PayPal API Signature: <td valign="top"><input type="text" name="paypal_pro_signature" size="30" value="' . $nt[paypal_pro_signature] . '"><td><img src="images/question.png" title="Enter your PayPal API Signature here.">';
			//echo '<tr><td valign="top">Test Mode: <td valign="top"><input type="checkbox" name="payflow_test_mode"';
			//if(!empty($nt[payflow_test_mode])){
			//	echo 'checked=true';
			//} 
			//echo '><td><img src="images/question.png" title="This option puts your card processing in test mode.">';  

		}
		if($nt[cc_processor]=="8"){ //LinkPOS
			echo '<tr><td valign="top">Company name: <input type="hidden" name="processor_type" value="8"><td valign="top"><input type="text" name="linkpos_company" size="30" value="' . $nt[linkpos_company] .'"><td><img src="images/question.png" title="Enter your LinkPOS campany name here.">';
			echo '<tr><td valign="top">Store name: <td valign="top"><input type="text" name="linkpos_store_name" size="30" value="'.$nt[linkpos_store_name].'"><td><img src="images/question.png" title="Enter your LinkPOS store name here.">';
			echo '<tr><td valign="top">Store password: <td valign="top"><input type="text" name="linkpos_store_pwd" size="30" value="' . $nt[linkpos_store_pwd] . '"><td><img src="images/question.png" title="Enter your LinkPOS store password here.">';
		}
			
	}
}
echo '<tr><td valign=top colspan=2><div align=right><input id="save-button" type="submit" name="save" value="Save Changes"></form>';

echo '</table><br><br>';


	
		

	echo '<br>
		
		</div></table></div>';
	

	

?>