Git Product home page Git Product logo

sdk-java's Introduction

Authorize.Net Java SDK

  <groupId>net.authorize</groupId>
  <artifactId>anet-java-sdk</artifactId>
  <version>1.8.1</version>

Prerequisites

  • JDK 1.5.0 or higher
  • Ant 1.6.2 or higher (build SDK only)
  • Maven 2.2.0 or higher (build SDK only)

Note: Support for building the SDK with either Ant or Maven has bee made. Please see the respective build processes below. All initial jars and docs were built with Ant however.

Dependencies

The SDK is offered with very few dependencies.

  • commons-logging-1.1.1.jar : logging
  • log4j-1.2.16.jar : logging
  • httpclient-4.0.1.jar : http communication with the payment gateway
  • httpcore-4.0.1.jar : http communication with the payment gateway
  • junit-4.8.2.jar : unit testing

Build process

  • Note: To properly run the unit tests, please reference the anet_java_sdk.properties.example file, which is a simple properties file that holds the API credentials for testing the SDK.

Build the SDK with Ant

To compile the SDK and create a jar...

$ ant jar

To run the unit tests...

$ ant unit-test

To create the javadocs...

$ ant javadoc

Build the SDK with Maven

To compile the SDK and create a jar...

$ mvn clean package

Test Code - Advanced Integration Method (AIM)

There are some sample unit tests that are located in the test directory. They capture basic auth/capture (product purchase) functionality, which most integrations are looking to get started with.

A simple auth/capture can be performed with the following code (JSP) :

<%@ 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 = "YOUR_API_LOGIN_ID"; String transactionKey = "YOUR_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>");
  out.println("Transaction Id: " + result.getTarget().getTransactionId());
} 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());
}

%>

Test Code - Advanced Integration Method (AIM) + Card Present

There are some sample unit tests that are located in the test directory. Similar to the AIM test, however they leverage the Card Present API.

A simple auth/capture can be performed with the following code (JSP) :

<%@ 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.cardpresent.Result" %> <%@ page import="net.authorize.aim.Transaction" %> <%@ page import="net.authorize.data." %> <%@ page import="net.authorize.data.creditcard." %> <% String apiLoginID = "YOUR_API_LOGIN_ID"; String transactionKey = "YOUR_TRANSACTION_KEY"; String MD5Value = "MD5_VALUE"; Merchant merchant = Merchant.createMerchant(Environment.SANDBOX, apiLoginID, transactionKey); merchant.setDeviceType(net.authorize.DeviceType.VIRTUAL_TERMINAL); merchant.setMarketType(net.authorize.MarketType.RETAIL); merchant.setMD5Value(MD5Value);

// create credit card
CreditCard creditCard = CreditCard.createCreditCard();
creditCard.setCardType(CardType.VISA);
creditCard.setTrack1("%B4111111111111111^CARDUSER/JOHN^1803101000000000020000831000000?");
creditCard.setTrack2(";4111111111111111=1803101000020000831?");

// 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>");
  out.println("Transaction Id: " + result.getTransId());
} else if (result.isDeclined()) {
  out.println("Declined.</br>");
  out.println(result.getResponseReasonCodes().get(0) + " : " +
    result.getResponseReasonCodes().get(0).getReasonText());
} else {
  out.println("Error.</br>");
  out.println(result.getResponseReasonCodes().get(0) + " : " +
    result.getResponseReasonCodes().get(0).getReasonText());
}

%>

Test Code - Server Integration Method (SIM)

The SDK implementation for SIM is fairly concise. To easily create a finger- print for your form POST, you can reference the following code :

Fingerprint fingerprint = Fingerprint.createFingerprint(
    "YOUR_API_LOGIN_ID",
    "YOUR_TRANSACTION_KEY",
    1234567890,
    "AMOUNT");

String x_fp_sequence = fingerprint.getSequence();
String x_fp_timestamp = fingerprint.getTimeStamp();
String x_fp_hash = fingerprint.getFingerprintHash();

Parsing a Relay Response is performed by using the ResponseParser class. It takes as it's only method parameter a pipe (|) delimited string that represents the transaction response passed to the merchant by Authorize.net.

HashMap<ResponseField, String> responseMap =
    ResponseParser.parseResponseString(responseString);

Setting up the necessary data containers and getting a form that can be displayed directly on the page can be performed via the following code (JSP) :

<%@ page import="net.authorize.sim." %> <%@ page import="net.authorize.sim.button." %> <%@ page import="net.authorize.data." %> <%@ page import="net.authorize.data.creditcard." %> <% String apiLoginID = "YOUR_API_LOGIN_ID"; String transactionKey = "YOUR_TRANSACTION_KEY"; String amount = "1.99"; Fingerprint fingerprint = Fingerprint.createFingerprint( apiLoginID, transactionKey, 1234567890, amount); long x_fp_sequence = fingerprint.getSequence(); long x_fp_timestamp = fingerprint.getTimeStamp(); String x_fp_hash = fingerprint.getFingerprintHash(); %>

<FORM NAME='formName' ID='formID' ACTION='https://test.authorize.net/gateway/transact.dll' METHOD='POST'>
  <INPUT TYPE='HIDDEN' NAME='x_login' VALUE='<%=net.authorize.util.StringUtils.sanitizeString(apiLoginID)%>'>
  <INPUT TYPE='HIDDEN' NAME='x_fp_sequence' VALUE='<%=net.authorize.util.StringUtils.sanitizeString(Long.toString(x_fp_sequence))%>'>
  <INPUT TYPE='HIDDEN' NAME='x_fp_timestamp' VALUE='<%=net.authorize.util.StringUtils.sanitizeString(Long.toString(x_fp_timestamp))%>'>
  <INPUT TYPE='HIDDEN' NAME='x_fp_hash' VALUE='<%=net.authorize.util.StringUtils.sanitizeString(x_fp_hash)%>'>
  <INPUT TYPE='HIDDEN' NAME='x_version' VALUE='3.1'>
  <INPUT TYPE='HIDDEN' NAME='x_method' VALUE='CC'>
  <INPUT TYPE='HIDDEN' NAME='x_type' VALUE='AUTH_CAPTURE'>
  <INPUT TYPE='TEXT' NAME='x_amount' VALUE='<%=net.authorize.util.StringUtils.sanitizeString(amount)%>'>
  <INPUT TYPE='HIDDEN' NAME='x_show_form' VALUE='payment_form'>
  <INPUT TYPE='HIDDEN' NAME='x_test_request' VALUE='FALSE'>
  <INPUT TYPE='SUBMIT' NAME='submit_button' VALUE='Submit' CLASS='null'>
</FORM>

If you were to load this JSP in your browser and hit submit, you should be taken to a page on the Authorize.Net servers that contains a form asking for payment information to be submitted.

Test Code - Direct Post Method (DPM)

  • This method is implemented via three code snippets. First, create the page that will host the form that will be submitted. We're calling this form.jsp, and you will be required to enter in your API_LOGIN_ID, TRANSACTION_KEY, and your public facing MERCHANT_HOST domain name. Note, you may want to alter the relayResponseUrl and place the jsp in a separate webapp container of your choosing.

    ======== form.jsp

    <%@ page import="net.authorize.sim.*" %> <% String apiLoginId = "YOUR_API_LOGIN_ID"; String transactionKey = "YOUR_TRANSACTION_KEY"; String relayResponseUrl = "http://MERCHANT_HOST/relay_response.jsp";

    String amount = "1.99"; Fingerprint fingerprint = Fingerprint.createFingerprint( apiLoginId, transactionKey, 1234567890, // random sequence used for the finger print creation amount);

    long x_fp_sequence = fingerprint.getSequence(); long x_fp_timestamp = fingerprint.getTimeStamp(); String x_fp_hash = fingerprint.getFingerprintHash(); %>

    CreditCardNumber Exp. Amount
  • Create a page that will receive the response. We're calling this relay_response.jsp (referenced in the form above by the relayResponseUrl). Once again, you will be required to enter in your API_LOGIN_ID and your public facing MERCHANT_HOST domain name. Additionally, you will have to provide your MD5_HASH_KEY. Unless you have explicitly set this in the merchant interface: Account > Settings > Security Settings > MD5-Hash, leave this as an empty string.

    ================== relay_response.jsp

    <%@ page import="java.util.Map" %> <%@ page import="net.authorize.*" %>

    <script type="text/javascript">

sdk-java's People

Contributors

ramittal avatar brianmc avatar vkandroiddev avatar aloise avatar benkiefer avatar ncpga avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.