|
|
|
Authorize.net Class
6
This is an authorize.net credit card processing class. Required elements are:
- Authorize.net account (live and/or sandbox)
- PHP w/ CURL installed
- SSL certificate installed on server
- My Simple Error Class
And use my Easy Credit Card Class as a pre-processor to this class.
For more information on authorize.net just go to their website and download their developers information.
- Authorize.net account (live and/or sandbox)
- PHP w/ CURL installed
- SSL certificate installed on server
- My Simple Error Class
And use my Easy Credit Card Class as a pre-processor to this class.
For more information on authorize.net just go to their website and download their developers information.
<?php
$config = array(
//Authnet Stuff
"auth_login" => "#######",
"auth_transkey" => "#######",
//Development mode
"dev" => true //true = sandbox, false = live CC processing
);
?>
<?php
class authnet
{
// Set these variables prior to use
var $login = '';
var $transkey = '';
var $test = '';
var $params = array();
var $results = array();
var $approved = false;
var $declined = false;
var $error = true;
var $fields;
var $response;
var $url;
function authnet(){
global $e, $config;
$this->login = $config['auth_login'];
$this->transkey = $config['auth_transkey'];
$this->test = $config['dev'];
if (empty($this->login) || empty($this->transkey)){
$e->setError("You have not configured your Authnet login credentials.");
}
$subdomain = ($this->test) ? 'test' : 'secure';
$this->url = "https://" . $subdomain . ".authorize.net/gateway/transact.dll";
$this->params['x_delim_data'] = "TRUE";
$this->params['x_delim_char'] = "|";
$this->params['x_relay_response'] = "FALSE";
$this->params['x_url'] = "FALSE";
$this->params['x_version'] = "3.1";
$this->params['x_method'] = "CC";
$this->params['x_type'] = "AUTH_CAPTURE";
$this->params['x_login'] = $this->login;
$this->params['x_tran_key'] = $this->transkey;
}
function toString(){
if (!$this->params) return (string) $this;
$output = "";
$output .= '<table summary="Authnet Results" id="authnet">' . "\n";
$output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Outgoing Parameters</b></th>' . "\n" . '</tr>' . "\n";
foreach ($this->params as $key => $value) {
$output .= "\t" . '<tr>' . "\n\t\t" . '<td><b>' . $key . '</b></td>';
$output .= '<td>' . $value . '</td>' . "\n" . '</tr>' . "\n";
}
if ($this->results) {
$output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Incomming Parameters</b></th>' . "\n" . '</tr>' . "\n";
$response = array("Response Code", "Response Subcode", "Response Reason Code",
"Response Reason Text", "Approval Code", "AVS Result Code",
"Transaction ID", "Invoice Number", "Description", "Amount",
"Method", "Transaction Type", "Customer ID", "Cardholder First Name",
"Cardholder Last Name", "Company", "Billing Address", "City",
"State", "Zip", "Country", "Phone", "Fax", "Email", "Ship to First Name",
"Ship to Last Name", "Ship to Company", "Ship to Address",
"Ship to City", "Ship to State", "Ship to Zip", "Ship to Country",
"Tax Amount", "Duty Amount", "Freight Amount", "Tax Exempt Flag",
"PO Number", "MD5 Hash", "Card Code (CVV2/CVC2/CID) Response Code",
"Cardholder Authentication Verification Value (CAVV) Response Code");
foreach ($this->results as $key => $value){
if ($key > 40) break;
$output .= "\t" . '<tr>' . "\n\t\t" . '<td><b>' . $response[$key] . '</b></td>';
$output .= '<td>' . $value . '</td>' . "\n" . '</tr>' . "\n";
}
}
$output .= '</table>' . "\n";
return $output;
}
function process($retries = 3){
$this->prepareParameters();
$ch = curl_init($this->url);
$count = 0;
while ($count < $retries){
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($this->fields, "& "));
$this->response = curl_exec($ch);
$this->parseResults();
if ($this->getResultResponseFull() == "Approved"){
$this->approved = true;
$this->declined = false;
$this->error = false;
break;
}elseif ($this->getResultResponseFull() == "Declined"){
$this->approved = false;
$this->declined = true;
$this->error = false;
break;
}else{
$this->approved = false;
$this->declined = false;
$this->error = true;
}
$count++;
}
curl_close($ch);
}
function prepareParameters(){
foreach ($this->params as $key => $value){
$this->fields .= "$key=" . urlencode($value) . "&";
}
}
function parseResults(){
$this->results = explode("|", $this->response);
}
function setTransaction($cardnum, $expiration, $amount, $cvv = null){
global $e;
$this->params['x_card_num'] = (string) trim($cardnum);
$this->params['x_exp_date'] = (int) $expiration;
$this->params['x_amount'] = (float) $amount;
$this->params['x_card_code'] = (int) $cvv;
if (empty($this->params['x_card_num']) || empty($this->params['x_exp_date']) || empty($this->params['x_amount'])){
$e->setError("Required information for transaction processing omitted.");
}
}
function setParameter($field = "", $value = null){
global $e;
$field = (is_string($field)) ? trim($field) : $field;
$value = (is_string($value)) ? trim($value) : $value;
if (!is_string($field)){
$e->setError("setParameter() arg 1 must be a string or integer: " . gettype($field) . " given.");
}
if (!is_string($value) && !is_numeric($value) && !is_bool($value)){
$e->setError("setParameter() arg 2 must be a string, integer, or boolean value: " . gettype($value) . " given.");
}
if (empty($field)){
$e->setError("setParameter() requires a parameter field to be named.");
}
if ($value === "") {
$e->setError("setParameter() requires a parameter value to be assigned: $field");
}
$this->params[$field] = $value;
}
function setTransactionType($type = ""){
$type = strtoupper(trim($type));
$typeArray = array("AUTH_CAPTURE", "AUTH_ONLY", "PRIOR_AUTH_CAPTURE", "CREDIT", "CAPTURE_ONLY", "VOID");
if (!in_array($type, $typeArray)){
$e->setError("setTransactionType() requires a valid value to be assigned.");
}
$this->params['x_type'] = $type;
}
function getResultResponse(){
return $this->results[0];
}
function getResultResponseFull() {
$response = array("", "Approved", "Declined", "Error");
return $response[$this->results[0]];
}
function isApproved() {
return $this->approved;
}
function isDeclined(){
return $this->declined;
}
function isError(){
return $this->error;
}
function getResponseSubcode(){
return $this->results[1];
}
function getResponseCode() {
return $this->results[2];
}
function getResponseText() {
return $this->results[3];
}
function getAuthCode() {
return $this->results[4];
}
function getAVSResponse() {
return $this->results[5];
}
function getTransactionID() {
return $this->results[6];
}
function getInvoiceNumber(){
return $this->results[7];
}
function getDescription() {
return $this->results[8];
}
function getAmount(){
return $this->results[9];
}
function getPaymentMethod(){
return $this->results[10];
}
function getTransactionType(){
return $this->results[11];
}
function getCustomerID(){
return $this->results[12];
}
function getCHFirstName(){
return $this->results[13];
}
function getCHLastName(){
return $this->results[14];
}
function getCompany(){
return $this->results[15];
}
function getBillingAddress(){
return $this->results[16];
}
function getBillingCity(){
return $this->results[17];
}
function getBillingState(){
return $this->results[18];
}
function getBillingZip(){
return $this->results[19];
}
function getBillingCountry() {
return $this->results[20];
}
function getPhone(){
return $this->results[21];
}
function getFax(){
return $this->results[22];
}
function getEmail(){
return $this->results[23];
}
function getShippingFirstName(){
return $this->results[24];
}
function getShippingLastName() {
return $this->results[25];
}
function getShippingCompany(){
return $this->results[26];
}
function getShippingAddress(){
return $this->results[27];
}
function getShippingCity(){
return $this->results[28];
}
function getShippingState(){
return $this->results[29];
}
function getShippingZip(){
return $this->results[30];
}
function getShippingCountry(){
return $this->results[31];
}
function getTaxAmount(){
return $this->results[32];
}
function getDutyAmount(){
return $this->results[33];
}
function getFreightAmount(){
return $this->results[34];
}
function getTaxExemptFlag(){
return $this->results[35];
}
function getPONumber(){
return $this->results[36];
}
function getMD5Hash(){
return $this->results[37];
}
function getCVVResponse(){
return $this->results[38];
}
function getCAVVResponse(){
return $this->results[39];
}
}
?>
<?php
require_once "config.php";
require_once "class.error.php"; //My Simple Error Class
$e = new error;
require_once "class.authnet.php";
$auth = new authnet;
//You get all of the following information from your checkout forms on your site.
//set new transaction
$auth->setTransaction($cc_number, $date, $total, $cvv);
//Billing information
$auth->setParameter("x_email", $b_email);
$auth->setParameter("x_first_name", $b_fname);
$auth->setParameter("x_last_name", $b_lname);
$auth->setParameter("x_address", $b_address);
$auth->setParameter("x_city", $b_city);
$auth->setParameter("x_state", $b_state);
$auth->setParameter("x_zip", $b_zip);
$auth->setParameter("x_country", $b_country);
$auth->setParameter("x_description", $description); //Purchase description (Order #: 199 from yoursite.com)
//Shipping information
$auth->setParameter("x_ship_to_first_name", $s_fname);
$auth->setParameter("x_ship_to_last_name", $s_lname);
$auth->setParameter("x_ship_to_address", $s_address);
$auth->setParameter("x_ship_to_city", $s_city);
$auth->setParameter("x_ship_to_state", $s_state);
$auth->setParameter("x_ship_to_zip.", $s_zip);
$auth->setParameter("x_ship_to_country", $s_country);
//Process checkout information
$auth->process();
//Get response and do something with it
if ($auth->isApproved()) {
$e->setError("Transaction Completed! Please print the following for your records");
$e->showErrors();
//show completed checkout info and write new information to your database
}elseif($auth->isDeclined()){
$e->setError($auth->getResponseText());
$e->showErrors();
}else{
$e->setError("Transaction Error! Please try again");
$e->setError($auth->getResponseText());
$e->showErrors();
}
?>
<?php
$auth->toString();
?>




1. I have a donation form already made (donate.php) then it goes to (donate_confirm.php) which shows the customer what they entered. And now I created your (config.php), (class.error.php), (class.credit.php) and the (class.authnet.php)......I have all that figured out.
2. What I can't figure out is where to put the code in the second to last box and the code in the last box? Do I need to create another class or something? Do I need to include those on the page with the form? Im not sure but I attached the php snippets I am not sure where to put.
Thank you so much!
//Ok not sure where to put this code
//Do I need to make a new PHP file for it
//Or do I include it on the page of my form
<?php
require_once "config.php";
require_once "class.error.php"; //My Simple Error Class
$e = new error;
require_once "class.authnet.php";
$auth = new authnet;
//You get all of the following information from your checkout forms on your site.
//set new transaction
$auth->setTransaction($cc_number, $date, $total, $cvv);
//Billing information
$auth->setParameter("x_email", $b_email);
$auth->setParameter("x_first_name", $b_fname);
$auth->setParameter("x_last_name", $b_lname);
$auth->setParameter("x_address", $b_address);
$auth->setParameter("x_city", $b_city);
$auth->setParameter("x_state", $b_state);
$auth->setParameter("x_zip", $b_zip);
$auth->setParameter("x_country", $b_country);
$auth->setParameter("x_description", $description); //Purchase description (Order #: 199 from yoursite.com)
//Shipping information
$auth->setParameter("x_ship_to_first_name", $s_fname);
$auth->setParameter("x_ship_to_last_name", $s_lname);
$auth->setParameter("x_ship_to_address", $s_address);
$auth->setParameter("x_ship_to_city", $s_city);
$auth->setParameter("x_ship_to_state", $s_state);
$auth->setParameter("x_ship_to_zip.", $s_zip);
$auth->setParameter("x_ship_to_country", $s_country);
//Process checkout information
$auth->process();
//Get response and do something with it
if ($auth->isApproved()) {
$e->setError("Transaction Completed! Please print the following for your records");
$e->showErrors();
//show completed checkout info and write new information to your database
}elseif($auth->isDeclined()){
$e->setError($auth->getResponseText());
$e->showErrors();
}else{
$e->setError("Transaction Error! Please try again");
$e->setError($auth->getResponseText());
$e->showErrors();
}
?>
//End of the first snippet I dont know what to do with
//Ok not sure where to put this code either
//Do I need to make a new PHP file for it
//Or do I include it on the page of my form
<?php
$auth->toString();
?>
//End of the second snippet I dont know what to do with
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
Transaction Error! Please try agai
// Where does this go
<?php
$auth->toString();
?>
//Oh and I am getting this error when I try to submit the form
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
Required information for transaction processing omitted.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
setParameter() arg 2 must be a string, integer, or boolean value: NULL given.
Transaction Error! Please try again
//Please help me I would really appreciate it and will make sure to //metion your website to friends