Commerce
|
|
|
|
<? /////////////////////////////////////////////////////////////
//
// AuthorizenetClass
// file: authorizenet.class.php
// last modified: 7/28/2001
// prerequisites: PHP 4.0.2+, CURL, SSL
// version: 1.0
// author: Austin Butler, Corndog Software
// url: http://www.corndogsoftware.com/
// license: GPL
//
// Updated: 5/9/2003
// Version: 1.1
// Author: Matt Babineau
// URL: http://www.criticalcode.com
// Updates: Added compatibility for Auth.Net v3.1
// changed _processResult() function
//
/////////////////////////////////////////////////////////////
/*
// EXAMPLE USAGE
include("authorizenet.php");
$ac = new AuthorizenetClass();
$ac->setParameter("x_Login", "myaccount");
//$ac->setParameter("x_Test_Request", "TRUE");
$ac->setParameter("x_First_Name", "Austin");
$ac->setParameter("x_Last_Name", "Butler");
$ac->setParameter("x_Amount", "12.00");
$ac->setParameter("x_Card_Num", "4111111111111111");
$ac->setParameter("x_Card_Code", "456");
$ac->setParameter("x_Exp_Date", "072005");
$ac->setParameter("x_Invoice_Num", "12321398083");
$ac->setParameter("x_Address", "123 Fake St");
$ac->setParameter("x_City", "Hermosa Beach");
$ac->setParameter("x_State", "CA");
$ac->setParameter("x_Zip", "90001");
$result_code = $ac->process(); // 1 = accepted, 2 = declined, 3 = error
$result_array = $ac->getResults(); // return results array
foreach($result_array as $key => $value) {
print "$key: $value<br>n";
}
*/
class AuthorizenetClass {
var $vendor; // Credit card processor name
var $postURL; // URL to post to
// Set default values. Changes these with $ac->setParameter(string key, mixed value)
var $params = Array();
var $results = Array();
// Constructor: Defaults to authorizenet. Also accepts planetpayment and quickcommerce
function AuthorizenetClass($vendor = "authorizenet") {
if (!curl_version()) die ("ERROR (AuthrorizenetClass): cURL not installed");
$this->vendor = $vendor;
$this->postURL = $this->vendorPostURLs[$vendor];
$this->_setDefaultParams();
}
// setPostURL(string url): Set the URL to post to.
function setPostURL($url) {
$this->postURL = $url;
}
// setParemeter(string key, string value): Used to set each name/value pair to be sent
function setParameter($key, $value) {
$this->params[$key] = $value;
}
// process(): Submit to gateway
function process() {
if (!$this->params['x_Login'])
die("Error (AuthorizenetClass): x_Login is a required field");
if (!$this->params['x_Card_Num'])
die("Error (AuthorizenetClass): x_Card_Num is a required field");
if (!$this->params['x_Exp_Date'])
die("Error (AuthorizenetClass): x_Exp_Date is a required field");
$qString = "";
while(list($key, $val) = each($this->params))
$qString .= "$key=".urlencode($val)."&";
$qString = substr($qString, 0, strlen($qString)-1); // remove the last ampersand
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->postURL);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $qString);
$result = curl_exec ($ch);
curl_close ($ch);
if (!$result)
return 0;
return $this->_processResult($result);
}
// getResults(): Returns the results array
function getResults() {
return $this->results;
}
// reset(): Call before beginning a new transaction
function reset() {
$this->results = Array();
$this->params = Array();
$this->_setDefaultParams();
}
///////////////////////////////////////////////////////
// Internal Functions
//
// _processResults(string $results): Internal Function. Creates the results array
function _processResult($result) {
if ($result == 'Invalid Merchant Login or Account Inactive') {
$result = "3,0,0,".$result; // Bogus string with the proper error message
}
$resArray = explode(",", $result);
$len = count($this->resultFields);
$j = 0;
for($i=0; $i< $len; $i++) {
if ($this->resultFields[$i])
$this->results[$this->resultFields[$i]] = $resArray[$i];
else {
$j++;
$this->results["x_custom_$j"] = $resArray[$i];
}
}
return $this->results['x_response_code'];
}
// _setDefaultParams(): Internal Function. Reset the params array.
function _setDefaultParams() {
$this->params["x_Version"] = "3.1";
$this->params["x_Delim_Data"] = "TRUE";
//$this->params["x_Echo_Data"] = "TRUE";
//$this->params["x_ADC_URL"] = "FALSE";
$this->params["x_Type"] = "AUTH_CAPTURE";
$this->params["x_Method"] = "CC";
$this->params["x_Tran_Key"] = "somekey";
$this->params["x_Delim_Char"] = ", ";
$this->params["x_Encap_Char"] = "";
}
///////////////////////////////////////////////////////
// Associative arrays containing matching data
// holds vendor/url pairls
var $vendorPostURLs = Array (
'authorizenet' => 'https://secure.authorize.net/gateway/transact.dll',
'planetpayment' => 'https://secure.planetpayment.com/gateway/transact.dll',
'quickcommerce' => 'https://secure.quickcommerce.net/gateway/transact.dll');
// Array of response names. Used in the results array.
var $resultFields = 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");
} ?>
|
|
|
Usage Example
|
include("authorizenet.php");
$ac = new AuthorizenetClass();
$ac->setParameter("x_Login", "myaccount");
$ac->setParameter("x_Test_Request", "TRUE");
$ac->setParameter("x_First_Name", "Austin");
$ac->setParameter("x_Last_Name", "Butler");
$ac->setParameter("x_Amount", "12.00");
$ac->setParameter("x_Card_Num", "4111111111111111");
$ac->setParameter("x_Card_Code", "456");
$ac->setParameter("x_Exp_Date", "072005");
$ac->setParameter("x_Invoice_Num", "12321398083");
$ac->setParameter("x_Address", "123 Fake St");
$ac->setParameter("x_City", "Hermosa Beach");
$ac->setParameter("x_State", "CA");
$ac->setParameter("x_Zip", "90001");
$result_code = $ac->process(); // 1 = accepted, 2 = declined, 3 = error
$result_array = $ac->getResults(); // return results array
foreach($result_array as $key => $value) {
print "$key: $value<br>n";
}
|
|
|
Rate This Script
|
|
|
|