<commit_before>/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 developers-payu-latam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.payu.sdk.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.payu.sdk.PayU;
import com.payu.sdk.constants.Constants;
import com.payu.sdk.exceptions.InvalidParametersException;
import com.payu.sdk.exceptions.PayUException;
import com.payu.sdk.exceptions.SDKException.ErrorCode;
import com.payu.sdk.helper.SignatureHelper;
import com.payu.sdk.model.Address;
import com.payu.sdk.model.BankListInformation;
import com.payu.sdk.model.BcashRequest;
import com.payu.sdk.model.Buyer;
import com.payu.sdk.model.CreditCard;
import com.payu.sdk.model.CreditCardToken;
import com.payu.sdk.model.CreditCardTokenInformation;
import com.payu.sdk.model.Currency;
import com.payu.sdk.model.DocumentType;
import com.payu.sdk.model.ExtraParemeterNames;
import com.payu.sdk.model.Merchant;
import com.payu.sdk.model.Order;
import com.payu.sdk.model.Payer;
import com.payu.sdk.model.PaymentCountry;
import com.payu.sdk.model.PaymentMethod;
import com.payu.sdk.model.Person;
import com.payu.sdk.model.PersonType;
import com.payu.sdk.model.RemoveCreditCardToken;
import com.payu.sdk.model.Transaction;
import com.payu.sdk.model.TransactionSource;
import com.payu.sdk.model.TransactionType;
import com.payu.sdk.model.request.Command;
import com.payu.sdk.model.request.CommandRequest;
import com.payu.sdk.model.request.Request;
import com.payu.sdk.payments.model.CreditCardTokenListRequest;
import com.payu.sdk.payments.model.CreditCardTokenRequest;
import com.payu.sdk.payments.model.PaymentMethodRequest;
import com.payu.sdk.payments.model.PaymentRequest;
import com.payu.sdk.payments.model.RemoveCreditCardTokenRequest;
import com.payu.sdk.reporting.model.ReportingRequest;

/**
 * Utility for requests in the PayU SDK.
 *
 * @author <NAME>
 * @since 1.0.0
 * @version 1.0.0, 21/08/2013
 */
public final class RequestUtil extends CommonRequestUtil {

	/** The encoding used to send confirmation page */
	private static final String ENCODING = Constants.DEFAULT_ENCODING
			.toString();
	/** The character to append parameters */
	private static final String APPENDER = "&";
	/** The character to assign a value param */
	private static final String EQUALS = "=";

	/**
	 * Private Constructor
	 */
	private RequestUtil() {
	}

	/**
	 * Builds a payments ping request
	 *
	 * @return The complete payments ping request
	 */
	public static PaymentRequest buildPaymentsPingRequest() {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.PING);
		return request;

	}

	/**
	 * Builds a reporting ping request
	 *
	 * @return The complete reporting request to be sent to the server
	 */
	public static ReportingRequest buildReportingPingRequest() {

		ReportingRequest request = buildDefaultReportingRequest();
		request.setCommand(Command.PING);
		return request;

	}

	/* Payment Requests */

	/**
	 * Builds a get bank list request
	 *
	 * @param paymentCountry
	 *            The country in which the transaction is being done
	 * @return The complete bank list request
	 */
	public static Request buildBankListRequest(PaymentCountry paymentCountry) {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.GET_BANKS_LIST);

		request.setBankListInformation(new BankListInformation(
				PaymentMethod.PSE, paymentCountry));

		return request;
	}

	/**
	 * Builds a payment request
	 *
	 * @param parameters
	 *            The parameters to be sent to the server
	 * @param transactionType
	 *            The transaction that is being done
	 * @return The complete payment request
	 * @throws InvalidParametersException
	 */
	public static Request buildPaymentRequest(Map<String, String> parameters,
			TransactionType transactionType) throws InvalidParametersException {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.SUBMIT_TRANSACTION);

		request.setApiKey(getParameter(parameters, PayU.PARAMETERS.API_KEY));
		request.setApiLogin(getParameter(parameters, PayU.PARAMETERS.API_LOGIN));

		request.setTransaction(buildTransaction(parameters, transactionType));

		return request;
	}

	/**
	 * Builds the payment method list request
	 *
	 * @return The complete payment methods list request
	 */
	public static Request buildPaymentMethodsListRequest() {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.GET_PAYMENT_METHODS);

		return request;
	}

	/**
	 * Builds the payment method request
	 * @return
	 */
	public static Request buildPaymentMethodAvailability(String paymentMethod) {
		PaymentMethodRequest request = new PaymentMethodRequest();
		request = (PaymentMethodRequest) buildDefaultRequest(request);
		request.setTest(PayU.isTest);
		request.setCommand(Command.GET_PAYMENT_METHOD_AVAILABILITY);
		request.setPaymentMethod(paymentMethod);
		return request;
	}

	/* Reporting Requests */

	/**<commit_msg>PAGOSONLINEPPS-32880: Se agrega api key y api login al objeto payment method request<commit_after>/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 developers-payu-latam
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.payu.sdk.utils;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.payu.sdk.PayU;
import com.payu.sdk.constants.Constants;
import com.payu.sdk.exceptions.InvalidParametersException;
import com.payu.sdk.exceptions.PayUException;
import com.payu.sdk.exceptions.SDKException.ErrorCode;
import com.payu.sdk.helper.SignatureHelper;
import com.payu.sdk.model.Address;
import com.payu.sdk.model.BankListInformation;
import com.payu.sdk.model.BcashRequest;
import com.payu.sdk.model.Buyer;
import com.payu.sdk.model.CreditCard;
import com.payu.sdk.model.CreditCardToken;
import com.payu.sdk.model.CreditCardTokenInformation;
import com.payu.sdk.model.Currency;
import com.payu.sdk.model.DocumentType;
import com.payu.sdk.model.ExtraParemeterNames;
import com.payu.sdk.model.Merchant;
import com.payu.sdk.model.Order;
import com.payu.sdk.model.Payer;
import com.payu.sdk.model.PaymentCountry;
import com.payu.sdk.model.PaymentMethod;
import com.payu.sdk.model.Person;
import com.payu.sdk.model.PersonType;
import com.payu.sdk.model.RemoveCreditCardToken;
import com.payu.sdk.model.Transaction;
import com.payu.sdk.model.TransactionSource;
import com.payu.sdk.model.TransactionType;
import com.payu.sdk.model.request.Command;
import com.payu.sdk.model.request.CommandRequest;
import com.payu.sdk.model.request.Request;
import com.payu.sdk.payments.model.CreditCardTokenListRequest;
import com.payu.sdk.payments.model.CreditCardTokenRequest;
import com.payu.sdk.payments.model.PaymentMethodRequest;
import com.payu.sdk.payments.model.PaymentRequest;
import com.payu.sdk.payments.model.RemoveCreditCardTokenRequest;
import com.payu.sdk.reporting.model.ReportingRequest;

/**
 * Utility for requests in the PayU SDK.
 *
 * @author <NAME>
 * @since 1.0.0
 * @version 1.0.0, 21/08/2013
 */
public final class RequestUtil extends CommonRequestUtil {

	/** The encoding used to send confirmation page */
	private static final String ENCODING = Constants.DEFAULT_ENCODING
			.toString();
	/** The character to append parameters */
	private static final String APPENDER = "&";
	/** The character to assign a value param */
	private static final String EQUALS = "=";

	/**
	 * Private Constructor
	 */
	private RequestUtil() {
	}

	/**
	 * Builds a payments ping request
	 *
	 * @return The complete payments ping request
	 */
	public static PaymentRequest buildPaymentsPingRequest() {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.PING);
		return request;

	}

	/**
	 * Builds a reporting ping request
	 *
	 * @return The complete reporting request to be sent to the server
	 */
	public static ReportingRequest buildReportingPingRequest() {

		ReportingRequest request = buildDefaultReportingRequest();
		request.setCommand(Command.PING);
		return request;

	}

	/* Payment Requests */

	/**
	 * Builds a get bank list request
	 *
	 * @param paymentCountry
	 *            The country in which the transaction is being done
	 * @return The complete bank list request
	 */
	public static Request buildBankListRequest(PaymentCountry paymentCountry) {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.GET_BANKS_LIST);

		request.setBankListInformation(new BankListInformation(
				PaymentMethod.PSE, paymentCountry));

		return request;
	}

	/**
	 * Builds a payment request
	 *
	 * @param parameters
	 *            The parameters to be sent to the server
	 * @param transactionType
	 *            The transaction that is being done
	 * @return The complete payment request
	 * @throws InvalidParametersException
	 */
	public static Request buildPaymentRequest(Map<String, String> parameters,
			TransactionType transactionType) throws InvalidParametersException {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.SUBMIT_TRANSACTION);

		request.setApiKey(getParameter(parameters, PayU.PARAMETERS.API_KEY));
		request.setApiLogin(getParameter(parameters, PayU.PARAMETERS.API_LOGIN));

		request.setTransaction(buildTransaction(parameters, transactionType));

		return request;
	}

	/**
	 * Builds the payment method list request
	 *
	 * @return The complete payment methods list request
	 */
	public static Request buildPaymentMethodsListRequest() {

		PaymentRequest request = buildDefaultPaymentRequest();
		request.setCommand(Command.GET_PAYMENT_METHODS);

		return request;
	}

	/**
	 * Builds the payment method request
	 * 
	 * @param paymentMethod
	 * @param apiKey
	 * @param apiLogin
	 * @return the payment method request
	 */
	public static Request buildPaymentMethodAvailability(String paymentMethod, String apiKey, String apiLogin) {
		
		PaymentMethodRequest request = new PaymentMethodRequest();
		request = (PaymentMethodRequest) buildDefaultRequest(request);
		request.setTest(PayU.isTest);
		request.setCommand(Command.GET_PAYMENT_METHOD_AVAILABILITY);
		request.setPaymentMethod(paymentMethod);
		request.setApiKey(apiKey);
		request.setApiLogin(apiLogin);
		
		return request;
	}

	/* Reporting Requests */

	/**
{'fraction_non_alphanumeric': 0.08970284443031941, 'fraction_numerical': 0.0032575877959637694, 'mean_word_length': 4.651998203861697, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5929869', 'n_tokens_mistral': 3776, 'n_tokens_neox': 3487, 'n_words': 1034}