Location: PHPKode > projects > IGSGateway > Gateway.php
<?php
/*
 * 	      Gateway.php is copyright � 2010. EarthWalk Software.
 * 		  Licensed under the Academic Free License version 3.0.
 *        Refer to the file named License provided with the source.
 */
/**
 * Gateway.php
 * 
 * php5 / ZendFramework-1.7.2 implementation of the Innovative Gateway Solutions (IGS)
 * Gateway interface object.
 * @author Jay Wheeler <hide@address.com>
 * @copyright � 2010. EarthWalk Software.
 * @license refer to License file provided with the source.
 * @version 1.0
 * @package IGSGateway
 * @subpackage Gateway
 */
class Gateway
{
	//
	//	Exception Constants
	//

	/**
	 * Gateway Error Code - No error.
	 */
	const		GATEWAY_NO_ERROR        = 0;

	/**
	 * Gateway Error Code - Configuration not found.
	 */
	const	  	GATEWAY_CONFIG_NOTFOUND = 1;

	/**
	 * Gateway Error Code - Request failed (unable to open socket).
	 */
	const		GATEWAY_REQUEST_FAILED  = 2;

	/**
	 * Gateway Error Code - Invalid results returned from the gateway.
	 */
	const		GATEWAY_INVALID_RESULTS = 3;

	/**
	 * Gateway Error Code - Missing gateway url.
	 */
	const		GATEWAY_MISSING_URI     = 4;

	// ************************************************************************************

	/**
	 * Client object reference.
	 *
	 * Points to the current Client class object.
	 * @var Client $client
	 */
	protected 	$client    = null;

	/**
	 * Zend_HTTP_Respone object reference.
	 *
	 * Points to the Zend_HTTP_Response class object
	 * returned by the last Client request.
	 * @var Zend_HTTP_Response $response
	 */
	protected 	$response  = null;

	/**
	 * An array containing the decoded result.
	 *
	 * An associative array containing the
	 * field/value pairs decoded from the last client response.
	 * @var array[string]string $result
	 */
	protected 	$result    = array();

	/**
	 * Gateway exception.
	 *
	 * Contains the value of the last gateway exception.
	 * @var integer $exception
	 */
	protected 	$exception = 0;

	/**
	 * Gateway errormessage.
	 *
	 * A string containing the last received error message from the gateway.
	 * @var string $errormessage
	 */
	protected	$errormessage = '';

	/**
	 * Gateway parameter array.
	 *
	 * An associative array containing the gateway setup parameters.
	 * @var array[string]string $parameters
	 */
	protected	$parameters = array();

	/**
	 * Gateway connection configuration array.
	 *
	 * An associative array containing the optional gateway connection configuration parameters.
	 * @var array[string]string $configuration
	 */
	protected	$configuration = array();

	// ******************************************************************************

	/*
	 * __construct
	 * 
	 * Class constructor.
	 * @param array  $parameters = associative array containing gateway setup parameters
	 * @param string $config = (optional) connection configuration array
	 * @return Gateway new class object reference
	 */
	public function __construct($parameters, $config=null)
	{
		$this->result = array();
		$this->response = null;
		$this->exception = self::GATEWAY_NO_ERROR;
		$this->errormessage = '';

		$this->client = new Zend_Http_Client();
		$this->client->setMethod(Zend_Http_Client::POST);

		if ($config)
		{
			$this->setConfig($config);
		}

		$this->parameters = $parameters;
		if (! array_key_exists('uri', $this->parameters))
		{
			$this->exception = self::GATEWAY_MISSING_URI;
		}
		else
		{
			$this->setUri($this->parameters['uri']);
			unset($this->parameters['uri']);
		}
	}
	
	// ******************************************************************************

	/*
	 * __destruct
	 * 
	 * Class destructor.
	 */
	public function __destruct()
	{
		unset($this->client);
	}

	// ******************************************************************************

	/*
	 * send
	 * 
	 * send transaction to the gateway server and wait for response
	 * @param array $transaction = transaction parameters array
	 * @param string (optional) $uri = location of the gateway server
	 * @return boolean true = successful, false = error occurred
	 */
	public function send($transaction, $uri=null)
	{
		if ($uri)
		{
			$this->setUri($uri);
			if ($this->exception == self::GATEWAY_MISSING_URI)
			{
				$this->exception = self::GATEWAY_NO_ERROR;
			}
		}

		if ($this->exception != self::GATEWAY_NO_ERROR)
		{
			return false;
		}

		$this->response = null;

		/**
		 *  Associative array containing local copy of transaction parameters.
		 * @var array[string]string $parameters
		 */
		$parameters = array_merge($this->parameters, $transaction);
		$this->setParameterPost($parameters);

		$this->response = $this->client->request();

		if (! $this->response->isSuccessful())
		{
			$this->exception = self::GATEWAY_REQUEST_FAILED;
			$this->errormessage = 'Client request failed.';
			return false;
		}

		$this->result = array();
		/**
		 * $body is the body of the HTTP response.
		 * @var string
		 */
		$body = $this->response->getBody();

		/**
		 * $element is an array created by parsing $body
		 * @var array[string]string
		 */
		$element = explode('|', $body);
		if ((! is_array($element)) ||
		    (count($element) < 2))
		{
			$this->exception = self::GATEWAY_INVALID_RESULTS;
			$this->errormessage = $body;
		}
		else
			/**
			 * $pair is the next field/data pair in $element
			 * @var string $pair
			 */
			foreach ($element as $pair)
			{
				if ($pair)
				{
					/**
					 * $field and $value are the field name and value pair from $pair
					 * @var string $field
					 * @var string $value
					 */
					list($field, $value) = split('=', $pair);
					if ($field <> '')
					{
						$this->result[strtolower($field)] = $value;
					}
				}
			}

		$this->result['body'] = $body;
		$this->result['status'] = '';
		if (array_key_exists('error', $this->result))
		{
			$this->result['status'] = 'Not Approved';
			$this->errormessage = strip_tags($this->result['error']);
		}
		else
			if (array_key_exists('approval', $this->result))
			{
				$this->result['status'] = 'Approved';
			}
			else
			{
				$this->result['status'] = $this->result['body'];
			}

		if (array_key_exists('duplicate', $this->result))
		{
			if ($this->result['status'] != '')
			{
				$this->result['status'] .= ' - Duplicate';
			}
			else
			{
				$this->result['status'] = 'Duplicate Transaction.';
			}
		}

		return true;
	}

	// ******************************************************************************

	/*
	 * setUri
	 * 
	 * set server Uri
	 * @param string $url = url to connect to
	 * @return null
	 */
	public function setUri($uri)
	{
		$this->client->setUri($uri);
	}

	// ******************************************************************************

	/*
	 * setConfig
	 * 
	 * set server configuration array
	 * @param array $config = configuration array
	 * @return null
	 */
	public function setConfig($config)
	{
		$this->client->setConfig($config);		
	}

	// ******************************************************************************

	/*
	 * setParameterPost
	 * 
	 * set parameters from the parameters array
	 * @param array $parameters = parameters array
	 * @return null
	 */
	public function setParameterPost($parameters)
	{
		$this->client->setParameterPost($parameters);
	}

	// ******************************************************************************

	/*
	 * resetException
	 * 
	 * reset exception
	 * @param none
	 * @return null
	 */
	public function resetException()
	{
		$this->exception = self::GATEWAY_NO_ERROR;
	}

	// ******************************************************************************

	/*
	 * getResponse
	 * 
	 * get Zend_Http_Response object
	 * @param none
	 * @return Zend_Http_Response reference to response object
	 */
	public function getResponse()
	{
		return $this->response;
	}

	// ******************************************************************************

	/*
	 * getClient
	 * 
	 * get Zend_Http_Client object
	 * @param none
	 * @return Zend_Http_Client reference to client object
	 */
	public function getClient()
	{
		return $this->client;
	}

	// ******************************************************************************

	/*
	 * getResult
	 * 
	 * get result array (decoded body)
	 * @param none
	 * @return array returned fields and values
	 */
	public function getResult()
	{
		return $this->result;
	}
	
	// ******************************************************************************

	/*
	 * getException
	 * 
	 * get exception flag - set when an error occurs
	 * @param none
	 * @return integer 0 = no error, otherwise error code
	 */
	public function getException()
	{
		return $this->exception;
	}

	// ******************************************************************************

	/*
	 * getExceptionMessage
	 * 
	 * get exception message from exception
	 * @param integer $exception = exception number to decode
	 * @return string exception message
	 */
	public function getExceptionMessage($exception)
	{
		switch($this->exception)
		{
		case self::GATEWAY_NO_ERROR:
			return 'No Exception.';

		case self::GATEWAY_CONFIG_NOTFOUND:
			return 'Configuration Not Found.';

		case self::GATEWAY_REQUEST_FAILED:
			return 'Request Failed.';

		case self::GATEWAY_INVALID_RESULTS:
			return 'Invalid Results.';

		case self::GATEWAY_MISSING_URI:
			return 'Missing Gateway URI.';

		default:
			break;
		}

		return 'Unknown Error.';
	}

	// ******************************************************************************

	/*
	 * getError
	 * 
	 * get error message
	 * @param none
	 * @return string current errormessage setting
	 */
	public function getError()
	{
		return $this->errormessage;
	}

}

Return current item: IGSGateway