On Board Customer
We will first import the "java.io", "java.util", "java.net" and "org.json" packages.
Next, we create a public class "OnBoardCustomer".
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:
// OnboardCustomer.java
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.*;
public class OnboardCustomer {
public static void main(String args[]){
// api url
String apiServiceUrl = "http://tbankonline.com/SMUtBank_API/Gateway";
boolean verbose = true;
// response parameters
String globalErrorID;
String errorText;
String errorDetails;
String AccountID;
String CustomerID;
String PIN;
String serviceName = "onboardCustomer"
String IC_number = "T2345678C";
String familyName = "Megargel";
String givenName = "Alan";
String dateOfBirth = "01-12-00";
String gender = "Male";
String occupation = "Professor";
String city = "Singapore";
String state = "Singapore";
String country = "Singapore";
String postalCode = "670123";
String streetAddress = "BLK 888, Woodlands St. 81, #01-234
String emailAddress = "bob@gmail.com";
String countryCode = "+65";
String preferredUserID = "bob123";
String Currency = "SGD";
String bankID = "1";
try {
// build header
JSONObject jo = new JSONObject();
jo.put("serviceName", "onboardCustomer");
jo.put("userID", "");
jo.put("PIN", "");
jo.put("OTP", "");
JSONObject headerObj = new JSONObject();
headerObj.put("Header", jo);
String header = headerObj.toString();
// build content
jo = new JSONObject();
jo.put("IC_number", IC_number); // this must be unique, otherwise "duplicate" error.
jo.put("familyName", familyName);
jo.put("givenName", givenName);
jo.put("dateOfBirth", dateOfBirth);
jo.put("gender", gender);
jo.put("occupation", occupation);
jo.put("streetAddress", streetAddress);
jo.put("city", city);
jo.put("state", state);
jo.put("country", country);
jo.put("postalCode", postalCode);
jo.put("emailAddress", emailAddress);
jo.put("countryCode", countryCode);
jo.put("mobileNumber", mobileNumber);
jo.put("preferredUserID", preferredUserID);
jo.put("currency", currency);
jo.put("bankID", bankID);
JSONObject contentObj = new JSONObject();
contentObj.put("Content", jo);
String content = contentObj.toString();
content = URLEncoder.encode(content, StandardCharsets.UTF_8.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 CustomerDetailsObj = serviceRespObj.getJSONObject("CustomerDetails");
AccountID = CustomerDetailsObj.getString("AccountID");
CustomerID = CustomerDetailsObj.getString("CustomerID");
PIN = CustomerDetailsObj.getString("PIN");
System.out.println("AccountID="+AccountID);
System.out.println("CustomerID="+CustomerID);
System.out.println("PIN="+PIN);
}
}
catch(Exception e) {e.printStackTrace(System.out);}
}
}
Created with the Personal Edition of HelpNDoc: Single source CHM, PDF, DOC and HTML Help creation