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

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