Search in sources :

Example 16 with Authorization

use of com.braintreepayments.api.models.Authorization in project braintree_android by braintree.

the class ThreeDSecureVerificationTest method getFragment.

private BraintreeFragment getFragment(String authorization, String configuration) {
    try {
        Authorization auth = Authorization.fromString(authorization);
        writeMockConfiguration(getTargetContext(), auth.getConfigUrl(), auth.getBearer(), configuration);
        BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, authorization);
        while (!fragment.isAdded()) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ignored) {
            }
        }
        return fragment;
    } catch (InvalidArgumentException e) {
        fail(e.getMessage());
        return new BraintreeFragment();
    }
}
Also used : Authorization(com.braintreepayments.api.models.Authorization) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException)

Example 17 with Authorization

use of com.braintreepayments.api.models.Authorization 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

Authorization (com.braintreepayments.api.models.Authorization)17 Configuration (com.braintreepayments.api.models.Configuration)14 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Intent (android.content.Intent)9 Bundle (android.os.Bundle)5 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)4 SharedPreferences (android.content.SharedPreferences)3 SharedPreferencesHelper.clearSharedPreferences (com.braintreepayments.testutils.SharedPreferencesHelper.clearSharedPreferences)3 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 FragmentManager (android.app.FragmentManager)1 Context (android.content.Context)1 AppSwitchNotAvailableException (com.braintreepayments.api.exceptions.AppSwitchNotAvailableException)1 PaymentMethodNonceCallback (com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)1 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)1 VenmoAccountBuilder (com.braintreepayments.api.models.VenmoAccountBuilder)1 VenmoAccountNonce (com.braintreepayments.api.models.VenmoAccountNonce)1 TestConfigurationBuilder (com.braintreepayments.testutils.TestConfigurationBuilder)1