cvs: pear /Payment_Process Process.php /Payment_Process/Process Common.php
| From: | Joe Stump | Date: | Thu, 14 Jul 2005 22:33:15 +0000 |
| Subject: | cvs: pear /Payment_Process Process.php /Payment_Process/Process Common.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-33053@lists.php.net to get a copy of this message | ||
jstump Thu Jul 14 18:33:15 2005 EDT
Modified files:
/pear/Payment_Process Process.php
/pear/Payment_Process/Process Common.php
Log:
- Made Process a static class
- Moved all common code from Payment_Process to Payment_Process_Common
http://cvs.php.net/diff.php/pear/Payment_Process/Process.php?r1=1.44&r2=1.45&ty=u Index: pear/Payment_Process/Process.php diff -u pear/Payment_Process/Process.php:1.44 pear/Payment_Process/Process.php:1.45 --- pear/Payment_Process/Process.php:1.44 Thu Jul 7 20:15:40 2005 +++ pear/Payment_Process/Process.php Thu Jul 14 18:33:15 2005 @@ -1,9 +1,10 @@ <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ /** - * Main package file + * Process.php * - * Long description for file (if any)... + * Process.php is a unified OOP abstraction layer for credit card and echeck + * processing gateways (similar to what DB does for database calls). * * PHP versions 4 and 5 * @@ -19,7 +20,7 @@ * @author Joe Stump <joe@joestump.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Revision: 1.44 $ + * @version CVS: $Revision: 1.45 $ * @link http://pear.php.net/package/Payment_Process */ @@ -38,24 +39,25 @@ define('PAYMENT_PROCESS_ERROR_CVV', -4); /** - * Transaction actions - */ -/** * A normal transaction */ define('PAYMENT_PROCESS_ACTION_NORMAL', 200); + /** * Authorize only. No funds are transferred. */ define('PAYMENT_PROCESS_ACTION_AUTHONLY', 201); + /** * Credit funds back from a previously-charged transaction. */ define('PAYMENT_PROCESS_ACTION_CREDIT', 202); + /** * Post-authorize an AUTHONLY transaction. */ define('PAYMENT_PROCESS_ACTION_POSTAUTH', 203); + /** * Clear a previous transaction */ @@ -96,111 +98,6 @@ * @version @version@ */ class Payment_Process { - - /** - * Options. - * - * @var array - * @see setOptions() - * @access private; - */ - var $_options = ''; - - /** - * Your login name to use for authentication to the online processor. - * - * @var string - */ - var $login = ''; - - /** - * Your password to use for authentication to the online processor. - * - * @var string - */ - var $password = ''; - - /** - * Processing action. - * - * This should be set to one of the PAYMENT_PROCESS_ACTION_* constants. - * - * @var int - */ - var $action = ''; - - /** - * A description of the transaction (used by some processors to send - * information to the client, normally not a required field). - * @var string - */ - var $description = ''; - - /** - * The transaction amount. - * - * @var double - */ - var $amount = 0; - - /** - * An invoice number. - * - * @var mixed string or int - */ - var $invoiceNumber = ''; - - /** - * Customer identifier - * - * @var mixed string or int - */ - var $customerId = ''; - - /** - * Transaction source. - * - * This should be set to one of the PAYMENT_PROCESS_SOURCE_* constants. - * - * @var int - */ - var $transactionSource; - - /** - * Array of fields which are required. - * - * @var array - * @access private - * @see _makeRequired() - */ - var $_required = array(); - - /** - * Processor-specific data. - * - * @access private - * @var array - */ - var $_data = array(); - - /** - * $_driver - * - * @author Joe Stump <joe@joestump.net> - * @var string $_driver - * @access private - */ - var $_driver = null; - - /** - * PEAR::Log instance - * - * @var object - * @access protected - * @see Log - */ - var $_log; - /** * Return an instance of a specific processor. * @@ -224,72 +121,6 @@ } /** - * Set many fields. - * - * @param array $where Associative array of data to set, in the format - * 'field' => 'value', - * @return void - */ - function setFrom($where) - { - foreach ($this->getFields() as $field) { - if (isset($where[$field])) { - $this->$field = $where[$field]; - } - } - } - - /** - * Set a value. - * - * This will set a value, such as the credit card number. If the requested - * field is not part of the basic set of supported fields, it is set in - * $_options. - * - * @param string $field The field to set - * @param string $value The value to set - * @return void - */ - function set($field, $value) - { - if (!$this->fieldExists($field)) { - return PEAR::raiseError('Field "' . $field . '" does not exist.', PAYMENT_PROCESS_ERROR_INVALID); - } - $this->$field = $value; - return true; - } - - /** - * Mark a field (or fields) as being required. - * - * @param string $field Field name - * @param string ... - * @return boolean always true. - */ - function _makeRequired() - { - foreach (func_get_args() as $field) { - $this->_required[$field] = true; - } - return true; - } - - /** - * Mark a field as being optional. - * - * @param string $field Field name - * @param ... - * @return boolean always true. - */ - function _makeOptional() - { - foreach (func_get_args() as $field) { - unset($this->_required[$field]); - } - return true; - } - - /** * Determine if a field is required. * * @param string $field Field to check http://cvs.php.net/diff.php/pear/Payment_Process/Process/Common.php?r1=1.28&r2=1.29&ty=u Index: pear/Payment_Process/Process/Common.php diff -u pear/Payment_Process/Process/Common.php:1.28 pear/Payment_Process/Process/Common.php:1.29 --- pear/Payment_Process/Process/Common.php:1.28 Thu Jul 7 20:23:54 2005 +++ pear/Payment_Process/Process/Common.php Thu Jul 14 18:33:15 2005 @@ -19,14 +19,59 @@ * @author Joe Stump <joe@joestump.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Common.php,v 1.28 2005/07/08 00:23:54 jstump Exp $ + * @version CVS: $Id: Common.php,v 1.29 2005/07/14 22:33:15 jstump Exp $ * @link http://pear.php.net/package/Payment_Process */ require_once('Payment/Process.php'); require_once('Payment/Process/Type.php'); -class Payment_Process_Common extends Payment_Process { +class Payment_Process_Common { + // {{{ Private Properties + /** + * Options. + * + * @var array + * @see setOptions() + * @access private; + */ + var $_options = ''; + + /** + * Array of fields which are required. + * + * @var array + * @access private + * @see _makeRequired() + */ + var $_required = array(); + + /** + * Processor-specific data. + * + * @access private + * @var array + */ + var $_data = array(); + + /** + * $_driver + * + * @author Joe Stump <joe@joestump.net> + * @var string $_driver + * @access private + */ + var $_driver = null; + + /** + * PEAR::Log instance + * + * @var object + * @access protected + * @see Log + */ + var $_log; + /** * Mapping between API fields and processors' * @@ -46,10 +91,74 @@ * @see Payment_Process_Common::setPayment() */ var $_payment = null; + // }}} + // {{{ Public Properties + /** + * Your login name to use for authentication to the online processor. + * + * @var string + */ + var $login = ''; + + /** + * Your password to use for authentication to the online processor. + * + * @var string + */ + var $password = ''; + + /** + * Processing action. + * + * This should be set to one of the PAYMENT_PROCESS_ACTION_* constants. + * + * @var int + */ + var $action = ''; + + /** + * A description of the transaction (used by some processors to send + * information to the client, normally not a required field). + * @var string + */ + var $description = ''; + + /** + * The transaction amount. + * + * @var double + */ + var $amount = 0; /** + * An invoice number. + * + * @var mixed string or int + */ + var $invoiceNumber = ''; + + /** + * Customer identifier + * + * @var mixed string or int + */ + var $customerId = ''; + + /** + * Transaction source. + * + * This should be set to one of the PAYMENT_PROCESS_SOURCE_* constants. + * + * @var int + */ + var $transactionSource; + // }}} + // {{{ __construct($options = false) + /** * __construct * + * PHP 5.x constructor + * * @author Joe Stump <joe@joestump.net> * @access public */ @@ -57,16 +166,125 @@ { $this->setOptions($options); } - + // }}} + // {{{ Payment_Process_Common($options = false) + /** + * Payment_Process_Common + * + * PHP 4.x constructor + * + * @author Joe Stump <joe@joestump.net> + * @access public + */ function Payemnt_Process_Common($options = false) { $this->__construct(); } + // }}} + // {{{ setPayment(&$payment) + /** + * Sets payment + * + * Returns false if payment could not be set. This usually means the + * payment type is not valid or that the payment type is valid, but did + * not validate. It could also mean that the payment type is not supported + * by the given processor. + * + * @param mixed $payment Object of Payment_Process_Type + * @return bool + * @access public + * @author Joe Stump <joe@joestump.net> + */ + function setPayment(&$payment) + { + if (isset($this->_typeFieldMap[$payment->getType()]) && + is_array($this->_typeFieldMap[$payment->getType()]) && + count($this->_typeFieldMap[$payment->getType()])) { + if (Payment_Process_Type::isValid($payment)) { + + $this->_payment = $payment; + // Map over the payment specific fields. Check out + // $_typeFieldMap for more information. + $paymentType = $payment->getType(); + foreach ($this->_typeFieldMap[$paymentType] as $generic => $specific) { + + $func = '_handle'.ucfirst($generic); + if (method_exists($this, $func)) { + $result = $this->$func(); + if (PEAR::isError($result)) { + return $result; + } + } else { + // TODO This may screw things up - the problem is that + // CC information is no longer member variables, so we + // can't overwrite it. You could always handle this + // with a _handle funciton. I don't think it will cause + // problems, but it could. + if (!isset($this->_data[$specific])) { + $this->_data[$specific] = $this->_payment->$generic; + } + } + } + + return true; + } + } + + return false; + } + // }}} + // {{{ setFrom($where) + /** + * Set many fields. + * + * @param array $where Associative array of data to set, in the format + * 'field' => 'value', + * @return void + */ + function setFrom($where) + { + foreach ($this->getFields() as $field) { + if (isset($where[$field])) { + $this->$field = $where[$field]; + } + } + } + // }}} + // {{{ process() /** - * Validates data before processing. + * Processes the transaction. * - * This function may be overloaded by the processor. + * This function should be overloaded by the processor. + */ + function process() + { + return PEAR::raiseError("process() is not implemented in this processor.", PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); + } + // }}} + // {{{ &processCallback() + /** + * processCallback + * + * This should be overridden in driver classes. It will be used to process + * communications from gateways to your application. For instance, the + * Authorize.net gateway will post information about pending transactions + * to a URL you specify. This function should handle such requests + * + * @return object Payment_Process_Result on success, PEAR_Error on failure + */ + function &processCallback() + { + return PEAR::raiseError('processCallback() not implemented', + PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); + } + // }}} + // {{{ validate() + /** + * validate + * + * Validates data before processing. This function may be overloaded by + * the processor. * * @return boolean true if validation succeeded, PEAR_Error if it failed. */ @@ -92,17 +310,118 @@ return true; } - + // }}} + // {{{ set($field, $value) /** - * Processes the transaction. + * Set a value. * - * This function should be overloaded by the processor. + * This will set a value, such as the credit card number. If the requested + * field is not part of the basic set of supported fields, it is set in + * $_options. + * + * @param string $field The field to set + * @param string $value The value to set + * @return void */ - function process() + function set($field, $value) { - return PEAR::raiseError("process() is not implemented in this processor.", PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); + if (!$this->fieldExists($field)) { + return PEAR::raiseError('Field "' . $field . '" does not exist.', PAYMENT_PROCESS_ERROR_INVALID); + } + $this->$field = $value; + return true; } + // }}} + // {{{ isRequired($field) + /** + * Determine if a field is required. + * + * @param string $field Field to check + * @return boolean true if required, false if optional. + */ + function isRequired($field) + { + return (isset($this->_required[$field])); + } + // }}} + // {{{ fieldExists($field) + /** + * Determines if a field exists. + * + * @author Ian Eure <ieure@php.net> + * @param string $field Field to check + * @return boolean true if field exists, false otherwise + */ + function fieldExists($field) + { + return @in_array($field, $this->getFields()); + } + // }}} + // {{{ getFields() + /** + * Get a list of fields. + * + * This function returns an array containing all the possible fields which + * may be set. + * + * @author Ian Eure <ieure@php.net> + * @access public + * @return array Array of valid fields. + */ + function getFields() + { + $vars = array_keys(get_class_vars(get_class($this))); + foreach ($vars as $idx => $field) { + if ($field{0} == '_') { + unset($vars[$idx]); + } + } + return $vars; + } + // }}} + // {{{ setOptions($options = false, $defaultOptions = false) + /** + * Set class options. + * + * @author Ian Eure <ieure@php.net> + * @param Array $options Options to set + * @param Array $defaultOptions Default options + * @return void + */ + function setOptions($options = false, $defaultOptions = false) + { + $defaultOptions = $defaultOptions ? $defaultOptions : $this->_defaultOptions; $this->_options = @array_merge($defaultOptions, $options); + } + // }}} + // {{{ getOption($option) + /** + * Get an option value. + * + * @author Ian Eure <ieure@php.net> + * @param string $option Option to get + * @return mixed Option value + */ + function getOption($option) + { + return @$this->_options[$option]; + } + // }}} + // {{{ setOption($option,$value) + /** + * Set an option value + * + * @author Joe Stump <joe@joestump.net> + * @access public + * @param string $option Option name to set + * @param mixed $value Value to set + */ + function setOption($option,$value) + { + return ($this->_options[$option] = $value); + } + // }}} + // {{{ getResult() /** * Gets transaction result. * @@ -112,7 +431,71 @@ { return PEAR::raiseError("getResult() is not implemented in this processor.", PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); } + // }}} + // {{{ _isDefinedConstant($value, $class) + /** + * See if a value is a defined constant. + * + * This function checks to see if $value is defined in one of + * PAYMENT_PROCESS_{$class}_*. It's used to verify that e.g. + * $object->action is one of PAYMENT_PROCESS_ACTION_NORMAL, + * PAYMENT_PROCESS_ACTION_AUTHONLY etc. + * + * @access private + * @param mixed $value Value to check + * @param mixed $class Constant class to check + * @return boolean true if it is defined, false otherwise. + */ + function _isDefinedConst($value, $class) + { + $constClass = 'PAYMENT_PROCESS_'.strtoupper($class).'_'; + $length = strlen($constClass); + $consts = get_defined_constants(); + $found = false; + foreach ($consts as $constant => $constVal) { + if (strncmp($constClass, $constant, $length) === 0 && + $constVal == $value) { + $found = true; + break; + } + } + return $found; + } + // }}} + // {{{ _makeRequired() + /** + * Mark a field (or fields) as being required. + * + * @param string $field Field name + * @param string ... + * @return boolean always true. + */ + function _makeRequired() + { + foreach (func_get_args() as $field) { + $this->_required[$field] = true; + } + return true; + } + // }}} + // {{{ _makeOptional() + /** + * Mark a field as being optional. + * + * @param string $field Field name + * @param ... + * @return boolean always true. + */ + function _makeOptional() + { + foreach (func_get_args() as $field) { + unset($this->_required[$field]); + } + return true; + } + // }}} + // {{{ _validateType() /** * Validates transaction type. * @@ -123,7 +506,8 @@ { return $this->_isDefinedConst($this->type, 'type'); } - + // }}} + // {{{ _validateAction() /** * Validates transaction action. * @@ -134,7 +518,8 @@ { return (isset($GLOBALS['_Payment_Process_'.$this->_driver][$this->action])); } - + // }}} + // {{{ _validateSource() /** * Validates transaction source. * @@ -145,7 +530,8 @@ { return $this->_isDefinedConst($this->transactionSource, 'source'); } - + // }}} + // {{{ _validateAmount() /** * Validates the charge amount. * @@ -164,7 +550,22 @@ 'max' => 99999.99 )); } - + // }}} + // {{{ _handleAction() + /** + * Handles action + * + * Actions are defined in $GLOBALS['_Payment_Process_DriverName'] and then + * handled here. We may decide to abstract the defines in the driver. + * + * @access private + */ + function _handleAction() + { + $this->_data[$this->_fieldMap['action']] = $GLOBALS['_Payment_Process_'.$this->_driver][$this->action]; + } + // }}} + // {{{ _prepare() /** * Prepares the POST data. * @@ -213,87 +614,7 @@ return true; } - - /** - * Sets payment - * - * Returns false if payment could not be set. This usually means the - * payment type is not valid or that the payment type is valid, but did - * not validate. It could also mean that the payment type is not supported - * by the given processor. - * - * @param mixed $payment Object of Payment_Process_Type - * @return bool - * @access public - * @author Joe Stump <joe@joestump.net> - */ - function setPayment(&$payment) - { - if (isset($this->_typeFieldMap[$payment->getType()]) && - is_array($this->_typeFieldMap[$payment->getType()]) && - count($this->_typeFieldMap[$payment->getType()])) { - - if (Payment_Process_Type::isValid($payment)) { - - $this->_payment = $payment; - // Map over the payment specific fields. Check out - // $_typeFieldMap for more information. - $paymentType = $payment->getType(); - foreach ($this->_typeFieldMap[$paymentType] as $generic => $specific) { - - $func = '_handle'.ucfirst($generic); - if (method_exists($this, $func)) { - $result = $this->$func(); - if (PEAR::isError($result)) { - return $result; - } - } else { - // TODO This may screw things up - the problem is that - // CC information is no longer member variables, so we - // can't overwrite it. You could always handle this - // with a _handle funciton. I don't think it will cause - // problems, but it could. - if (!isset($this->_data[$specific])) { - $this->_data[$specific] = $this->_payment->$generic; - } - } - } - - return true; - } - } - - return false; - } - - /** - * processCallback - * - * This should be overridden in driver classes. It will be used to process - * communications from gateways to your application. For instance, the - * Authorize.net gateway will post information about pending transactions - * to a URL you specify. This function should handle such requests - * - * @return object Payment_Process_Result on success, PEAR_Error on failure - */ - function &processCallback() - { - return PEAR::raiseError('processCallback() not implemented', - PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); - } - - /** - * Handles action - * - * Actions are defined in $GLOBALS['_Payment_Process_DriverName'] and then - * handled here. We may decide to abstract the defines in the driver. - * - * @access private - */ - function _handleAction() - { - $this->_data[$this->_fieldMap['action']] = $GLOBALS['_Payment_Process_'.$this->_driver][$this->action]; - } + // }}} } ?>
http://cvs.php.net/diff.php/pear/Payment_Process/Process.php?r1=1.44&r2=1.45&ty=u Index: pear/Payment_Process/Process.php diff -u pear/Payment_Process/Process.php:1.44 pear/Payment_Process/Process.php:1.45 --- pear/Payment_Process/Process.php:1.44 Thu Jul 7 20:15:40 2005 +++ pear/Payment_Process/Process.php Thu Jul 14 18:33:15 2005 @@ -1,9 +1,10 @@ <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ /** - * Main package file + * Process.php * - * Long description for file (if any)... + * Process.php is a unified OOP abstraction layer for credit card and echeck + * processing gateways (similar to what DB does for database calls). * * PHP versions 4 and 5 * @@ -19,7 +20,7 @@ * @author Joe Stump <joe@joestump.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Revision: 1.44 $ + * @version CVS: $Revision: 1.45 $ * @link http://pear.php.net/package/Payment_Process */ @@ -38,24 +39,25 @@ define('PAYMENT_PROCESS_ERROR_CVV', -4); /** - * Transaction actions - */ -/** * A normal transaction */ define('PAYMENT_PROCESS_ACTION_NORMAL', 200); + /** * Authorize only. No funds are transferred. */ define('PAYMENT_PROCESS_ACTION_AUTHONLY', 201); + /** * Credit funds back from a previously-charged transaction. */ define('PAYMENT_PROCESS_ACTION_CREDIT', 202); + /** * Post-authorize an AUTHONLY transaction. */ define('PAYMENT_PROCESS_ACTION_POSTAUTH', 203); + /** * Clear a previous transaction */ @@ -96,111 +98,6 @@ * @version @version@ */ class Payment_Process { - - /** - * Options. - * - * @var array - * @see setOptions() - * @access private; - */ - var $_options = ''; - - /** - * Your login name to use for authentication to the online processor. - * - * @var string - */ - var $login = ''; - - /** - * Your password to use for authentication to the online processor. - * - * @var string - */ - var $password = ''; - - /** - * Processing action. - * - * This should be set to one of the PAYMENT_PROCESS_ACTION_* constants. - * - * @var int - */ - var $action = ''; - - /** - * A description of the transaction (used by some processors to send - * information to the client, normally not a required field). - * @var string - */ - var $description = ''; - - /** - * The transaction amount. - * - * @var double - */ - var $amount = 0; - - /** - * An invoice number. - * - * @var mixed string or int - */ - var $invoiceNumber = ''; - - /** - * Customer identifier - * - * @var mixed string or int - */ - var $customerId = ''; - - /** - * Transaction source. - * - * This should be set to one of the PAYMENT_PROCESS_SOURCE_* constants. - * - * @var int - */ - var $transactionSource; - - /** - * Array of fields which are required. - * - * @var array - * @access private - * @see _makeRequired() - */ - var $_required = array(); - - /** - * Processor-specific data. - * - * @access private - * @var array - */ - var $_data = array(); - - /** - * $_driver - * - * @author Joe Stump <joe@joestump.net> - * @var string $_driver - * @access private - */ - var $_driver = null; - - /** - * PEAR::Log instance - * - * @var object - * @access protected - * @see Log - */ - var $_log; - /** * Return an instance of a specific processor. * @@ -224,72 +121,6 @@ } /** - * Set many fields. - * - * @param array $where Associative array of data to set, in the format - * 'field' => 'value', - * @return void - */ - function setFrom($where) - { - foreach ($this->getFields() as $field) { - if (isset($where[$field])) { - $this->$field = $where[$field]; - } - } - } - - /** - * Set a value. - * - * This will set a value, such as the credit card number. If the requested - * field is not part of the basic set of supported fields, it is set in - * $_options. - * - * @param string $field The field to set - * @param string $value The value to set - * @return void - */ - function set($field, $value) - { - if (!$this->fieldExists($field)) { - return PEAR::raiseError('Field "' . $field . '" does not exist.', PAYMENT_PROCESS_ERROR_INVALID); - } - $this->$field = $value; - return true; - } - - /** - * Mark a field (or fields) as being required. - * - * @param string $field Field name - * @param string ... - * @return boolean always true. - */ - function _makeRequired() - { - foreach (func_get_args() as $field) { - $this->_required[$field] = true; - } - return true; - } - - /** - * Mark a field as being optional. - * - * @param string $field Field name - * @param ... - * @return boolean always true. - */ - function _makeOptional() - { - foreach (func_get_args() as $field) { - unset($this->_required[$field]); - } - return true; - } - - /** * Determine if a field is required. * * @param string $field Field to check http://cvs.php.net/diff.php/pear/Payment_Process/Process/Common.php?r1=1.28&r2=1.29&ty=u Index: pear/Payment_Process/Process/Common.php diff -u pear/Payment_Process/Process/Common.php:1.28 pear/Payment_Process/Process/Common.php:1.29 --- pear/Payment_Process/Process/Common.php:1.28 Thu Jul 7 20:23:54 2005 +++ pear/Payment_Process/Process/Common.php Thu Jul 14 18:33:15 2005 @@ -19,14 +19,59 @@ * @author Joe Stump <joe@joestump.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Common.php,v 1.28 2005/07/08 00:23:54 jstump Exp $ + * @version CVS: $Id: Common.php,v 1.29 2005/07/14 22:33:15 jstump Exp $ * @link http://pear.php.net/package/Payment_Process */ require_once('Payment/Process.php'); require_once('Payment/Process/Type.php'); -class Payment_Process_Common extends Payment_Process { +class Payment_Process_Common { + // {{{ Private Properties + /** + * Options. + * + * @var array + * @see setOptions() + * @access private; + */ + var $_options = ''; + + /** + * Array of fields which are required. + * + * @var array + * @access private + * @see _makeRequired() + */ + var $_required = array(); + + /** + * Processor-specific data. + * + * @access private + * @var array + */ + var $_data = array(); + + /** + * $_driver + * + * @author Joe Stump <joe@joestump.net> + * @var string $_driver + * @access private + */ + var $_driver = null; + + /** + * PEAR::Log instance + * + * @var object + * @access protected + * @see Log + */ + var $_log; + /** * Mapping between API fields and processors' * @@ -46,10 +91,74 @@ * @see Payment_Process_Common::setPayment() */ var $_payment = null; + // }}} + // {{{ Public Properties + /** + * Your login name to use for authentication to the online processor. + * + * @var string + */ + var $login = ''; + + /** + * Your password to use for authentication to the online processor. + * + * @var string + */ + var $password = ''; + + /** + * Processing action. + * + * This should be set to one of the PAYMENT_PROCESS_ACTION_* constants. + * + * @var int + */ + var $action = ''; + + /** + * A description of the transaction (used by some processors to send + * information to the client, normally not a required field). + * @var string + */ + var $description = ''; + + /** + * The transaction amount. + * + * @var double + */ + var $amount = 0; /** + * An invoice number. + * + * @var mixed string or int + */ + var $invoiceNumber = ''; + + /** + * Customer identifier + * + * @var mixed string or int + */ + var $customerId = ''; + + /** + * Transaction source. + * + * This should be set to one of the PAYMENT_PROCESS_SOURCE_* constants. + * + * @var int + */ + var $transactionSource; + // }}} + // {{{ __construct($options = false) + /** * __construct * + * PHP 5.x constructor + * * @author Joe Stump <joe@joestump.net> * @access public */ @@ -57,16 +166,125 @@ { $this->setOptions($options); } - + // }}} + // {{{ Payment_Process_Common($options = false) + /** + * Payment_Process_Common + * + * PHP 4.x constructor + * + * @author Joe Stump <joe@joestump.net> + * @access public + */ function Payemnt_Process_Common($options = false) { $this->__construct(); } + // }}} + // {{{ setPayment(&$payment) + /** + * Sets payment + * + * Returns false if payment could not be set. This usually means the + * payment type is not valid or that the payment type is valid, but did + * not validate. It could also mean that the payment type is not supported + * by the given processor. + * + * @param mixed $payment Object of Payment_Process_Type + * @return bool + * @access public + * @author Joe Stump <joe@joestump.net> + */ + function setPayment(&$payment) + { + if (isset($this->_typeFieldMap[$payment->getType()]) && + is_array($this->_typeFieldMap[$payment->getType()]) && + count($this->_typeFieldMap[$payment->getType()])) { + if (Payment_Process_Type::isValid($payment)) { + + $this->_payment = $payment; + // Map over the payment specific fields. Check out + // $_typeFieldMap for more information. + $paymentType = $payment->getType(); + foreach ($this->_typeFieldMap[$paymentType] as $generic => $specific) { + + $func = '_handle'.ucfirst($generic); + if (method_exists($this, $func)) { + $result = $this->$func(); + if (PEAR::isError($result)) { + return $result; + } + } else { + // TODO This may screw things up - the problem is that + // CC information is no longer member variables, so we + // can't overwrite it. You could always handle this + // with a _handle funciton. I don't think it will cause + // problems, but it could. + if (!isset($this->_data[$specific])) { + $this->_data[$specific] = $this->_payment->$generic; + } + } + } + + return true; + } + } + + return false; + } + // }}} + // {{{ setFrom($where) + /** + * Set many fields. + * + * @param array $where Associative array of data to set, in the format + * 'field' => 'value', + * @return void + */ + function setFrom($where) + { + foreach ($this->getFields() as $field) { + if (isset($where[$field])) { + $this->$field = $where[$field]; + } + } + } + // }}} + // {{{ process() /** - * Validates data before processing. + * Processes the transaction. * - * This function may be overloaded by the processor. + * This function should be overloaded by the processor. + */ + function process() + { + return PEAR::raiseError("process() is not implemented in this processor.", PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); + } + // }}} + // {{{ &processCallback() + /** + * processCallback + * + * This should be overridden in driver classes. It will be used to process + * communications from gateways to your application. For instance, the + * Authorize.net gateway will post information about pending transactions + * to a URL you specify. This function should handle such requests + * + * @return object Payment_Process_Result on success, PEAR_Error on failure + */ + function &processCallback() + { + return PEAR::raiseError('processCallback() not implemented', + PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); + } + // }}} + // {{{ validate() + /** + * validate + * + * Validates data before processing. This function may be overloaded by + * the processor. * * @return boolean true if validation succeeded, PEAR_Error if it failed. */ @@ -92,17 +310,118 @@ return true; } - + // }}} + // {{{ set($field, $value) /** - * Processes the transaction. + * Set a value. * - * This function should be overloaded by the processor. + * This will set a value, such as the credit card number. If the requested + * field is not part of the basic set of supported fields, it is set in + * $_options. + * + * @param string $field The field to set + * @param string $value The value to set + * @return void */ - function process() + function set($field, $value) { - return PEAR::raiseError("process() is not implemented in this processor.", PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); + if (!$this->fieldExists($field)) { + return PEAR::raiseError('Field "' . $field . '" does not exist.', PAYMENT_PROCESS_ERROR_INVALID); + } + $this->$field = $value; + return true; } + // }}} + // {{{ isRequired($field) + /** + * Determine if a field is required. + * + * @param string $field Field to check + * @return boolean true if required, false if optional. + */ + function isRequired($field) + { + return (isset($this->_required[$field])); + } + // }}} + // {{{ fieldExists($field) + /** + * Determines if a field exists. + * + * @author Ian Eure <ieure@php.net> + * @param string $field Field to check + * @return boolean true if field exists, false otherwise + */ + function fieldExists($field) + { + return @in_array($field, $this->getFields()); + } + // }}} + // {{{ getFields() + /** + * Get a list of fields. + * + * This function returns an array containing all the possible fields which + * may be set. + * + * @author Ian Eure <ieure@php.net> + * @access public + * @return array Array of valid fields. + */ + function getFields() + { + $vars = array_keys(get_class_vars(get_class($this))); + foreach ($vars as $idx => $field) { + if ($field{0} == '_') { + unset($vars[$idx]); + } + } + return $vars; + } + // }}} + // {{{ setOptions($options = false, $defaultOptions = false) + /** + * Set class options. + * + * @author Ian Eure <ieure@php.net> + * @param Array $options Options to set + * @param Array $defaultOptions Default options + * @return void + */ + function setOptions($options = false, $defaultOptions = false) + { + $defaultOptions = $defaultOptions ? $defaultOptions : $this->_defaultOptions; $this->_options = @array_merge($defaultOptions, $options); + } + // }}} + // {{{ getOption($option) + /** + * Get an option value. + * + * @author Ian Eure <ieure@php.net> + * @param string $option Option to get + * @return mixed Option value + */ + function getOption($option) + { + return @$this->_options[$option]; + } + // }}} + // {{{ setOption($option,$value) + /** + * Set an option value + * + * @author Joe Stump <joe@joestump.net> + * @access public + * @param string $option Option name to set + * @param mixed $value Value to set + */ + function setOption($option,$value) + { + return ($this->_options[$option] = $value); + } + // }}} + // {{{ getResult() /** * Gets transaction result. * @@ -112,7 +431,71 @@ { return PEAR::raiseError("getResult() is not implemented in this processor.", PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); } + // }}} + // {{{ _isDefinedConstant($value, $class) + /** + * See if a value is a defined constant. + * + * This function checks to see if $value is defined in one of + * PAYMENT_PROCESS_{$class}_*. It's used to verify that e.g. + * $object->action is one of PAYMENT_PROCESS_ACTION_NORMAL, + * PAYMENT_PROCESS_ACTION_AUTHONLY etc. + * + * @access private + * @param mixed $value Value to check + * @param mixed $class Constant class to check + * @return boolean true if it is defined, false otherwise. + */ + function _isDefinedConst($value, $class) + { + $constClass = 'PAYMENT_PROCESS_'.strtoupper($class).'_'; + $length = strlen($constClass); + $consts = get_defined_constants(); + $found = false; + foreach ($consts as $constant => $constVal) { + if (strncmp($constClass, $constant, $length) === 0 && + $constVal == $value) { + $found = true; + break; + } + } + return $found; + } + // }}} + // {{{ _makeRequired() + /** + * Mark a field (or fields) as being required. + * + * @param string $field Field name + * @param string ... + * @return boolean always true. + */ + function _makeRequired() + { + foreach (func_get_args() as $field) { + $this->_required[$field] = true; + } + return true; + } + // }}} + // {{{ _makeOptional() + /** + * Mark a field as being optional. + * + * @param string $field Field name + * @param ... + * @return boolean always true. + */ + function _makeOptional() + { + foreach (func_get_args() as $field) { + unset($this->_required[$field]); + } + return true; + } + // }}} + // {{{ _validateType() /** * Validates transaction type. * @@ -123,7 +506,8 @@ { return $this->_isDefinedConst($this->type, 'type'); } - + // }}} + // {{{ _validateAction() /** * Validates transaction action. * @@ -134,7 +518,8 @@ { return (isset($GLOBALS['_Payment_Process_'.$this->_driver][$this->action])); } - + // }}} + // {{{ _validateSource() /** * Validates transaction source. * @@ -145,7 +530,8 @@ { return $this->_isDefinedConst($this->transactionSource, 'source'); } - + // }}} + // {{{ _validateAmount() /** * Validates the charge amount. * @@ -164,7 +550,22 @@ 'max' => 99999.99 )); } - + // }}} + // {{{ _handleAction() + /** + * Handles action + * + * Actions are defined in $GLOBALS['_Payment_Process_DriverName'] and then + * handled here. We may decide to abstract the defines in the driver. + * + * @access private + */ + function _handleAction() + { + $this->_data[$this->_fieldMap['action']] = $GLOBALS['_Payment_Process_'.$this->_driver][$this->action]; + } + // }}} + // {{{ _prepare() /** * Prepares the POST data. * @@ -213,87 +614,7 @@ return true; } - - /** - * Sets payment - * - * Returns false if payment could not be set. This usually means the - * payment type is not valid or that the payment type is valid, but did - * not validate. It could also mean that the payment type is not supported - * by the given processor. - * - * @param mixed $payment Object of Payment_Process_Type - * @return bool - * @access public - * @author Joe Stump <joe@joestump.net> - */ - function setPayment(&$payment) - { - if (isset($this->_typeFieldMap[$payment->getType()]) && - is_array($this->_typeFieldMap[$payment->getType()]) && - count($this->_typeFieldMap[$payment->getType()])) { - - if (Payment_Process_Type::isValid($payment)) { - - $this->_payment = $payment; - // Map over the payment specific fields. Check out - // $_typeFieldMap for more information. - $paymentType = $payment->getType(); - foreach ($this->_typeFieldMap[$paymentType] as $generic => $specific) { - - $func = '_handle'.ucfirst($generic); - if (method_exists($this, $func)) { - $result = $this->$func(); - if (PEAR::isError($result)) { - return $result; - } - } else { - // TODO This may screw things up - the problem is that - // CC information is no longer member variables, so we - // can't overwrite it. You could always handle this - // with a _handle funciton. I don't think it will cause - // problems, but it could. - if (!isset($this->_data[$specific])) { - $this->_data[$specific] = $this->_payment->$generic; - } - } - } - - return true; - } - } - - return false; - } - - /** - * processCallback - * - * This should be overridden in driver classes. It will be used to process - * communications from gateways to your application. For instance, the - * Authorize.net gateway will post information about pending transactions - * to a URL you specify. This function should handle such requests - * - * @return object Payment_Process_Result on success, PEAR_Error on failure - */ - function &processCallback() - { - return PEAR::raiseError('processCallback() not implemented', - PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED); - } - - /** - * Handles action - * - * Actions are defined in $GLOBALS['_Payment_Process_DriverName'] and then - * handled here. We may decide to abstract the defines in the driver. - * - * @access private - */ - function _handleAction() - { - $this->_data[$this->_fieldMap['action']] = $GLOBALS['_Payment_Process_'.$this->_driver][$this->action]; - } + // }}} } ?>