Payment_Process
[ class tree: Payment_Process ] [ index: Payment_Process ] [ all elements ]

Source for file Common.php

Documentation is available at Common.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Ian Eure <ieure@php.net>                                    |
  17. // |          Joe Stump <joe@joestump.net>                                |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Common.php,v 1.19 2004/04/01 18:25:42 jstump Exp $
  21.  
  22. require_once 'Payment/Process.php';
  23. require_once 'Payment/Process/Type.php';
  24.  
  25.     /**
  26.     * $_typeFieldMap
  27.     *
  28.     * @author Joe Stump <joe@joestump.net>
  29.     * @access protected
  30.     * @var mixed $_typeFieldMap 
  31.     */
  32.     var $_typeFieldMap = array();
  33.     
  34.     /**
  35.     * $_payment
  36.     *
  37.     * An internal reference to the Payment_Process_Type that is currently
  38.     * being processed.
  39.     *
  40.     * @author Joe Stump <joe@joestump.net>
  41.     * @access protected
  42.     * @var mixed $_payment Instance of Payment_Type
  43.     * @see Payment_Process_Common::setPayment()
  44.     */
  45.     var $_payment = null;
  46.  
  47.     /**
  48.      * Validate data before processing.
  49.      *
  50.      * This function may be overloaded by the processor.
  51.      *
  52.      * @return boolean true if validation succeeded, PEAR_Error if it failed.
  53.      */
  54.     function validate()
  55.     {
  56.         foreach ($this->getFields(as $field{
  57.             $func '_validate'.ucfirst($field);
  58.             
  59.             // Don't validate unset optional fields
  60.             if ($this->isRequired($field&& !strlen($this->field)) {
  61.                 continue;
  62.             }
  63.             
  64.             if (method_exists($this$func)) {
  65.                 $res $this->$func();
  66.                 if (PEAR::isError($res)) {
  67.                     return $res;
  68.                 elseif (is_bool($res&& $res == false{
  69.                     return PEAR::raiseError('Validation of field "'.$field.'" failed.'PAYMENT_PROCESS_ERROR_INVAILD);
  70.                 }
  71.             }
  72.         }
  73.  
  74.         return true;
  75.     }
  76.  
  77.     /**
  78.      * Process the transaction.
  79.      *
  80.      * This function should be overloaded by the processor.
  81.      */
  82.     function process()
  83.     {
  84.         return PEAR::raiseError("process() is not implemented in this processor."PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED);
  85.     }
  86.  
  87.     /**
  88.      * Get transaction result.
  89.      *
  90.      * This function should be overloaded by the processor.
  91.      */
  92.     function getResult()
  93.     {
  94.         return PEAR::raiseError("getResult() is not implemented in this processor."PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED);
  95.     }
  96.  
  97.     /**
  98.      * Validate transaction type.
  99.      *
  100.      * @access private
  101.      * @return boolean true on success, false on failure.
  102.      */
  103.     function _validateType()
  104.     {
  105.         return $this->_isDefinedConst($this->type'type');
  106.     }
  107.  
  108.     /**
  109.      * Validate transaction acion.
  110.      *
  111.      * @access private
  112.      * @return boolean true on success, false on failure.
  113.      */
  114.     function _validateAction()
  115.     {
  116.         return (isset($GLOBALS['_Payment_Process_'.$this->_driver][$this->action]));
  117.     }
  118.  
  119.     /**
  120.      * Validate transaction source.
  121.      *
  122.      * @access private
  123.      * @return boolean true on success, false on failure.
  124.      */
  125.     function _validateSource()
  126.     {
  127.         return $this->_isDefinedConst($this->transactionSource'source');
  128.     }
  129.     
  130.     /**
  131.      * Validate the charge amount.
  132.      *
  133.      * Charge amount must be 8 characters long, double-precision.
  134.      * Current min/max are rather arbitrarily set to $0.99 and $99999.99,
  135.      * respectively.
  136.      *
  137.      * @return boolean true on success, false otherwise
  138.      */
  139.     function _validateAmount()
  140.     {
  141.         return Validate::number($this->amountarray(
  142.             'decimal' => '.',
  143.             'dec_prec' => 2,
  144.             'min' => 0.99,
  145.             'max' => 99999.99
  146.         ));
  147.     }
  148.  
  149.     /**
  150.      * Prepare the POST data.
  151.      *
  152.      * This function handles translating the data set in the front-end to the
  153.      * format needed by the back-end. The prepared data is stored in
  154.      * $this->_data. If a '_handleField' method exists in this class (e.g.
  155.      * '_handleCardNumber()'), that function is called and /must/ set
  156.      * $this->_data correctly. If no field-handler function exists, the data
  157.      * from the front-end is mapped into $_data using $this->_fieldMap.
  158.      *
  159.      * @access private
  160.      * @return array Data to POST
  161.      */
  162.     function _prepare()
  163.     {
  164.         if ($this->_options['debug']{
  165.             echo '----------- PREPARE A ----------'."\n";
  166.             echo print_r($this->_data);
  167.             echo '----------- PREPARE A ----------'."\n";
  168.         }
  169.  
  170.         /*
  171.          * FIXME - because this only loops through stuff in the fieldMap, we
  172.          *         can't have handlers for stuff which isn't specified in there.
  173.          *         But the whole point of having a _handler() is that you need
  174.          *         to do something more than simple mapping.
  175.          */
  176.         foreach ($this->_fieldMap as $generic => $specific{
  177.             $func '_handle'.ucfirst($generic);
  178.             if (method_exists($this$func)) {
  179.                 $result $this->$func();
  180.                 if (PEAR::isError($result)) {
  181.                     return $result;
  182.                 }
  183.             else {
  184.                 // TODO This may screw things up - the problem is that
  185.                 // CC information is no longer member variables, so we
  186.                 // can't overwrite it. You could always handle this with
  187.                 // a _handle funciton. I don't think it will cause problems,
  188.                 // but it could.
  189.                 if (!isset($this->_data[$specific])) {
  190.                     $this->_data[$specific$this->$generic;
  191.  
  192.                     // Form of payments data overrides those set in the 
  193.                     // Payment_Process_Common.
  194.                     if(isset($this->_payment->$generic))
  195.                     {
  196.                       $this->_data[$specific$this->_payment->$generic;
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.  
  202.         if ($this->_options['debug']{
  203.             echo '----------- PREPARE ----------'."\n";
  204.             echo print_r($this->_data);
  205.             echo '----------- PREPARE ----------'."\n";
  206.         }
  207.                                                                                 
  208.         return true;
  209.     }
  210.  
  211.     /**
  212.     * Set payment
  213.     *
  214.     * Returns false if payment could not be set. This usually means the
  215.     * payment type is not valid  or that the payment type is valid, but did
  216.     * not validate. It could also mean that the payment type is not supported
  217.     * by the given processor.
  218.     *
  219.     * @author Joe Stump <joe@joestump.net>
  220.     * @access public
  221.     * @param mixed $payment Object of Payment_Process_Type
  222.     * @return bool 
  223.     */
  224.     function setPayment($payment)
  225.     {
  226.         if (is_array($this->_typeFieldMap[$payment->getType()]&&
  227.             count($this->_typeFieldMap[$payment->getType()])) {
  228.         
  229.             if (Payment_Process_Type::isValid($payment)) {
  230.  
  231.  
  232.                 $this->_payment = $payment;
  233.                 // Map over the payment specific fiels. Check out 
  234.                 // $_typeFieldMap for more information.
  235.                 $paymentType $payment->getType();
  236.                 foreach ($this->_typeFieldMap[$paymentTypeas $key => $val{
  237.                     if(!isset($this->_data[$val])) {
  238.                         $this->_data[$val$this->_payment->$key;
  239.                     }
  240.                 }
  241.  
  242.                 return true;
  243.             }
  244.         }
  245.  
  246.         return false;
  247.     }
  248.  
  249.     /**
  250.      * Handle action
  251.      *
  252.      * Actions are defined in $GLOBALS['_Payment_Process_DriverName'] and then
  253.      * handled here. We may decide to abstract the defines in the driver.
  254.      *
  255.      * @author Joe Stump <joe@joestump.net>
  256.      * @access private
  257.      * @return void 
  258.      */
  259.     function _handleAction()
  260.     {
  261.         $this->_data[$this->_fieldMap['action']] $GLOBALS['_Payment_Process_'.$this->_driver][$this->action];
  262.     }
  263.     
  264.     /**
  265.      * Print a debug message.
  266.      *
  267.      * This will only print the message if 'debug' is set in the Processor
  268.      * options.
  269.      *
  270.      * @param string $msg Message to print
  271.      * @return void 
  272.      * @author Ian Eure <ieure@php.net>
  273.      */
  274.     function debug($msg)
  275.     {
  276.         if ($this->_options['debug']{
  277.             print $msg."\n";
  278.         }
  279.     }
  280. }
  281.  
  282. ?>

Documentation generated on Mon, 11 Mar 2019 13:51:26 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.