We help IT Professionals succeed at work.

Authorize.net

umaxim
umaxim asked
on
Hi i have a problem with authorize.net
I have code
<?php
require_once 'AuthorizeNet.php'; // Make sure this path is correct.
$transaction = new AuthorizeNetAIM('74jk7zvQJ9', '9ZGmrT2489tbm8p7');
$transaction->amount = '100';
$transaction->card_num = '4007000000027';
$transaction->exp_date = '10/16';

$response = $transaction->authorizeAndCapture();

if ($response->approved) {
  echo "<h1>Success! The test credit card has been charged!</h1>";
  echo "Transaction ID: " . $response->transaction_id;
} else {
  echo $response->error_message;
}
?>

But when i pay one time i can not pay second time. In paypal i was just assing transaction id and it worked. But how it work in authorize.net
Comment
Watch Question

Commented:
What is the error message you are getting back from Authorize.net?

Author

Commented:
AuthorizeNet Error: Response Code: 3 Response Subcode: 1 Response Reason Code: 11 Response Reason Text: A duplicate transaction has been submitted.

Author

Commented:
how i can assing my own transaction id to payment in Authorize.net
Commented:
There are two optional fields you could pass to authorize.net.

x_invoice_num is the field you could use to set your invoice number

x_duplicate_window is the field you could set a value in seconds (0 to 28800).  This field is the time frame between processing invoices with the same number.  If it is set to 0 seconds then it will always allow duplicate invoices.  If left blank or not included, the default value of 2 minutes is used.

here is your code revised:
<?php
require_once 'AuthorizeNet.php'; // Make sure this path is correct.
$transaction = new AuthorizeNetAIM('74jk7zvQJ9', '9ZGmrT2489tbm8p7');
$transaction->x_invoice_num= '1234'; //this is your invoice number
$transaction->x_duplicate_window = '60'; //seconds between orders with same invoice number
$transaction->amount = '100';
$transaction->card_num = '4007000000027';
$transaction->exp_date = '10/16';

$response = $transaction->authorizeAndCapture();

if ($response->approved) {
  echo "<h1>Success! The test credit card has been charged!</h1>";
  echo "Transaction ID: " . $response->transaction_id;
} else {
  echo $response->error_message;
}
?>

Open in new window


I have implemented authorize.net before, but for a client that needed classic ASP.  This isn't too different.

Don't hesitate to ask me any questions.

-Slim


Author

Commented:
So basically i can process few orders with the same credit card and the same amount. Because my client want to charge money from client online for ordering website. So if for exaple he need 2 website and he want to pay twice the same ammount then i just need to change x_invoice_num to charge him again the same amount.

Commented:
That is correct, if you want both charges to have a different invoice number.

The invoice number should really be populated dynamically, pulled from a database, that way you don't have to hard code it in.  But if you can't do that, then hard coding will work I guess....

If you don't care if all of your charges have the same invoice number (seems like a bad idea to me), then you can set x_duplicate_window  to zero. That will allow processing of duplicate invoice numbers without a waiting period between charges.
ie: $transaction->x_duplicate_window = '0';

-Slim

Author

Commented:
No i will create different charge number everytime from database by increasing id in database. Thank you for help

Author

Commented:
And last question maybe if of topic by how long it take to charge client with authorize.net because i test my developer account it said pending. And in Authorize.net i do not see that people feel out special security code of credit card which is 3-4 symbols often on the back of card.

Author

Commented:
it said Captured/Pending Settlement

Commented:
Sadly I don't know much about the 'backend' of authorize.net.  So I don't know how long it takes to process an order.  

I do see that you didn't include the x_type field, so by default Authorize.net does a "Authorize and Capture".  I don't know about the extra steps it may take (maybe none at all) to actually 'charge' the customer, as it may be automatic...  I have set the x_type field to: CAPTURE_ONLY and it seems to 'take' the money without any issues.

As for the security code on the credit card you will need to add another field to your code: x_card_code

Have you looked at the documentation provided by Authorize.net?  Here is a link to the AIM documentation, which I believe is the form of integration you are using.  In the PDF, page 18 starts listing the fields you can include in your call.

Author

Commented:
thank you because i found the code in internet and i did not read that information.

Author

Commented:
its given me error Fatal error: Uncaught exception 'AuthorizeNetException' with message 'Error: no field x_invoice_num exists in the AIM API. To set a custom field use setCustomField('field','value') instead

Commented:
Post your code please.

Author

Commented:
<?php
require_once 'AuthorizeNet.php'; // Make sure this path is correct.
$transaction = new AuthorizeNetAIM('74jk7zvQJ9', '9ZGmrT2489tbm8p7');
$transaction->amount = '100';
$transaction->card_num = '5555555555554444';
$transaction->exp_date = '10/16';
$transaction->x_type = "CAPTURE_ONLY";
$transaction->x_invoice_num= time();

$response = $transaction->authorizeAndCapture();

if ($response->approved) {
  echo "<h1>Success! The test credit card has been charged!</h1>";
  echo "Transaction ID: " . $response->transaction_id;
} else {
  echo $response->error_message;
}
?>

Commented:
that code look good...

I know that x_invoice_num should only be letter and number (not longer than 20 characters).  I wonder if your time() function is messing it up.  Try running it again with a hard value for the invoice number.

Author

Commented:
Fatal error: Uncaught exception 'AuthorizeNetException' with message 'Error: no field x_type exists in the AIM API. To set a custom field use setCustomField('field','value') instead.' in /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/lib/AuthorizeNetAIM.php:308 Stack trace: #0 /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/lib/AuthorizeNetAIM.php(224): AuthorizeNetAIM->setField('x_type', 'CAPTURE_ONLY') #1 /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/index.php(7): AuthorizeNetAIM->__set('x_type', 'CAPTURE_ONLY') #2 {main} thrown in /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/lib/AuthorizeNetAIM.php on line 308

Commented:
Remove the x_type field.  That way it will default to Auth_Capture.  Here is what Authorize.net has to say about that field:
Authorization and Capture
This is the most common type of credit card transaction and is the default payment gateway transaction type. The amount is sent for authorization, and if approved, is automatically submitted for settlement.

Remove that field.

Author

Commented:
Fatal error: Uncaught exception 'AuthorizeNetException' with message 'Error: no field x_invoice_num exists in the AIM API. To set a custom field use setCustomField('field','value') instead.' in /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/lib/AuthorizeNetAIM.php:308 Stack trace: #0 /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/lib/AuthorizeNetAIM.php(224): AuthorizeNetAIM->setField('x_invoice_num', '22323') #1 /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/index.php(7): AuthorizeNetAIM->__set('x_invoice_num', '22323') #2 {main} thrown in /var/www/vhosts/iwebpixels.com/subdomains/files/httpdocs/payment/lib/AuthorizeNetAIM.php on line 308

Commented:
Is this the code you are still using?

$transaction->x_invoice_num= time();


if it is, try
$transaction->x_invoice_num= '111';

Commented:
You are missing quite a bit of data from the code, take a look at this code I downloaded from Authorize.net:
<!--
This sample code is designed to connect to Authorize.net using the AIM method.
For API documentation or additional sample code, please visit:
http://developer.authorize.net
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<HTML lang='en'>
<HEAD>
	<TITLE> Sample AIM Implementation </TITLE>
</HEAD>
<BODY>
<P> This sample code is designed to generate a post using Authorize.net's
Advanced Integration Method (AIM) and display the results of this post to
the screen. </P>
<P> For details on how this is accomplished, please review the readme file,
the comments in the sample code, and the Authorize.net AIM API documentation
found at http://developer.authorize.net </P>
<HR />

<?PHP

// By default, this sample code is designed to post to our test server for
// developer accounts: https://test.authorize.net/gateway/transact.dll
// for real accounts (even in test mode), please make sure that you are
// posting to: https://secure.authorize.net/gateway/transact.dll
$post_url = "https://test.authorize.net/gateway/transact.dll";

$post_values = array(
	
	// the API Login ID and Transaction Key must be replaced with valid values
	"x_login"			=> "API_LOGIN_ID",
	"x_tran_key"		=> "TRANSACTION_KEY",

	"x_version"			=> "3.1",
	"x_delim_data"		=> "TRUE",
	"x_delim_char"		=> "|",
	"x_relay_response"	=> "FALSE",

	"x_type"			=> "AUTH_CAPTURE",
	"x_method"			=> "CC",
	"x_card_num"		=> "4111111111111111",
	"x_exp_date"		=> "0115",

	"x_amount"			=> "19.99",
	"x_description"		=> "Sample Transaction",

	"x_first_name"		=> "John",
	"x_last_name"		=> "Doe",
	"x_address"			=> "1234 Street",
	"x_state"			=> "WA",
	"x_zip"				=> "98004"
	// Additional fields can be added here as outlined in the AIM integration
	// guide at: http://developer.authorize.net
);

// This section takes the input fields and converts them to the proper format
// for an http post.  For example: "x_login=username&x_tran_key=a1B2c3D4"
$post_string = "";
foreach( $post_values as $key => $value )
	{ $post_string .= "$key=" . urlencode( $value ) . "&"; }
$post_string = rtrim( $post_string, "& " );

// The following section provides an example of how to add line item details to
// the post string.  Because line items may consist of multiple values with the
// same key/name, they cannot be simply added into the above array.
//
// This section is commented out by default.
/*
$line_items = array(
	"item1<|>golf balls<|><|>2<|>18.95<|>Y",
	"item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>Y",
	"item3<|>book<|>Golf for Dummies<|>1<|>21.99<|>Y");
	
foreach( $line_items as $value )
	{ $post_string .= "&x_line_item=" . urlencode( $value ); }
*/

// This sample code uses the CURL library for php to establish a connection,
// submit the post, and record the response.
// If you receive an error, you may want to ensure that you have the curl
// library enabled in your php configuration
$request = curl_init($post_url); // initiate curl object
	curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
	curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
	curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
	curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
	$post_response = curl_exec($request); // execute curl post and store results in $post_response
	// additional options may be required depending upon your server configuration
	// you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object

// This line takes the response and breaks it into an array using the specified delimiting character
$response_array = explode($post_values["x_delim_char"],$post_response);

// The results are output to the screen in the form of an html numbered list.
echo "<OL>\n";
foreach ($response_array as $value)
{
	echo "<LI>" . $value . "&nbsp;</LI>\n";
	$i++;
}
echo "</OL>\n";
// individual elements of the array could be accessed to read certain response
// fields.  For example, response_array[0] would return the Response Code,
// response_array[2] would return the Response Reason Code.
// for a list of response fields, please review the AIM Implementation Guide
?>
</BODY>
</HTML>

Open in new window


The code is their from their sample code for PHP and their AIM processing.  See if this sample code works better than the one you found off of the internet...

Have you gone to Authorize.net's website and looked at the documentation yet?

Explore More ContentExplore courses, solutions, and other research materials related to this topic.