setParam(array(
// 'x_Login' => 'myaccount',
// 'x_Test_Request' => 'TRUE',
// 'x_First_Name' => 'John',
// 'x_Last_Name' => 'Doe',
// 'x_Amount' => '1.20',
// 'x_Card_Num' => '4111111111111111',
// 'x_Card_Code' => '123',
// 'x_Exp_Date' => '042008',
// 'x_Invoice_Num' => '100',
// 'x_Address' => '10 rue Levouvé',
// 'x_City' => 'SomeCity',
// 'x_State' => 'CA',
// 'x_Zip' => '75010',
// ));
//
// $result_code = $authorizenet->process(); // Returns one of: false = error, 1 = accepted, 2 = declined, 3 = error
// $result_array = $authorizenet->getResults();
//
// foreach ($result_array as $key => $value) {
// print "$key: $value
\n";
// }
/**
* The AuthorizeNet class provides an abstract interface for communicating
* with authorize.net's AIM interface. Supports Auth.Net v3.1
*
* @author Quinn Comendant
* @version 1.0
* @date 2004-04-06
*/
class AuthorizeNet
{
var $post_url = ''; // The URL to post data to.
var $md5_hash_value = ','; // A custom value for the response delimination character.
var $_results = array();
var $_params = array();
var $_default_params = array(
'x_version' => '3.1',
'x_relay_response' => 'FALSE',
'x_delim_data' => 'TRUE',
'x_echo_data' => 'TRUE',
'x_adc_url' => 'FALSE',
'x_type' => 'AUTH_CAPTURE',
'x_method' => 'CC',
'x_login' => '',
'x_tran_key' => '',
'x_delim_char' => ',',
'x_encap_char' => '',
);
// Array of response names. Used in the results array.
var $_result_fields = Array(
'x_response_code',
'x_response_subcode',
'x_response_reason_code',
'x_response_reason_text',
'x_auth_code',
'x_avs_code',
'x_trans_id',
'x_invoice_num',
'x_description',
'x_amount',
'x_method',
'x_type',
'x_cust_id',
'x_first_name',
'x_last_name',
'x_company',
'x_address',
'x_city',
'x_state',
'x_zip',
'x_country',
'x_phone',
'x_fax',
'x_email',
'x_ship_to_first_name',
'x_ship_to_last_name',
'x_ship_to_company',
'x_ship_to_address',
'x_ship_to_city',
'x_ship_to_state',
'x_ship_to_zip',
'x_ship_to_country',
'x_tax',
'x_duty',
'x_freight',
'x_tax_exempt',
'x_po_num',
'x_md5_hash',
'x_card_code'
);
/**
* Constructs a new authentication object.
*
* @access public
*
* @param optional array $_params A hash containing parameters.
*/
function AuthorizeNet($params = array())
{
if (!function_exists('curl_init')) {
trigger_error('AuthorizeNet error: curl not installed.', E_USER_ERROR);
}
// The authorize.net url to post to.
$this->post_url = isset($params['post_url']) ? $params['post_url'] : 'https://secure.authorize.net/gateway/transact.dll';
// A custom value for the response delimination character.
$this->md5_hash_value = isset($params['md5_hash_value']) ? $params['md5_hash_value'] : '';
// Set default parameters.
$this->_params = $this->_default_params;
}
/**
* Set parameters.
*
* @access public
* @param array $array_merge Associative array of parameters.
*/
function setParam($params)
{
$this->_params = array_merge($this->_params, $params);
}
/**
* Returns a specified value from a registered parameter.
*
* @access public
* @param string $key Which value to return.
* @param array $array_merge Associative array of parameters.
*/
function getParam($key)
{
if (isset($this->_params[$key])) {
return $this->_params[$key];
}
}
/**
* Submit parameters to gateway.
*
* @access public
*
* @return mixed False or x_response_code: false = error, 1 = accepted, 2 = declined, 3 = error
*/
function process()
{
if (empty($this->_params['x_login'])) {
$this->_results['x_response_reason_text'] = _("Transaction gateway temporarily not available. Please try again later.");
logMsg(sprintf('x_login not specified.', null), LOG_ERR, __FILE__, __LINE__);
return false;
}
if (empty($this->_params['x_card_num'])) {
$this->_results['x_response_reason_text'] = _("Transaction gateway temporarily not available. Please try again later.");
logMsg(sprintf('x_card_num not specified.', null), LOG_ERR, __FILE__, __LINE__);
return false;
}
// Generate query string from params.
$q = '';
$delim = '';
foreach ($this->_params as $key=>$val) {
$q .= $delim . $key . '=' . urlencode($val);
$delim = '&';
}
// Setup curl and execute request.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->post_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
$result = curl_exec($ch);
curl_close($ch);
if (!$result) {
return false;
}
return $this->_processResult($result);
}
/**
* Returns the results array. Returns a specific element if $key is provided.
*
* @access public
*
* @return array Returns the results array.
*/
function getResult($key=null)
{
if (isset($key)) {
return $this->_results[$key];
} else {
return $this->_results;
}
}
/**
* Tests a returned md5 hash value with a locally computated one.
*
* @access public
*
* @return bool True if the hash is valid, false otherwise.
*/
function validMD5Hash()
{
return (
strtolower($this->getResult('x_md5_hash')) == strtolower(md5(
$this->md5_hash_value .
$this->getParam('x_login') .
$this->getResult('x_trans_id') .
$this->getResult('x_amount'))
)
);
}
/**
* Reset all variables. Call before beginning a new transaction.
*
* @access public
*/
function reset()
{
$this->_results = Array();
$this->_params = $this->_default_params;
}
/**
* Process the result from the curl execution to create an associative array of returned data.
*
* @access private.
*
* @param mixed $result The result from the curl execution.
*
* @return integer Transaction result code.
*/
function _processResult($result)
{
$this->_results = Array();
$results = explode($this->getParam('x_delim_char'), $result);
$num = sizeof($this->_result_fields);
for ($i=0, $j=0; $i<$num; $i++) {
if (isset($this->_result_fields[$i])) {
$this->_results[$this->_result_fields[$i]] = $results[$i];
} else {
$j++;
$this->_results["x_custom_$j"] = $results[$i];
}
}
return $this->_results['x_response_code'];
}
}
?>