Get Transaction History
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetTransactionHistory".
Inside the public class, we will create a public static void, and declare all the input and output variables.
Then we create a try clause, and create both the header and content object.
Then, we will connect to the API serviceName, build the request parameters, send the request, and get the response object.
Then we will parse the Content, ServiceResponse, and ServiceRespHeader, and store the "GlobalErrorID", "ErrorText" and "ErrorDetails" in "globalErrorID", "errorText" and "errorDetails" respectively.
If the globalErrorID is '010041', it means that the OTP provided has expired. We will then display the errorText and errorDetails as well as a message 'OTP has expired, new OTP will be sent'.
If it is not '010000', we will display the ErrorText and the ErrorDetails.
Else, it means that there is no error, and we will use the "instanceof" operator to check whether the "transaction_Detail" object is an JSONArray or JSONObject, and display the results accordingly.
The method shown in the red box is a Reference Data API 'getTransactionTypes', which can be referred under Reference Data Sub-Menu
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetTransactionHistory
{
public static void main(String args[])
{
boolean verbose = true;
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
// response parameters
String serviceRespTag = "Get_Transaction_History_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String accountFrom;
String accountTo;
String accountTo_interimBalance;
String currency;
String exchangeRate;
String interimBalance;
String narrative;
String officerID;
String paymentMode;
String quoteCurrency;
String transactionAmount;
String transactionBranch;
String transactionDate;
String transactionID;
String transactionReferenceNumber;
String transactionType;
String serviceName = "getTransactionHistory";
String userID = "bobsmith1";
String PIN = "123456";
String OTP = "123456";
String accountID = "2222";
String startDate = "2010-01-01 00:00:00";
String endDate = "2019-01-01 23:59:59";
String numRecordsPerPage = "5";
String pageNum = "1";
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", serviceName);
jo.put("userID", userID);
jo.put("PIN", PIN);
jo.put("OTP", OTP);
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
// build content
jo = new JSONObject();
jo.put("accountID", accountID);
jo.put("startDate", startDate);
jo.put("endDate", endDate);
jo.put("numRecordsPerPage", numRecordsPerPage);
jo.put("pageNum", pageNum);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
// connect to API service
HttpURLConnection urlConnection = (HttpURLConnection) new URL(apiServiceUrl).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
// build request parameters
String parameters
= "Header="+header+"&"
+ "Content="+content;
// send request
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
br.write(parameters);
br.close();
// get response
String response = "";
Scanner s = new Scanner(urlConnection.getInputStream());
while (s.hasNextLine()){
response += s.nextLine();
}
s.close();
// get response object
boolean apiError;
JSONObject responseObj = new JSONObject(response);
responseObj = new JSONObject(response);
if (verbose){
System.out.println(responseObj.toString(4)); // indent 4 spaces
System.out.println();
}
// parse {"Content"}
contentObj = responseObj.getJSONObject("Content");
// parse {"ServiceResponse"}
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
// parse {"ServiceRespHeader"}
JSONObject serviceRespHeaderObj = serviceRespObj.getJSONObject("ServiceRespHeader");
globalErrorID = serviceRespHeaderObj.getString("GlobalErrorID");
errorText = serviceRespHeaderObj.getString("ErrorText");
errorDetails = serviceRespHeaderObj.getString("ErrorDetails");
if (verbose){
System.out.println("GlobalErrorID="+globalErrorID);
System.out.println("ErrorText="+errorText);
System.out.println("ErrorDetails="+errorDetails);
System.out.println();
}
if(globalErrorID.equals("010041"))
{
System.out.println(errorText);
System.out.println(errorDetails);
System.out.println("OTP expired, new OTP will be sent");
}
else if(!globalErrorID.equals("010000"))
{
System.out.println(errorText);
System.out.println(errorDetails);
}
else
{
JSONObject transactionObj = serviceRespObj.getJSONObject("CDMTransactionDetail");
Object testObj = transactionObj.get("transaction_Detail");
if (testObj instanceof JSONArray)
{
JSONArray tstObj = transactionObj.getJSONArray("transaction_Detail");
for (int i = 0; i < tstObj.length(); i++)
{
JSONObject tstobj = tstObj.getJSONObject(i);
accountFrom = tstobj.getString("accountFrom");
accountTo = tstobj.getString("accountTo");
accountTo_interimBalance = tstobj.getString("accountTo_interimBalance");
currency = tstobj.getString("currency");
exchangeRate = tstobj.getString("exchangeRate");
interimBalance = tstobj.getString("interimBalance");
narrative = tstobj.getString("narrative");
officerID = tstobj.getString("officerID");
paymentMode = tstobj.getString("paymentMode");
quoteCurrency = tstobj.getString("quoteCurrency");
transactionAmount = tstobj.getString("transactionAmount");
transactionBranch = tstobj.getString("transactionBranch");
transactionDate = tstobj.getString("transactionDate");
transactionID = tstobj.getString("transactionID");
transactionReferenceNumber = tstobj.getString("transactionReferenceNumber");
System.out.println("Account from = " +accountFrom);
System.out.println("Account to = " +accountTo);
System.out.println("Account to interim Balance = " +accountTo_interimBalance);
System.out.println("Currency = " +currency);
System.out.println("Exchange Rate = " +exchangeRate);
System.out.println("Interim Balance = " +interimBalance);
System.out.println("Narrative = " +narrative);
System.out.println("Office ID = " +officerID);
System.out.println("Payment Mode = " +paymentMode);
System.out.println("Quote Currency = " +quoteCurrency);
System.out.println("Transaction Amount = " +transactionAmount);
System.out.println("Transaction Branch = " +transactionBranch);
System.out.println("Transaction Date = " +transactionDate);
System.out.println("Transaction ID = " +transactionID);
System.out.println("Transaction Reference Number = " +transactionReferenceNumber);
transactionType = tstobj.getString("transactionType");
getTransactionTypes getTransactionTypesObj = new getTransactionTypes();
getTransactionTypesObj.getTransactionTypes(transactionType);
System.out.println("Transaction Type = " +transactionType);
}
}
else if(testObj instanceof JSONObject)
{
JSONObject tstObj = transactionObj.getJSONObject("transaction_Detail");
accountFrom = tstObj.getString("accountFrom");
accountTo = tstObj.getString("accountTo");
accountTo_interimBalance = tstObj.getString("accountTo_interimBalance");
currency = tstObj.getString("currency");
exchangeRate = tstObj.getString("exchangeRate");
interimBalance = tstObj.getString("interimBalance");
narrative = tstObj.getString("narrative");
officerID = tstObj.getString("officerID");
paymentMode = tstObj.getString("paymentMode");
quoteCurrency = tstObj.getString("quoteCurrency");
transactionAmount = tstObj.getString("transactionAmount");
transactionBranch = tstObj.getString("transactionBranch");
transactionDate = tstObj.getString("transactionDate");
transactionID = tstObj.getString("transactionID");
transactionReferenceNumber = tstObj.getString("transactionReferenceNumber");
System.out.println("Account from = " +accountFrom);
System.out.println("Account to = " +accountTo);
System.out.println("Account to interim Balance = " +accountTo_interimBalance);
System.out.println("Currency = " +currency);
System.out.println("Exchange Rate = " +exchangeRate);
System.out.println("Interim Balance = " +interimBalance);
System.out.println("Narrative = " +narrative);
System.out.println("Office ID = " +officerID);
System.out.println("Payment Mode = " +paymentMode);
System.out.println("Quote Currency = " +quoteCurrency);
System.out.println("Transaction Amount = " +transactionAmount);
System.out.println("Transaction Branch = " +transactionBranch);
System.out.println("Transaction Date = " +transactionDate);
System.out.println("Transaction ID = " +transactionID);
System.out.println("Transaction Reference Number = " +transactionReferenceNumber);
transactionType = tstObj.getString("transactionType");
getTransactionTypes getTransactionTypesObj = new getTransactionTypes();
getTransactionTypesObj.getTransactionTypes(transactionType);
}
else
{
System.out.println("You have no transaction history.");
}
return;
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Easy to use tool to create HTML Help files and Help web sites