Java

Features ›› Wealth ›› getDCDAccountDetails ›› Sample Code ››
Parent Previous Next

Get DCD Account Details


We will first import the "java.io", "java.util", "java.net" and "org.json" packages into the solution.




Next, we create a public class "GetDCDAccountDetails".

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 the 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:

// GetDCDAccountDetails.java


import java.io.*;

import java.util.*;

import java.net.*;

import org.json.*;


public class GetDCDAccountDetails {


       public static void main(String args[])

       {

               boolean verbose = true;

       

               // api url

               String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";

               

               //response parameters

               String serviceRespTag = "DCD_Account_Details_ReadResponse";

               String globalErrorID;

               String errorText;

               String errorDetails;

               String accountID = "5068";

               String serviceName = "getDCDAccountDetails";

               String userID = "alan";

               String PIN = "987654";

               String OTP = "987654";

               String accountID2;

               String bankID;

               String baseCurrency;

               String channel;

               String customerID;

               String depositAmount;

               String depositTerm;

               String feeDeductionAccount;

               String interestPayoutAccount;

               String interestRate;

               String maturityDate;

               String narrative;

               String penaltyRate;

               String productID;

               String quoteCurrency;

               String settlementAccount;

               String spotRate;

               String startDate;

               String status;

               String strikeRate;


               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(parameters);

                               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 DCDADObj = serviceRespObj.getJSONObject("DCDAccountDetails");

                               accountID2 = DCDADObj.getString("accountID");

                               bankID = DCDADObj.getString("bankID");

                               baseCurrency = DCDADObj.getString("baseCurrency");

                               channel = DCDADObj.getString("channel");

                               customerID = DCDADObj.getString("customerID");

                               depositAmount = DCDADObj.getString("depositAmount");

                               depositTerm = DCDADObj.getString("depositTerm");

                               feeDeductionAccount = DCDADObj.getString("feeDeductionAccount");

                               interestPayoutAccount = DCDADObj.getString("interestPayoutAccount");

                               interestRate = DCDADObj.getString("interestRate");

                               maturityDate = DCDADObj.getString("maturityDate");

                               narrative = DCDADObj.getString("narrative");

                               penaltyRate = DCDADObj.getString("penaltyRate");

                               productID = DCDADObj.getString("productID");

                               quoteCurrency = DCDADObj.getString("quoteCurrency");

                               settlementAccount = DCDADObj.getString("settlementAccount");

                               spotRate = DCDADObj.getString("spotRate");

                               startDate = DCDADObj.getString("startDate");

                               status = DCDADObj.getString("status");

                               strikeRate = DCDADObj.getString("strikeRate");

                               

                               

                               System.out.println("Account ID = " +accountID2);

                               System.out.println("Bank ID = " +bankID);

                               System.out.println("Base Currency = " +baseCurrency);

                               System.out.println("Channel = " +channel);

                               System.out.println("Customer ID  = " +customerID);

                               System.out.println("Deposit Amount = " +depositAmount);

                               System.out.println("Deposit Term = " +depositTerm);

                               System.out.println("Fee Deduction Account = " +feeDeductionAccount);

                               System.out.println("Interest Payout Account = " +interestPayoutAccount);

                               System.out.println("Interest Rate  = " +interestRate);

                               System.out.println("Maturity Date = " +maturityDate);

                               System.out.println("Narrative = " +narrative);

                               System.out.println("Penalty Rate = " +penaltyRate);

                               System.out.println("Product ID = " +productID);

                               System.out.println("Quote Currency  = " +quoteCurrency);

                               System.out.println("Settlement Account = " +settlementAccount);

                               System.out.println("Spot Rate = " +spotRate);

                               System.out.println("Start Date = " +startDate);

                               System.out.println("Status = " +status);

                               System.out.println("Strike Rate  = " +strikeRate);

                               

                               return;

                       }

               }

               catch(Exception e)

               {

                       e.printStackTrace(System.out);

               }

       }

}


Download

Created with the Personal Edition of HelpNDoc: Full-featured Kindle eBooks generator