Source for file AuthNet.class.php

  1. <?php
  2.     /**
  3.     * This class encapsulates transaction processing with Authorize.Net.
  4.     * @author Richard Sharp
  5.     * @copyright 2005-2010
  6.     * @version 3.5
  7.     * @package Payment
  8.     */
  9.     
  10.     /* Test Credit Card Numbers
  11.     370000000000002 - American Express
  12.      6011000000000012 - Discover
  13.      5424000000000015 - MasterCard
  14.      4007000000027 - Visa
  15.     4222222222222 - Forced Failure
  16.      */
  17.  
  18.  
  19.     class AuthNetGateway
  20.     {
  21.         //////////////////// public variables ////////////////////////////
  22.         
  23.         
  24.         /**
  25.         * Required. Sets the billing street address.
  26.         *    @var string 
  27.         */
  28.         var $Address="";
  29.         
  30.         /**
  31.         * Required. Sets the Amount to Charge
  32.         *    @var decimal 
  33.         */
  34.         var $Amount=0;
  35.         
  36.         /**
  37.         * Gets the returned apprival code.
  38.         *    @var string 
  39.         */
  40.         var $ApprovalCode="";
  41.         
  42.         /**
  43.         * Required. Sets the Authorize.Net API Login ID
  44.         *    @var string 
  45.         */
  46.         var $AuthNetLoginID="";
  47.         
  48.         /**
  49.         * Required. Sets the Authorize.Net API Transaction Key
  50.         *    @var string 
  51.         */
  52.         var $AuthNetTransactionKey="";
  53.         
  54.         /**
  55.         * Required. Sets the Credit Card Number. All non-numeric characters will be removed.
  56.         *    @var string 
  57.         */
  58.         var $CardNumber="";
  59.         
  60.         /**
  61.         * Required. Sets the billing city.
  62.         *    @var string 
  63.         */
  64.         var $City="";
  65.         
  66.         /**
  67.         * Optional. Sets the billing country.
  68.         *    @var string 
  69.         */
  70.         var $Country="US";
  71.         
  72.         /**
  73.         * Optional. Sets any custom information to be sent to Authorize.Net.
  74.         *    @var string 
  75.         */
  76.         var $Custom="";
  77.         
  78.         /**
  79.         * Options. Sets the Customer ID.
  80.         *    @var string 
  81.         */
  82.         var $CustomerID="";
  83.         
  84.         /**
  85.         * Required. Sets the transaction description.
  86.         *    @var string 
  87.         */
  88.         var $Description="";
  89.         
  90.         /**
  91.         * Optional. Sets the time window (in seconds) to prevent duplicate transactions. Default is 120 seconds.
  92.         *    @var string 
  93.         */
  94.         var $DuplicateWindow=120;
  95.         
  96.         /**
  97.         * Optional. Sets the billing email address.
  98.         *    @var string 
  99.         */
  100.         var $Email="";
  101.         
  102.         /**
  103.         * Required. Sets the credit card expiration date. Format: MMYY.
  104.         *    @var string 
  105.         */
  106.         var $ExpirationDate="";
  107.         
  108.         /**
  109.         * Required. Sets the billing first name.
  110.         *    @var string 
  111.         */
  112.         var $FirstName="";
  113.         
  114.         /**
  115.         * Optional. Sets the invoice number.
  116.         *    @var string 
  117.         */
  118.         var $InvoiceNumber="";
  119.         
  120.         /**
  121.         * Gets the error returned, if any.
  122.         *    @var string 
  123.         */
  124.         var $LastError="";
  125.         
  126.         /**
  127.         * Required. Sets the billing last name.
  128.         *    @var string 
  129.         */
  130.         var $LastName="";
  131.         
  132.         /**
  133.         * Gets the raw text of the last request sent to Authorize.Net
  134.         *    @var string 
  135.         */
  136.         var $LastRequest="";
  137.         
  138.         /**
  139.         * Gets the raw text of the last response from Authorize.Net
  140.         *    @var string 
  141.         */
  142.         var $LastResponse="";
  143.         
  144.         /**
  145.         * Optional. The billing phone number.
  146.         *    @var string 
  147.         */
  148.         var $Phone="";
  149.         
  150.         /**
  151.         * Gets the reason for the last response.
  152.         *    @var string 
  153.         */
  154.         var $ResponseReason="";
  155.         
  156.         /**
  157.         * Optional. Sets an email address to receive a copy of the confirmation.
  158.         *    @var string 
  159.         */
  160.         var $MerchantReceiptEmail="";
  161.         
  162.         /**
  163.         * Required. Sets the credit card security code.
  164.         *    @var string 
  165.         */
  166.         var $SecurityCode="";
  167.         
  168.         /**
  169.         * Required. Sets the billing state.
  170.         *    @var string 
  171.         */
  172.         var $State="";
  173.         
  174.         /**
  175.         * Optional. If true, the transaction will be processed in test mode.
  176.         *    @var string 
  177.         */
  178.         var $Test=false;
  179.         
  180.         /**
  181.         * Gets the returned Transaction ID.
  182.         *    @var string 
  183.         */
  184.         var $TransactionID="";
  185.         
  186.         /**
  187.         * Required. Gets the transaction Type. Possible values are: AUTH_CAPTURE, AUTH_ONLY, CAPTURE_ONLY, CREDIT, VOID, PRIOR_AUTH_CAPTURE
  188.         *    @var string 
  189.         */
  190.         var $TransactionType="AUTH_CAPTURE";
  191.         
  192.         /**
  193.         * Sets the billing zip code.
  194.         *    @var string 
  195.         */
  196.         var $Zip="";
  197.         
  198.                         
  199.         
  200.         ///////////////////// private_variables ///////////////////////////////////
  201.         
  202.         /**
  203.         * The URL to use in LIVE mdoe.
  204.         * @access private
  205.         *    @var string 
  206.         */
  207.         var $_live_url"https://secure.authorize.net/gateway/transact.dll";
  208.         
  209.         /**
  210.         * The URL to use in TEST mdoe.
  211.         * @access private
  212.         *    @var string 
  213.         */
  214.         var $_test_url"https://test.authorize.net/gateway/transact.dll";
  215.         
  216.         
  217.         
  218.         //////////////////////// public functions ////////////////////////////////
  219.         
  220.         /**
  221.          * Constructor for the class.
  222.          * @return void 
  223.          */ 
  224.         function AuthNetGateway({}
  225.         
  226.         /**
  227.          * Processes the transaction
  228.          * @return bool Returns true on success, false on error.
  229.          */ 
  230.         function ProcessTransaction()
  231.         {
  232.             $ok=false;
  233.             //validate the expiration date
  234.             $this->ExpirationDate=preg_replace("/[^0-9]/","",$this->ExpirationDate);
  235.             if(strlen($this->ExpirationDate)<4$this->ExpirationDate="0".$this->ExpirationDate;
  236.             if(strlen($this->ExpirationDate)!=4$error[]="Invalid date. Required format: MMYY";
  237.             
  238.             if(!$error)
  239.             {
  240.                 if($this->Test$url=$this->_test_url;
  241.                 else $url=$this->_live_url;
  242.                 
  243.                 $authnet_values                array
  244.                 (
  245.                     "x_login"                => $this->AuthNetLoginID,
  246.                     "x_version"                => "3.1",
  247.                     "x_delim_char"            => "|",
  248.                     "x_delim_data"            => "TRUE",
  249.                     "x_url"                    => "FALSE",
  250.                     "x_type"                => $this->TransactionType,
  251.                     "x_method"                => "CC",
  252.                     "x_tran_key"            => $this->AuthNetTransactionKey,
  253.                     "x_relay_response"        => "FALSE",
  254.                     "x_card_num"            => ereg_replace("[^0-9]","",$this->CardNumber),
  255.                     "x_exp_date"            => $this->ExpirationDate,
  256.                     "x_description"            => $this->Description,
  257.                     "x_duplicate_window"    => $this->DuplicateWindow,
  258.                     "x_amount"                => $this->Amount,
  259.                     "x_invoice_num"            => $this->InvoiceNumber,
  260.                     "x_first_name"            => $this->FirstName,
  261.                     "x_last_name"            => $this->LastName,
  262.                     "x_address"                => $this->Address,
  263.                     "x_city"                => $this->City,
  264.                     "x_state"                => $this->State,
  265.                     "x_country"                => $this->Country,
  266.                     "x_zip"                    => $this->Zip,
  267.                     "x_phone"                    => $this->Phone,
  268.                     "x_email"                => $this->Email,
  269.                     "x_email_customer"            => "FALSE",
  270.                     "SpecialCode"            => $this->Custom,
  271.                     "x_cust_id"            => $this->CustomerID,
  272.                     "x_merchant_email"            => $this->MerchantReceiptEmail,
  273.                     "x_card_code"            => $this->SecurityCode,
  274.                     "x_trans_id"            => $this->TransactionID
  275.                 );
  276.                 if($this->Test$authnet_values["x_test_request"]="TRUE";
  277.                 else $authnet_values["x_test_request"]="FALSE";
  278.                 
  279.                 foreach($authnet_values as $key => $value$params[]=$key."=".urlencode($value);
  280.                 
  281.                 //connect and send the request
  282.                 $ch curl_init($url)
  283.                 curl_setopt($chCURLOPT_HEADER0)// set to 0 to eliminate header info from response
  284.                 curl_setopt($chCURLOPT_RETURNTRANSFER1)// Returns response data instead of TRUE(1)
  285.                 curl_setopt($chCURLOPT_POSTFIELDSimplode("&",$params ))// use HTTP POST to send form data
  286.                 $resp curl_exec($ch)//execute post and get results
  287.                 curl_close ($ch);
  288.                 
  289.                 $this->LastRequest=implode("&",$params);
  290.                 $this->LastResponse = $resp;
  291.                 
  292.                 //parse the response
  293.                 $response_parts=explode("|",$resp);
  294.                 if($response_parts[0]==1//approved
  295.                 {
  296.                     $this->ApprovalCode=$response_parts[4];
  297.                     $this->TransactionID=$response_parts[6];
  298.                     $this->ResponseReason="The transaction has been approved.";
  299.                     $ok=true;
  300.                 }
  301.                 else //declined
  302.                 {
  303.                     switch($response_parts[2])
  304.                     {
  305.                         case 5:
  306.                             $this->ResponseReason="A valid amount is required.";
  307.                             break;
  308.                         case 6:
  309.                             $this->ResponseReason="The credit card number is invalid.";
  310.                             break;
  311.                         case 7:
  312.                             $this->ResponseReason="The credit card expiration date is invalid.";
  313.                             break;
  314.                         case 8:
  315.                             $this->ResponseReason="The credit card has expired.";
  316.                             break;
  317.                         case 11:
  318.                             $this->ResponseReason="A duplicate transaction has been submitted.";
  319.                             break;
  320.                         case 12:
  321.                             $this->ResponseReason="An authorization code is required but not present.";
  322.                             break;
  323.                         case 13:
  324.                             $this->ResponseReason="The merchant API login ID is invalid or the account is inactive.";
  325.                             break;
  326.                         case 15:
  327.                             $this->ResponseReason="The transaction ID is invalid.";
  328.                             break;
  329.                         case 16:
  330.                             $this->ResponseReason="The transaction was not found.";
  331.                             break;
  332.                         case 17:
  333.                             $this->ResponseReason="The merchant does not accept this type of credit card.";
  334.                             break;
  335.                         case 27:
  336.                             $this->ResponseReason="The transaction resulted in an AVS mismatch. The address provided does not match billing address of cardholder.";
  337.                             break;
  338.                         case 28:
  339.                             $this->ResponseReason="The merchant does not accept this type of credit card.";
  340.                             break;
  341.                         case 36:
  342.                             $this->ResponseReason="The authorization was approved, but settlement failed.";
  343.                             break;
  344.                         case 37:
  345.                             $this->ResponseReason="The credit card number is invalid.";
  346.                             break;
  347.                         case 42:
  348.                             $this->ResponseReason="There is missing or invalid information in a required field.";
  349.                             break;
  350.                         case 47:
  351.                             $this->ResponseReason="The amount requested for settlement may not be greater than the original amount authorized.";
  352.                             break;
  353.                         case 48:
  354.                             $this->ResponseReason="This processor does not accept partial reversals.";
  355.                             break;
  356.                         case 50:
  357.                             $this->ResponseReason="This transaction is awaiting settlement and cannot be refunded.";
  358.                             break;
  359.                         case 51:
  360.                             $this->ResponseReason="The sum of all credits against this transaction is greater than the original transaction amount.";
  361.                             break;
  362.                         case 52:
  363.                             $this->ResponseReason="The transaction was authorized, but the client could not be notified; the transaction will not be settled.";
  364.                             break;
  365.                         case 55:
  366.                             $this->ResponseReason="The sum of credits against the referenced transaction would exceed the original debit amount.";
  367.                             break;
  368.                         default:
  369.                             $this->ResponseReason="The transaction has been declined.";
  370.                             break;
  371.                     }
  372.                 }
  373.             }
  374.             if($error$this->LastError=$error;
  375.             return($ok);
  376.         }
  377.     }
  378. ?>