Search in sources :

Example 6 with IinDetailsResponse

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

the class ReadInternalStorage method getIinResponsesFromCache.

/**
 * Reads all iinResponses from the cache on disk
 * @param context, used for reading files
 */
@SuppressWarnings("unchecked")
public Map<String, IinDetailsResponse> getIinResponsesFromCache() {
    if (context == null) {
        throw new InvalidParameterException("Error getting response in cache, context may not be null");
    }
    // Create new map which contains all the iinResponses
    Map<String, IinDetailsResponse> iinResponses = new HashMap<String, IinDetailsResponse>();
    // Check if the cachefile exists
    String directory = context.getFilesDir() + Constants.DIRECTORY_IINRESPONSES;
    File file = new File(directory, Constants.FILENAME_IINRESPONSE_CACHE);
    if (file.exists()) {
        // read the content and parse it to Map<String, IinDetailsResponse>
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);
            iinResponses = (Map<String, IinDetailsResponse>) objectInputStream.readObject();
        } catch (StreamCorruptedException e) {
            Log.e(TAG, "Error getting List<PaymentProduct> from internal file ", e);
        } catch (IOException e) {
            Log.e(TAG, "Error getting List<PaymentProduct> from internal file ", e);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "Error getting List<PaymentProduct> from internal file ", e);
        } finally {
            try {
                objectInputStream.close();
                fileInputStream.close();
            } catch (IOException e) {
            }
        }
    }
    return iinResponses;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) HashMap(java.util.HashMap) IinDetailsResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsResponse) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with IinDetailsResponse

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

the class C2sCommunicator method getPaymentProductIdByCreditCardNumber.

/**
 * Get the IIN details for the entered partial creditcardnumber
 *
 * @param context, used for reading device metada which is send to the GC gateway
 * @param partialCreditCardNumber, entered partial creditcardnumber
 * @param paymentContext, meta data for the payment that is used to get contextual information from the GC gateway
 *
 * @return IinDetailsResponse which contains the result of the IIN lookup, or null when an error has occured
 */
public IinDetailsResponse getPaymentProductIdByCreditCardNumber(String partialCreditCardNumber, Context context, PaymentContext paymentContext) {
    if (partialCreditCardNumber == null) {
        throw new InvalidParameterException("Error getting IinDetails, partialCreditCardNumber may not be null");
    }
    // Trim partialCreditCardNumber to MAX_CHARS_PAYMENT_PRODUCT_ID_LOOKUP digits
    if (partialCreditCardNumber.length() > MAX_CHARS_PAYMENT_PRODUCT_ID_LOOKUP) {
        partialCreditCardNumber = partialCreditCardNumber.substring(0, MAX_CHARS_PAYMENT_PRODUCT_ID_LOOKUP);
    }
    HttpURLConnection connection = null;
    try {
        // Construct the url for the IIN details call
        String paymentProductPath = Constants.GC_GATEWAY_IIN_LOOKUP_PATH.replace("[cid]", configuration.getCustomerId());
        String url = configuration.getBaseUrl() + paymentProductPath;
        // Serialise the IinDetailsRequest to json, so it can be added to the postbody
        IinDetailsRequest iinRequest = new IinDetailsRequest(partialCreditCardNumber, paymentContext);
        String iinRequestJson = gson.toJson(iinRequest);
        // Do the call and deserialise the result to IinDetailsResponse
        connection = doHTTPPostRequest(url, configuration.getClientSessionId(), configuration.getMetadata(context), iinRequestJson);
        String responseBody = new Scanner(connection.getInputStream(), "UTF-8").useDelimiter("\\A").next();
        // Log the response
        if (Constants.ENABLE_REQUEST_LOGGING) {
            logResponse(connection, responseBody);
        }
        IinDetailsResponse iinResponse = gson.fromJson(responseBody, IinDetailsResponse.class);
        return iinResponse;
    } catch (Exception e) {
        Log.i(TAG, "Error getting PaymentProductIdByCreditCardNumber response:" + e.getMessage());
        return null;
    } finally {
        try {
            if (connection != null) {
                connection.getInputStream().close();
                connection.disconnect();
            }
        } catch (IOException e) {
            Log.i(TAG, "Error while getting PaymentProductIdByCreditCardNumber response:" + e.getMessage());
        }
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) IinDetailsRequest(com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsRequest) Scanner(java.util.Scanner) HttpURLConnection(java.net.HttpURLConnection) IinDetailsResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsResponse) 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 8 with IinDetailsResponse

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

the class WriteInternalStorage method storeIinResponseInCache.

/**
 * Stores the given iinResponses in the cache on disk
 *
 * @param context, used for writing files
 * @param partialCreditCardNumber, entered partial creditcardnumber
 * @param IinDetailsResponse, the IinDetailsResponse which is added to the current cache
 */
public void storeIinResponseInCache(String partialCreditCardNumber, IinDetailsResponse iinResponse) {
    if (partialCreditCardNumber == null) {
        throw new InvalidParameterException("Error storing response in partialCreditCardNumber, context may not be null");
    }
    if (iinResponse == null) {
        throw new InvalidParameterException("Error storing response in iinResponse, iinResponse may not be null");
    }
    // Retrieve the currenct cached items and add the new one to it
    Map<String, IinDetailsResponse> currentCachedIinResponses = storage.getIinResponsesFromCache();
    // Overwrite old value if it exists
    if (currentCachedIinResponses.containsKey(partialCreditCardNumber)) {
        currentCachedIinResponses.remove(partialCreditCardNumber);
    }
    currentCachedIinResponses.put(partialCreditCardNumber, iinResponse);
    // Then write the currentCachedIinResponses to disk
    String directory = context.getFilesDir() + Constants.DIRECTORY_IINRESPONSES;
    File file = new File(directory, Constants.FILENAME_IINRESPONSE_CACHE);
    file.getParentFile().mkdirs();
    FileOutputStream fileOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(currentCachedIinResponses);
    } catch (StreamCorruptedException e) {
        Log.e(TAG, "Error storing BasicPaymentProducts on internal device storage", e);
    } catch (IOException e) {
        Log.e(TAG, "Error storing BasicPaymentProducts on internal device storage", e);
    } finally {
        try {
            objectOutputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
        }
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) IinDetailsResponse(com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsResponse) FileOutputStream(java.io.FileOutputStream) StreamCorruptedException(java.io.StreamCorruptedException) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) File(java.io.File)

Aggregations

IinDetailsResponse (com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsResponse)8 IinLookupAsyncTask (com.globalcollect.gateway.sdk.client.android.sdk.asynctask.IinLookupAsyncTask)5 ArrayList (java.util.ArrayList)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 InvalidParameterException (java.security.InvalidParameterException)3 File (java.io.File)2 StreamCorruptedException (java.io.StreamCorruptedException)2 CommunicationException (com.globalcollect.gateway.sdk.client.android.sdk.exception.CommunicationException)1 AmountOfMoney (com.globalcollect.gateway.sdk.client.android.sdk.model.AmountOfMoney)1 PaymentContext (com.globalcollect.gateway.sdk.client.android.sdk.model.PaymentContext)1 IinDetailsRequest (com.globalcollect.gateway.sdk.client.android.sdk.model.iin.IinDetailsRequest)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1