Get Deposit Accounts Details
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetDepositAccountsDetails".
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 display the result.
Entire Code:
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class GetDepositAccountsDetails
{
public static void main(String args[])
{
boolean verbose = true;
//api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "Account_Deposit_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String accountOpenDate;
String assignedAccountForAccountManagementFeeDeduction;
String balance;
String currency;
String currentStatus;
String customerID;
String homeBranch;
String interestRate;
String isServiceChargeWaived;
String maturityDate;
String narrative;
String officerID;
String penaltyRate;
String compoundInterestRateBasis;
String dateBasisForRate;
String productID;
String productName;
String rateChartCode;
String accountCloseDate;
String accrueInterestAmount;
String depositTerm;
String dueInterestAmount;
String interestPayoutAccount;
String isRestricted;
String minimumAmount;
String minorStatus;
String parentAccountFlag;
String lastMaintenanceOfficer;
String lastTransactionBranch;
String serviceName = "getDepositAccountDetails";
String userID = "bobsmith1";
String PIN = "123456";
String OTP = "123456";
String accountID = "1111";
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);
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(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 depositAccountObj = serviceRespObj.getJSONObject("DepositAccount");
balance = depositAccountObj.getString("balance");
System.out.println("balance="+balance);
// parse {"DepositAccount"}
depositAccountObj = serviceRespObj.getJSONObject("DepositAccount");
accountOpenDate = depositAccountObj.getString("accountOpenDate");
assignedAccountForAccountManagementFeeDeduction = depositAccountObj.getString("assignedAccountForAccountManagementFeeDeduction");
balance = depositAccountObj.getString("balance");
currency = depositAccountObj.getString("currency");;
currentStatus = depositAccountObj.getString("currentStatus");;
customerID = depositAccountObj.getString("customerID");;
homeBranch = depositAccountObj.getString("homeBranch");;
interestRate = depositAccountObj.getString("interestRate");;
isServiceChargeWaived = depositAccountObj.getString("isServiceChargeWaived");;
maturityDate = depositAccountObj.getString("maturityDate");
narrative = depositAccountObj.getString("narrative");
officerID = depositAccountObj.getString("officerID");
penaltyRate = depositAccountObj.getString("penaltyRate");
if (verbose){
System.out.println("accountOpenDate="+accountOpenDate);
System.out.println("assignedAccountForAccountManagementFeeDeduction="+assignedAccountForAccountManagementFeeDeduction);
System.out.println("balance="+balance);
System.out.println("currency="+currency);
System.out.println("currentStatus="+currentStatus);
System.out.println("customerID="+customerID);
System.out.println("homeBranch="+homeBranch);
System.out.println("interestRate="+interestRate);
System.out.println("isServiceChargeWaived="+isServiceChargeWaived);
System.out.println("maturityDate="+maturityDate);
System.out.println("narrative="+narrative);
System.out.println("officerID="+officerID);
System.out.println("penaltyRate="+penaltyRate);
System.out.println();
}
// parse {"product"}
JSONObject productObj = depositAccountObj.getJSONObject("product");
productID = productObj.getString("productID");
productName = productObj.getString("productName");
compoundInterestRateBasis = productObj.getString("compoundInterestRateBasis");
dateBasisForRate = productObj.getString("dateBasisForRate");
rateChartCode = productObj.getString("rateChartCode");
if (verbose){
System.out.println("compoundInterestRateBasis="+compoundInterestRateBasis);
System.out.println("dateBasisForRate="+dateBasisForRate);
System.out.println("productID="+productID);
System.out.println("productName="+productName);
System.out.println("rateChartCode="+rateChartCode);
System.out.println();
}
// parse {"casaaccount"}
JSONObject casaaccountObj = depositAccountObj.getJSONObject("casaaccount");
accountCloseDate = casaaccountObj.getString("accountCloseDate");
accrueInterestAmount = casaaccountObj.getString("accrueInterestAmount");
depositTerm = casaaccountObj.getString("depositTerm");
dueInterestAmount = casaaccountObj.getString("dueInterestAmount");
interestPayoutAccount = casaaccountObj.getString("interestPayoutAccount");
isRestricted = casaaccountObj.getString("isRestricted");
minimumAmount = casaaccountObj.getString("minimumAmount");
minorStatus = casaaccountObj.getString("minorStatus");
parentAccountFlag = casaaccountObj.getString("parentAccountFlag");
if (verbose){
System.out.println("accountCloseDate="+accountCloseDate);
System.out.println("accrueInterestAmount="+accrueInterestAmount);
System.out.println("depositTerm="+depositTerm);
System.out.println("dueInterestAmount="+dueInterestAmount);
System.out.println("interestPayoutAccount="+interestPayoutAccount);
System.out.println("isRestricted="+isRestricted);
System.out.println("minimumAmount="+minimumAmount);
System.out.println("minorStatus="+minorStatus);
System.out.println("parentAccountFlag="+parentAccountFlag);
System.out.println();
}
// parse {"maintenancehistory"}
JSONObject maintenancehistoryObj = depositAccountObj.getJSONObject("maintenancehistory");
lastMaintenanceOfficer = maintenancehistoryObj.getString("lastMaintenanceOfficer");
lastTransactionBranch = maintenancehistoryObj.getString("lastTransactionBranch");
if (verbose){
System.out.println("lastMaintenanceOfficer="+lastMaintenanceOfficer);
System.out.println("lastTransactionBranch="+lastTransactionBranch);
System.out.println();
}
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Full-featured Kindle eBooks generator