Reply
Highlighted
Member
Posts: 8
Registered: ‎02-28-2012

how to get back transaction id

I had somebody implement a script for me, so I'm a little lost on how/where to set the code to give me back a transaction id for tracking or for the customer's to print for their records.

 

I have a payment processing page that includes the below code. I seeat the top,var $trans_key = ''; and down lower, several options under "// response information". But I'm just not sure how to set these to give me that information to echo/use on the confirmation page.

 

 

<?php
/**
* CLASS AIM
*
*/
class AIM {
	// login credentials that Authorize.net uses for verification
	var $login_id = '';
	var $trans_key = '';

	// server
	var $server = '';
	
	// testing name stuff


	// credit card information
	var $cc_name = '';
	var $cc_number = '';
	var $cc_month = '';
	var $cc_year = '';
	var $cc_code = '';
	var $cc_type = '';

	// error stack array
	var $errorStack = array();

	// modes
	var $testMode = false;
	var $debugMode = false;
	var $errorRetries = 2;

	// buyer information
	var $buyer = array();

	// response information
	var $status = '';
	var $subcode = '';
	var $response_code = '';
	var $response_text = '';
	var $approval_code = '';
	var $md5hash = '';
	var $code = '';
	var $remaining = array();

	// constructor
	function AIM($login_id, $trans_key) {
		$this->login_id = $login_id;
		$this->trans_key = $trans_key;

		$this->setTesting(0);
	}

	/** loads option array
	* @param	fName	string containing the first name
	* @param	lName	string containing the last name
	* @param	address	string containing the address
	* @param	city	string containing the city
	* @param	state	string containing the state
	* @param	zip		string containing the zipcode
	* @param	phone	string containing the phone number
	* @return	true
	*/
	function setBuyer ($fname, $lname, $address, $city, $state, $zip, $phone = "(000)000-0000") {
		$this->buyer = array
			(
				"FirstName"	=> $fname,
				"LastName"	=> $lname,
				"Address"	=> $address,
				"City"		=> $city,
				"State"		=> $state,
				"Zip"		=> $zip,
				"Phone"		=> $phone
			);

		return true;
	}

	/** sets the card information variables
	* @param	cc_name		string containing the name on the credit card
	* @param	cc_number	string containing the credit card number
	* @param	cc_month	string containing the expiration month
	* @param	cc_year		string containing the expiration year
	* @param	cc_code		string containing the security code
	* @return	boolean
	*/
	function setCard ($cc_name, $cc_number, $cc_month, $cc_year, $cc_code) {
		$this->cc_name = $cc_name;
		$this->cc_number = ereg_replace("[^0-9]", "", $cc_number);
		$this->cc_month = ereg_replace("[^0-9]", "", $cc_month);
		$this->cc_year = ereg_replace("[^0-9]", "", $cc_year);
		$this->cc_code = ereg_replace("[^0-9]", "", $cc_code);

		return $this->verifyCard();
	}

	/** verifies correct format of card
	* @return	boolean
	*/
	function verifyCard () {
		// these can be combined. however, if card type is needed, split up works better
		if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number))
			$this->cc_type = 'Visa';
		elseif (ereg('^5[1-5][0-9]{14}$', $this->cc_number))
			$this->cc_type = 'Master Card';
		elseif (ereg('^3[47][0-9]{13}$', $this->cc_number))
			$this->cc_type = 'American Express';
		elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->cc_number))
			$this->cc_type = 'Diners Club';
		elseif (ereg('^6011[0-9]{12}$', $this->cc_number))
			$this->cc_type = 'Discover';
		elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->cc_number))
			$this->cc_type = 'JCB';
		else{
			$this->errorStack[] = "Invalid card number.";
			return false;
		}

		// if the year is before the current
		if ($this->cc_year < date ("Y")) {
			$this->errorStack[] = "Invalid card expiration date (card has expired).";
			return false;
		}

		// if the year is the current, and the month is less than the current
		if ($this->cc_year == date ("Y") && $this->cc_month < date ("m")) {
			$this->errorStack[] = "Invalid card expiration date (card has expired).";
			return false;
		}	

		// who has a name less than 5 characters?
		if (strlen($this->cc_name) < 5) {
			$this->errorStack[] = "The name on the credit card appears invalid.";
			return false;
		}

		// make sure the security is atleast 3 characters long and larger than 000
		if (strlen($this->cc_code) < 3 || $this->cc_code < 1) {
			$this->errorStack[] = "Invalid security code.";
			return false;
		}

		return true;
	}

	/** set testing mode
	* @param	test	int/boolean containing testing setting
	* @return	true
	*/
	function setTesting ($test) {
		if ($test == 1 || $test == true) {
			$this->testMode = true;
			$this->server = "https://certification.authorize.net/gateway/transact.dll";
		}else{
			$this->testMode = false;
			$this->server = "https://secure.authorize.net/gateway/transact.dll";
		}

		return true;
	}

	/** process the card
	* @param	item	string containing a description of the item being sold
	* @param	amount	float containing the amount to be charge
	* @return	boolean
	*/
	function processCard ($item, $amount) {
		// run the regular expression to verify card format
		if (!$this->verifyCard()) {
			$this->errorStack[] = "Invalid card.";
			return false;
		}

		// populate the array of items to send to authorize.net
		$values					= array
		(
			"x_login"			=> $this->login_id,
			"x_tran_key"		=> $this->trans_key,
			"x_version"			=> "3.1",
			"x_delim_char"		=> "|",
			"x_delim_data"		=> "TRUE",
			"x_url"				=> "FALSE",
			"x_type"			=> "AUTH_CAPTURE",
			"x_method"			=> "CC",
			"x_relay_response"	=> "TRUE",
			"x_card_num"		=> $this->cc_number,
			"x_exp_date"		=> $this->cc_month . $this->cc_year,
			"x_card_code"		=> $this->cc_code,
			"x_description"		=> $item,
			"x_amount"			=> $amount,
			"x_first_name"		=> htmlspecialchars($this->buyer["FirstName"]),
			"x_last_name"		=> htmlspecialchars($this->buyer["LastName"]),
			"x_address"			=> htmlspecialchars($this->buyer["Address"]),
			"x_city"			=> htmlspecialchars($this->buyer["City"]),
			"x_state"			=> htmlspecialchars($this->buyer["State"]),
			"x_zip"				=> htmlspecialchars($this->buyer["Zip"]),
			"x_country"			=> 'US',
			"x_phone"			=> htmlspecialchars($this->buyer["Phone"])
		);

		// makes for a safe transfer
		$fields = "";
		foreach( $values as $key => $value ) $fields .= "$key=" . urlencode($value) . "&";

		// begin setting up cURL
		$ch = curl_init($this->server); 
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($fields, "& "));

		// lets talk
		$resp = curl_exec($ch);
		curl_close ($ch);

		// parse the response
		$this->parseReturn ($resp);

		// if we had any errors parsing, return right now
		if (sizeof ($this->errorStack) > 0)
			return false;

		return $this->approval_code;
	}

	/** parse the returned delimited string
	* @param	response	string containing the authorize.net response
	* @return	boolean
	*/
	function parseReturn ($response) {
		// figure out how many responses we got back
		$text = $response;
		$h = substr_count($text, "|");
		$h++;

		// loop through the result string, one delimited (|) chunk at time
		for($j=1; $j <= $h; $j++) {
			// get the location of the end of the item to process
			$p = strpos($text, "|");

			if ((!$p === false)) {
				$p++;

				$pstr = substr($text, 0, $p);

				$pstr_trimmed = substr($pstr, 0, -1); // removes "|" at the end

				// nothing found?
				if ($pstr_trimmed == "") {
					$this->errorStack[] = "Error communicating with payment processing server.";
					return false;
				}

				// what did we just get?
				switch($j) {
					// case 1 is the general approved/declined/error result
					case 1:
						if ($pstr_trimmed=="1")
							$this->status = "Approved";
						elseif ($pstr_trimmed=="2") {
							$this->status = "Declined";
							//$this->errorStack[] = "The card was declined.";
						}elseif ($pstr_trimmed=="3") {
							$this->status = "Error";
							//$this->errorStack[] = "An error occurred.";
						}
						break;

					// case 2 is the sub code
					case 2:
						$this->subcode = $pstr_trimmed;
						break;

					// case 3 is the response code
					case 3:
						$this->response_code = $pstr_trimmed;
						break;

					// case 4 is the detailed response text (readable)
					case 4:
						$this->response_text = $pstr_trimmed;
						if ($this->status == "Declined" || $this->status == "Error")
							$this->errorStack[] = $this->response_text;
						break;

					// case 5 is the authorize.net approval code
					case 5:
						$this->approval_code = $pstr_trimmed;
						break;

					// case 38 is the MD5 to prove it came from authorize.net
					case 38:
						$this->md5hash = $pstr_trimmed;
						break;

					// case 39 is the code result
					case 39:
						if ($pstr_trimmed == "M")
							$this->code = "M = Match";
						elseif ($pstr_trimmed == "N") {
							$this->code = "N = No Match";
							$this->errorStack[] = "The security code supplied did not match.";
						}elseif ($pstr_trimmed == "P") {
							$this->code = "P = Not Processed";
							$this->errorStack[] = "We were unable to process your card's security code. Please contact Reliance.";
						}elseif ($pstr_trimmed == "S")
							$this->code = "S = Should have been present";
						elseif ($pstr_trimmed == "U")
							$this->code = "U = Issuer unable to process request";
						else
							$this->code = "NO VALUE RETURNED";
						break;

					// the rest of the messages are just stored for debugging purposes (or random use)
					default:
						$this->remaining[$j] = $pstr_trimmed;
						break;
				}

				// drop the processed item
				$text = substr($text, $p);
			}
		}

		return true;
	}
}

?>

 

Posts: 1,609
Topics: 15
Kudos: 201
Solutions: 121
Registered: ‎06-23-2011

Re: how to get back transaction id

After this:

 

// case 5 is the authorize.net approval code
case 5:
        $this->approval_code = $pstr_trimmed;
        break;

 Add this:

 

// case 7 is the authorize.net transaction ID
case 7:
        $this->transaction_id = $pstr_trimmed;
        break;

 Now, before this line:

 

return $this->approval_code;

 You will be able to access it via:

 

$this->transaction_id

 Assuming, of course, this wall of code works the way it appears to.

Member
Posts: 8
Registered: ‎02-28-2012

Re: how to get back transaction id

[ Edited ]

hmmm... I think I'm missing the last bit on this? On the page itself (that includes the AIM.class.php) where I wanted to echo it and set it as a variable, I simply put "echo $response->transaction_id;" thinking that would be all I need. It looked like it was done that way in some of the sample files from authorize.net

 

I did this part as you suggested:

 

// parse the response
		$this->parseReturn ($resp);
		
		$this->transaction_id;
	        // also tried... return $this->transaction_id;
		// if we had any errors parsing, return right now
		if (sizeof ($this->errorStack) > 0)
			return false;
		// also tried putting return $this->transaction_id; here
		return $this->approval_code;

 

Posts: 1,609
Topics: 15
Kudos: 201
Solutions: 121
Registered: ‎06-23-2011

Re: how to get back transaction id

Try this:

 

return array($this->approval_code, $this->transaction_id);

Then you'll have find the code that calls processCard() and look for the line(s) after that that reference the approval code. Add something to print the transaction ID.

 

If you still need further work on this, I suggest hiring someone for a few dollars. We're here to support you in integrating Authorize.net, but this is getting into general PHP coding at this point.

http://www.authorize.net/solutions/merchantsolutions/merchantservices/certifieddeveloperdirectory/