• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Authorize API

#1
[eluser]mikeyhell[/eluser]
Had to write this for a project... It's somewhat unfinished and I had to take some things out for security, but this should be a good starting point for someone looking to integrate authorize.net

Part 1:
Code:
<?php


class Authorize
{

//****************************************************************************

   function __construct( $processXML )
   {
      $this->processXML = $processXML;
   }

//****************************************************************************

   /**
   * Process the Credit Card Charge
   * @param array $params An assosciative array of paramaters to pass to Authorize.net
   * @return array Array of the results from the payment gateway
   */

   function chargeCard( $params, $processor = array() )
   {
      /**
      * Configuration
      */

      $x_Login    = (string)$this->processXML->user;     // Your authorize.net login
      $x_Password = (string)$this->processXML->password; // Your authorize.net password (if Password-Required Mode is enabled)
      $x_Tran_Key = (string)$this->processXML->tranKey;  // Your authorize.net tran_key

      if( isset( $processor['user'] ) && $processor['user'] != '' )
      {
         $x_Login    = $processor['user'];
         $x_Password = $processor['password'];
         $x_Tran_Key = $processor['transactionKey'];
      }
      
      $x_Delim_Data = "TRUE";    // Delimited response from the gateway (or set in the Setting Menu)
      $x_Delim_Char = "|";       // Character that will be used to separate fields
      $x_Encap_Char = "";        // Character that will be used to encapsulate fields
      
      if( !isset( $params['transType'] ) )
      {
         $x_Type         = "AUTH_CAPTURE";  // Default transaction type
      }
      else
      {
         $xType          = $params['transType'];

         if( $params['transType'] == 'AUTHCAP' )
            $params['transType'] = 'AUTH_CAPTURE';
         else if( $params['transType'] == 'AUTH' )
            $params['transType'] = 'AUTH_ONLY';
         else if( $params['transType'] == 'CAPTURE' )
            $params['transType'] = 'PRIOR_AUTH_CAPTURE';
         else if( $params['transType'] == 'ECHECK' )
            $params['transType'] = 'AUTH_CAPTURE';

         $x_Type = $params['transType'];
      }

      $x_Test_Request = "TRUE";          // Make this a test transaction
//      $x_Test_Request = "FALSE";
      
      /**
      * Customer Information
      */

      /**
      * Required
      */

      if( $xType == 'ECHECK' )
      {
         $x_Method         = 'ECHECK';
         $x_bank_aba_code  = $params['checkRouting'];
         $x_bank_acct_num  = $params['checkAccount'];
         $x_bank_acct_type = $params['checkType'];
         $x_bank_name      = $params['checkBankName'];
         $x_bank_acct_name = $params['checkName'];

         if( $params['checkType'] == 'BUSINESSCHECKING' )
            $x_echeck_type = 'CCD';
         else
            $x_echeck_type    = 'WEB';
      }
      else
      {
         $x_Method     = "CC";
         $x_Card_Num   = $params['ccardNum'];

         if( strlen( $params['expirationDate'] ) == 3 )
            $params['expirationDate'] = '0' . $params['expirationDate'];
      
         $x_Exp_Date   = $params['expirationDate'][0] . $params['expirationDate'][1] . '/' .
                         $params['expirationDate'][2] . $params['expirationDate'][3];
      }

      $x_Amount     = $params['totalAmount'];
      $x_First_Name = $params['firstName'];
      $x_Last_Name  = $params['lastName'];

      if( ( $xType == 'CREDIT' || $xType == 'VOID' ) && $x_Amount < 0 )
      {
         $x_Amount = ( $x_Amount * -1 );
      }

      /**
      * Optional
      */

      if( $params['description'] != '' )
         $x_Description = $params['description'];
      else if( $params['comment'] != '' )
         $x_Description = $params['comment'];
      else
         $x_Description = $params['comments'];

      if( isset( $params['zip'] ) )
         $x_Zip = $params['zip'];
      else if( isset( $params['postalCode'] ) )
         $x_Zip = $params['postalCode'];
      else
         $x_Zip = $params['zipPostalCode'];

      if( isset( $x_Zip ) )
         $x_Zip = strtoupper( str_replace( ' ', '', $x_Zip ) );

      //$x_Zip             = isset( $params['zip'] ) ? $params['zip'] : $params['postalCode'];

      $x_Company         = $params['company'];
      $x_Address         = isset( $params['address'] ) ? $params['address'] : $params['address1'];
      $x_City            = $params['city'];
      $x_State           = isset( $params['state'] ) ? $params['state'] : $params['stateProvince'];
      $x_Country         = $params['country'];
      $x_Phone           = $params['phone'];
      $x_Fax             = $params['fax'];
      $x_Cust_Id         = isset( $params['custId'] ) ? $params['custId'] : $params['userId'];
      $x_Customer_Ip     = $params['customerIp'];
      $x_Customer_Tax_Id = $params['customerTaxId'];
      $x_Invoice_Num     = isset( $params['invoiceNum'] ) ? $params['invoiceNum'] : $params['invoiceId'];
      $x_CVV2            = $params['cvv2'];

#2
[eluser]mikeyhell[/eluser]
Part 2:
Code:
/**
      * Build fields string to post
      */

      $fields =  "x_version=3.1&x;_login=$x_Login&x;_delim_data=$x_Delim_Data&x;_delim_char=$x_Delim_Char" .
                 "&x;_encap_char=$x_Encap_Char&x;_type=$x_Type&x;_test_request=$x_Test_Request&x;_method=$x_Method" .
                 "&x;_amount=$x_Amount&x;_first_name=$x_First_Name&x;_last_name=$x_Last_Name&x;_tran_key=$x_Tran_Key";

      if( $x_Method == 'ECHECK' )
      {
         $fields .= "&x;_bank_aba_code=$x_bank_aba_code&x;_bank_acct_num=$x_bank_acct_num" .
                    "&x;_bank_acct_type=$x_bank_acct_type" .
                    "&x;_bank_name=$x_bank_name&x;_bank_acct_name=$x_bank_acct_name&x;_echeck_type=$x_echeck_type";
      }
      else
      {
         $fields .= "&x;_card_num=$x_Card_Num&x;_exp_date=$x_Exp_Date";
      }

      if( $x_Password != '' )
      {
        $fields .= "&x;_Password=$x_Password";
      }

      if( $xType == 'CREDIT' || $xType == 'CAPTURE' || $xType == 'VOID' )
      {
         $fields .= "&x;_trans_id=" . $params['x_trans_id'];
      }

      /**
      * Optional
      */

      if( $x_Company != '' )
      {
         $fields .= "&x;_company=$x_Company";
      }
      if( $x_Address != '' )
      {
         $fields .= "&x;_address=$x_Address";
      }
      if( $x_City != '' )
      {
         $fields .= "&x;_city=$x_City";
      }
      if( $x_State != '' )
      {
         $fields .= "&x;_state=$x_State";
      }
      if( $x_Zip != '' )
      {
         $fields .= "&x;_zip=$x_Zip";
      }
      if( $x_Country != '' )
      {
         $fields .= "&x;_country=$x_Country";
      }
      if( $x_Phone != '' )
      {
         $fields .= "&x;_phone=$x_Phone";
      }
      if( $x_Fax != '' )
      {
         $fields .= "&x;_fax=$x_Fax";
      }
      if( $x_Cust_Id != '' )
      {
         $fields .= "&x;_cust_id=$x_Cust_Id";
      }
      if( $x_Customer_Ip != '' )
      {
         $fields .= "&x;_customer_ip=$x_Customer_Ip";
      }
      if( $x_Customer_Tax_Id != '' )
      {
         $fields .= "&x;_customer_tax_id=$x_Customer_Tax_Id";
      }
      if( $x_Invoice_Num != '' )
      {
         $fields .= "&x;_invoice_num=$x_Invoice_Num";
      }
      if( $x_Description != '' )
      {
         $fields .= "&x;_description=$x_Description";
      }
      if( $x_CVV2 != '' )
      {
         //$fields .= "&x;_card_code=$x_CVV2";
      }

//echo $fields . "<BR>";
      /**
      * Start CURL session
      */

      $ch = curl_init( "https://secure.authorize.net/gateway/transact.dll" );
      curl_setopt( $ch, CURLOPT_HEADER,         0 );
      curl_setopt( $ch, CURLOPT_POSTFIELDS,     $fields ); // set the fields to post
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );       // make sure we get the response back

      $loopCount = 0;

      /**
      * Added loop to retry a few times before finally failing on 8/21/2007 at 11:35 AM
      */
      
      do
      {
         if( $loopCount > 0 )
            sleep(  2 * $loopCount );

         if( $loopCount >= 4 )
            break;

         ++$loopCount;

         unset( $buffer );

         $buffer = curl_exec( $ch );                          // execute the post

      } while( $buffer === false );
      
      curl_close($ch);                                     // close our session
      
      $details = explode( $x_Delim_Char, $buffer );        // create an array of the response values

//Hardcoded Success for now
$details[0] = 1;
//$details[0] = 3;
//$details[2] = 'Hardcoded to fail';
//$details[3] = 'Eric';

      switch( $details[0] )
      {
         case 1:
           $response['RESULT']  = 'APPROVED';
           $response['MESSAGE'] = $details[3];
           break;
         case 2:
           $response['RESULT']  = 'DECLINED';
           $response['MESSAGE'] = $details[2] . ' - ' . $details[3];
           break;
         case 3:
           $response['RESULT']  = 'ERROR';
           $response['MESSAGE'] = $details[2] . ' - ' . $details[3];
           break;
         default:
           $response['RESULT']  = 'ERROR';
           $response['MESSAGE'] = 'Error processing payment, please try again later.';
           break;
      }

      $response['AUTH_CODE'] = $details[4];
      $response['AVS']       = $details[5];
      $response['TRANS_ID']  = $details[6];

      $response['params'] = $params;

      return( $response );
   }
                                                                                
//****************************************************************************
                                                                                
}
?&gt;

#3
[eluser]Overlord[/eluser]
Thanks for the code Michael.

Do you have any examples of using this class with CodeIgniter?

#4
[eluser]mikeyhell[/eluser]
I Believe this has been replaced. Check the wiki for examples.

#5
[eluser]Overlord[/eluser]
Didn't find anything on the wiki for Authorize(.Net), and only PayPal came up under a search of payment...

#6
[eluser]manilodisan[/eluser]
This is good resource...thanks man.

#7
[eluser]mikeyhell[/eluser]
Haven't found the time to do too much to it beyond my own stuff, but hopefully it will save someone some work.

#8
[eluser]Overlord[/eluser]
So I'm going to go back with my original question (that might help others also):

"Do you have any examples of using this class with CodeIgniter?"

#9
[eluser]manilodisan[/eluser]
I will add it to my project. I need to find something or create my own for 2checkout and I'll add them.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.