Get Direct Debit Authorization List
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetDirectDebitAuthorizationList".
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 "Authorization" object is an JSONArray or JSONObject, and display the results accordingly.
The method shown in the red box is a Reference Data API 'getBillingOrganizations', 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 GetDirectDebitAuthorizationList
{
public static void main(String args[])
{
boolean verbose = true;
//api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "Direct_Debit_Authorization_List_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String billingOrgAccountID;
String creationDate;
String directDebitAuthorizationID;
String billingOrgID;
String customerAccountID;
String serviceName = "getDirectDebitAuthorizationList";
String userID = "bobsmith1";
String PIN = "123456";
String OTP = "123456";
try
{
//building of the 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();
//building of the content
jo = new JSONObject();
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
//Connect to API serviceName
HttpURLConnection urlConnection = (HttpURLConnection) new URL(apiServiceUrl).openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
//build request parameters
String parameters = "Header=" +header+ "&" + "Content=" +content;
//sending of the request
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
br.write(parameters);
br.close();
// getting the response
String response = "";
Scanner s = new Scanner(urlConnection.getInputStream());
while (s.hasNextLine())
{
response += s.nextLine();
}
s.close();
//getting the 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();
}
//parsing of content
contentObj = responseObj.getJSONObject("Content");
//parsing the service Response
JSONObject serviceRespObj = contentObj.getJSONObject("ServiceResponse");
// parsing the service response header
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 authorizationObj = serviceRespObj.getJSONObject("AuthorizationList");
Object testObj = authorizationObj.get("Authorization");
if (testObj instanceof JSONArray)
{
JSONArray athObj = authorizationObj.getJSONArray("Authorization");
for (int i = 0; i < athObj.length(); i++)
{
JSONObject athobj = athObj.getJSONObject(i);
billingOrgAccountID = athobj.getString("BillingOrgAccountID");
creationDate = athobj.getString("CreationDate");
directDebitAuthorizationID = athobj.getString("DirectDebitAuthorizationID");
billingOrgID = athobj.getString("BillingOrgID");
customerAccountID = athobj.getString("CustomerAccountID");
System.out.println("Billing Organization Account ID = "+billingOrgAccountID);
System.out.println("Creation Date = " +creationDate);
System.out.println("Direct Debit Authorization ID = " +directDebitAuthorizationID);
String billingOrgName = athobj.getString("BillingOrgID");
getBillingOrganizations getBillingOrganizationsObj = new getBillingOrganizations();
getBillingOrganizationsObj.getBillingOrganizations(billingOrgName);
System.out.println("Billing Organization ID = " +billingOrgID);
System.out.println("Customer Account ID = " +customerAccountID);
System.out.println("");
}
}
else if(testObj instanceof JSONObject)
{
//parse accounts
JSONObject athObj = authorizationObj.getJSONObject("Authorization");
billingOrgAccountID = athObj.getString("BillingOrgAccountID");
creationDate = athObj.getString("CreationDate");
directDebitAuthorizationID = athObj.getString("DirectDebitAuthorizationID");
billingOrgID = athObj.getString("BillingOrgID");
customerAccountID = athObj.getString("CustomerAccountID");
System.out.println("Billing Organization Account ID = "+billingOrgAccountID);
System.out.println("Creation Date = " +creationDate);
System.out.println("Direct Debit Authorization ID = " +directDebitAuthorizationID);
String billingOrgName = athObj.getString("BillingOrgID");
getBillingOrganizations getBillingOrganizationsObj = new getBillingOrganizations();
getBillingOrganizationsObj.getBillingOrganizations(billingOrgName);
System.out.println("Billing Organization ID = " +billingOrgID);
System.out.println("Customer Account ID = " +customerAccountID);
}
else
{
System.out.println("Customer has no accounts.");
}
return;
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Easily create Qt Help files