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

Source for file Result.php

Documentation is available at Result.php

  1. <?php
  2. require_once 'Payment/Process2/Exception.php';
  3. require_once 'Payment/Process2/Common.php';
  4. /**
  5.  * Payment_Process2_Result
  6.  *
  7.  * The core result class that should be returned from each driver's process()
  8.  * function. This should be exte33nded as Payment_Process2_Result_DriverName and
  9.  * then have the appropriate fields mapped out accordingly.
  10.  *
  11.  * Take special care to appropriately create a parse() function in your result
  12.  * class. You can then call _mapFields() with a resultArray (ie. exploded
  13.  * result) to map your results from parse() into the member variables.
  14.  *
  15.  * Please note that this class keeps your original codes intact so they can
  16.  * be accessed directly and then uses the function wrappers to return uniform
  17.  * Payment_Process codes.
  18.  *
  19.  * @category Payment
  20.  * @package  Payment_Process2
  21.  * @author   Joe Stump <joe@joestump.net>
  22.  * @license  http://www.opensource.org/licenses/bsd-license.php BSD License
  23.  * @version  Release: @package_version@
  24.  * @link     http://pear.php.net/package/Payment_Process2
  25.  */
  26. {
  27.     /**
  28.      * Processor instance which this result was instantiated from.
  29.      *
  30.      * This should contain a reference to the requesting Processor.
  31.      *
  32.      * @author Ian Eure <ieure@php.net>
  33.      * @access private
  34.      * @var    Object 
  35.      */
  36.     var $_request;
  37.  
  38.     /**
  39.      * The raw response
  40.      *
  41.      * @author Joe Stump <joe@joestump.net>
  42.      * @access protected
  43.      * @var    string  $_rawResponse 
  44.      */
  45.     var $_rawResponse = null;
  46.  
  47.     /**
  48.      * The approval/decline code
  49.      *
  50.      * The value returned by your gateway as approved/declined should be mapped
  51.      * into this variable. Valid results should then be mapped into the
  52.      * appropriate Payment_Process2::RESULT_* code using the $_statusCodeMap
  53.      * array. Values returned into $code should be mapped as keys in the map
  54.      * with Payment_Process2::RESULT_* as the values.
  55.      *
  56.      * @author  Joe Stump <joe@joestump.net>
  57.      * @access  public
  58.      * @var     mixed  $code 
  59.      * @see    Payment_Process2::RESULT_APPROVED, Payment_Process2::RESULT_DECLINED
  60.      * @see    Payment_Process2::RESULT_OTHER, $_statusCodeMap
  61.      */
  62.     var $code;
  63.  
  64.     /**
  65.      * Message/Response Code
  66.      *
  67.      * Along with the response (yes/no) you usually get a response/message
  68.      * code that translates into why it was approved/declined. This is where
  69.      * you map that code into. Your $_statusCodeMessages would then be keyed by
  70.      * valid messageCode values.
  71.      *
  72.      * @author  Joe Stump <joe@joestump.net>
  73.      * @access  public
  74.      * @var     mixed  $messageCode 
  75.      * @see     $_statusCodeMessages
  76.      */
  77.     var $messageCode;
  78.  
  79.     /**
  80.      * Message from gateway
  81.      *
  82.      * Map the textual message from the gateway into this variable. It is not
  83.      * currently returned or used (in favor of the $_statusCodeMessages map, but
  84.      * can be accessed directly for debugging purposes.
  85.      *
  86.      * @author  Joe Stump <joe@joestump.net>
  87.      * @access  public
  88.      * @var     string   $message 
  89.      * @see     $_statusCodeMessages
  90.      */
  91.     var $message;
  92.  
  93.     /**
  94.      * Authorization/Approval code
  95.      *
  96.      * @author  Joe Stump <joe@joestump.net>
  97.      * @access  public
  98.      * @var     string  $approvalCode 
  99.      */
  100.     var $approvalCode;
  101.  
  102.     /**
  103.      * Address verification code
  104.      *
  105.      * The AVS code returned from your gateway. This should then be mapped to
  106.      * the appropriate Payment_Process2::AVS_* code using $_avsCodeMap. This value
  107.      * should also be mapped to the appropriate textual message via the
  108.      * $_avsCodeMessages array.
  109.      *
  110.      * @author  Joe Stump <joe@joestump.net>
  111.      * @access  public
  112.      * @var     string  $avsCode 
  113.      * @see     Payment_Process2::AVS_MISMATCH, Payment_Process2::AVS_ERROR
  114.      * @see     Payment_Process2::AVS_MATCH, Payment_Process2::AVS_NOAPPLY, $_avsCodeMap
  115.      * @see     $_avsCodeMessages
  116.      */
  117.     var $avsCode;
  118.  
  119.     /**
  120.      * Transaction ID
  121.      *
  122.      * This is the unique transaction ID, which is used by gateways to modify
  123.      * transactions (credit, update, etc.). Map the appropriate value into this
  124.      * variable.
  125.      *
  126.      * @author  Joe Stump <joe@joestump.net>
  127.      * @access  public
  128.      * @var     string $transactionId 
  129.      */
  130.     var $transactionId;
  131.  
  132.     /**
  133.      * Invoice Number
  134.      *
  135.      * Unique internal invoiceNumber (ie. your company's order/invoice number
  136.      * that you assign each order as it is processed). It is always a good idea
  137.      * to pass this to the gateway (which is usually then echo'd back).
  138.      *
  139.      * @author Joe Stump <joe@joestump.net>
  140.      * @access public
  141.      * @var string $invoiceNumber 
  142.      */
  143.     var $invoiceNumber;
  144.  
  145.     /**
  146.      * Customer ID
  147.      *
  148.      * Unique internal customer ID (ie. your company's customer ID used to
  149.      * track individual customers).
  150.      *
  151.      * @author Joe Stump <joe@joestump.net>
  152.      * @access public
  153.      * @var string $customerId 
  154.      */
  155.     var $customerId;
  156.  
  157.     /**
  158.      * CVV Code
  159.      *
  160.      * The CVV code is the 3-4 digit number on the back of most credit cards.
  161.      * This value should be mapped via the $_cvvCodeMap variable to the
  162.      * appropriate Payment_Process2::CVV_* values.
  163.      *
  164.      * @author Joe Stump <joe@joestump.net>
  165.      * @access public
  166.      * @var string $cvvCode 
  167.      */
  168.     var $cvvCode = Payment_Process2::CVV_NOAPPLY;
  169.  
  170.     /**
  171.      * CVV Message
  172.      *
  173.      * Your cvvCode value should be mapped to appropriate messages via the
  174.      * $_cvvCodeMessage array. This value is merely here to hold the value
  175.      * returned from the gateway (if any).
  176.      *
  177.      * @author Joe Stump <joe@joestump.net>
  178.      * @access public
  179.      * @var string $cvvMessage 
  180.      */
  181.     var $cvvMessage = 'No CVV message from gateway';
  182.  
  183.  
  184.     var $_avsCodeMap = array();
  185.  
  186.     var $_cvvCodeMap = array();
  187.  
  188.     /**
  189.      * Class constructor
  190.      *
  191.      * @param string $rawResponse Raw response
  192.      * @param Payment_Process2_Common $request     Request
  193.      */
  194.     function __construct($rawResponse,  Payment_Process2_Common $request)
  195.     {
  196.         $this->_rawResponse = $rawResponse;
  197.         $this->_request     $request;
  198.     }
  199.  
  200.  
  201.     /**
  202.     * factory
  203.     *
  204.     * @param string $type        Type
  205.     * @param string $rawResponse Raw response
  206.     * @param  Payment_Process2_Common $request     Request
  207.     *
  208.     * @return Payment_Process2_Result 
  209.     * @throws Payment_Process2_Exception
  210.     * @author Joe Stump <joe@joestump.net>
  211.     * @author Ian Eure <ieure@php.net>
  212.     */
  213.     function factory($type$rawResponsePayment_Process2_Common $request)
  214.     {
  215.  
  216.  
  217.         $class 'Payment_Process2_Result_'.$type;
  218.  
  219.         $path 'Payment/Process2/Result/'.$type.'.php';
  220.         if (@fclose(@fopen($path'r'true))) {
  221.             include_once $path;
  222.  
  223.             if (class_exists($class)) {
  224.                 $ret = new $class($rawResponse$request);
  225.                 return $ret;
  226.             }
  227.         }
  228.  
  229.         throw new Payment_Process2_Exception('Invalid response type: '.$type.'('.$class.')');
  230.     }
  231.  
  232.     /**
  233.      * validate
  234.      *
  235.      * @author Joe Stump <joe@joestump.net>
  236.      * @access public
  237.      * @return mixed 
  238.      */
  239.     function validate()
  240.     {
  241.         $request $this->_request;
  242.         $payment $request->_payment;
  243.  
  244.         if ($request->getOption('avsCheck'=== true{
  245.             if ($this->getAVSCode(!= Payment_Process2::AVS_MATCH{
  246.                 throw new Payment_Process2_Exception('AVS check failed',
  247.                                         Payment_Process2::ERROR_AVS);
  248.             }
  249.         }
  250.  
  251.         if ($request->getOption('cvvCheck'=== true &&
  252.             $payment instanceof Payment_Process2_Type_CreditCard{
  253.  
  254.             if ($this->getCvvCode(!= Payment_Process2::CVV_MATCH{
  255.                 throw new Payment_Process2_Exception('CVV check failed',
  256.                                         Payment_Process2::ERROR_CVV);
  257.             }
  258.  
  259.         }
  260.  
  261.         if ($this->getCode(!= Payment_Process2::RESULT_APPROVED{
  262.             throw new Payment_Process2_Exception($this->getMessage(),
  263.                                     Payment_Process2::RESULT_DECLINED);
  264.         }
  265.  
  266.         return true;
  267.     }
  268.  
  269.     /**
  270.      * getCode
  271.      *
  272.      * @return integer one of Payment_Process2::RESULT_* constant
  273.      * @author Joe Stump <joe@joestump.net>
  274.      * @access public
  275.      */
  276.     function getCode()
  277.     {
  278.         if (isset($this->_statusCodeMap[$this->code])) {
  279.             return $this->_statusCodeMap[$this->code];
  280.         }
  281.  
  282.         return Payment_Process2::RESULT_DECLINED;
  283.     }
  284.  
  285.     /**
  286.      * getMessage
  287.      *
  288.      * Return the message from the code map, or return the raw message if
  289.      * there is one. Otherwise, return a worthless message.
  290.      *
  291.      * @author Joe Stump <joe@joestump.net>
  292.      * @access public
  293.      * @return string 
  294.      */
  295.     function getMessage()
  296.     {
  297.         if (isset($this->_statusCodeMessages[$this->messageCode])) {
  298.             return $this->_statusCodeMessages[$this->messageCode];
  299.         elseif (strlen($this->message)) {
  300.             return $this->message;
  301.         }
  302.  
  303.         return 'No message reported';
  304.     }
  305.  
  306.     /**
  307.      * Returns the AVS code
  308.      *
  309.      * @return integer one of Payment_Process2::AVS_* constants
  310.      */
  311.     function getAVSCode()
  312.     {
  313.         return isset($this->_avsCodeMap[$this->avsCode]$this->_avsCodeMap[$this->avsCode: null;
  314.     }
  315.  
  316.     /**
  317.      * Returns the AVS message
  318.      *
  319.      * @return string 
  320.      */
  321.     function getAVSMessage()
  322.     {
  323.         return isset($this->_avsCodeMessages[$this->avsCode]$this->_avsCodeMessages[$this->avsCode: null;
  324.     }
  325.  
  326.  
  327.     /**
  328.      * Return the CVV match code
  329.      *
  330.      * @todo Think if this should raise exceptions for unknown cvvcode?
  331.      *
  332.      * @return integer One of Payment_Process2::CVV_* constants or null
  333.      */
  334.     function getCvvCode()
  335.     {
  336.         return isset($this->_cvvCodeMap[$this->cvvCode])$this->_cvvCodeMap[$this->cvvCode: null;
  337.     }
  338.  
  339.     /**
  340.      * Returns the CVV match message
  341.      *
  342.      * @return string 
  343.      */
  344.     function getCvvMessage()
  345.     {
  346.         return $this->_cvvCodeMessages[$this->cvvCode];
  347.     }
  348.  
  349.     /**
  350.      * _mapFields
  351.      *
  352.      * @param mixed $responseArray Response array
  353.      *
  354.      * @return void 
  355.      * @author Joe Stump <joe@joestump.net>
  356.      * @access private
  357.      */
  358.     function _mapFields($responseArray)
  359.     {
  360.         foreach ($this->_fieldMap as $key => $val{
  361.             $this->$val (array_key_exists($key$responseArray))
  362.                           ? $responseArray[$key]
  363.                           : null;
  364.         }
  365.     }
  366.  
  367.     /**
  368.      * Accept an object
  369.      *
  370.      * @param object $object Object to accept
  371.      *
  372.      * @return boolean  TRUE if accepted, FALSE otherwise
  373.      */
  374.     function accept($object)
  375.     {
  376.         if (is_a($object'Log')) {
  377.             $this->_log $object;
  378.             return true;
  379.         }
  380.         return false;
  381.     }
  382.  
  383.     /**
  384.      * Log a message
  385.      *
  386.      * @param string $message  Message to log
  387.      * @param string $priority Message priority
  388.      *
  389.      * @return mixed  Return value of Log::log(), or false if no Log instance
  390.      *                 has been accepted.
  391.      */
  392.     function log($message$priority = null)
  393.     {
  394.         if (isset($this->_log&& is_object($this->_log)) {
  395.             return $this->_log->log($message$priority);
  396.         }
  397.         return false;
  398.     }
  399. }

Documentation generated on Mon, 11 Mar 2019 15:40:37 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.