[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'];