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

Source for file TrustCommerce.php

Documentation is available at TrustCommerce.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: Joe Stump <joe@joestump.net>                                |
  17. // |          Robert Peake <robert.peake@trustcommerce.com>               |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: TrustCommerce.php,v 1.5 2005/07/08 00:13:25 jstump Exp $
  21.  
  22. require_once('Payment/Process.php');
  23. require_once('Payment/Process/Common.php');
  24. require_once('Net/Curl.php');
  25.  
  26. $GLOBALS['_Payment_Process_TrustCommerce'= array(
  27.     PAYMENT_PROCESS_ACTION_NORMAL   => 'sale',
  28.     PAYMENT_PROCESS_ACTION_AUTHONLY => 'preauth',
  29.     PAYMENT_PROCESS_ACTION_POSTAUTH => 'postauth'
  30. );
  31.  
  32. /**
  33.  * Payment_Process_TrustCommerce
  34.  *
  35.  * This is a processor for TrustCommerce's merchant payment gateway.
  36.  * (http://www.trustcommerce.com/)
  37.  *
  38.  * *** WARNING ***
  39.  * This is ALPHA code, and has not been fully tested. It is not recommended
  40.  * that you use it in a production envorinment without further testing.
  41.  *
  42.  * @package Payment_Process
  43.  * @author Robert Peake <robert.peake@trustcommerce.com>
  44.  * @version @version@
  45.  */
  46. class Payment_Process_TrustCommerce extends Payment_Process_Common {
  47.     /**
  48.      * Front-end -> back-end field map.
  49.      *
  50.      * This array contains the mapping from front-end fields (defined in
  51.      * the Payment_Process class) to the field names TrustCommerce requires.
  52.      *
  53.      * @see _prepare()
  54.      * @access private
  55.      */
  56.     var $_fieldMap = array(
  57.         // Required
  58.         'login' => 'custid',
  59.         'password' => 'password',
  60.         'action' => 'action',
  61.         'amount' => 'amount',
  62.         //PostAuth
  63.         'transactionId' => 'transid',
  64.         // Optional
  65.         'name' => 'name',
  66.         'address' => 'address1',
  67.         'city' => 'city',
  68.         'state' => 'state',
  69.         'country' => 'country',
  70.         'phone' => 'phone',
  71.         'email' => 'email',
  72.         'zip' => 'zip',
  73.         'currency' => 'currency',
  74.     );
  75.  
  76.     /**
  77.     * $_typeFieldMap
  78.     *
  79.     * @author Robert Peake <robert.peake@trustcommerce.com>
  80.     * @access protected
  81.     */
  82.     var $_typeFieldMap = array(
  83.  
  84.            'CreditCard' => array(
  85.  
  86.                     'cardNumber' => 'cc',
  87.                     'cvv' => 'cvv',
  88.                     'expDate' => 'exp'
  89.  
  90.            ),
  91.  
  92.            'eCheck' => array(
  93.  
  94.                     'routingCode' => 'routing',
  95.                     'accountNumber' => 'account',
  96.                     'name' => 'name'
  97.  
  98.            )
  99.     );
  100.  
  101.     /**
  102.      * Default options for this processor.
  103.      *
  104.      * @see Payment_Process::setOptions()
  105.      * @access private
  106.      */
  107.     var $_defaultOptions = array();
  108.  
  109.     /**
  110.      * Has the transaction been processed?
  111.      *
  112.      * @type boolean
  113.      * @access private
  114.      */
  115.     var $_processed = false;
  116.  
  117.     /**
  118.      * The response body sent back from the gateway.
  119.      *
  120.      * @access private
  121.      */
  122.     var $_responseBody '';
  123.  
  124.     /**
  125.      * Constructor.
  126.      *
  127.      * @param  array  $options  Class options to set.
  128.      * @see Payment_Process::setOptions()
  129.      * @return void 
  130.      */
  131.     function __construct($options = false)
  132.     {
  133.         parent::__construct($options);
  134.         $this->_driver 'TrustCommerce';
  135.     }
  136.  
  137.     function Payment_Process_TrustCommerce($options = false)
  138.     {
  139.         $this->__construct($options);
  140.     }
  141.  
  142.     /**
  143.      * Process the transaction.
  144.      *
  145.      * @return mixed Payment_Process_Result on success, PEAR_Error on failure
  146.      */
  147.     function &process()
  148.     {
  149.         // Sanity check
  150.         $result $this->validate();
  151.         if(PEAR::isError($result)) {
  152.             return $result;
  153.         }
  154.  
  155.         // Prepare the data
  156.         $result $this->_prepare();
  157.         if (PEAR::isError($result)) {
  158.             return $result
  159.         }
  160.  
  161.         // Don't die partway through
  162.         PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  163.  
  164.         $fields $this->_prepareQueryString();
  165.  
  166.         if(function_exists('tclink_send')) {
  167.             /** USE TCLINK **/
  168.             $result = tclink_send($fields);
  169.             $r_keys array_keys($result);
  170.             for($i=0;$i<sizeof($r_keys);$i++{
  171.                 $key $r_keys[$i];
  172.                 $value $result[$key];
  173.                 $result_string .= $key.'='.$value."\n";
  174.             }
  175.             if (PEAR::isError($result_string)) {
  176.                 PEAR::popErrorHandling();
  177.                 return $result_string;
  178.             else {
  179.                 $result $result_string;
  180.             }
  181.         else {
  182.             /** USE CURL **/
  183.             $curl new Net_Curl('https://vault.trustcommerce.com/trans/');
  184.             if (PEAR::isError($curl)) {
  185.                 PEAR::popErrorHandling();
  186.                 return $curl;
  187.             }
  188.  
  189.             $curl->type = 'PUT';
  190.             $curl->fields = $fields;
  191.             $curl->userAgent = 'PEAR Payment_Process_TrustCommerce 0.1a';
  192.  
  193.             $result &$curl->execute();
  194.             if (PEAR::isError($result)) {
  195.                 PEAR::popErrorHandling();
  196.                 return $result;
  197.             else {
  198.                 $curl->close();
  199.             }
  200.         }
  201.         /** END TCLINK/CURL CASE STATEMENT **/
  202.  
  203.         $this->_responseBody trim($result);
  204.         $this->_processed = true;
  205.  
  206.         // Restore error handling
  207.         PEAR::popErrorHandling();
  208.  
  209.         $response &Payment_Process_Result::factory($this->_driver,
  210.                                                      $this->_responseBody
  211.                                                      &$this);
  212.  
  213.         if (!PEAR::isError($response)) {
  214.             $response->parse();
  215.         }
  216.  
  217.         return $response;
  218.     }
  219.  
  220.     /**
  221.      * Get (completed) transaction status.
  222.      *
  223.      * @return boolean status.
  224.      */
  225.     function getStatus()
  226.     {
  227.         return false;
  228.     }
  229.  
  230.     /**
  231.      * Prepare the POST query string.
  232.      *
  233.      * @access private
  234.      * @return string The query string
  235.      */
  236.     function _prepareQueryString()
  237.     {
  238.  
  239.         $data $this->_data;
  240.  
  241.         /* expiration is expressed as mmyy */
  242.         $fulldate $data['exp'];
  243.         $month strtok($fulldate,'/');
  244.         $year strtok('');
  245.         $exp $month.substr($year,2,2);
  246.         $data['exp'$exp;
  247.         /* end expiration mangle */
  248.  
  249.         /* amount is expressed in cents with leading zeroes */
  250.         $data['amount'$data['amount']*100;
  251.         if (strlen($data['amount']== 1{
  252.             $data['amount'"00".$data['amount'];
  253.         else if(strlen($data['amount']< 3{
  254.             $data['amount'"0".$data['amount'];
  255.         else if(strlen($data['amount']> 8{
  256.             $amount_message 'Amount: '.$data['amount'].' too large.';
  257.             PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  258.             PEAR::raiseError($amount_message);
  259.             PEAR::popErrorHandling();
  260.         }
  261.         /* end amount mangle */
  262.  
  263.         if ($this->_payment->getType(== 'CreditCard' && 
  264.             $this->action != PAYMENT_PROCESS_ACTION_POSTAUTH{
  265.             $data['media''cc';
  266.         }
  267.  
  268.         if ($this->_payment->getType(== 'eCheck'{
  269.             $data['media''ach';
  270.         }
  271.  
  272.         $return = array();
  273.         $sets = array();
  274.         foreach ($data as $key => $val{
  275.             if (strlen($val)) {
  276.                 $return[$key$val;
  277.                 $sets[$key.'='.urlencode($val);
  278.             }
  279.         }
  280.         
  281.         $this->_options['authorizeUri''https://vault.trustcommerce.com/trans/?'.implode('&',$sets);
  282.  
  283.         return $return;
  284.     }
  285. }
  286.  
  287.  
  288.     var $_statusCodeMap = array('approved' => PAYMENT_PROCESS_RESULT_APPROVED,
  289.                                 'accepted' => PAYMENT_PROCESS_RESULT_APPROVED,
  290.                                 'declined' => PAYMENT_PROCESS_RESULT_DECLINED,
  291.                                 'baddata' => PAYMENT_PROCESS_RESULT_OTHER,
  292.                                 'error' => PAYMENT_PROCESS_RESULT_OTHER);
  293.  
  294.     /**
  295.      * TrustCommerce status codes
  296.      *
  297.      * This array holds response codes.
  298.      *
  299.      * @see getStatusText()
  300.      * @access private
  301.      */
  302.     var $_statusCodeMessages = array(
  303.         'approved' => 'The transaction was successfully authorized.',
  304.         'accepted' => 'The transaction has been successfully accepted into the system.',
  305.         'decline' => 'The transaction was declined, see declinetype for further details.',
  306.         'baddata' => 'Invalid parameters passed, see error for further details.',
  307.         'error' => 'System error when processing the transaction, see errortype for details.',
  308.     );
  309.  
  310.     var $_avsCodeMap = array(
  311.         'N' => PAYMENT_PROCESS_AVS_MISMATCH,
  312.         'U' => PAYMENT_PROCESS_AVS_NOAPPLY,
  313.         'G' => PAYMENT_PROCESS_AVS_NOAPPLY,
  314.         'R' => PAYMENT_PROCESS_AVS_ERROR,
  315.         'E' => PAYMENT_PROCESS_AVS_ERROR,
  316.         'S' => PAYMENT_PROCESS_AVS_ERROR,
  317.         'O' => PAYMENT_PROCESS_AVS_ERROR
  318.     );
  319.  
  320.     var $_avsCodeMessages = array(
  321.          'X' => 'Exact match, 9 digit zipcode.',
  322.          'Y' => 'Exact match, 5 digit zipcode.',
  323.          'A' => 'Street address match only.',
  324.          'W' => '9 digit zipcode match only.',
  325.          'Z' => '5 digit zipcode match only.',
  326.          'N' => 'No mtach on street address or zipcode.',
  327.          'U' => 'AVS unavailable on this card.',
  328.          'G' => 'Non-US card issuer, AVS unavailable.',
  329.          'R' => 'Card issuer system currently down, try again later.',
  330.          'E' => 'Error, ineligible - not a mail/phone order.',
  331.          'S' => 'Service not supported.',
  332.          'O' => 'General decline or other error'
  333.     );
  334.  
  335.     var $_cvvCodeMap = array('cvv' => PAYMENT_PROCESS_CVV_MISMATCH
  336.     );
  337.  
  338.     var $_cvvCodeMessages = array'cvv' => 'The CVV number is not valid.'
  339.     );
  340.  
  341.     var $_fieldMap = array('status'  => 'code',
  342.                            'avs'  => 'avsCode',
  343.                            'transid'  => 'transactionId'
  344.     );
  345.  
  346.  
  347.     function Payment_Process_Response_TrustCommerce($rawResponse
  348.     {
  349.         $this->Payment_Process_Response($rawResponse);
  350.     }
  351.  
  352.     function parse()
  353.     {
  354.       $array preg_split("/\n/",$this->_rawResponse,0,PREG_SPLIT_NO_EMPTY);
  355.       for($i=0;$i<sizeof($array);$i++)
  356.       {
  357.           $response_line $array[$i];
  358.           $response_array preg_split("/=/",$response_line);
  359.           $key $response_array[0];
  360.           $value $response_array[1];
  361.           $responseArray[$key$value;
  362.       }
  363.       $this->_mapFields($responseArray);
  364.     }
  365.  
  366.     function _mapFields($responseArray)
  367.     {
  368.         foreach($this->_fieldMap as $key => $val{
  369.             $this->$val $responseArray[$key];
  370.         }
  371.         if (!isset($this->_statusCodeMessages[$this->messageCode])) 
  372.         {
  373.             $message $this->_statusCodeMessages[$responseArray['status']];
  374.             if($responseArray['error'])
  375.             {
  376.                 $message .= "\nError type: ".$responseArray['error'].'.';
  377.                 if($responseArray['offenders'])
  378.                 {
  379.                     $message .= "\nOffending fields: ".$responseArray['offenders'].'.';
  380.                 }
  381.             }
  382.             $this->_statusCodeMessages[$this->messageCode$message;
  383.         
  384.     }
  385. }
  386. ?>

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