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

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

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

import com.payu.sdk.api.exceptions.InvalidParametersException;
import com.payu.sdk.api.model.BankListInformation;
import com.payu.sdk.api.model.Language;
import com.payu.sdk.api.model.Payer;
import com.payu.sdk.api.model.mobile.DeviceAuthorizationRequest;
import com.payu.sdk.api.model.mobile.DeviceIdentifierRequest;
import com.payu.sdk.api.model.mobile.DeviceIdentifiersItems;
import com.payu.sdk.api.model.payments.PaymentBanksRequest;
import com.payu.sdk.api.model.report.ReportingRequest;
import com.payu.sdk.api.model.request.Command;
import com.payu.sdk.api.model.request.Merchant;
import com.payu.sdk.api.model.request.Request;
import com.payu.sdk.api.util.ParametersUtils;
import java.util.HashMap;
import java.util.Map;

import static com.payu.sdk.api.constants.Parameters.ACCOUNT_ID;
import static com.payu.sdk.api.constants.Parameters.INSTALLATION_ID;
import static com.payu.sdk.api.constants.Parameters.ORDER_ID;
import static com.payu.sdk.api.constants.Parameters.PAYER_EMAIL;
import static com.payu.sdk.api.constants.Parameters.PAYER_NAME;
import static com.payu.sdk.api.constants.Parameters.PRIVATE_KEY;
import static com.payu.sdk.api.constants.Resources.PSE_CODE;
import static com.payu.sdk.api.model.CountryCode.CO;
import static com.payu.sdk.api.model.mobile.DeviceIdentifiersItems.MESSAGING_IDENTIFIER;
import static com.payu.sdk.api.model.request.Command.GET_BANKS_LIST;
import static com.payu.sdk.api.model.request.Command.ORDER_DETAIL;
import static com.payu.sdk.api.model.request.Command.ORDER_DETAIL_BY_REFERENCE_CODE;
import static com.payu.sdk.api.model.request.Command.TRANSACTION_RESPONSE_DETAIL;
import static com.payu.sdk.api.util.ParametersUtils.getParameter;
import static com.payu.sdk.base.Preconditions.checkArg;

final class RequestCreator {

  private final String apiKey;
  private final String apiLogin;
  private final String applicationFingerprint;
  private final String applicationPackage;
  private Language language = Language.en;
  private boolean isTest = false;

  RequestCreator(String apiKey, String apiLogin, String applicationFingerprint,
      String applicationPackage) {
    this.apiKey = apiKey;
    this.apiLogin = apiLogin;
    this.applicationFingerprint = applicationFingerprint;
    this.applicationPackage = applicationPackage;
  }

  public RequestCreator isTest(boolean isTest) {
    this.isTest = isTest;
    return this;
  }

  public RequestCreator language(Language language) {
    checkArg(language, "Language should not be null");
    this.language = language;
    return this;
  }

  public Request buildRequest(Command command) {
    Request basicRequest = new Request();
    buildDefaultRequest(basicRequest);
    basicRequest.setCommand(command);
    return basicRequest;
  }

  private void buildDefaultRequest(Request request) {
    request.setLanguage(language);
    request.setTest(isTest);
    request.setMerchant(new Merchant(apiLogin, apiKey));
  }

  public ReportingRequest buildReportingRequest(Map parameters)
      throws InvalidParametersException {
    Integer orderId = ParametersUtils.getIntegerParameter(parameters, ORDER_ID);
    Map details = new HashMap<>();
    details.put(ORDER_ID, orderId);
    return buildDefaultReportingRequest(ORDER_DETAIL, details);
  }

  public ReportingRequest buildReportingRequestByReferenceCode(Map parameters) {
    return buildDefaultReportingRequest(ORDER_DETAIL_BY_REFERENCE_CODE,
        new HashMap(parameters));
  }

  public ReportingRequest buildReportingRequestByTransactionResponse(
      Map parameters) {
    return buildDefaultReportingRequest(TRANSACTION_RESPONSE_DETAIL,
        new HashMap(parameters));
  }

  public PaymentBanksRequest buildPaymentBankRequestByPSE() {
    PaymentBanksRequest paymentBanksRequest = new PaymentBanksRequest();
    buildDefaultRequest(paymentBanksRequest);
    paymentBanksRequest.setCommand(GET_BANKS_LIST);
    paymentBanksRequest.setBankListInformation(new BankListInformation(PSE_CODE, CO));
    return paymentBanksRequest;
  }

  private ReportingRequest buildDefaultReportingRequest(Command command,
      Map details) {
    ReportingRequest reportingRequest = new ReportingRequest();
    buildDefaultRequest(reportingRequest);
    reportingRequest.setCommand(command);
    reportingRequest.setDetails(details);
    return reportingRequest;
  }

  public DeviceIdentifierRequest buildDeviceIdentifierRequest(Map parameters) {
    DeviceIdentifierRequest deviceIdentifierRequest = new DeviceAuthorizationRequest();
    completeDeviceIdentifier(deviceIdentifierRequest, parameters);
    return deviceIdentifierRequest;
  }

  public DeviceAuthorizationRequest buildDeviceAuthorizationRequest(
      Map parameters) {
    DeviceAuthorizationRequest deviceAuthorization = new DeviceAuthorizationRequest();
    completeDeviceAuthorization(deviceAuthorization, parameters);
    return deviceAuthorization;
  }

  private void completeDeviceAuthorization(DeviceAuthorizationRequest deviceAuthorization,
      Map parameters) {
    String installationId = getParameter(parameters, INSTALLATION_ID);
    String privateKey = getParameter(parameters, PRIVATE_KEY);

    deviceAuthorization.setInstallationId(installationId);
    deviceAuthorization.setPrivateKey(privateKey);

    completeDeviceIdentifier(deviceAuthorization, parameters);
  }

  private void completeDeviceIdentifier(DeviceIdentifierRequest deviceIdentifier,
      Map parameters) {
    deviceIdentifier.setApplicationFingerprint(applicationFingerprint);
    deviceIdentifier.setApplicationPackage(applicationPackage);

    String messagingIdentifier = getParameter(parameters, MESSAGING_IDENTIFIER.name());
    String payerEmail = getParameter(parameters, PAYER_EMAIL);
    String payerName = getParameter(parameters, PAYER_NAME);

    String accountId = getParameter(parameters, ACCOUNT_ID);

    Payer payer = new Payer();
    payer.setEmailAddress(payerEmail);
    payer.setFullName(payerName);

    deviceIdentifier.setPayer(payer);
    deviceIdentifier.setMessagingIdentifier(messagingIdentifier);

    if (accountId != null) {
      deviceIdentifier.setAccountId(Integer.valueOf(accountId));
    }

    Map deviceIdentifiers = new HashMap<>();
    for (DeviceIdentifiersItems key : DeviceIdentifiersItems.values()) {
      deviceIdentifiers.put(key, parameters.get(key.name()));
    }

    deviceIdentifier.setDeviceIdentifiers(deviceIdentifiers);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy