The Authorize.Net Developer Blog

Posts from Authorize.Net employees, community members and experts about integrating with the Authorize.Net Payment Gateway: sample code, tutorials, and problem-solving techniques, just to name a few.

Take Payments Quickly using the Java SDK

by on ‎11-11-2010 02:27 PM (74,824 Views)

The Authorize.Net API's are powerful and feature-rich.  Hand-crafting libraries in your language of choice can provide you with a robust solution which accomplishes all of your payments needs, but it can take a bit of time and research, especially if it is your first time doing it.  Recently, new Authorize.Net SDKs have been unveiled which enable developers to integrate in 15 minutes or less.  Don't believe it?  Follow me through the following steps and you'll see this is no fairy tale.  Rather, it is the future of enabling payments.

 

Today I'll walk you through setting up an Authorize.Net payment integration using the new Java SDK.  I'm going to assume that you already have a test account, but if you don't head over to https://developer.authorize.net/testaccount and get yourself setup.  The 15 minutes or less flow is right there as well, which you're free to walk through.  Or you can return to my blog post here and get the benefit of a little more explanation.

 

The Advanced Integration Method (AIM) is the most flexible way to process payments through Authorize.Net.  To start off, we need to identify ourselves as a merchant.  

The Merchant object defines the environment that we want to process transactions against.  It also defines who is making the requests with the API_LOGIN_ID and the TRANSACTION_KEY.  Make sure to keep those under lock and key.

 

 

Merchant merchant = Merchant.createMerchant(Environment.SANDBOX,
YOUR_API_LOGIN_ID, YOUR_TRANSACTION_KEY);

 

A credit card is required to process a payment through the gateway.  Let's create a simple Credit Card object.   At a bare minimum, the credit card number and expiration date are required. 

 

CreditCard creditCard = CreditCard.createCreditCard();
creditCard.setCreditCardNumber("4111 1111 1111 1111");
creditCard.setExpirationMonth("12");
creditCard.setExpirationYear("2015");

 

Now we need to create a Transaction object.  This container is used to collect information about the transaction that is to be processed.  For the sake of simplicity,  we'll just set the credit card data from above to be the only piece of information we'll use.  You should investigate the other various pieces of data that the Transaction object can facilitate.  For example, did you know that Authorize.Net can handle sending email receipts to your customers with the EmailReceipt object (net.authorize.data.EmailReceipt)?  Check it out if you don't already send out receipts via email.  Customers will appreciate it!

 

 

Transaction authCaptureTransaction = merchant.createAIMTransaction(
  TransactionType.AUTH_CAPTURE, new BigDecimal(1.99));

authCaptureTransaction.setCreditCard(creditCard);

 

 

So far, this is standard fare.  The next step, which is posting the transaction to the gateway handles much of the heavy lifting.  A Result object is returned and in it contains all the information that you might want regarding the processing of the transaction.

 

 

Result<Transaction> result = merchant.postTransaction(
  authCaptureTransaction);

 

There are various helper methods in the Result object, like isApproved() which will tell the developer immediately if the transaction was successful or not.  To access the complete data set, tap into the Result method named getTarget().  The SDK is using generics, and the "target" is really the generic type Transaction, which is being used to handle the response data.  Please don't feel shy about tapping into this data set -  treat it like a Choose Your Own Adventure book (http://en.wikipedia.org/wiki/Choose_Your_Own_Adventure).  You might find something interesting in the data that you could use elsewhere to make your life easier.  For example, Address Verification System (AVS) data is returned and can be useful for stopping fraud and chargebacks which can hurt a business.

 

 

To wrap this all up, I've created the following JSP snippet that encompasses all the above code blurbs with the correct imports.  I've also thrown in a dash, and I do mean a dash, of response data that the gateway has returned back. 

 

  <%@ page import="java.math.BigDecimal" %>
  <%@ page import="java.util.Map" %>
  <%@ page import="net.authorize.Environment" %>
  <%@ page import="net.authorize.Merchant" %>
  <%@ page import="net.authorize.TransactionType" %>
  <%@ page import="net.authorize.aim.Result" %>
  <%@ page import="net.authorize.aim.Transaction" %>
  <%@ page import="net.authorize.data.*" %>
  <%@ page import="net.authorize.data.creditcard.*" %>
  <%
    String apiLoginID = "API_LOGIN_ID";
    String transactionKey = "TRANSACTION_KEY";
    Merchant merchant = Merchant.createMerchant(Environment.SANDBOX,
        apiLoginID, transactionKey);

    // create credit card
    CreditCard creditCard = CreditCard.createCreditCard();
    creditCard.setCreditCardNumber("4111 1111 1111 1111");
    creditCard.setExpirationMonth("12");
    creditCard.setExpirationYear("2015");

    // create transaction
    Transaction authCaptureTransaction = merchant.createAIMTransaction(
        TransactionType.AUTH_CAPTURE, new BigDecimal(1.99));
    authCaptureTransaction.setCreditCard(creditCard);

    Result<Transaction> result = (Result<Transaction>)merchant
        .postTransaction(authCaptureTransaction);

    if(result.isApproved()) {
      out.println("Approved!</br>");
    } else if (result.isDeclined()) {
      out.println("Declined.</br>");
      out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
    } else {
      out.println("Error.</br>");
      out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
    }
  %>

 

Now What?

 

At this point, you're probably saying to yourself that this is great and everything, but seriously now what do I do?  My suggestion is to grab the API doc(s) in the dev center -  https://developer.authorize.net/api/aim, look  at the sample app(s), and take a look at the unit tests and Javadocs that are included in the SDK download.  Don't forget, the SDK source is all there to investigate, debug against, and extend/alter if you like.

 

 

While you can get up and running taking payments in less than 15 minutes with the new SDK's, the reality is these SDK's still sit on top of a very powerful set of APIs, and if you're implementing an Authorize.Net integration, you should become as proficient as possible in all that can be accomplished with Authorize.Net.  Use them as a reference and the SDK as the tool to get the job done.  Learn from it, use it, but more importantly start making money from it!

 

 

---
divesnob  is a guest contributor for Authorize.Net