<?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();
?>