Skip to content

Instantly share code, notes, and snippets.

@t2-support-gists
Created May 3, 2012 20:59
Show Gist options
  • Select an option

  • Save t2-support-gists/2589453 to your computer and use it in GitHub Desktop.

Select an option

Save t2-support-gists/2589453 to your computer and use it in GitHub Desktop.
Payment Java app2
AT&T API Samples - Payment app 1
------------------------------
This file describes how to set up, configure and run the Java Applications of the AT&T HTML5 Program sample applications.
It covers all steps required to register the application on DevConnect and, based on the generated API keys and secrets,
create and run one's own full-fledged sample applications.
1. Configuration
2. Installation
3. Parameters
4. Running the application
1. Configuration
Configuration consists of a few steps necessary to get an application registered on DevConnect with the proper services and
endpoints, depending on the type of client-side application (autonomous/non-autonomous).
To register an application, go to https://devconnect-api.att.com/ and login with your valid username and password.
Next, choose "My Apps" from the bar at the top of the page and click the "Setup a New Application" button.
Fill in the form, in particular all fields marked as "required".
Be careful while filling in the "OAuth Redirect URL" field. It should contain the URL that the oAuth provider will redirect
users to when he/she successfully authenticates and authorizes your application. For this application, it should point to
the oauth.jsp file inside this application folder. For example, if running on a local machine in a default Tomcat installation,
your OAuth Redirect URL might be http://localhost:8080/SampleApp/oauth.jsp
Also be careful when filling in the "Payment Listener URL" field when setting up your organization profile. This should point
to the paymentlistener.jsp file packaged with this application. This listener URL must be a public URL or IP address that the
gateway will be able to reach. For example, if running this application on a server with IP address 1.2.3.4 in a default Tomcat
instance, your listener URL might be https://1.2.3.4:8080/SampleApp/paymentlistener.jsp
NOTE: You MUST select Payment in the list of services under field 'Services' in order to use this sample application code.
Having your application registered, you will get back an important pair of data: an API key and Secret key. They are
necessary to get your applications working with the AT&T HTML5 APIs. See 'Adjusting parameters' below to learn how to use
these keys.
Initially your newly registered application is restricted to the "Sandbox" environment only. To move it to production,
you may promote it by clicking the "Promote to production" button. Notice that you will get a different API key and secret,
so these values in your application should be adjusted accordingly.
Depending on the kind of authentication used, an application may be based on either the Autonomous Client or the Web-Server
Client OAuth flow (see https://devconnect-api.att.com/docs/oauth20/autonomous-client-application-oauth-flow or
https://devconnect-api.att.com/docs/oauth20/web-server-client-application-oauth-flow respectively).
2. Installation
** Requirements
To run the examples you need a Java environment and at least Apache Tomcat 6, or another Java web server such as Jetty.
** Setting up multiple sample applications simultaneously
In case multiple applications need to be run at the same time, make sure to put each app in a separate folder and
adjust your OAuth Redirect URL accordingly.
3. Parameters
Each sample application contains a config.jsp file. It holds configurable parameters described in an easy to read format.
Please populate the following parameters in config.jsp as specified below:
1) clientIdAut : {set the value as per your registered appliaction 'API key' field value}
2) clientSecretAut : {set the value as per your registered appliaction 'Secret key' field value}
3) FQDN : https://api.att.com
Note: If your application is promoted from Sandbox environment to Production environment and you decide to use production
application settings, you must update parameters 1-2 as per production application details.
4. Running the application
To run the application, put the entire contents of the application folder into a separate folder named SampleApp inside the webapps
folder in your Apache Tomcat home directory. If you have specified a different home directory in Tomcat for your web applications,
put it there instead.
Depending on your security settings in Apache Tomcat, you might need to enable write access to the OauthStorage.jsp file.
Once you start tomcat, typically using the command "<your-tomcat-root-folder>/bin/startup.sh", your application becomes available
in a web browser, so you may visit: http://localhost:8080/SampleApp/singlepay.jsp to see it working.
<%
String clientIdAut = "";
String clientSecretAut = "";
String clientIdWeb = "";
String clientSecretWeb = "";
String FQDN = "https://api.att.com";
String subscriptionRedirect = "";
%>
<%@ page contentType="application/json" language="java" %><%@ page import="java.io.*" %><%@ page import="java.util.Arrays" %><%@ page import="java.util.Collections" %><%@ page import="java.util.Comparator" %><%@ include file="config.jsp" %><%
String notificationId = "";
String notificationType = "";
String transactionId = "";
String merchantTransactionId = "";
File directory = new File(application.getRealPath("notifications/"));
File[] files = directory.listFiles();
if(files.length>0) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
} });
Collections.reverse(Arrays.asList(files));
}
%>{"totalNumberOfNotifications":"<%=directory.listFiles().length%>","notificationList":[<%
if(directory.listFiles().length>0) {
int i = 0;
for(File imageFile : files){
String imageFileName = imageFile.getName();
RandomAccessFile inFile1 = new RandomAccessFile(application.getRealPath("notifications/" + imageFileName),"r");
notificationId = inFile1.readLine();
notificationType = inFile1.readLine();
transactionId = inFile1.readLine();
merchantTransactionId = inFile1.readLine();
if(notificationId==null || notificationId.equalsIgnoreCase("null"))
notificationId = "";
if(notificationId.indexOf(194)!=-1)
notificationId = notificationId.substring(0, notificationId.length()-2);
if(notificationType==null || notificationType.equalsIgnoreCase("null"))
notificationType = "";
if(notificationType.indexOf(194)!=-1)
notificationType = notificationType.substring(0, notificationType.length()-2);
if(transactionId==null || transactionId.equalsIgnoreCase("null"))
transactionId = "";
if(transactionId.indexOf(194)!=-1)
transactionId = transactionId.substring(0, transactionId.length()-2);
if(merchantTransactionId==null || merchantTransactionId.equalsIgnoreCase("null"))
merchantTransactionId = "";
if(merchantTransactionId.indexOf(194)!=-1)
merchantTransactionId = merchantTransactionId.substring(0, merchantTransactionId.length()-2);
inFile1.close();
if((i==directory.listFiles().length-1) || (i==4)) {
%>{"notificationId":"<%=notificationId%>","notificationType":"<%=notificationType%>","transactionId":"<%=transactionId%>", "merchantTransactionId":"<%=merchantTransactionId%>"}]}<%
} else {
%>{"notificationId":"<%=notificationId%>","notificationType":"<%=notificationType%>","transactionId":"<%=transactionId%>", "merchantTransactionId":"<%=merchantTransactionId%>"},<%
}
i += 1;
if(i==5)
break;
}
} else {
%>]}<%
}
%>
<%@ page contentType="application/json" language="java" %><%@ page import="java.io.*" %><%@ page import="java.util.Arrays" %><%@ page import="java.util.Collections" %><%@ page import="java.util.Comparator" %><%@ include file="config.jsp" %><%
String transactionId = "";
String merchantTransactionId = "";
String transactionAuthCode = "";
String consumerId = "";
File directory = new File(application.getRealPath("/transactionData/"));
File[] files = directory.listFiles();
if(files.length>0) {
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
} });
Collections.reverse(Arrays.asList(files));
}
%>{"totalNumberOfTransactions":"<%=directory.listFiles().length%>","transactionList":[<%
if(directory.listFiles().length>0) {
int i = 0;
for(File imageFile : files){
String imageFileName = imageFile.getName();
RandomAccessFile inFile1 = new RandomAccessFile(application.getRealPath("transactionData/" + imageFileName),"r");
transactionId = inFile1.readLine();
merchantTransactionId = inFile1.readLine();
transactionAuthCode = inFile1.readLine();
consumerId = inFile1.readLine();
if(transactionId==null || transactionId.equalsIgnoreCase("null"))
transactionId = "";
if(transactionId.indexOf(194)!=-1)
transactionId = transactionId.substring(0, transactionId.length()-2);
if(merchantTransactionId==null || merchantTransactionId.equalsIgnoreCase("null"))
merchantTransactionId = "";
if(merchantTransactionId.indexOf(194)!=-1)
merchantTransactionId = merchantTransactionId.substring(0, merchantTransactionId.length()-2);
if(transactionAuthCode==null || transactionAuthCode.equalsIgnoreCase("null"))
transactionAuthCode = "";
if(transactionAuthCode.indexOf(194)!=-1)
transactionAuthCode = transactionAuthCode.substring(0, transactionAuthCode.length()-2);
if(consumerId==null || consumerId.equalsIgnoreCase("null"))
consumerId = "";
if(consumerId.indexOf(194)!=-1)
consumerId = consumerId.substring(0, consumerId.length()-2);
inFile1.close();
if((i==directory.listFiles().length-1) || (i==4)) {
%>{"transactionId":"<%=transactionId%>","merchantTransactionId":"<%=merchantTransactionId%>","transactionAuthCode":"<%=transactionAuthCode%>","consumerId":"<%=consumerId%>"}]}<%
} else {
%>{"transactionId":"<%=transactionId%>","merchantTransactionId":"<%=merchantTransactionId%>","transactionAuthCode":"<%=transactionAuthCode%>","consumerId":"<%=consumerId%>"},<%
}
i += 1;
if(i==5)
break;
}
} else {
%>]}<%
}
%>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="org.apache.commons.httpclient.*"%>
<%@ page import="org.apache.commons.httpclient.methods.*"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="org.json.JSONArray"%>
<%@ page import="java.io.*" %>
<%@ include file="OauthStorage.jsp" %>
<%@ include file="config.jsp" %>
<%
//Initialize some variables here, check if relevant variables were passed in, if not then check session, otherwise set default.
String scope = "PAYMENT";
String accessToken = "";
String refreshToken = "";
String expires_in = "";
Long date = System.currentTimeMillis();
//This application uses the Autonomous Client OAuth consumption model
//Check if there is a valid access token that has not expired
if(date < savedAccessTokenExpiry) {
accessToken = savedAccessToken;
} else if(date < savedRefreshTokenExpiry) { //Otherwise if there is a refresh token that has not expired, use that to renew and save to file
String url = FQDN + "/oauth/token";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
String b = "client_id=" + clientIdAut + "&client_secret=" + clientSecretAut + "&grant_type=refresh_token&refresh_token=" + savedRefreshToken;
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
method.setRequestBody(b);
int statusCode = client.executeMethod(method);
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString());
accessToken = rpcObject.getString("access_token");
refreshToken = rpcObject.getString("refresh_token");
expires_in = rpcObject.getString("expires_in");
savedAccessTokenExpiry = date + (Long.parseLong(expires_in)*1000);
savedRefreshTokenExpiry = date + Long.parseLong("86400000");
method.releaseConnection();
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter(application.getRealPath("/OauthStorage.jsp"))), false);
String toSave = "\u003C\u0025\nString savedAccessToken = \"" + accessToken + "\";\nLong savedAccessTokenExpiry = Long.parseLong(\"" + savedAccessTokenExpiry + "\");\nString savedRefreshToken = \"" + refreshToken + "\";\nLong savedRefreshTokenExpiry = Long.parseLong(\"" + savedRefreshTokenExpiry + "\");\n\u0025\u003E";
outWrite.write(toSave);
outWrite.close();
} else if(date > savedRefreshTokenExpiry) { //Otherwise get a new access token and refresh token, and save them to file
String url = FQDN + "/oauth/token";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
String b = "client_id=" + clientIdAut + "&client_secret=" + clientSecretAut + "&grant_type=client_credentials&scope=" + scope;
method.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
method.setRequestBody(b);
int statusCode = client.executeMethod(method);
JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString());
accessToken = rpcObject.getString("access_token");
refreshToken = rpcObject.getString("refresh_token");
expires_in = rpcObject.getString("expires_in");
savedAccessTokenExpiry = date + (Long.parseLong(expires_in)*1000);
savedRefreshTokenExpiry = date + Long.parseLong("86400000");
method.releaseConnection();
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter(application.getRealPath("/OauthStorage.jsp"))), false);
String toSave = "\u003C\u0025\nString savedAccessToken = \"" + accessToken + "\";\nLong savedAccessTokenExpiry = Long.parseLong(\"" + savedAccessTokenExpiry + "\");\nString savedRefreshToken = \"" + refreshToken + "\";\nLong savedRefreshTokenExpiry = Long.parseLong(\"" + savedRefreshTokenExpiry + "\");\n\u0025\u003E";
outWrite.write(toSave);
outWrite.close();
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
<title>AT&amp;T Sample Notary Application - Sign Payload Application</title>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="style/common.css"/ >
<script type="text/javascript" src="js/helper.js">
</script>
<style type="text/css">
pre {
white-space: pre; /* CSS 2.0 */
white-space: pre-wrap; /* CSS 2.1 */
white-space: pre-line; /* CSS 3.0 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: -moz-pre-wrap; /* Mozilla */
white-space: -hp-pre-wrap; /* HP Printers */
word-wrap: break-word; /* IE 5+ */
}
</style>
<body>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="org.apache.commons.httpclient.*"%>
<%@ page import="org.apache.commons.httpclient.methods.*"%>
<%@ page import="org.json.*"%>
<%@ page import="java.io.*" %>
<%@ include file="config.jsp" %>
<%
String scope = "PAYMENT";
String accessToken = "";
String refreshToken = "";
String expires_in = "";
Long date = System.currentTimeMillis();
String signPayload = request.getParameter("signPayload");
String payload = request.getParameter("payload");
if(payload==null || payload.equalsIgnoreCase("null"))
payload = (String) session.getAttribute("payload");
if(payload==null || payload.equalsIgnoreCase("null"))
payload = "{\"Amount\":0.99,\n \"Category\":2,\n \"Channel\":"+
"\"MOBILE_WEB\",\n\"Description\":\"5 puzzles per month plan\",\n"+
"\"MerchantTransactionId\":\"user573transaction1377\",\n \"MerchantProductId\":\"SudokuMthlyPlan5\",\n"+
"\"MerchantApplicationId\":\"Sudoku\",\n"+
"\"MerchantPaymentRedirectUrl\":"+
"\"http://somewhere.com/OauthResponse.php\",\n"+
"\"MerchantSubscriptionIdList\":"+
"[\"p1\","+
"\"p2\",\"p3\",\"p4\",\"p5\"],\n"+
"\"IsPurchaseOnNoActiveSubscription\":false,\n"+
"\"SubscriptionRecurringNumber\": 5,\n \"SubscriptionRecurringPeriod\" : \"MONTHLY\",\n \"SubscriptionRecurringPeriodAmount\" : 1, }";
String signedPayload = request.getParameter("signedPayload");
if(signedPayload==null || signedPayload.equalsIgnoreCase("null"))
signedPayload = (String) session.getAttribute("signedPayload");
if(signedPayload==null || signedPayload.equalsIgnoreCase("null"))
signedPayload = "Sbe gur Abgnel ncc, fvzcyr gbby. Gurer fubhyq whfg or n Erdhrfg fvqr ba gur yrsg, pbagnvavat bar YNETR grkg obk jvgu ab qrsnhyg inyhr. Guvf vf jurer gur hfre pna chg va n obql bs grkg jvgu nyy gur cnenzrgref sbe n cnlzrag genafnpgvba, ohg gurl jvyy perngr guvf grkg gurzfryirf onfrq ba gur genafnpgvba qrgnvyf. Gura gurl pyvpx gur ohggba, juvpu qvfcynlf n grkg obk ba gur evtug jvgu gur Fvtarq Cnlybnq, naq gur Fvtangher, obgu bs juvpu gur hfre fubhyq or noyr gb pbcl rnfvyl naq cnfgr vagb gur cnlzrag nccyvpngvba yngre ba. Va erny yvsr, guvf jvyy or qbar nhgbzngvpnyyl ol pbqr, ohg guvf ncc whfg arrqf gb fubj gur onfvp vagrenpgvba jvgu guvf arj Abgnel NCV, juvpu yvgrenyyl whfg gnxrf gur grkg lbh fraq, naq ergheaf gur fvtarq cnlybnq (grkg) naq gur fvtangher. V ubcr gung znxrf frafr";
String signature = request.getParameter("signature");
if(signature==null || signature.equalsIgnoreCase("null"))
signature = (String) session.getAttribute("signature");
if(signature==null || signature.equalsIgnoreCase("null"))
signature = "hfd7adsf76asffs987sdf98fs6a7a98ff6a";
%>
<div id="container">
<!-- open HEADER --><div id="header">
<div>
<div id="hcRight">
<%=new java.util.Date()%>
</div>
<div id="hcLeft">Server Time:</div>
</div>
<div>
<div id="hcRight"><script language="JavaScript" type="text/javascript">
var myDate = new Date();
document.write(myDate);
</script></div>
<div id="hcLeft">Client Time:</div>
</div>
<div>
<div id="hcRight"><script language="JavaScript" type="text/javascript">
document.write("" + navigator.userAgent);
</script></div>
<div id="hcLeft">User Agent:</div>
</div>
<br clear="all" />
</div><!-- close HEADER -->
<div id="wrapper">
<div id="content">
<h1>A&amp;T Sample Notary Application - Sign Payload Application</h1>
</div>
</div>
<%
//If Sign Payload button was clicked, do this.
if(signPayload!=null) {
session.setAttribute("payload", payload);
String url = FQDN + "/Security/Notary/Rest/1/SignedPayload";
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
method.addRequestHeader("Accept","application/json");
method.addRequestHeader("client_id", clientIdAut);
method.addRequestHeader("client_secret", clientSecretAut);
method.setRequestBody(payload);
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
signedPayload = jsonResponse.getString("SignedDocument");
session.setAttribute("signedPayload", signedPayload);
signature = jsonResponse.getString("Signature");
session.setAttribute("signature", signature);
if(request.getParameter("return")!=null)
response.sendRedirect(request.getParameter("return") + "?signedPayload=" + signedPayload + "&signature=" + signature);
} else {
%><%=method.getResponseBodyAsString()%><%
}
method.releaseConnection();
}
%>
<div id="wrapper">
<div id="content">
<h2><br />
Feature 1: Sign Payload</h2>
<br/>
</div>
</div>
<form method="post" name="signPayload">
<div id="navigation">
<table border="0" width="950px">
<tbody>
<tr>
<td valign="top" class="label">Request:</td>
<td class="cell" ><textarea rows="20" cols="60" name="payload" ><%=payload.replaceAll(",", ",\n")%></textarea>
</td>
<td width="50px"></td>
<td valign="top" class="label">Signed Payload:</td>
<td class="cell" width="400px"><%=signedPayload.replaceAll("(.{5})", "$1 ")%></td>
</tr>
<tr>
<td></td>
<td></td>
<td width="50px"></td>
<td valign="top" class="label">Signature:</td>
<td class="cell"><%=signature.replaceAll("(.{5})", "$1 ")%></td>
</tr>
<tr>
<td></td>
<td class="cell" align="right"><button type="submit" name="signPayload">Sign Payload</button></td>
</tr>
</tbody></table>
</div>
<br clear="all" />
</form>
<div id="footer">
<div style="float: right; width: 20%; font-size: 9px; text-align: right">Powered by AT&amp;T Virtual Mobile</div>
<p>&#169; 2011 AT&amp;T Intellectual Property. All rights reserved. <a href="http://developer.att.com/" target="_blank">http://developer.att.com</a>
<br>
The Application hosted on this site are working examples intended to be used for reference in creating products to consume AT&amp;T Services and not meant to be used as part of your product. The data in these pages is for test purposes only and intended only for use as a reference in how the services perform.
<br>
For download of tools and documentation, please go to <a href="https://devconnect-api.att.com/" target="_blank">https://devconnect-api.att.com</a>
<br>
For more information contact <a href="mailto:developer.support@att.com">developer.support@att.com</a>
</div>
</div>
</body></html>
<%
String savedAccessToken = "ad13138f408bbdaabbdafa355b0b9ee8";
Long savedAccessTokenExpiry = Long.parseLong("99");
String savedRefreshToken = "eb039c57a6efa7216e06a5114cce4a7f6576f2f3";
Long savedRefreshTokenExpiry = Long.parseLong("99");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
<title>AT&T Sample Payment Application - Subscription Application</title>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="style/common.css"/ >
<script type="text/javascript" src="js/helper.js">
</script>
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-28378273-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%@ page import="org.apache.commons.httpclient.*"%>
<%@ page import="org.apache.commons.httpclient.methods.*"%>
<%@ page import="org.json.JSONObject"%>
<%@ page import="org.json.JSONArray"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.Random" %>
<%@ include file="getToken.jsp" %>
<%
String newSubscription = request.getParameter("newSubscription");
String getSubscriptionStatus = request.getParameter("getSubscriptionStatus");
String getSubscriptionDetails = request.getParameter("getSubscriptionDetails");
String refundSubscription = request.getParameter("refundSubscription");
String refundReasonText = "User did not like product";
String trxId = (String) session.getAttribute("trxId");
if(trxId==null || trxId.equalsIgnoreCase("null"))
trxId = "";
String trxIdRefund = request.getParameter("trxIdRefund");
if(trxIdRefund==null || trxIdRefund.equalsIgnoreCase("null"))
trxIdRefund = "";
String merchantTrxId = request.getParameter("merchantTrxId");
if(merchantTrxId==null || merchantTrxId.equalsIgnoreCase("null"))
merchantTrxId = (String) session.getAttribute("merchantTrxId");
if(merchantTrxId==null || merchantTrxId.equalsIgnoreCase("null"))
merchantTrxId = "";
String authCode = request.getParameter("SubscriptionAuthCode");
if(authCode==null || authCode.equalsIgnoreCase("null"))
authCode = (String) session.getAttribute("authCode");
if(authCode==null || authCode.equalsIgnoreCase("null"))
authCode = "";
String consumerId = request.getParameter("consumerId");
if(consumerId==null || consumerId.equalsIgnoreCase("null"))
consumerId = (String) session.getAttribute("consumerId");
if(consumerId==null || consumerId.equalsIgnoreCase("null"))
consumerId = "";
Random randomGenerator = new Random();
int product = 0;
if(request.getParameter("product")!=null)
product = Integer.parseInt(request.getParameter("product"));
String amount = "";
String description = "";
String merchantProductId = "";
String trxIdGetDetails = "";
if(product==1) {
amount = "1.99";
description = "Word Game Subscription 1";
merchantProductId = "WordGameSubscription1";
} else if(product==2) {
amount = "3.99";
description = "Number Game Subscription 1";
merchantProductId = "NumberGameSubscription1";
}
%>
<div id="container">
<!-- open HEADER --><div id="header">
<div>
<div id="hcRight">
<%=new java.util.Date()%>
</div>
<div id="hcLeft">Server Time:</div>
</div>
<div>
<div id="hcRight"><script language="JavaScript" type="text/javascript">
var myDate = new Date();
document.write(myDate);
</script></div>
<div id="hcLeft">Client Time:</div>
</div>
<div>
<div id="hcRight"><script language="JavaScript" type="text/javascript">
document.write("" + navigator.userAgent);
</script></div>
<div id="hcLeft">User Agent:</div>
</div>
<br clear="all" />
</div><!-- close HEADER -->
<div id="wrapper">
<div id="content">
<h1>AT&T Sample Payment Application - Subscription Application</h1>
<h2>Feature 1: Create New Subscription</h2><br/>
</div>
</div>
<form method="post" name="newSubscription" >
<div id="navigation">
<table border="0" width="100%">
<tbody>
<tr>
<td width="50%" valign="top" class="label">Subscribe for $1.99 per month:</td>
<td class="cell"><input type="radio" name="product" value="1" checked>
</td>
</tr>
<tr>
<td width="50%" valign="top" class="label">Subscribe for $3.99 per month:</td>
<td class="cell"><input type="radio" name="product" value="2">
</td></tr>
</tbody></table>
</div>
<div id="extra">
<table>
<tbody>
<tr>
<td><br /><br /><button type="submit" name="newSubscription">Subscribe</button></td>
</tr>
</tbody></table>
</div>
<br clear="all" />
<div align="center"></div>
</form>
<% if(newSubscription!=null) {
merchantTrxId = "user" + randomGenerator.nextInt(100000) + "subscription" + randomGenerator.nextInt(1000000);
session.setAttribute("merchantTrxId", merchantTrxId);
session.setAttribute("trxId", null);
session.setAttribute("authCode", null);
session.setAttribute("consumerId", null);
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter(application.getRealPath("/transactionData/" + merchantTrxId + ".txt"))), false);
String toSave = trxId + "\n" + merchantTrxId + "\n" + authCode;
outWrite.write(toSave);
outWrite.close();
String forNotary = "notary.jsp?signPayload=true&return=subscription.jsp&payload="+
"{\"Amount\":" + amount + ","+
"\"Category\":1, \"Channel\":\"MOBILE_WEB\","+
"\"Description\":\"" + description + "\","+
"\"MerchantTransactionId\":\"" + merchantTrxId + "\","+
"\"MerchantProductId\":\"" + merchantProductId + "\","+
"\"MerchantPaymentRedirectUrl\":\"" + subscriptionRedirect + "\","+
"\"MerchantSubscriptionIdList\":\"sampleSubscription1\","+
"\"IsPurchaseOnNoActiveSubscription\":\"false\","+
"\"SubscriptionRecurringNumber\":99999,"+
"\"SubscriptionRecurringPeriod\":\"MONTHLY\","+
"\"SubscriptionRecurringPeriodAmount\":1}";
response.sendRedirect(forNotary);
} %>
<% if(request.getParameter("SubscriptionAuthCode")!=null) {
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter(application.getRealPath("/transactionData/" + merchantTrxId + ".txt"))), false);
String toSave = trxId + "\n" + merchantTrxId + "\n" + authCode;
outWrite.write(toSave);
outWrite.close();
session.setAttribute("authCode", authCode);
%>
<div class="successWide">
<strong>SUCCESS:</strong><br />
<strong>Merchant Subscription ID</strong> <%=(String) session.getAttribute("merchantTrxId")%><br/>
<strong>Subscription Auth Code</strong> <%=authCode%><br /><br/>
<form name="getNotaryDetails" action="notary.jsp">
<input type="submit" name="getNotaryDetails" value="View Notary Details" />
</form>
</div><br/>
<% } %>
<%
if(request.getParameter("signedPayload")!=null && request.getParameter("signature")!=null){
response.sendRedirect(FQDN + "/Commerce/Payment/Rest/2/Subscriptions?clientid=" + clientIdAut + "&SignedPaymentDetail=" + request.getParameter("signedPayload") + "&Signature=" + request.getParameter("signature"));
}
%>
<div id="wrapper">
<div id="content">
<h2><br />
Feature 2: Get Subscription Status</h2>
</div>
</div>
<form method="post" name="getSubscriptionStatus" action="subscription.jsp">
<div id="navigation" align="center">
<table style="width: 750px" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th style="width: 150px" class="cell" align="right"></th>
<th style="width: 100px" class="cell"></th>
<th style="width: 240px" class="cell" align="left"></th>
</tr>
</thead>
<tbody>
<tr>
<td class="cell" align="right">
<input type="radio" name="getTransactionType" value="1" checked /> Merchant Sub. ID:
</td>
<td></td>
<td class="cell" align="left"><%=merchantTrxId%></td>
</tr>
<tr>
<td class="cell" align="right">
<input type="radio" name="getTransactionType" value="2" /> Auth Code:
<td></td>
<td class="cell" align="left"><%=authCode%></td>
</td>
</tr>
<% if(!trxId.equalsIgnoreCase("")) { %>
<tr>
<td class="cell" align="right">
<input type="radio" name="getTransactionType" value="3" /> Subscription ID:
<td></td>
<td class="cell" align="left"><%=trxId%></td>
</td>
</tr>
<% } %>
<tr>
<td></td>
<td></td>
<td></td>
<td class="cell"><button type="submit" name="getSubscriptionStatus">Get Subscription Status</button>
</td>
</tr>
</tbody></table>
</form>
</div>
<br clear="all" />
<% if(getSubscriptionStatus!=null) {
int getTransactionType = Integer.parseInt(request.getParameter("getTransactionType"));
String url = "";
if(getTransactionType==1)
url = FQDN + "/Commerce/Payment/Rest/2/Subscriptions/MerchantTransactionId/" + merchantTrxId;
if(getTransactionType==2)
url = FQDN + "/Commerce/Payment/Rest/2/Subscriptions/SubscriptionAuthCode/" + authCode;
if(getTransactionType==3)
url = FQDN + "/Commerce/Payment/Rest/2/Subscriptions/SubscriptionId/" + trxId;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.setQueryString("access_token=" + accessToken);
method.addRequestHeader("Accept","application/json");
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
trxId = jsonResponse.getString("SubscriptionId");
session.setAttribute("trxId", trxId);
consumerId = jsonResponse.getString("ConsumerId");
session.setAttribute("consumerId", consumerId);
merchantTrxId = jsonResponse.getString("MerchantTransactionId");
session.setAttribute("merchantTrxId", merchantTrxId);
JSONArray parameters = jsonResponse.names();
PrintWriter outWrite = new PrintWriter(new BufferedWriter(new FileWriter(application.getRealPath("/transactionData/" + merchantTrxId + ".txt"))), false);
String toSave = trxId + "\n" + merchantTrxId + "\n" + authCode + "\n" + consumerId;
outWrite.write(toSave);
outWrite.close();
%>
<div class="successWide">
<strong>SUCCESS:</strong><br />
<strong>Transaction ID</strong> <%=trxId%><br />
<strong>Merchant Transaction ID</strong> <%=merchantTrxId%><br/>
</div><br/>
<div align="center"><table style="width: 650px" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th style="width: 100px" class="cell" align="right"><strong>Parameter</strong></th>
<th style="width: 100px" class="cell"><strong></strong></th>
<th style="width: 275px" class="cell" align="left"><strong>Value</strong></th>
</tr>
</thead>
<tbody>
<% for(int i=0; i<parameters.length(); i++) { %>
<tr>
<td align="right" class="cell"><%=parameters.getString(i)%></td>
<td align="center" class="cell"></td>
<td align="left" class="cell"><%=jsonResponse.getString(parameters.getString(i))%></td>
</tr>
<% } %>
</tbody>
</table>
</div><br/>
<%
} else {
%>
<div class="errorWide">
<strong>ERROR:</strong><br />
<%=method.getResponseBodyAsString()%>
</div><br/>
<%
}
method.releaseConnection();
} %>
<div id="wrapper">
<div id="content">
<h2><br />Feature 3: Get Subscription Details</h2>
</div>
</div>
<br/>
<form method="post" name="getSubscriptionDetails" >
<div id="navigation" align="center">
<table style="width: 900px" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th style="width: 150px" class="cell" align="right"><strong>Consumer ID</strong></th>
<th style="width: 50px" class="cell"></th>
<th style="width: 240px" class="cell" align="left"><strong>Merchant Subscription ID</strong></th>
<td><div class="warning">
<strong>WARNING:</strong><br />
You must use Get Subscription Status to get the Consumer ID before you can get details.
</div></td>
</tr>
</thead>
<tbody>
<%
if(true) {
String url = request.getRequestURL().toString().substring(0,request.getRequestURL().toString().lastIndexOf("/")) + "/getLatestTransactions.jsp";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
JSONArray transactionList = new JSONArray(jsonResponse.getString("transactionList"));
for(int i=0; i<transactionList.length(); i++) {
if(transactionList.length()>0) {
JSONObject transaction = new JSONObject(transactionList.getString(i));
if(consumerId.equalsIgnoreCase(transaction.getString("consumerId")))
trxIdGetDetails = transaction.getString("merchantTransactionId");
if(i==0) {
%>
<tr>
<td class="cell" align="right">
<input type="radio" name="trxIdGetDetails" value="<%=transaction.getString("consumerId")%>" checked /><%=transaction.getString("consumerId")%>
</td>
<td></td>
<td class="cell" align="left"><%=transaction.getString("merchantTransactionId")%></td>
</tr>
<%
} else {
%>
<tr>
<td class="cell" align="right">
<input type="radio" name="trxIdGetDetails" value="<%=transaction.getString("consumerId")%>" /><%=transaction.getString("consumerId")%>
</td>
<td></td>
<td class="cell" align="left"><%=transaction.getString("merchantTransactionId")%></td>
</tr>
<%
}
}
}
method.releaseConnection();
}
}
%>
<tr>
<td></td>
<td></td>
<td></td>
<td class="cell"><button type="submit" name="getSubscriptionDetails">Get Subscription Details</button>
</td>
</tr>
</tbody></table>
</form>
</div>
<br clear="all" />
<% if(getSubscriptionDetails!=null) {
String url = FQDN + "/Commerce/Payment/Rest/2/Subscriptions/sampleSubscription1/Detail/" + consumerId;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.setQueryString("access_token=" + accessToken);
method.addRequestHeader("Accept","application/json");
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
JSONArray parameters = jsonResponse.names();
%>
<div class="successWide">
<strong>SUCCESS:</strong><br />
<strong>Consumer ID</strong> <%=consumerId%><br />
</div><br/>
<div align="center"><table style="width: 650px" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th style="width: 100px" class="cell" align="right"><strong>Parameter</strong></th>
<th style="width: 100px" class="cell"><strong></strong></th>
<th style="width: 275px" class="cell" align="left"><strong>Value</strong></th>
</tr>
</thead>
<tbody>
<% for(int i=0; i<parameters.length(); i++) { %>
<tr>
<td align="right" class="cell"><%=parameters.getString(i)%></td>
<td align="center" class="cell"></td>
<td align="left" class="cell"><%=jsonResponse.getString(parameters.getString(i))%></td>
</tr>
<% } %>
</tbody>
</table>
</div><br/>
<%
} else {
%>
<div class="errorWide">
<strong>ERROR:</strong><br />
<%=method.getResponseBodyAsString()%>
</div><br/>
<%
}
method.releaseConnection();
}
%>
<div id="wrapper">
<div id="content">
<h2><br />Feature 4: Refund Subscription</h2>
</div>
</div>
<form method="post" name="refundSubscription" >
<div id="navigation" align="center">
<table style="width: 750px" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th style="width: 150px" class="cell" align="right"><strong>Subscription ID</strong></th>
<th style="width: 100px" class="cell"></th>
<th style="width: 240px" class="cell" align="left"><strong>Merchant Subscription ID</strong></th>
<td><div class="warning">
<strong>WARNING:</strong><br />
You must use Get Subscription Status to get the Subscription ID before you can refund it.
</div></td>
</tr>
</thead>
<tbody>
<%
if(true) {
String url = request.getRequestURL().toString().substring(0,request.getRequestURL().toString().lastIndexOf("/")) + "/getLatestTransactions.jsp";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
JSONArray transactionList = new JSONArray(jsonResponse.getString("transactionList"));
for(int i=0; i<transactionList.length(); i++) {
if(transactionList.length()>0) {
JSONObject transaction = new JSONObject(transactionList.getString(i));
if(i==0) {
%>
<tr>
<td class="cell" align="right">
<input type="radio" name="trxIdRefund" value="<%=transaction.getString("transactionId")%>" checked /><%=transaction.getString("transactionId")%>
</td>
<td></td>
<td class="cell" align="left"><%=transaction.getString("merchantTransactionId")%></td>
</tr>
<%
} else {
%>
<tr>
<td class="cell" align="right">
<input type="radio" name="trxIdRefund" value="<%=transaction.getString("transactionId")%>" /><%=transaction.getString("transactionId")%>
</td>
<td></td>
<td class="cell" align="left"><%=transaction.getString("merchantTransactionId")%></td>
</tr>
<%
}
}
}
method.releaseConnection();
}
}
%>
<tr>
<td></td>
<td></td>
<td></td>
<td class="cell"><button type="submit" name="refundSubscription">Refund Subscription</button>
</td>
</tr>
</tbody></table>
</form>
</div>
<br clear="all" />
<% if(refundSubscription!=null) {
String url = FQDN + "/Commerce/Payment/Rest/2/Transactions/" + trxIdRefund;
HttpClient client = new HttpClient();
PutMethod method = new PutMethod(url);
method.setQueryString("access_token=" + accessToken + "&Action=refund");
method.addRequestHeader("Content-Type","application/json");
method.addRequestHeader("Accept","application/json");
JSONObject bodyObject = new JSONObject();
String reasonCode = "1";
bodyObject.put("RefundReasonCode",Double.parseDouble(reasonCode));
bodyObject.put("RefundReasonText",refundReasonText);
method.setRequestBody(bodyObject.toString());
int statusCode = client.executeMethod(method);
if(statusCode==200) {
//JSONObject rpcObject = new JSONObject(method.getResponseBodyAsString());
%>
<div class="successWide">
<strong>SUCCESS:</strong><br />
<strong>Transaction ID</strong> <%=method.getResponseBodyAsString()%><br />
</div><br/>
<%
} else {
%>
<div class="errorWide">
<strong>ERROR:</strong><br />
<%=method.getResponseBodyAsString()%>
</div><br/>
<%
}
method.releaseConnection();
}
%>
<div id="wrapper">
<div id="content">
<h2><br />Feature 5: Notifications</h2>
</div>
</div>
<form method="post" name="refreshNotifications" >
<div id="navigation"><br/>
<div align="center"><table style="width: 650px" cellpadding="1" cellspacing="1" border="0">
<thead>
<tr>
<th style="width: 100px" class="cell"><strong>Notification ID</strong></th>
<th style="width: 100px" class="cell"><strong>Notification Type</strong></th>
<th style="width: 125px" class="cell"><strong>Subscription ID</strong></th>
<th style="width: 175px" class="cell"><strong>Merchant Subscription ID</strong></th>
</tr>
</thead>
<tbody>
<%
if(true) {
String url = request.getRequestURL().toString().substring(0,request.getRequestURL().toString().lastIndexOf("/")) + "/getLatestNotifications.jsp";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = client.executeMethod(method);
if(statusCode==200) {
JSONObject jsonResponse = new JSONObject(method.getResponseBodyAsString());
JSONArray notificationList = new JSONArray(jsonResponse.getString("notificationList"));
for(int i=0; i<notificationList.length(); i++) {
if(notificationList.length()>0) {
JSONObject notification = new JSONObject(notificationList.getString(i));
%>
<tr>
<td align="center" class="cell"><%=notification.getString("notificationId")%></td>
<td align="center" class="cell"><%=notification.getString("notificationType")%></td>
<td align="center" class="cell"><%=notification.getString("transactionId")%></td>
<td align="center" class="cell"><%=notification.getString("merchantTransactionId")%></td>
</tr>
<%
}
}
method.releaseConnection();
}
}
%>
</tbody>
</table>
</div>
<div id="extra"><br/>
<table border="0" width="100%">
<tbody>
<tr>
<td class="cell"><button type="submit" name="refreshNotifications">Refresh</button>
</td>
</tr>
</tbody></table>
</div>
<br clear="all" />
</form></div>
<div id="footer">
<div style="float: right; width: 20%; font-size: 9px; text-align: right">Powered by AT&amp;T Virtual Mobile</div>
<p>&#169; 2011 AT&amp;T Intellectual Property. All rights reserved. <a href="http://developer.att.com/" target="_blank">http://developer.att.com</a>
<br>
The Application hosted on this site are working examples intended to be used for reference in creating products to consume AT&amp;T Services and not meant to be used as part of your product. The data in these pages is for test purposes only and intended only for use as a reference in how the services perform.
<br>
For download of tools and documentation, please go to <a href="https://devconnect-api.att.com/" target="_blank">https://devconnect-api.att.com</a>
<br>
For more information contact <a href="mailto:developer.support@att.com">developer.support@att.com</a>
</div>
</div>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment