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

Source for file Process.php

Documentation is available at Process.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: Process.php,v 1.27 2004/03/31 16:00:21 jstump Exp $
  21.  
  22. require_once 'PEAR.php';
  23. require_once 'Validate.php';
  24. require_once 'Payment/Process/Type.php';
  25.  
  26. // Error codes
  27. define('PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED'-100);
  28. define('PAYMENT_PROCESS_ERROR_NOFIELD'-101);
  29. define('PAYMENT_PROCESS_ERROR_NOPROCESSOR'-102);
  30. define('PAYMENT_PROCESS_ERROR_INCOMPLETE'-1);
  31. define('PAYMENT_PROCESS_ERROR_INVAILD'-2);
  32. define('PAYMENT_PROCESS_ERROR_AVS',-3);
  33. define('PAYMENT_PROCESS_ERROR_CVV',-4);
  34.  
  35. // Transaction actions
  36. // A normal transaction
  37. define('PAYMENT_PROCESS_ACTION_NORMAL'200);
  38. // Authorize only. No funds are transferred.
  39. define('PAYMENT_PROCESS_ACTION_AUTHONLY'201);
  40. // Credit funds back from a previously-charged transaction.
  41. define('PAYMENT_PROCESS_ACTION_CREDIT'202);
  42. // Post-authorize an AUTHONLY transaction.
  43. define('PAYMENT_PROCESS_ACTION_POSTAUTH'203);
  44.  
  45. // Transaction sources
  46. define('PAYMENT_PROCESS_SOURCE_POS'300);
  47. define('PAYMENT_PROCESS_SOURCE_ONLINE'301);
  48.  
  49. // Results
  50. define('PAYMENT_PROCESS_RESULT_APPROVED'400);
  51. define('PAYMENT_PROCESS_RESULT_DECLINED'401);
  52. define('PAYMENT_PROCESS_RESULT_OTHER'402);
  53.  
  54. define('PAYMENT_PROCESS_AVS_MATCH',500);
  55. define('PAYMENT_PROCESS_AVS_MISMATCH',501);
  56. define('PAYMENT_PROCESS_AVS_ERROR',502);
  57. define('PAYMENT_PROCESS_AVS_NOAPPLY',503);
  58.  
  59. define('PAYMENT_PROCESS_CVV_MATCH',600);
  60. define('PAYMENT_PROCESS_CVV_MISMATCH',601);
  61. define('PAYMENT_PROCESS_CVV_ERROR',602);
  62. define('PAYMENT_PROCESS_CVV_NOAPPLY',603);
  63.  
  64. /**
  65.  * Payment_Process
  66.  *
  67.  * @author Ian Eure <ieure@php.net>
  68.  * @package Payment_Process
  69.  * @category Payment
  70.  * @version @version@
  71.  */
  72. class Payment_Process extends PEAR {
  73.  
  74.     /**
  75.      * Options.
  76.      *
  77.      * @see setOptions()
  78.      * @access private;
  79.      */
  80.     var $_options '';
  81.  
  82.     /**
  83.      * Your login name to use for authentication to the online processor.
  84.      */
  85.     var $login = '';
  86.  
  87.     /**
  88.      * Your password to use for authentication to the online processor.
  89.      */
  90.     var $password = '';
  91.  
  92.     /**
  93.      * Processing action.
  94.      *
  95.      * This should be set to one of the PAYMENT_PROCESS_ACTION_* constants.
  96.      */
  97.     var $action = '';
  98.  
  99.     /**
  100.      * A description of the transaction (used by some processors to send
  101.      * information to the client, normally not a required field).
  102.      */
  103.     var $description = '';
  104.  
  105.     /**
  106.      * The transaction amount.
  107.      */
  108.     var $amount = 0;
  109.  
  110.     /**
  111.      * An invoice number.
  112.      */
  113.     var $invoiceNumber = '';
  114.  
  115.     /**
  116.      * Customer identifier
  117.      */
  118.     var $customerId = '';
  119.  
  120.     /**
  121.      * Transaction source.
  122.      *
  123.      * This should be set to one of the PAYMENT_PROCESS_SOURCE_* constants.
  124.      */
  125.     var $transactionSource;
  126.  
  127.     /**
  128.      * Array of fields which are required.
  129.      *
  130.      * @type array
  131.      * @access private
  132.      * @see _makeRequired()
  133.      */
  134.     var $_required = array();
  135.     
  136.     /**
  137.      * Processor-specific data.
  138.      *
  139.      * @access private
  140.      * @type array
  141.      */
  142.     var $_data = array();
  143.  
  144.     /**
  145.      * $_driver
  146.      *
  147.      * @author Joe Stump <joe@joestump.net>
  148.      * @var string $_driver 
  149.      * @access private
  150.      */
  151.     var $_driver = null;
  152.  
  153.     /**
  154.      * Return an instance of a specific processor.
  155.      *
  156.      * @param  string  $type     Name of the processor
  157.      * @param  array   $options  Options for the processor
  158.      * @return mixed Instance of the processor object, or a PEAR_Error object.
  159.      */
  160.     function &factory($type$options = false)
  161.     {
  162.         $class "Payment_Process_".$type;
  163.         if (include_once "Payment/Process/{$type}.php"{
  164.             if (class_exists($class)) {
  165.                 $object new $class($options);
  166.                 $object->_driver = $type;
  167.                 return $object;
  168.             
  169.         }
  170.  
  171.         return PEAR::raiseError('"'.$type.'" processor does not exist'
  172.                                 PAYMENT_PROCESS_ERROR_NOPROCESSOR);
  173.  
  174.     }
  175.  
  176.     /**
  177.      * Set many fields.
  178.      *
  179.      * @param  array  $where  Associative array of data to set, in the format
  180.      *                        'field' => 'value',
  181.      * @return void 
  182.      */
  183.     function setFrom($where)
  184.     {
  185.         foreach ($this->getFields(as $field{
  186.             if (isset($where[$field])) {
  187.                 $this->$field $where[$field];
  188.             }
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Set a value.
  194.      *
  195.      * This will set a value, such as the credit card number. If the requested
  196.      * field is not part of the basic set of supported fields, it is set in
  197.      * $_options.
  198.      *
  199.      * @param  string  $field  The field to set
  200.      * @param  string  $value  The value to set
  201.      * @return void 
  202.      */
  203.     function set($field$value)
  204.     {
  205.         if (!$this->fieldExists($field)) {
  206.             return PEAR::raiseError("Field \"$field\" does not exist."PAYMENT_PROCESS_ERROR_INVALID);
  207.         }
  208.         $this->$field $value;
  209.         return true;
  210.     }
  211.     
  212.     /**
  213.      * Mark a field as being required.
  214.      *
  215.      * @param $field Field name
  216.      * @param ... 
  217.      * @return boolean always true.
  218.      */
  219.     function _makeRequired()
  220.     {
  221.         foreach (func_get_args(as $field{
  222.             $this->_required[$field= true;
  223.         }
  224.         return true;
  225.     }
  226.     
  227.     /**
  228.      * Mark a field as being optional.
  229.      *
  230.      * @param $field Field name
  231.      * @param ... 
  232.      * @return boolean always true.
  233.      */
  234.     function _makeOptional()
  235.     {
  236.         foreach (func_get_args(as $field{
  237.             unset($this->_required[$field]);
  238.         }
  239.         return true;
  240.     }
  241.     
  242.     /**
  243.      * Determine if a field is required.
  244.      *
  245.      * @param string $field Field to check
  246.      * @return boolean true if required, false if optional.
  247.      */
  248.     function isRequired($field)
  249.     {
  250.         return (isset($this->_required[$field]));
  251.     }
  252.  
  253.     /**
  254.      * Determines if a field exists.
  255.      *
  256.      * @author Ian Eure <ieure@php.net>
  257.      * @param  string  $field  Field to check
  258.      * @return boolean true if field exists, false otherwise
  259.      */
  260.     function fieldExists($field)
  261.     {
  262.         return @in_array($field$this->getFields());
  263.     }
  264.  
  265.     /**
  266.      * Get a list of fields.
  267.      *
  268.      * This function returns an array containing all the possible fields which
  269.      * may be set.
  270.      *
  271.      * @author Ian Eure <ieure@php.net>
  272.      * @access public
  273.      * @return array Array of valid fields.
  274.      */
  275.     function getFields()
  276.     {
  277.         $vars array_keys(get_class_vars(get_class($this)));
  278.         foreach ($vars as $idx => $field{
  279.             if (ereg('^_+'$field)) {
  280.                 unset($vars[$idx]);
  281.             }
  282.         }
  283.         return $vars;
  284.     }
  285.  
  286.     /**
  287.      * Set class options.
  288.      *
  289.      * @author Ian Eure <ieure@php.net>
  290.      * @param  Array  $options         Options to set
  291.      * @param  Array  $defaultOptions  Default options
  292.      * @return void 
  293.      */
  294.     function setOptions($options = false$defaultOptions = false)
  295.     {
  296.         $defaultOptions $defaultOptions $defaultOptions $this->_defaultOptions;
  297.         $this->_options @array_merge($defaultOptions$options);
  298.     }
  299.  
  300.     /**
  301.      * Get an option value.
  302.      *
  303.      * @author Ian Eure <ieure@php.net>
  304.      * @param  string  $option  Option to get
  305.      * @return mixed Option value
  306.      */
  307.     function getOption($option)
  308.     {
  309.         return @$this->_options[$option];
  310.     }
  311.  
  312.     /**
  313.      * Set an option value
  314.      *
  315.      * @author Joe Stump <joe@joestump.net>
  316.      * @access public
  317.      * @param string $option Option name to set
  318.      * @param mixed $value Value to set
  319.      */
  320.     function setOption($option,$value)
  321.     {
  322.         return ($this->_options[$option$value);
  323.     }
  324.  
  325.     /**
  326.      * See if a value is a defined constant.
  327.      *
  328.      * This function checks to see if $value is defined in one of
  329.      * PAYMENT_PROCESS_{$class}_*. It's used to verify that e.g. $object->action is one of
  330.      * PAYMENT_PROCESS_ACTION_NORMAL, PAYMENT_PROCESS_ACTION_AUTHONLY etc.
  331.      *
  332.      * @access private
  333.      * @param  mixed  $value  Value to check
  334.      * @param  mixed  $class  Constant class to check
  335.      * @return boolean true if it is defined, false otherwise.
  336.      */
  337.     function _isDefinedConst($value$class)
  338.     {
  339.         $re '^PAYMENT_PROCESS_'.strtoupper($class).'_.*';
  340.         $consts get_defined_constants();
  341.         foreach ($consts as $constant => $constVal{
  342.             if (ereg($re$constant)) {
  343.                 $valid[$constVal;
  344.             }
  345.         }
  346.         return @in_array($value$valid);
  347.     }
  348.  
  349.     /**
  350.     * Statically check a Payment_Result class for success
  351.     *
  352.     * @author Joe Stump <joe@joestump.net>
  353.     * @access public
  354.     * @param mixed $obj 
  355.     */
  356.     function isSuccess($obj)
  357.     {
  358.         if (is_a($obj,'Payment_Process_Result')) {
  359.             if ($obj->getCode(== PAYMENT_PROCESS_RESULT_APPROVED{
  360.                 return true;
  361.             }
  362.         }
  363.   
  364.         return false;
  365.     }
  366.  
  367.     /**
  368.     * Statically check a Payment_Result class for error
  369.     *
  370.     * @author Joe Stump <joe@joestump.net>
  371.     * @access public
  372.     * @param mixed $obj 
  373.     */
  374.     function isError($obj)
  375.     {
  376.         if (PEAR::isError($obj)) {
  377.             return true; 
  378.         }
  379.  
  380.         if (is_a($obj,'Payment_Process_Result')) {
  381.             if ($obj->getCode(!= PAYMENT_PROCESS_RESULT_APPROVED{
  382.                 return true;
  383.             }
  384.         }
  385.  
  386.         return false;
  387.     
  388. }
  389.  
  390. /**
  391.  * Payment_Process_Result
  392.  *
  393.  * The core result class that should be returned from each driver's process()
  394.  * function. This should be extended as Payment_Process_Result_DriverName and
  395.  * then have the appropriate fields mapped out accordingly.
  396.  *
  397.  * Take special care to appropriately create a parse() function in your result
  398.  * class. You can then call _mapFields() with a resultArray (ie. exploded
  399.  * result) to map your results from parse() into the member variables.
  400.  *
  401.  * Please note that this class keeps your original codes intact so they can
  402.  * be accessed directly and then uses the function wrappers to return uniform
  403.  * Payment_Process codes.
  404.  *
  405.  * @author Joe Stump <joe@joestump.net>
  406.  * @package Payment_Process
  407.  * @category Payment
  408.  * @version @version@
  409.  */
  410.  
  411.     /**
  412.      * Processor instance which this result was instantiated from.
  413.      *
  414.      * This should contain a reference to the requesting Processor.
  415.      *
  416.      * @author Ian Eure <ieure@php.net>
  417.      * @access private
  418.      * @type Object
  419.      */
  420.     var $_request;
  421.     
  422.     /**
  423.     * The raw response (ie. from cURL)
  424.     *
  425.     * @author Joe Stump <joe@joestump.net>
  426.     * @access protected
  427.     * @var string $_rawResponse 
  428.     */
  429.     var $_rawResponse = null;
  430.  
  431.     /**
  432.     * The approval/decline code
  433.     *
  434.     * The value returned by your gateway as approved/declined should be mapped
  435.     * into this variable. Valid results should then be mapped into the
  436.     * appropriate PAYMENT_PROCESS_RESULT_* code using the $_statusCodeMap
  437.     * array. Values returned into $code should be mapped as keys in the map
  438.     * with PAYMENT_PROCESS_RESULT_* as the values.
  439.     *
  440.     * @author Joe Stump <joe@joestump.net>
  441.     * @access public
  442.     * @var mixed $code 
  443.     * @see PAYMENT_PROCESS_RESULT_APPROVED, PAYMENT_PROCESS_RESULT_DECLINED
  444.     * @see PAYMENT_PROCESS_RESULT_OTHER, $_statusCodeMap
  445.     */
  446.     var $code;
  447.  
  448.     /**
  449.     * Message/Response Code
  450.     *
  451.     * Along with the response (yes/no) you usually get a response/message
  452.     * code that translates into why it was approved/declined. This is where
  453.     * you map that code into. Your $_statusCodeMessages would then be keyed by
  454.     * valid messageCode values.
  455.     *
  456.     * @author Joe Stump <joe@joestump.net>
  457.     * @access public
  458.     * @var mixed $messageCode 
  459.     * @see $_statusCodeMessages
  460.     */
  461.     var $messageCode;
  462.  
  463.     /**
  464.     * Message from gateway
  465.     *
  466.     * Map the textual message from the gateway into this variable. It is not
  467.     * currently returned or used (in favor of the $_statusCodeMessages map, but
  468.     * can be accessed directly for debugging purposes.
  469.     *
  470.     * @author Joe Stump <joe@joestump.net>
  471.     * @access public
  472.     * @var string $message 
  473.     * @see $_statusCodeMessages
  474.     */
  475.     var $message = 'No message from gateway';
  476.  
  477.     /**
  478.     * Authorization/Approval code
  479.     *
  480.     * @author Joe Stump <joe@joestump.net>
  481.     * @access public
  482.     * @var string $approvalCode 
  483.     */
  484.     var $approvalCode;
  485.  
  486.     /**
  487.     * Address verification code
  488.     *
  489.     * The AVS code returned from your gateway. This should then be mapped to
  490.     * the appropriate PAYMENT_PROCESS_AVS_* code using $_avsCodeMap. This value
  491.     * should also be mapped to the appropriate textual message via the
  492.     * $_avsCodeMessages array.
  493.     *
  494.     * @author Joe Stump <joe@joestump.net>
  495.     * @access public
  496.     * @var string $avsCode 
  497.     * @see PAYMENT_PROCESS_AVS_MISMATCH, PAYMENT_PROCESS_AVS_ERROR
  498.     * @see PAYMENT_PROCESS_AVS_MATCH, PAYMENT_PROCESS_AVS_NOAPPLY, $_avsCodeMap
  499.     * @see $_avsCodeMessages
  500.     */
  501.     var $avsCode;
  502.  
  503.     /**
  504.     * Transaction ID
  505.     *
  506.     * This is the unique transaction ID, which is used by gateways to modify
  507.     * transactions (credit, update, etc.). Map the appropriate value into this
  508.     * variable.
  509.     *
  510.     * @author Joe Stump <joe@joestump.net>
  511.     * @access public
  512.     * @var string $transactionId 
  513.     */
  514.     var $transactionId;
  515.  
  516.     /**
  517.     * Invoice Number
  518.     *
  519.     * Unique internal invoiceNumber (ie. your company's order/invoice number
  520.     * that you assign each order as it is processed). It is always a good idea
  521.     * to pass this to the gateway (which is usually then echo'd back).
  522.     *
  523.     * @author Joe Stump <joe@joestump.net>
  524.     * @access public
  525.     * @var string $invoiceNumber 
  526.     */
  527.     var $invoiceNumber;
  528.  
  529.     /**
  530.     * Customer ID
  531.     *
  532.     * Unique internall customer ID (ie. your company's customer ID used to
  533.     * track individual customers).
  534.     *
  535.     * @author Joe Stump <joe@joestump.net>
  536.     * @access public
  537.     * @var string $customerId 
  538.     */
  539.     var $customerId;
  540.  
  541.     /**
  542.     * CVV Code
  543.     *
  544.     * The CVV code is the 3-4 digit number on the back of most credit cards.
  545.     * This value should be mapped via the $_cvvCodeMap variable to the
  546.     * appropriate PAYMENT_PROCESS_CVV_* values.
  547.     *
  548.     * @author Joe Stump <joe@joestump.net>
  549.     * @access public
  550.     * @var string $cvvCode 
  551.     */
  552.     var $cvvCode = PAYMENT_PROCESS_CVV_NOAPPLY;
  553.  
  554.     /**
  555.     * CVV Message
  556.     *
  557.     * Your cvvCode value should be mapped to appropriate messages via the
  558.     * $_cvvCodeMessage array. This value is merely here to hold the value
  559.     * returned from the gateway (if any).
  560.     *
  561.     * @author Joe Stump <joe@joestump.net>
  562.     * @access public
  563.     * @var string $cvvMessage 
  564.     */
  565.     var $cvvMessage = 'No CVV message from gateway';
  566.  
  567.     function Payment_Process_Result($rawResponse
  568.     {
  569.         $this->_rawResponse = $rawResponse;
  570.     }
  571.  
  572.     function &factory($type,$rawResponse)
  573.     {
  574.         $class 'Payment_Process_Result_'.$type;
  575.         if (class_exists($class)) {
  576.             return new $class($rawResponse);
  577.         }
  578.  
  579.         return PEAR::raiseError('Invalid response type: '.$type.'('.$class.')');
  580.     }
  581.  
  582.     function validate()
  583.     {
  584.         if ($this->_request->options['avsCheck'=== true{
  585.             if ($this->getAVSCode(!= PAYMENT_PROCESS_AVS_MATCH{
  586.                 return PEAR::raiseError('AVS check failed',PAYMENT_PROCESS_ERROR_AVS);
  587.             }
  588.         }    
  589.  
  590.         if ($this->_request->options['cvvCheck'=== true && 
  591.             $this->_request->_payment->_driver == PAYMENT_PROCESS_TYPE_CREDITCARD{
  592.             if ($this->getCvvCode(!= PAYMENT_PROCESS_CVV_MATCH{
  593.                 return PEAR::raiseError('CVV check failed',PAYMENT_PROCESS_ERROR_CVV);
  594.             }
  595.         }
  596.  
  597.         if ($this->getCode(!= PAYMENT_PROCESS_RESULT_APPROVED{
  598.             return PEAR::raiseError($this->getMessage(),PAYMENT_PROCESS_RESULT_DECLINED)
  599.         
  600.  
  601.         return true;
  602.     }
  603.  
  604.     function parse(
  605.     {
  606.         return PEAR::raiseError('parse() not implemented',PAYMENT_PROCESS_ERROR_NOTIMPLEMENTED);
  607.     }
  608.  
  609.     function getCode(
  610.     {
  611.       return $this->_statusCodeMap[$this->code];
  612.     }
  613.  
  614.     function getMessage(
  615.     {
  616.       return $this->_statusCodeMessages[$this->messageCode];
  617.     }
  618.  
  619.     function getAVSCode(
  620.     {
  621.         return $this->_avsCodeMap[$this->avsCode];
  622.     }
  623.  
  624.     function getAVSMessage(
  625.     {
  626.         return $this->_avsCodeMessages[$this->avsCode];
  627.     }
  628.  
  629.     function getCvvCode()
  630.     {
  631.         return $this->_cvvCodeMap[$this->cvvCode];
  632.     }
  633.  
  634.     function getCvvMessage()
  635.     {
  636.         return $this->_cvvCodeMessages[$this->cvvCode];
  637.     }
  638.  
  639.     function _mapFields($responseArray{
  640.         foreach($this->_fieldMap as $key => $val{
  641.             $this->$val $responseArray[$key]
  642.         }
  643.     }
  644. }
  645.  
  646. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:21 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.