Authorize.Net Integration into Java

Authorize.Net Integration into Java

The Merchant Interface at https://secure.authorize.net is a secure website where you can manage your payment gateway account, submit manual transactions, monitor and review unsettled transactions, search for and view settled transactions, view account billing statements, configure account settings and more.

Authorize.Net can be integarted in your website using following steps :

1. Sign up for Test Account :

Sign up for a test account to obtain an API Login ID and Transaction Key. These keys will authenticate requests to the payment gateway.

2. Install the Authorize.Net SDK:

Download anet-java-sdk-1.4.2.jar and place into your project library files.

3. Create the page that will host the form that will be submitted:

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 purchaseForm.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 choice.

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

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

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

<FORM NAME='secure_redirect_form' ID='secure_redirect_form_id'
ACTION='https://test.authorize.net/gateway/transact.dll' METHOD='POST'>
<label>CreditCardNumber</label><input type='text' class='text' name='x_card_num' size='15'></input>
<label>Exp.</label><input type='text' class='text' name='x_exp_date' size='4'></input>
<label>Amount</label><input type='text' class='text' name='x_amount' size='9' readonly value='<%=amount%>'></input>
<INPUT TYPE='HIDDEN' NAME='x_invoice_num' VALUE='<%=System.currentTimeMillis()%>'>
<INPUT TYPE='HIDDEN' NAME='x_relay_url' VALUE='<%=relayResponseUrl%>'>
<INPUT TYPE='HIDDEN' NAME='x_login' VALUE='<%=apiLoginId%>'>
<INPUT TYPE='HIDDEN' NAME='x_fp_sequence' VALUE='<%=x_fp_sequence%>'>
<INPUT TYPE='HIDDEN' NAME='x_fp_timestamp' VALUE='<%=x_fp_timestamp%>'>
<INPUT TYPE='HIDDEN' NAME='x_fp_hash' VALUE='<%=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='HIDDEN' NAME='x_amount' VALUE='<%=amount%>'>
<INPUT TYPE='HIDDEN' NAME='x_test_request' VALUE='FALSE'>
<INPUT TYPE='HIDDEN' NAME='notes' VALUE='extra hot please'>
<INPUT TYPE='SUBMIT' NAME='buy_button' VALUE='BUY'>
</FORM>

4. Create a page, Called Authorizerelay_response.jsp :

which will receive the response. 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.

<%@ page import="java.util.Map" %> <%@ page import="net.authorize.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--
var referrer = document.referrer;
if (referrer.substr(0,7)=="http://") referrer = referrer.substr(7);
if (referrer.substr(0,8)=="https://") referrer = referrer.substr(8);
if(referrer && referrer.indexOf(document.location.hostname) != 0) {
<%
String apiLoginId = "YOUR_API_LOGIN_ID";
String receiptPageUrl = "http://MERCHANT_HOST/ Authorizeorder_receipt.jsp";
/*
* Leave the MD5HashKey as is - unless you have explicitly set it in the
* merchant interface: Account > Settings > Security Settings > MD5-Hash
*/
String MD5HashKey = "YOUR_API_LOGIN_ID";

net.authorize.sim.Result result = net.authorize.sim.Result.createResult(apiLoginId,MD5HashKey, request.getParameterMap());
// perform Java server side processing...
// ...
// build receipt url buffer
StringBuffer receiptUrlBuffer = new StringBuffer(receiptPageUrl);

if(result != null) {
receiptUrlBuffer.append("?");
receiptUrlBuffer.append(ResponseField.RESPONSE_CODE.getFieldName()).append("=");
receiptUrlBuffer.append(result.getResponseCode().getCode());
receiptUrlBuffer.append("&");
receiptUrlBuffer.append(ResponseField.RESPONSE_REASON_CODE.getFieldName()).append("=");
receiptUrlBuffer.append(result.getReasonResponseCode().getResponseReasonCode());
receiptUrlBuffer.append("&");
receiptUrlBuffer.append(ResponseField.RESPONSE_REASON_TEXT.getFieldName()).append("=");
receiptUrlBuffer.append(result.getResponseMap().get(
ResponseField.RESPONSE_REASON_TEXT.getFieldName()));

if(result.isApproved()) {
receiptUrlBuffer.append("&").append(ResponseField.TRANSACTION_ID.getFieldName()
).append("=");
receiptUrlBuffer.append(result.getResponseMap().get(
ResponseField.TRANSACTION_ID.getFieldName()));
}
}
%>
// Use Javascript to redirect the page to the receipt redirect url. If Javascript is not
// available, then the <meta> refresh tag will handle the redirect.
document.location = "<%=receiptUrlBuffer.toString()%>";
}
//-->
</script>
<noscript><meta http-equiv="refresh" content="0;url=<%=receiptUrlBuffer.toString()%>">
</noscript>
</body>
</html>

5. Create a page, called Authorizeorder_receipt.jsp:

which will host the receipt information.

<%@ page import="java.math.BigDecimal" %> <%@ page import="java.util.Map" %> <%@ page import="net.authorize.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<h1>Your Receipt Page</h1></br>
<%
// Show the confirmation data
Map<String, String[]> requestParameterMap = request.getParameterMap();

if(requestParameterMap != null && requestParameterMap.containsKey(ResponseField.RESPONSE_CODE.getFieldName())) {
String transactionId = "";
if(requestParameterMap.containsKey(ResponseField.TRANSACTION_ID.getFieldName())) {
transactionId = requestParameterMap.get(ResponseField.TRANSACTION_ID.getFieldName())[0];
}

// 1 means we have a successful transaction
if("1".equals(requestParameterMap.get(ResponseField.RESPONSE_CODE.getFieldName())[0])) {
%>
<h2>Success!</h2>
<h3>Your transaction ID:</h3>
<div><%=net.authorize.util.StringUtils.sanitizeString(transactionId)%></div>
<%
} else {
%>
<h2>Error!</h2>
<h3><%=net.authorize.util.StringUtils.sanitizeString(
requestParameterMap.get(ResponseField.RESPONSE_REASON_TEXT.getFieldName())[0])%></h3>
<table>
<tr>
<td>response code</td>
<td><%=net.authorize.util.StringUtils.sanitizeString(
requestParameterMap.get(ResponseField.RESPONSE_CODE.getFieldName())[0])%></td>
</tr>
<tr>
<td>response reason code</td>
<td><%=net.authorize.util.StringUtils.sanitizeString(
requestParameterMap.get(ResponseField.RESPONSE_REASON_CODE.getFieldName())[0])%></td>
</tr>
</table>
</div>
<%
}
}
%>
</body>
</html>

6. Verify the transaction is processed:

Upon loading purchaseForm.jsp and following the steps, you should have been able to successfully enter in your credit card information hit submit and receive a success or error message that is hosted on your merchant server.

0 comments:

Post a Comment