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

com.payu.sdk.api.Reports Maven / Gradle / Ivy

The newest version!
package com.payu.sdk.api;

import com.payu.sdk.api.constants.Parameters;
import com.payu.sdk.api.exceptions.AuthenticationException;
import com.payu.sdk.api.exceptions.HttpException;
import com.payu.sdk.api.exceptions.InvalidParametersException;
import com.payu.sdk.api.exceptions.NetworkException;
import com.payu.sdk.api.exceptions.PayUException;
import com.payu.sdk.api.model.Order;
import com.payu.sdk.api.model.TransactionResponse;
import com.payu.sdk.api.model.report.ReportingRequest;
import com.payu.sdk.api.model.report.ReportingResponse;
import com.payu.sdk.api.model.report.ReportingResponseOrderList;
import com.payu.sdk.api.model.report.ReportingResponseTransaction;
import com.payu.sdk.api.model.request.Command;
import com.payu.sdk.api.model.request.Request;
import com.payu.sdk.api.model.response.Response;
import com.payu.sdk.api.model.response.ResponseCode;
import java.util.List;
import java.util.Map;

import static com.payu.sdk.api.util.CallbackUtils.checkCallback;
import static com.payu.sdk.api.util.ParametersUtils.validateParameters;
import static com.payu.sdk.api.util.ResponseUtils.validateResponse;

public final class Reports {

  private final RawApi rawApi;
  private final RequestCreator requestCreator;

  Reports(RawApi rawApi, RequestCreator requestCreator) {
    this.rawApi = rawApi;
    this.requestCreator = requestCreator;
  }

  void doPing(Callback callback) {
    checkCallback(callback);
    try {
      callback.success(doPing());
    } catch (HttpException e) {
      callback.httpError(e);
    } catch (AuthenticationException e) {
      callback.authenticationError(e);
    } catch (PayUException e) {
      callback.callApiError(e);
    } catch (NetworkException e) {
      callback.networkError(e);
    }
  }

  void doOrderDetailReportingById(Map parameters, Callback callback) {
    checkCallback(callback);
    try {
      callback.success(doOrderDetailReportingById(parameters));
    } catch (InvalidParametersException e) {
      callback.invalidParametersError(e);
    } catch (HttpException e) {
      callback.httpError(e);
    } catch (PayUException e) {
      callback.callApiError(e);
    } catch (AuthenticationException e) {
      callback.authenticationError(e);
    } catch (NetworkException e) {
      callback.networkError(e);
    }
  }

  void doOrderDetailReportingByReferenceCode(Map parameters,
      Callback> callback) {
    checkCallback(callback);
    try {
      callback.success(doOrderDetailReportingByReferenceCode(parameters));
    } catch (HttpException e) {
      callback.httpError(e);
    } catch (PayUException e) {
      callback.callApiError(e);
    } catch (InvalidParametersException e) {
      callback.invalidParametersError(e);
    } catch (AuthenticationException e) {
      callback.authenticationError(e);
    } catch (NetworkException e) {
      callback.networkError(e);
    }
  }

  void doTransactionResponseReporting(Map parameters,
      Callback callback) {
    checkCallback(callback);
    try {
      callback.success(doTransactionResponseReporting(parameters));
    } catch (InvalidParametersException e) {
      callback.invalidParametersError(e);
    } catch (AuthenticationException e) {
      callback.authenticationError(e);
    } catch (PayUException e) {
      callback.callApiError(e);
    } catch (HttpException e) {
      callback.httpError(e);
    } catch (NetworkException e) {
      callback.networkError(e);
    }
  }

  private boolean doPing()
      throws HttpException, PayUException, NetworkException, AuthenticationException {
    Request pingRequest = requestCreator.buildRequest(Command.PING);
    Response response = rawApi.reportServices().ping(pingRequest);
    validateResponse(response);
    return ResponseCode.SUCCESS.equals(response.getCode());
  }

  private Order doOrderDetailReportingById(Map parameters)
      throws AuthenticationException, PayUException, HttpException, InvalidParametersException,
      NetworkException {
    validateParameters(parameters, Parameters.ORDER_ID);
    ReportingRequest reportingRequest = requestCreator.buildReportingRequest(parameters);
    ReportingResponse reportingResponse =
        rawApi.reportServices().getOrderDetailById(reportingRequest);
    validateResponse(reportingResponse);
    return reportingResponse.getOrder();
  }

  private List doOrderDetailReportingByReferenceCode(Map parameters)
      throws AuthenticationException, PayUException, HttpException, InvalidParametersException,
      NetworkException {
    validateParameters(parameters, Parameters.REFERENCE_CODE);
    ReportingRequest reportingRequest =
        requestCreator.buildReportingRequestByReferenceCode(parameters);

    ReportingResponseOrderList reportingResponseOrderList =
        rawApi.reportServices().getOrderDetailByReferenceCode(reportingRequest);
    validateResponse(reportingResponseOrderList);
    return reportingResponseOrderList.getOrders();
  }

  private TransactionResponse doTransactionResponseReporting(Map parameters)
      throws InvalidParametersException, AuthenticationException, PayUException, HttpException,
      NetworkException {
    validateParameters(parameters, Parameters.TRANSACTION_ID);
    ReportingRequest reportingRequest =
        requestCreator.buildReportingRequestByTransactionResponse(parameters);
    ReportingResponseTransaction reportingResponseTransaction =
        rawApi.reportServices().getTransactionResponse(reportingRequest);
    validateResponse(reportingResponseTransaction);
    return reportingResponseTransaction.getTransactionResponse();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy