Search in sources :

Example 1 with CommunicationException

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

the class C2sCommunicator method getPaymentProductDirectory.

/**
 * Retrieves a list of directories for a given paymentproduct
 *
 * @param productId, for which product must the lookup be done
 * @param currencyCode, for which currencyCode must the lookup be done
 * @param countryCode, for which countryCode must the lookup be done
 * @param context, used for reading device metada which is send to the GC gateway
 *
 * @return PaymentProductDirectoryResponse, or null when an error has occured
 */
public PaymentProductDirectoryResponse getPaymentProductDirectory(String productId, CurrencyCode currencyCode, CountryCode countryCode, Context context) {
    if (productId == null) {
        throw new InvalidParameterException("Error getting PaymentProduct directory, productId may not be null");
    }
    if (currencyCode == null) {
        throw new InvalidParameterException("Error getting PaymentProduct directory, currencyCode may not be null");
    }
    if (countryCode == null) {
        throw new InvalidParameterException("Error getting PaymentProduct directory, countryCode may not be null");
    }
    if (context == null) {
        throw new InvalidParameterException("Error getting PaymentProduct directory, context may not be null");
    }
    HttpURLConnection connection = null;
    try {
        // Build the complete url which is called
        String baseUrl = configuration.getBaseUrl();
        String paymentProductPath = Constants.GC_GATEWAY_RETRIEVE_PAYMENTPRODUCT_DIRECTORY_PATH.replace("[cid]", configuration.getCustomerId()).replace("[pid]", productId);
        String completePath = baseUrl + paymentProductPath;
        // Add query parameters
        StringBuilder queryString = new StringBuilder();
        queryString.append("?currencyCode=").append(currencyCode.name());
        queryString.append("&countryCode=").append(countryCode.name());
        queryString.append("&").append(createCacheBusterParameter());
        completePath += queryString.toString();
        // Do the call and deserialise the result to PaymentProductDirectoryResponse
        connection = doHTTPGetRequest(completePath, 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);
        }
        return gson.fromJson(responseBody, PaymentProductDirectoryResponse.class);
    } catch (CommunicationException e) {
        Log.i(TAG, "Error while getting paymentproduct directory:" + e.getMessage());
        return null;
    } catch (Exception e) {
        Log.i(TAG, "Error while getting paymentproduct directory:" + e.getMessage());
        return null;
    } finally {
        try {
            if (connection != null) {
                connection.getInputStream().close();
                connection.disconnect();
            }
        } catch (IOException e) {
            Log.i(TAG, "Error while getting paymentproduct directory:" + e.getMessage());
        }
    }
}
Also used : 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)

Example 2 with CommunicationException

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

the class C2sCommunicator method getPaymentProductPublicKey.

/**
 * Retrieves the Payment Product publickey from the GC gateway
 *
 * @param context, used for reading device metada which is send to the GC gateway
 * @param productId, used to determine for which payment product to retrieve the public key
 *
 * @return PaymentProductPublicKeyResponse response , or null when an error has occured
 */
public PaymentProductPublicKeyResponse getPaymentProductPublicKey(Context context, String productId) {
    HttpURLConnection connection = null;
    try {
        // Construct the url for the PublicKey call
        String paymentProductPath = Constants.GC_GATEWAY_PAYMENTPRODUCT_PUBLIC_KEY_PATH.replace("[cid]", configuration.getCustomerId()).replace("[pid]", productId);
        String url = configuration.getBaseUrl() + paymentProductPath;
        // Do the call and deserialize the result to PaymentProductPublicKeyResponse
        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);
        }
        return gson.fromJson(responseBody, PaymentProductPublicKeyResponse.class);
    } catch (CommunicationException e) {
        Log.i(TAG, "Error getting Payment Product Public key response:" + e.getMessage());
        return null;
    } catch (Exception e) {
        Log.i(TAG, "Error getting Payment Product Public key response:" + e.getMessage());
        return null;
    } finally {
        try {
            if (connection != null) {
                connection.getInputStream().close();
                connection.disconnect();
            }
        } catch (IOException e) {
            Log.i(TAG, "Error getting Payment Product Public key response:" + e.getMessage());
        }
    }
}
Also used : 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)

Example 3 with CommunicationException

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

the class C2sCommunicator method getCustomerDetails.

public CustomerDetailsResponse getCustomerDetails(String productId, CountryCode countryCode, List<KeyValuePair> values, Context context) {
    if (productId == null) {
        throw new InvalidParameterException("Error getting CustomerDetails, productId may not be null");
    }
    if (countryCode == null) {
        throw new InvalidParameterException("Error getting CustomerDetails, countryCode may not be null");
    }
    if (values == null) {
        throw new InvalidParameterException("Error getting CustomerDetails, values may not be null");
    }
    HttpURLConnection connection = null;
    try {
        // Construct the url for the getCustomerDetailsCall
        String paymentProductPath = Constants.GC_GATEWAY_CUSTOMERDETAILS_PATH.replace("[cid]", configuration.getCustomerId()).replace("[pid]", productId);
        String url = configuration.getBaseUrl() + paymentProductPath;
        // Serialise the getCustomerDetailsRequest to json, so it can be added to the postbody
        CustomerDetailsRequest customerDetailsRequest = new CustomerDetailsRequest(countryCode, values);
        String customerDetailsRequestJson = gson.toJson(customerDetailsRequest);
        // Do the call and deserialize the result to IinDetailsResponse
        connection = doHTTPPostRequest(url, configuration.getClientSessionId(), configuration.getMetadata(context), customerDetailsRequestJson);
        String responseBody = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A").next();
        // Log the response
        if (Constants.ENABLE_REQUEST_LOGGING) {
            logResponse(connection, responseBody);
        }
        CustomerDetailsResponse customerDetailsResponse = gson.fromJson(responseBody, CustomerDetailsResponse.class);
        return customerDetailsResponse;
    } catch (CommunicationException e) {
        Log.i(TAG, "Error while getting customer details:" + e.getMessage());
        return null;
    } catch (Exception e) {
        Log.i(TAG, "Error while getting customer details:" + e.getMessage());
        return null;
    } finally {
        try {
            if (connection != null) {
                connection.getInputStream().close();
                connection.disconnect();
            }
        } catch (IOException e) {
            Log.i(TAG, "Error while getting customer details:" + e.getMessage());
        }
    }
}
Also used : 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) CustomerDetailsResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.CustomerDetailsResponse) CommunicationException(com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException) InvalidParameterException(java.security.InvalidParameterException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CustomerDetailsRequest(com.globalcollect.gateway.sdk.client.android.sdk.model.CustomerDetailsRequest)

Example 4 with CommunicationException

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

the class C2sCommunicator method doHTTPGetRequest.

/**
 * Does a GET request with HttpURLConnection
 *
 * @param location, url where the request is sent to
 * @param clientSessionId, used for session identification on the GC gateway
 * @param metadata, map filled with metadata, which is added to the request
 *
 * @return HttpURLConnection, which contains the response of the request
 *
 * @throws CommunicationException
 */
private HttpURLConnection doHTTPGetRequest(String location, String clientSessionId, Map<String, String> metadata) throws CommunicationException {
    // Initialize the connection
    try {
        URL url = new URL(location);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // Add sessionId header
        if (clientSessionId != null) {
            connection.addRequestProperty(HTTP_HEADER_SESSION_ID, "GCS v1Client:" + clientSessionId);
        }
        // Add metadata entries header
        if (metadata != null) {
            connection.addRequestProperty(HTTP_HEADER_METADATA, GcUtil.getBase64EncodedMetadata(metadata));
        }
        // Log the request
        if (Constants.ENABLE_REQUEST_LOGGING) {
            logRequest(connection, null);
        }
        // Check if the response code is HTTP_OK
        if (connection.getResponseCode() != 200) {
            throw new CommunicationException("No status 200 received, status is :" + connection.getResponseCode());
        }
        return connection;
    } catch (MalformedURLException e) {
        Log.e(TAG, "doHTTPGetRequest, Unable to parse url " + location);
        throw new CommunicationException("Unable to parse url " + location);
    } catch (IOException e) {
        Log.e(TAG, "doHTTPGetRequest, IOException while opening connection " + e.getMessage());
        throw new CommunicationException("IOException while opening connection " + e.getMessage(), e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpURLConnection(java.net.HttpURLConnection) CommunicationException(com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with CommunicationException

use of com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException 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)16 IOException (java.io.IOException)13 HttpURLConnection (java.net.HttpURLConnection)13 MalformedURLException (java.net.MalformedURLException)13 InvalidParameterException (java.security.InvalidParameterException)11 Scanner (java.util.Scanner)11 Drawable (android.graphics.drawable.Drawable)2 AssetManager (com.globalcollect.gateway.sdk.client.android.sdk.manager.AssetManager)2 ApiException (com.ingenico.connect.gateway.sdk.java.ApiException)2 URL (java.net.URL)2 ConvertedAmountResponse (com.globalcollect.gateway.sdk.client.android.sdk.model.ConvertedAmountResponse)1 CustomerDetailsRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.CustomerDetailsRequest)1 CustomerDetailsResponse (com.globalcollect.gateway.sdk.client.android.sdk.model.CustomerDetailsResponse)1 PaymentRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.PaymentRequest)1 PreparedPaymentRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.PreparedPaymentRequest)1 BasicPaymentProduct (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProduct)1 BasicPaymentProductGroup (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProductGroup)1 BasicPaymentProductGroups (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProductGroups)1 BasicPaymentProducts (com.globalcollect.gateway.sdk.client.android.sdk.model.paymentproduct.BasicPaymentProducts)1 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)1