<?php

class FirstData {
	
	var $sFdPosId;
	var $sFdSharedKey;
	var $sFdTest;
	
	function __construct(){
		$this->sFdPosId = $GLOBALS['config']['firstdata_pos_id'];
		$this->sFdSharedKey = $GLOBALS['config']['firstdata_shared_key'];
		$this->sFdTest = $GLOBALS['config']['firstdata_test'];
	}
	
	function checkSettings(){
		return ( isset( $this->sFdPosId ) && isset( $this->sFdSharedKey ) ) ? true : false;
	}
	
	function saveLog( $aData ){
		file_put_contents( 'logs/firstdata_log.txt', var_export( array( $aData ), true ), FILE_APPEND );
	}
	
	function calculateControlData( $aParams ){
		$sParams = $this->createString( $aParams );
		$hexLength = strlen( $this->sFdSharedKey );
		$saltBin = "";
		for( $x = 1; $x <= $hexLength/2; $x++ ){
			$saltBin .= (pack( "H*", substr( $this->sFdSharedKey, 2 * $x - 2, 2) ) );
		}
		
		$this->saveLog( array( $sParams, $saltBin, hash( "sha256", $sParams.$saltBin ) ) );
		
		return hash( "sha256", $sParams.$saltBin );
	}
	
	function createString( $aParams ){
		foreach( $aParams as $sKey => $sValue ){
			$aString[] = $sKey . "=" . $sValue;
		}
		
		return implode( "&", $aString );
	}
	
}

?>