Get Customer Details
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "GetCustomerDetails".
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.
The method shown in the red box is a Reference Data API 'getCustomerTypes', 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 GetCustomerDetails
{
public static void main(String args[])
{
boolean verbose = true;
//api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
//response parameters
String serviceRespTag = "Customer_Details_ReadResponse";
String globalErrorID;
String errorText;
String errorDetails;
String customerID;
String dateOfBirth;
String familyName;
String givenName;
String address;
String city;
String country;
String postalCode;
String state;
String streetAddress1;
String streetAddress2;
String countryCode;
String phoneNumber;
String certificateExpiryDate;
String certificateIssuer;
String certificateNo;
String certificateType;
String lastMaintenanceTellerID;
String registrationDate;
String areaCode;
String localNumber;
String bankID;
String customerType;
String email;
String ethnicGroup;
String fax;
String gender;
String isBillingOrg;
String isMerchant;
String nationality;
String occupation;
String serviceName = "getCustomerDetails";
String userID = "bobsmith1";
String PIN = "123456";
String OTP = "123456";
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();
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 reponse object
boolean apiError;
JSONObject responseObj = new JSONObject(response);
responseObj = new JSONObject(response);
if (verbose)
{
System.out.println(responseObj.toString(4));
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
{
//parse {"customer"}
JSONObject CDMObject = serviceRespObj.getJSONObject("CDMCustomer");
JSONObject customerObj = CDMObject.getJSONObject("customer");
customerID = customerObj.getString("customerID");
System.out.println("Customer Type = " +customerID);
dateOfBirth = CDMObject.getString("dateOfBirth");
familyName = CDMObject.getString("familyName");
givenName = CDMObject.getString("givenName");
System.out.println("dateOfBirth= " +dateOfBirth);
System.out.println("familyName= " +familyName);
System.out.println("givenName= " +givenName);
JSONObject addressObj = CDMObject.getJSONObject("address");
city = addressObj.getString("city");
country = addressObj.getString("country");
postalCode = addressObj.getString("postalCode");
state = addressObj.getString("state");
streetAddress1 = addressObj.getString("streetAddress1");
streetAddress2 = addressObj.getString("streetAddress2");
System.out.println("city= " +city);
System.out.println("country= " +country);
System.out.println("postalCode= " +postalCode);
System.out.println("state= " +state);
System.out.println("streetAddress1= "+streetAddress1);
System.out.println("streetAddress2= "+streetAddress2);
System.out.println("");
JSONObject cellphoneObj = CDMObject.getJSONObject("cellphone");
countryCode = cellphoneObj.getString("countryCode");
phoneNumber = cellphoneObj.getString("phoneNumber");
System.out.println("countryCode= " +countryCode);
System.out.println("phoneNumber= " +phoneNumber);
System.out.println("");
JSONObject certificateObj = CDMObject.getJSONObject("certificate");
certificateExpiryDate = certificateObj.getString("certificateExpiryDate");
certificateIssuer = certificateObj.getString("certificateIssuer");
certificateNo = certificateObj.getString("certificateNo");
certificateType = certificateObj.getString("certificateType");
System.out.println("certificateExpiryDate= " +certificateExpiryDate);
System.out.println("certificateIssuer= " +certificateIssuer);
System.out.println("certificateNo= " +certificateNo);
System.out.println("certificateType= " +certificateType);
System.out.println("");
JSONObject maintenacehistoryObj = CDMObject.getJSONObject("maintenacehistory");
lastMaintenanceTellerID = maintenacehistoryObj.getString("lastMaintenanceTellerId");
registrationDate = maintenacehistoryObj.getString("registrationDate");
System.out.println("lastMaintenanceTellerID= " +lastMaintenanceTellerID);
System.out.println("registrationDate= " +registrationDate);
System.out.println("");
JSONObject phoneObj = CDMObject.getJSONObject("phone");
areaCode = phoneObj.getString("areaCode");
localNumber = phoneObj.getString("localNumber");
System.out.println("areaCode= " +areaCode);
System.out.println("localNumber= " +localNumber);
System.out.println("");
JSONObject profileObj = CDMObject.getJSONObject("profile");
bankID = profileObj.getString("bankID");
email = profileObj.getString("email");
ethnicGroup = profileObj.getString("ethnicGroup");
fax = profileObj.getString("fax");
gender = profileObj.getString("gender");
isBillingOrg = profileObj.getString("isBillingOrg");
isMerchant = profileObj.getString("isMerchant");
nationality = profileObj.getString("nationality");
occupation = profileObj.getString("occupation");
System.out.println("bankID= " +bankID);
customerType = profileObj.getString("customerType");
getCustomerTypes getCustomerTypesObj = new getCustomerTypes();
getCustomerTypesObj.getCustomerTypes(customerType);
System.out.println("email= " +email);
System.out.println("ethnicGroup= " +ethnicGroup);
System.out.println("fax= " +fax);
System.out.println("gender= " +gender);
System.out.println("isBillingOrg= " +isBillingOrg);
System.out.println("isMerchant= " +isMerchant);
System.out.println("nationality= " +nationality);
System.out.println("occupation= " +occupation);
return;
}
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
Created with the Personal Edition of HelpNDoc: Free EPub producer