All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.smarttransaction.network.SmartTransactionService Maven / Gradle / Ivy

package com.smarttransaction.network;




import java.util.concurrent.TimeUnit;

import com.smarttransaction.model.SmartTransactionGenericRequest;
import com.smarttransaction.model.SmartTransactionGenericResponse;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

/**
 * user : Omm
 * date : 8/29/2016
 * description : this class used for Creating a Singleton object of SmartTransactionService class and handle
 * all Retrofit Request and Response.
 */
public class SmartTransactionService {
    private static SmartTransactionService sDataBuilder;
    private static SmartTransactionNetworkService mSmartTransactionNetworkService;
    private static ResponseListener sResponseListener;
    public static final int TIMEOUT = 30;


    private SmartTransactionService(String url, int timeOut) {
        mSmartTransactionNetworkService = initDataServices(url, timeOut);
    }


    /*user : Omm
   * date : 8/23/2016
   * description : returns SmartTransactionService instance if it is null.
   *
   */
    public synchronized static SmartTransactionService instanceOf(ResponseListener listener, String url,int timeOut) {

        sResponseListener = listener;
        if (sDataBuilder == null) {
            sDataBuilder = new SmartTransactionService(url,timeOut);
        }
        if(mSmartTransactionNetworkService==null)
            return null;

        return sDataBuilder;
    }


    /*user : Omm
     * date : 8/23/2016
     * description : returns SmartTransactionService instance if it is null.
     *
     */
    public synchronized static SmartTransactionService instanceOf(ResponseListener listener, String url) {

        sResponseListener = listener;
        if (sDataBuilder == null) {
            sDataBuilder = new SmartTransactionService(url,0);
        }else{
            mSmartTransactionNetworkService = null;
            initDataServices(url, 0);
        }
        if(mSmartTransactionNetworkService==null)
            return null;

        return sDataBuilder;
    }



   /*user : Omm
    * date : 8/23/2016
    * description : returns SmartTransactionService instance if it is null.
     *
     */

    public synchronized static SmartTransactionService instanceOf(ResponseListener listener) {
        sResponseListener = listener;
        if (sDataBuilder == null) {
            sDataBuilder = new SmartTransactionService(SmartTransactionNetworkConstant.BASE_URL,0);
        }
        return sDataBuilder;
    }


    /*user : Omm
    * date : 9/2/2016
    * description : set timeout for HttpRequest which are used by Retrofit object.
    *
    */
    /*public void setTimeOut(int timeout) {
        this.timeout = timeout;
    }*/


    /*user : Omm
    * date : 9/2/2016
    * description : return timeout for HttpRequest which are used by Retrofit object.
    *
    */
    /*public int getTimeOut() {
        return timeout;
    }
   */

    /*
     *user : Omm
    * date : 8/24/2016
    * description :  initialise retrofit  and initialize using base url a SimpleXmlConverterFactory
     * and return data services to use web services methods
    * @return SmartTransactionNetworkService which is using to call method of different Web services
    */
    private static SmartTransactionNetworkService initDataServices(String url, int timeOut) {

        try {
              Retrofit client = new Retrofit.Builder()
                        .baseUrl( ((url != null ) && !url.isEmpty()) ? url : SmartTransactionNetworkConstant.BASE_URL)
                        .addConverterFactory(SimpleXmlConverterFactory.create())
                        .client(getRequestHeader(timeOut))
                        .build();
                mSmartTransactionNetworkService = client.create(SmartTransactionNetworkService.class);

        } catch (Exception ex) {
           // Log.e("In Retrofit Builder", "Exception is:- " + ex);
            final SmartTransactionResponse genericResponse = new SmartTransactionResponse();
            final SmartTransactionResponseStatus responseStatus = new SmartTransactionResponseStatus();

            responseStatus.setErrorCode(1);
            responseStatus.setStatusCode(404);
            responseStatus.setMessage(SmartTransactionNetworkConstant.URL_NOT_VALID);
            genericResponse.setData(null);
            genericResponse.setStatus(responseStatus);
            if (sResponseListener != null) {
                sResponseListener.onFailedResponse(genericResponse);
            }
        }
        return mSmartTransactionNetworkService;
    }

    /**
     * user : Omm
     * date : 8/31/2016
     * description : this method return OkHttpClient object with default timeout.
     *
     * @return Object of OkHttpClient.
     */

    private static OkHttpClient getRequestHeader(int timeOut) {
        OkHttpClient httpClient = new OkHttpClient();
         int timeout;
        try {
            if (timeOut == 0) {
                timeout = SmartTransactionService.TIMEOUT;
            } else {
                timeout = timeOut;
            }
            httpClient.newBuilder().connectTimeout(timeout, TimeUnit.SECONDS)
                    .readTimeout(timeout, TimeUnit.SECONDS).build();
        } catch (Exception ex) {
         //   Log.e("In OkHttpClient", "Exception is:- " + ex);
        }
        return httpClient;
    }


    /*
     * user : Omm
     * date : 8/26/2016
     * description : return response of corresponding request.
   */
    public String setRequest(SmartTransactionGenericRequest genericRequest) {
        try {
            if (genericRequest != null) {
                String merchantNumber = genericRequest.getMerchantNumber();
                String terminalID = genericRequest.getTerminalID();
                String businessType = genericRequest.getBusinessType();
                String actionCode = genericRequest.getActionCode();
                String transType = genericRequest.getTransType();
                String cardNumber = genericRequest.getCardNumber();
                // double transactionAmount =  addCashRequest.getTransactionAmount();
                if (merchantNumber == null || merchantNumber.isEmpty() || terminalID == null || terminalID.isEmpty()
                        || businessType == null || businessType.isEmpty() ||
                        actionCode == null || actionCode.isEmpty() || transType == null || transType.isEmpty() || cardNumber == null || cardNumber.isEmpty()) {

                    if (merchantNumber == null || merchantNumber.isEmpty()){
                        return SmartTransactionNetworkConstant.MERCHANT_ERROR_MSG;
                    }
                    if(terminalID == null || terminalID.isEmpty()){
                        return SmartTransactionNetworkConstant.TERMINAL_ID_ERROR_MSG;
                    }
                    if( businessType == null || businessType.isEmpty() ) {
                        return SmartTransactionNetworkConstant.BUSINESS_TYPE_ERROR_MSG;
                    }
                    if(actionCode == null || actionCode.isEmpty()) {
                        return SmartTransactionNetworkConstant.ACTION_CODE_ERROR_MSG;
                    }
                    if(transType == null || transType.isEmpty() ) {
                        return SmartTransactionNetworkConstant.TRANS_TYPE_ERROR_MSG;
                    }
                    if(cardNumber == null || cardNumber.isEmpty()) {
                        return SmartTransactionNetworkConstant.CARD_NUMBER_ERROR_MSG;
                    }

                } else {
                    final SmartTransactionResponse genericResponse = new SmartTransactionResponse();
                    final SmartTransactionResponseStatus responseStatus = new SmartTransactionResponseStatus();
                    genericRequest = setRequestParameters(genericRequest);
                    if(mSmartTransactionNetworkService!=null) {
                        Call call = mSmartTransactionNetworkService.getGenericResponse(genericRequest);
                        call.enqueue(new Callback() {
                            @Override
                            public void onResponse(Call call, Response response) {
                                try {
                                    //   tv.setText("Amt "+response.body().getAmountBalance());
                                    if (response.body().getResponseCode() != null && response.body().getResponseCode().equals("00")) {
                                        responseStatus.setErrorCode(0);
                                        responseStatus.setStatusCode(0);
                                        responseStatus.setMessage(SmartTransactionNetworkConstant.SUCCESS_MSG);
                                        genericResponse.setData(response.body());
                                        genericResponse.setStatus(responseStatus);
                                        if (sResponseListener != null) {
                                            sResponseListener.onSuccessResponse(genericResponse);
                                        }
                                    } else {
                                        responseStatus.setErrorCode(1);
                                        responseStatus.setStatusCode(1);
                                        responseStatus.setMessage(SmartTransactionNetworkConstant.ERROR_MSG);
                                        genericResponse.setData(response.body());
                                        genericResponse.setStatus(responseStatus);
                                        if (sResponseListener != null) {
                                            sResponseListener.onFailedResponse(genericResponse);
                                        }
                                    }


                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }

                            @Override
                            public void onFailure(Call call, Throwable t) {

                                responseStatus.setErrorCode(1);
                                responseStatus.setStatusCode(404);
                                responseStatus.setMessage(t.getMessage());
                                genericResponse.setData(null);
                                genericResponse.setStatus(responseStatus);
                                if (sResponseListener != null) {
                                    sResponseListener.onFailedResponse(genericResponse);
                                }
                            }


                        });
                    }
                }
            }
        } catch (Exception ex) {
           // Log.e("In SetRequest Method", "Exception is:- " + ex);
        }

        return SmartTransactionNetworkConstant.EMPTY;
    }


    /*
      user : Omm
    * date : 9/5/2016
    * description : this method set default  request parameter which not provided by user which
     * is empty and return complete request.
   */
    private SmartTransactionGenericRequest setRequestParameters(SmartTransactionGenericRequest genericRequest) {
        SmartTransactionGenericRequest localGenericRequest = new SmartTransactionGenericRequest();
        localGenericRequest.setMerchantNumber(genericRequest.getMerchantNumber());
        localGenericRequest.setTerminalID(genericRequest.getTerminalID());
        localGenericRequest.setBusinessType(genericRequest.getBusinessType());
        localGenericRequest.setActionCode(genericRequest.getActionCode());
        localGenericRequest.setTransType(genericRequest.getTransType());
        localGenericRequest.setCardNumber(genericRequest.getCardNumber());
        localGenericRequest.setTransactionAmount((genericRequest.getTransactionAmount() != null) ? genericRequest.getTransactionAmount() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setAddr1((genericRequest.getAddr1() != null) ? genericRequest.getAddr1() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setAddr2((genericRequest.getAddr2() != null) ? genericRequest.getAddr2() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setAddr3((genericRequest.getAddr3() != null) ? genericRequest.getAddr3() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setBirthDay((genericRequest.getBirthDay() != null) ? genericRequest.getBirthDay() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setCity((genericRequest.getCity() != null) ? genericRequest.getCity() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setComments((genericRequest.getComments() != null) ? genericRequest.getComments() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setEmailAddr((genericRequest.getEmailAddr() != null) ? genericRequest.getEmailAddr() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setExpirationDate((genericRequest.getExpirationDate() != null) ? genericRequest.getExpirationDate() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setFirstName((genericRequest.getFirstName() != null) ? genericRequest.getFirstName() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setMiddleName((genericRequest.getMiddleName() != null) ? genericRequest.getMiddleName() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setLastName((genericRequest.getLastName() != null) ? genericRequest.getLastName() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setOldCardNumber((genericRequest.getOldCardNumber() != null) ? genericRequest.getOldCardNumber() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setPhone1((genericRequest.getPhone1() != null) ? genericRequest.getPhone1() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setPhone2((genericRequest.getPhone2() != null) ? genericRequest.getPhone2() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setPosEntryMode((genericRequest.getPosEntryMode() != null) ? genericRequest.getPosEntryMode() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setState((genericRequest.getState() != null) ? genericRequest.getState() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTicket((genericRequest.getTicket() != null) ? genericRequest.getTicket() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTotalDate((genericRequest.getTotalDate() != null) ? genericRequest.getTotalDate() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTrackData1((genericRequest.getTrackData1() != null) ? genericRequest.getTrackData1() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTrackData2((genericRequest.getTrackData2() != null) ? genericRequest.getTrackData2() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTransactionID((genericRequest.getTransactionID() != null) ? genericRequest.getTransactionID() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setZip((genericRequest.getZip() != null) ? genericRequest.getZip() : SmartTransactionNetworkConstant.EMPTY);
       // localGenericRequest.setPointsAmount((genericRequest.getPointsAmount() != null) ? genericRequest.getPointsAmount() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setPin((genericRequest.getPin() != null) ? genericRequest.getPin() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTableNumber((genericRequest.getTableNumber() != null) ? genericRequest.getTableNumber() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setPassword((genericRequest.getPassword() != null) ? genericRequest.getPassword() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setAuthReference((genericRequest.getAuthReference() != null) ? genericRequest.getAuthReference() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setClerkID((genericRequest.getClerkID() != null) ? genericRequest.getClerkID() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setServerID((genericRequest.getServerID() != null) ? genericRequest.getServerID() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setTipAmount((genericRequest.getTipAmount() != null) ? genericRequest.getTipAmount() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setSendTo((genericRequest.getSendTo() != null) ? genericRequest.getSendTo() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setSenderName((genericRequest.getSenderName() != null) ? genericRequest.getSenderName() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setRecipientName((genericRequest.getRecipientName() != null) ? genericRequest.getRecipientName() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setSenderMsg((genericRequest.getSenderMsg() != null) ? genericRequest.getSenderMsg() : SmartTransactionNetworkConstant.EMPTY);
        localGenericRequest.setSendOn((genericRequest.getSendOn() != null) ? genericRequest.getSendOn() : SmartTransactionNetworkConstant.EMPTY);

        return localGenericRequest;
    }


    /*user : Omm
    * date : 8/26/2016
    * description : this interface is a callback interface wich is implements by client
    */

    public interface ResponseListener {

        // this method is a call back and  call on suceess response of Retrofit.
        void onSuccessResponse(T t);

        /**
         * on failing of web services a string error message to pass
         *
         * @param t T type  which is pass on fail of web services
         */
        void onFailedResponse(T t);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy