Search in sources :

Example 16 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class TokenizationClient method tokenize.

/**
 * Create a {@link PaymentMethodNonce} in the Braintree Gateway.
 * <p/>
 * On completion, returns the {@link PaymentMethodNonce} to {@link PaymentMethodNonceCallback}.
 * <p/>
 * If creation fails validation, {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 * will be called with the resulting {@link ErrorWithResponse}.
 * <p/>
 * If an error not due to validation (server error, network issue, etc.) occurs, {@link
 * com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)} (Throwable)}
 * will be called with the {@link Exception} that occurred.
 *
 * @param paymentMethodBuilder {@link PaymentMethodBuilder} for the {@link PaymentMethodNonce}
 *        to be created.
 */
static void tokenize(final BraintreeFragment fragment, final PaymentMethodBuilder paymentMethodBuilder, final PaymentMethodNonceCallback callback) {
    paymentMethodBuilder.setSessionId(fragment.getSessionId());
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (paymentMethodBuilder instanceof CardBuilder && configuration.getGraphQL().isFeatureEnabled(GraphQLConfiguration.TOKENIZE_CREDIT_CARDS_FEATURE)) {
                tokenizeGraphQL(fragment, (CardBuilder) paymentMethodBuilder, callback);
            } else {
                tokenizeRest(fragment, paymentMethodBuilder, callback);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) CardBuilder(com.braintreepayments.api.models.CardBuilder) GraphQLConfiguration(com.braintreepayments.api.models.GraphQLConfiguration) Configuration(com.braintreepayments.api.models.Configuration)

Example 17 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class AmericanExpress method getRewardsBalance.

/**
 * Gets the rewards balance associated with a Braintree nonce. Only for American Express cards.
 *
 * @param fragment the {@link BraintreeFragment} This fragment will also be responsible
 * for handling callbacks to it's listeners
 * @param nonce A nonce representing a card that will be used to look up the rewards balance
 * @param currencyIsoCode The currencyIsoCode to use. Example: 'USD'
 */
public static void getRewardsBalance(final BraintreeFragment fragment, final String nonce, final String currencyIsoCode) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            String getRewardsBalanceUrl = Uri.parse(AMEX_REWARDS_BALANCE_PATH).buildUpon().appendQueryParameter("paymentMethodNonce", nonce).appendQueryParameter("currencyIsoCode", currencyIsoCode).build().toString();
            fragment.sendAnalyticsEvent("amex.rewards-balance.start");
            fragment.getHttpClient().get(getRewardsBalanceUrl, new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    fragment.sendAnalyticsEvent("amex.rewards-balance.success");
                    try {
                        fragment.postAmericanExpressCallback(AmericanExpressRewardsBalance.fromJson(responseBody));
                    } catch (JSONException e) {
                        fragment.sendAnalyticsEvent("amex.rewards-balance.parse.failed");
                        fragment.postCallback(e);
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.postCallback(exception);
                    fragment.sendAnalyticsEvent("amex.rewards-balance.error");
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException)

Example 18 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class AndroidPay method requestAndroidPay.

/**
 * @deprecated Android Pay is deprecated, use {@link GooglePayment} instead. For more information see the
 * <a href="https://developers.braintreepayments.com/guides/pay-with-google/overview">documentation</a>
 *
 * Launch an Android Pay masked wallet request. This method will show the payment instrument
 * chooser to the user.
 *
 * @param fragment The current {@link BraintreeFragment}.
 * @param cart The cart representation with price, currency code, and optionally items.
 * @param shippingAddressRequired {@code true} if this request requires a shipping address, {@code false} otherwise.
 * @param phoneNumberRequired {@code true} if this request requires a phone number, {@code false} otherwise.
 * @param allowedCountries ISO 3166-2 country codes that shipping is allowed to.
 */
@Deprecated
public static void requestAndroidPay(final BraintreeFragment fragment, @NonNull final Cart cart, final boolean shippingAddressRequired, final boolean phoneNumberRequired, final ArrayList<CountrySpecification> allowedCountries) {
    fragment.sendAnalyticsEvent("android-pay.selected");
    if (!validateManifest(fragment.getApplicationContext())) {
        fragment.postCallback(new BraintreeException("AndroidPayActivity was not found in the Android manifest, " + "or did not have a theme of R.style.bt_transparent_activity"));
        fragment.sendAnalyticsEvent("android-pay.failed");
        return;
    }
    if (cart == null) {
        fragment.postCallback(new BraintreeException("Cannot pass null cart to performMaskedWalletRequest"));
        fragment.sendAnalyticsEvent("android-pay.failed");
        return;
    }
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fragment.sendAnalyticsEvent("android-pay.started");
            Intent intent = new Intent(fragment.getApplicationContext(), AndroidPayActivity.class).putExtra(EXTRA_ENVIRONMENT, GooglePayment.getEnvironment(configuration.getAndroidPay())).putExtra(EXTRA_MERCHANT_NAME, configuration.getAndroidPay().getDisplayName()).putExtra(EXTRA_CART, cart).putExtra(EXTRA_TOKENIZATION_PARAMETERS, GooglePayment.getTokenizationParameters(fragment)).putIntegerArrayListExtra(EXTRA_ALLOWED_CARD_NETWORKS, GooglePayment.getAllowedCardNetworks(fragment)).putExtra(EXTRA_SHIPPING_ADDRESS_REQUIRED, shippingAddressRequired).putExtra(EXTRA_PHONE_NUMBER_REQUIRED, phoneNumberRequired).putParcelableArrayListExtra(EXTRA_ALLOWED_COUNTRIES, allowedCountries).putExtra(EXTRA_REQUEST_TYPE, AUTHORIZE);
            fragment.startActivityForResult(intent, BraintreeRequestCodes.ANDROID_PAY);
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) Intent(android.content.Intent)

Example 19 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class BraintreeFragment method fetchConfiguration.

@VisibleForTesting
protected void fetchConfiguration() {
    if (getConfiguration() != null || ConfigurationManager.isFetchingConfiguration() || mAuthorization == null || mHttpClient == null) {
        return;
    }
    if (mConfigurationRequestAttempts >= 3) {
        postCallback(new ConfigurationException("Configuration retry limit has been exceeded. Create a new " + "BraintreeFragment and try again."));
        return;
    }
    mConfigurationRequestAttempts++;
    ConfigurationManager.getConfiguration(this, new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            setConfiguration(configuration);
            postConfigurationCallback();
            flushCallbacks();
        }
    }, new BraintreeResponseListener<Exception>() {

        @Override
        public void onResponse(final Exception e) {
            final ConfigurationException exception = new ConfigurationException("Request for configuration has failed: " + e.getMessage() + ". " + "Future requests will retry up to 3 times", e);
            postCallback(exception);
            postOrQueueCallback(new QueuedCallback() {

                @Override
                public boolean shouldRun() {
                    return mConfigurationErrorListener != null;
                }

                @Override
                public void run() {
                    mConfigurationErrorListener.onResponse(exception);
                }
            });
            flushCallbacks();
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) GoogleApiClientException(com.braintreepayments.api.exceptions.GoogleApiClientException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) QueuedCallback(com.braintreepayments.api.interfaces.QueuedCallback) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 20 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class BraintreeFragmentUnitTest method waitForConfiguration_postsCallbackWhenFragmentIsAttached.

@Test
public void waitForConfiguration_postsCallbackWhenFragmentIsAttached() throws JSONException, InvalidArgumentException {
    final Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/configuration.json"));
    mockConfigurationManager(configuration);
    final BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration returnedConfiguration) {
            assertTrue(fragment.isAdded());
            mCalled.set(true);
        }
    });
    assertTrue(mCalled.get());
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)37 Configuration (com.braintreepayments.api.models.Configuration)36 Test (org.junit.Test)21 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)15 JSONException (org.json.JSONException)12 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)10 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)10 ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 BraintreeFragmentTestUtils.getFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration)4 BraintreeFragmentTestUtils.getMockFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration)4 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)4 BraintreeHttpClient (com.braintreepayments.api.internal.BraintreeHttpClient)4 JSONObject (org.json.JSONObject)4 Bundle (android.os.Bundle)3 Intent (android.content.Intent)2 BraintreeResponseListener (com.braintreepayments.api.interfaces.BraintreeResponseListener)2 UnionPayConfiguration (com.braintreepayments.api.models.UnionPayConfiguration)2 Uri (android.net.Uri)1