Search in sources :

Example 1 with ConvertedAmountResponse

use of com.globalcollect.gateway.sdk.client.android.sdk.model.ConvertedAmountResponse in project connect-sdk-client-android by Ingenico-ePayments.

the class C2sCommunicator method convertAmount.

/**
 * Converts a given amount in cents from the given source currency to the given target currency
 *
 * @param amount,  the amount in cents to be converted
 * @param source,  source currency
 * @param target,  target currency
 * @param context, needed for reading metadata
 *
 * @return converted amount
 */
public Long convertAmount(Long amount, String source, String target, Context context) {
    if (amount == null) {
        throw new InvalidParameterException("Error converting amount, amount may not be null");
    }
    if (source == null) {
        throw new InvalidParameterException("Error converting amount, source may not be null");
    }
    if (target == null) {
        throw new InvalidParameterException("Error converting amount, target may not be null");
    }
    if (context == null) {
        throw new InvalidParameterException("Error converting amount, context may not be null");
    }
    HttpURLConnection connection = null;
    try {
        // Construct the url for the PublicKey call
        String paymentProductPath = Constants.GC_GATEWAY_CONVERT_AMOUNT_PATH.replace("[cid]", configuration.getCustomerId());
        String url = configuration.getBaseUrl() + paymentProductPath;
        // Add query parameters
        StringBuilder queryString = new StringBuilder();
        queryString.append("?amount=").append(amount);
        queryString.append("&source=").append(source);
        queryString.append("&target=").append(target);
        queryString.append("&").append(createCacheBusterParameter());
        url += queryString.toString();
        // Do the call and deserialise the result to PublicKeyResponse
        connection = doHTTPGetRequest(url, configuration.getClientSessionId(), configuration.getMetadata(context));
        String responseBody = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A").next();
        // Log the response
        if (Constants.ENABLE_REQUEST_LOGGING) {
            logResponse(connection, responseBody);
        }
        ConvertedAmountResponse convertedAmountResponse = gson.fromJson(responseBody, ConvertedAmountResponse.class);
        return convertedAmountResponse.getConvertedAmount();
    } catch (CommunicationException e) {
        Log.i(TAG, "Error converting amount:" + e.getMessage());
        return null;
    } catch (Exception e) {
        Log.i(TAG, "Error converting amount:" + e.getMessage());
        return null;
    } finally {
        try {
            if (connection != null) {
                connection.getInputStream().close();
                connection.disconnect();
            }
        } catch (IOException e) {
            Log.i(TAG, "Error converting amount:" + e.getMessage());
        }
    }
}
Also used : ConvertedAmountResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.ConvertedAmountResponse) InvalidParameterException(java.security.InvalidParameterException) Scanner(java.util.Scanner) HttpURLConnection(java.net.HttpURLConnection) CommunicationException(com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException) IOException(java.io.IOException) CommunicationException(com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException) InvalidParameterException(java.security.InvalidParameterException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

CommunicationException (com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException)1 ConvertedAmountResponse (com.globalcollect.gateway.sdk.client.android.sdk.model.ConvertedAmountResponse)1 IOException (java.io.IOException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 InvalidParameterException (java.security.InvalidParameterException)1 Scanner (java.util.Scanner)1