[thelist] Basic e-commerce

Nan Harbison nan at nanharbison.com
Sun Apr 5 18:08:06 CDT 2009


Hi Kevin,

Here is the class. (The cURL settings are specific to godaddy hosting.) It
came with the number of retries set to 3, and I changed it to one because I
kept getting the error message that it was a duplicate transaction. I don't
get why the class was set up that way. Otherwise it works like a charm.

It would be interesting to see which is less expensive to use, PayPal or
authorize.net. My boss preferred authnet because PayPal seemed too fluffy,
but these days PayPal is pretty widely used and accepted.

Cheers,
Nan


class authnet
{
    // Set these variables prior to use
    var $login    = 'numbers and letters here';
    var $transkey = 'more numbers and letters!';
    var $test     = 'FALSE';

    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) ? 'certification' : 'secure';  //i
think this one is wrong
		//https://certification.authorize.net/gateway/transact.dll.
instead of test it should be certification
		//the website says:
https://secure.authorize.net/gateway/transact.dll
		//$subdomain = ($this->test) ? 'test' : 'secure';
        //$this->url = "https://" . $subdomain .
".authorize.net/gateway/transact.dll";
		//$this->url =
"https://test.authorize.net/gateway/transact.dll";
		$this->url =
"https://secure.authorize.net/gateway/transact.dll";
        
		$this->params['x_delim_data']     = "TRUE";
		$this->params['x_test_request']   = "FALSE";
		
        $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 = 1){
	
		if ($this->prepareParameters()) {
			//echo "prepareparameters works";
		}

        $ch = curl_init($this->url);
		//MY ADDED CODE
		//echo $this->url;  // this was
https://secure.authorize.net/gateway/transact.dll
		
		
		
        $count = 0;
        while ($count < $retries){
			//echo $count;
			//necessary for godaddy hosting accounts
			curl_setopt($ch, CURLOPT_VERBOSE, 1);
			//curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
			curl_setopt ($ch, CURLOPT_PROXYTYPE,
CURLPROXY_HTTP);
			curl_setopt ($ch,
CURLOPT_PROXY,"http://proxy.shr.secureserver.net:3128");
			curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
			//curl_setopt ($ch, CURLOPT_URL, $URL);
			curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
			
			//this came with the class
            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;
				//echo "approved";
                //break;
            }elseif ($this->getResultResponseFull() == "Declined"){
                $this->approved = false;
                $this->declined = true;
                $this->error    = false;
				//echo "declined";
                //break;
            }else{
				$this->approved = false;
                $this->declined = false;
                $this->error    = true;
				echo $this->getResultResponseFull();
			}
			
            $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];
    }
	
} 

-----Original Message-----
From: Kevin Timmins [mailto:kipper_timmins at live.co.uk] 
Sent: Sunday, April 05, 2009 6:39 PM
To: nan at nanharbison.com; thelist at lists.evolt.org
Subject: Re: [thelist] Basic e-commerce

I think i'll check out autorize.net, it sounds like it could be interesting
to use.
It would be interesting to see the class to see how you do it, although i
would probably want to write my own more for a sense of achievement than
anything else :) cheers, kevin

--------------------------------------------------
From: "Nan Harbison" <nan at nanharbison.com>
Sent: Sunday, April 05, 2009 4:53 PM
To: <thelist at lists.evolt.org>
Subject: Re: [thelist] Basic e-commerce

> My company uses PHP and authorize.net as well, but we only have one or 
> two types of payments that people make for services we offer.
> You can google for authorize.net classes so that you don't have to
> (tediously) do it yourself. It is pretty easy to use.
> Also, dealing with customer payments in their back end is simple, 
> including refunds - you just search for the customer by name and click 
> on the transaction and press the button called "refund".
>
> If you decide to go this way, I can send you the class I use.
>
> Nan
>
> -----Original Message-----
> From: thelist-bounces at lists.evolt.org
> [mailto:thelist-bounces at lists.evolt.org] On Behalf Of Jack Timmons
> Sent: Sunday, April 05, 2009 9:17 AM
> To: thelist at lists.evolt.org
> Subject: Re: [thelist] Basic e-commerce
>
> We use PHP for all of our websites, and we have an Authorize.net 
> account that we use to handle transactions. The basic API is pretty 
> simple, and can get as complex as you (probably) may need, if you 
> desire.
> Basically, the process goes as such:
>
> 1 - Customer requests your processing page, supplying credit card 
> number, month, year, name, etc.
> 2  - *After validating* you supply the needed information in an array 
> (I think, I haven't looked at it in a while, but the point is 
> supplying the information), including your merchant ID, *your 
> *transaction number, billing title, etc.
> 3 - Charge gateway validates information, either returns errors or 
> charges, and returns errors or success with iniformation.
> 4 - You go from there.
>
> Using Paypal is probably easier, and more than likely what you'd need 
> if you didn't have a shared security ticket available.
>
> --
> -Jack Timmons
> http://www.trotlc.com
> Twitter: @codeacula
> --
>
> * * Please support the community that supports you.  * * 
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester and 
> archives of thelist go to: http://lists.evolt.org Workers of the Web, 
> evolt !
>
> --
>
> * * Please support the community that supports you.  * * 
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester and 
> archives of thelist go to: http://lists.evolt.org Workers of the Web, 
> evolt !
> 




More information about the thelist mailing list