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

com.yodlee.sdk.api.validators.VerificationValidator Maven / Gradle / Ivy

There is a newer version: 1.0.29.beta1
Show newest version
/**
 * Copyright (c) 2019 Yodlee, Inc. All Rights Reserved.
 *
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
package com.yodlee.sdk.api.validators;

import java.util.ArrayList;
import java.util.List;
import com.yodlee.api.model.account.enums.BankTransferCodeType;
import com.yodlee.api.model.account.enums.ItemAccountStatus;
import com.yodlee.api.model.enums.Container;
import com.yodlee.api.model.validator.Problem;
import com.yodlee.api.model.verification.MatchingVerification;
import com.yodlee.api.model.verification.UpdateVerification;
import com.yodlee.api.model.verification.Verification;
import com.yodlee.api.model.verification.VerificationAccount;
import com.yodlee.api.model.verification.VerificationBankTransferCode;
import com.yodlee.api.model.verification.VerificationTransaction;
import com.yodlee.api.model.verification.enums.VerificationType;
import com.yodlee.api.model.verification.enums.VerifiedAccountsVerificationStatus;
import com.yodlee.api.model.verification.request.UpdateVerificationRequest;
import com.yodlee.api.model.verification.request.VerificationMatchingRequest;
import com.yodlee.api.model.verification.request.VerificationRequest;
import com.yodlee.sdk.api.AccountsApi;
import com.yodlee.sdk.api.VerificationApi;
import com.yodlee.sdk.api.exception.ApiException;
import com.yodlee.sdk.api.util.ApiUtils;
import com.yodlee.sdk.api.validators.AccountsValidator.IncludeParameterValue;

public class VerificationValidator {

	private VerificationValidator() {}

	private static final String VERIFICATIONS_VERIFICATION_TYPE_INVALID = "verifications.verificationType.invalid";
	
	private static final String PARAM_TRUE = "true";
	
	private static final String PARAM_FALSE = "false";

	public static void validateGetVerificationDetails(VerificationApi verificationApi, String methodName,
			Long[] providerAccountId, Long[] accountId, VerificationType verificationType) throws ApiException {
		Class[] argTypes = new Class[] {Long[].class, Long[].class, VerificationType.class};
		Object[] argValues = new Object[] {providerAccountId, accountId, verificationType};
		List methodProblems = ApiValidator.validate(verificationApi, methodName, argTypes, argValues);
		List verificationInputProblems = validateVerificationInput(providerAccountId, accountId);
		List contextProblems = ApiValidator.validateUserContext(verificationApi);
		methodProblems.addAll(ApiValidator.validateId(accountId, "verifications.param.accountId.invalid"));
		methodProblems
				.addAll(ApiValidator.validateId(providerAccountId, "verifications.param.providerAccountId.invalid"));
		ApiValidator.collectProblems(methodProblems, contextProblems, verificationInputProblems);
	}

	private static List validateVerificationInput(Long[] providerAccountId, Long[] accountId) {
		List problems = new ArrayList<>();
		if ((providerAccountId != null && providerAccountId.length > 0)
				|| (accountId != null && accountId.length > 0)) {
			return problems;
		}
		problems.add(new Problem(ApiUtils.getErrorMessage("verifications.param.id.required"), ""));
		return problems;
	}

	private static List validateInput(Long providerAccountId, Long accountId) {
		List problems = new ArrayList<>();
		if (providerAccountId == null && accountId == null) {
			problems.add(new Problem(ApiUtils.getErrorMessage("verifications.param.id.required"), ""));
		}
		return problems;
	}

	public static void validateInitiateCDVerification(VerificationApi verificationApi, String methodName,
			VerificationRequest verificationParam) throws ApiException {
		Class[] argTypes = new Class[] {VerificationRequest.class};
		Object[] argValues = new Object[] {verificationParam};
		List methodProblems = ApiValidator.validate(verificationApi, methodName, argTypes, argValues);
		List modelProblems = ApiValidator.validate(verificationParam);
		List contextProblems = ApiValidator.validateUserContext(verificationApi);
		List cdvInputProblems = validateCDVInput(verificationParam);
		ApiValidator.collectProblems(methodProblems, modelProblems, contextProblems, cdvInputProblems);
	}

	public static void validateMatchingVerification(VerificationApi verificationApi, String methodName,
			VerificationMatchingRequest verificationParam) throws ApiException {
		Class[] argTypes = new Class[] {VerificationMatchingRequest.class};
		Object[] argValues = new Object[] {verificationParam};
		List methodProblems = ApiValidator.validate(verificationApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(verificationApi);
		List modelProblems = ApiValidator.validate(verificationParam);
		List matchingInputProblems = validateMatchingInput(verificationParam);
		ApiValidator.collectProblems(methodProblems, modelProblems, contextProblems, matchingInputProblems);
	}

	private static List validateCDVInput(VerificationRequest verificationParam) {
		List problems = new ArrayList<>();
		Verification verification = verificationParam.getVerification();
		VerificationType verType = verification.getVerificationType();
		if (!verType.equals(VerificationType.CHALLENGE_DEPOSIT)) {
			problems.add(new Problem(ApiUtils.getErrorMessage(VERIFICATIONS_VERIFICATION_TYPE_INVALID), ""));
		} else {
			VerificationAccount accountVo = verification.getAccount();
			if (accountVo == null) {
				problems.add(new Problem(ApiUtils.getErrorMessage("verifications.account.details.required"), ""));
			} else {
				VerificationBankTransferCode bankTransferCodeVo = accountVo.getBankTransferCode();
				if (bankTransferCodeVo != null && bankTransferCodeVo.getType() != BankTransferCodeType.ROUTING_NUMBER) {
					problems.add(
							new Problem(ApiUtils.getErrorMessage("verifications.banktransfercode.type.invalid"), ""));
				}
			}
		}
		return problems;
	}

	private static List validateMatchingInput(VerificationMatchingRequest verificationParam) {
		List problems = new ArrayList<>();
		MatchingVerification verification = verificationParam.getVerification();
		VerificationType verType = verification.getVerificationType();
		if (!verType.equals(VerificationType.MATCHING)) {
			problems.add(new Problem(ApiUtils.getErrorMessage(VERIFICATIONS_VERIFICATION_TYPE_INVALID), ""));
		} else {
			Long accountId = verification.getAccountId();
			Long providerAccountId = verification.getProviderAccountId();
			problems.addAll(validateInput(providerAccountId, accountId));
		}
		return problems;
	}

	public static void validatePerformCDVerification(VerificationApi verificationApi, String methodName,
			UpdateVerificationRequest updateVerificationRequest) throws ApiException {
		Class[] argTypes = new Class[] {UpdateVerificationRequest.class};
		Object[] argValues = new Object[] {updateVerificationRequest};
		List methodProblems = ApiValidator.validate(verificationApi, methodName, argTypes, argValues);
		List modelProblems = ApiValidator.validate(updateVerificationRequest);
		List contextProblems = ApiValidator.validateUserContext(verificationApi);
		List validateVerificationRequestProblems =
				validateUpdateVerificationRequest(updateVerificationRequest);
		ApiValidator.collectProblems(methodProblems, modelProblems, contextProblems,
				validateVerificationRequestProblems);
	}

	private static List
			validateUpdateVerificationRequest(UpdateVerificationRequest updateVerificationRequest) {
		List problems = new ArrayList<>();
		UpdateVerification verification = updateVerificationRequest.getVerification();
		VerificationType verificationType = verification.getVerificationType();
		if (!verificationType.equals(VerificationType.CHALLENGE_DEPOSIT)) {
			problems.add(new Problem(ApiUtils.getErrorMessage(VERIFICATIONS_VERIFICATION_TYPE_INVALID), ""));
		} else {
			List verificationTransactions = verification.getTransactions();
			if (verificationTransactions != null) {
				for (VerificationTransaction txn : verificationTransactions) {
					if (txn.getAmount() != null) {
						processVerificationTxn(problems, txn);
					}
				}
			}
		}
		return problems;
	}

	private static void processVerificationTxn(List problems, VerificationTransaction txn) {
		if (txn.getAmount().getCurrency() == null) {
			String errorMessage = ApiUtils.getErrorMessage("verifications.transaction.currency.required");
			problems.add(new Problem(errorMessage, ""));
		}
		String amount = String.valueOf(txn.getAmount().getAmount().doubleValue());
		if (amount.contains(".")) {
			String amountSplit = amount.substring(amount.indexOf('.') + 1);
			if (amountSplit.length() > 2) {
				String errorMessage = ApiUtils.getErrorMessage("verifications.transaction.amount.invalid.length");
				problems.add(new Problem(errorMessage, ""));
			}
		}
	}
	
	private static void validateIsSelected(List problems, String[] isSelected) {
		if (isSelected == null) {
			return;
		}
		for(String isSelectedValue : isSelected) {
			if(PARAM_TRUE.equals(isSelectedValue)  || PARAM_FALSE.equals(isSelectedValue)) {
				continue;
			}
			problems.add(new Problem(ApiUtils.getErrorMessage("verifications.param.isselected.invalid"), ""));
		}
	}
	
	public static void validateVerifiedAccounts(VerificationApi verificationApi, String methodName, long providerAccountId, Long[] accountId,
			VerifiedAccountsVerificationStatus[] verificationStatus,
			String[] isSelected) throws ApiException {
		List problems = new ArrayList<>();
		validateIsSelected(problems, isSelected);
		Class[] argTypes = new Class[] {long.class, Long[].class, VerifiedAccountsVerificationStatus[].class, String[].class};
		Object[] argValues = new Object[] {providerAccountId, accountId, verificationStatus, isSelected};
		List methodProblems = ApiValidator.validate(verificationApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(verificationApi);
		ApiValidator.collectProblems(problems, methodProblems, contextProblems);
	}
	
	public static void validateHolderProfile(VerificationApi verificationApi, String methodName, long providerAccountId, 
			long accountId) throws ApiException {
		List problems = new ArrayList<>();
		Class[] argTypes = new Class[] {long.class, long.class};
		Object[] argValues = new Object[] {providerAccountId, accountId};
		List methodProblems = ApiValidator.validate(verificationApi, methodName, argTypes, argValues);
		List contextProblems = ApiValidator.validateUserContext(verificationApi);
		ApiValidator.collectProblems(problems, methodProblems, contextProblems);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy