Search in sources :

Example 31 with InvalidArgumentException

use of com.braintreepayments.api.exceptions.InvalidArgumentException in project braintree_android by braintree.

the class BraintreeFragment method newInstance.

/**
 * Create a new instance of {@link BraintreeFragment} using the client token and add it to the
 * {@link Activity}'s {@link FragmentManager}.
 *
 * @param activity The {@link Activity} to add the {@link Fragment} to.
 * @param authorization The tokenization key or client token to use.
 * @return {@link BraintreeFragment}
 * @throws InvalidArgumentException If the tokenization key or client token is not valid or cannot be
 *         parsed.
 */
public static BraintreeFragment newInstance(Activity activity, String authorization) throws InvalidArgumentException {
    if (activity == null) {
        throw new InvalidArgumentException("Activity is null");
    }
    FragmentManager fm = activity.getFragmentManager();
    BraintreeFragment braintreeFragment = (BraintreeFragment) fm.findFragmentByTag(TAG);
    if (braintreeFragment == null) {
        braintreeFragment = new BraintreeFragment();
        Bundle bundle = new Bundle();
        try {
            Authorization auth = Authorization.fromString(authorization);
            bundle.putParcelable(EXTRA_AUTHORIZATION_TOKEN, auth);
        } catch (InvalidArgumentException e) {
            throw new InvalidArgumentException("Tokenization Key or client token was invalid.");
        }
        bundle.putString(EXTRA_SESSION_ID, UUIDHelper.getFormattedUUID());
        bundle.putString(EXTRA_INTEGRATION_TYPE, IntegrationType.get(activity));
        braintreeFragment.setArguments(bundle);
        try {
            if (VERSION.SDK_INT >= VERSION_CODES.N) {
                try {
                    fm.beginTransaction().add(braintreeFragment, TAG).commitNow();
                } catch (IllegalStateException | NullPointerException e) {
                    fm.beginTransaction().add(braintreeFragment, TAG).commit();
                    try {
                        fm.executePendingTransactions();
                    } catch (IllegalStateException ignored) {
                    }
                }
            } else {
                fm.beginTransaction().add(braintreeFragment, TAG).commit();
                try {
                    fm.executePendingTransactions();
                } catch (IllegalStateException ignored) {
                }
            }
        } catch (IllegalStateException e) {
            throw new InvalidArgumentException(e.getMessage());
        }
    }
    braintreeFragment.mContext = activity.getApplicationContext();
    return braintreeFragment;
}
Also used : FragmentManager(android.app.FragmentManager) Authorization(com.braintreepayments.api.models.Authorization) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Bundle(android.os.Bundle)

Aggregations

InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)31 Test (org.junit.Test)27 JSONException (org.json.JSONException)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)12 BraintreeErrorListener (com.braintreepayments.api.interfaces.BraintreeErrorListener)12 IOException (java.io.IOException)12 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)10 Configuration (com.braintreepayments.api.models.Configuration)7 MalformedURLException (java.net.MalformedURLException)6 Intent (android.content.Intent)4 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)4 Authorization (com.braintreepayments.api.models.Authorization)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ErrorWithResponse (com.braintreepayments.api.exceptions.ErrorWithResponse)3 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)3 PaymentMethodNoncesUpdatedListener (com.braintreepayments.api.interfaces.PaymentMethodNoncesUpdatedListener)3 IdealBank (com.braintreepayments.api.models.IdealBank)3 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)3 List (java.util.List)3