Search in sources :

Example 1 with ConfigurationException

use of com.braintreepayments.api.exceptions.ConfigurationException 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 2 with ConfigurationException

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

the class UnionPayUnitTest method enroll_failsIfUnionPayIsDisabled.

@Test
public void enroll_failsIfUnionPayIsDisabled() throws JSONException {
    UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder().cardNumber("some-card-number");
    BraintreeFragment fragment = new MockFragmentBuilder().configuration(Configuration.fromJson(stringFromFixture("configuration/configuration.json"))).build();
    UnionPay.enroll(fragment, unionPayCardBuilder);
    ArgumentCaptor<ConfigurationException> argumentCaptor = ArgumentCaptor.forClass(ConfigurationException.class);
    verify(fragment).postCallback(argumentCaptor.capture());
    assertEquals("UnionPay is not enabled", argumentCaptor.getValue().getMessage());
}
Also used : ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with ConfigurationException

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

the class UnionPay method fetchCapabilities.

/**
 * Fetches the capabilities of a card. If the card needs to be enrolled use {@link
 * UnionPay#enroll(BraintreeFragment, UnionPayCardBuilder)}.
 * <p/>
 * On completion, returns the {@link UnionPayCapabilities} to
 * {@link com.braintreepayments.api.interfaces.UnionPayListener#onCapabilitiesFetched(UnionPayCapabilities)}
 * <p/>
 * On error, an exception will be passed back to
 * {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 *
 * @param fragment {@link BraintreeFragment}
 * @param cardNumber The card number to check for Union Pay capabilities.
 */
public static void fetchCapabilities(final BraintreeFragment fragment, final String cardNumber) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (!configuration.getUnionPay().isEnabled()) {
                fragment.postCallback(new ConfigurationException("UnionPay is not enabled"));
                return;
            }
            String fetchCapabilitiesUrl = Uri.parse(UNIONPAY_CAPABILITIES_PATH).buildUpon().appendQueryParameter("creditCard[number]", cardNumber).build().toString();
            fragment.getHttpClient().get(fetchCapabilitiesUrl, new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    fragment.postCallback(UnionPayCapabilities.fromJson(responseBody));
                    fragment.sendAnalyticsEvent("union-pay.capabilities-received");
                }

                @Override
                public void failure(Exception exception) {
                    fragment.postCallback(exception);
                    fragment.sendAnalyticsEvent("union-pay.capabilities-failed");
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) Configuration(com.braintreepayments.api.models.Configuration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Example 4 with ConfigurationException

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

the class UnionPay method enroll.

/**
 * Enrolls a Union Pay card. Only call this method if the card needs to be enrolled. Check {@link
 * UnionPay#fetchCapabilities(BraintreeFragment, String)} if your card needs to be enrolled.
 * <p/>
 * On completion, returns a enrollmentId to
 * {@link com.braintreepayments.api.interfaces.UnionPayListener#onSmsCodeSent(String, boolean)}
 * This enrollmentId needs to be applied to {@link UnionPayCardBuilder} along with the SMS code
 * collected from the merchant before invoking {@link UnionPay#tokenize(BraintreeFragment, UnionPayCardBuilder)}
 * <p/>
 * On error, an exception will be passed back to
 * {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 *
 * @param fragment {@link BraintreeFragment}
 * @param unionPayCardBuilder {@link UnionPayCardBuilder}
 */
public static void enroll(final BraintreeFragment fragment, final UnionPayCardBuilder unionPayCardBuilder) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            UnionPayConfiguration unionPayConfiguration = configuration.getUnionPay();
            if (!unionPayConfiguration.isEnabled()) {
                fragment.postCallback(new ConfigurationException("UnionPay is not enabled"));
                return;
            }
            try {
                JSONObject enrollmentPayloadJson = unionPayCardBuilder.buildEnrollment();
                fragment.getHttpClient().post(UNIONPAY_ENROLLMENT_PATH, enrollmentPayloadJson.toString(), new HttpResponseCallback() {

                    @Override
                    public void success(String responseBody) {
                        try {
                            JSONObject response = new JSONObject(responseBody);
                            String enrollmentId = response.getString(UNIONPAY_ENROLLMENT_ID_KEY);
                            boolean smsCodeRequired = response.getBoolean(UNIONPAY_SMS_REQUIRED_KEY);
                            fragment.postUnionPayCallback(enrollmentId, smsCodeRequired);
                            fragment.sendAnalyticsEvent("union-pay.enrollment-succeeded");
                        } catch (JSONException e) {
                            failure(e);
                        }
                    }

                    @Override
                    public void failure(Exception exception) {
                        fragment.postCallback(exception);
                        fragment.sendAnalyticsEvent("union-pay.enrollment-failed");
                    }
                });
            } catch (JSONException exception) {
                fragment.postCallback(exception);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Aggregations

ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)4 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)3 Configuration (com.braintreepayments.api.models.Configuration)3 JSONException (org.json.JSONException)3 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)2 UnionPayConfiguration (com.braintreepayments.api.models.UnionPayConfiguration)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)1 GoogleApiClientException (com.braintreepayments.api.exceptions.GoogleApiClientException)1 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)1 QueuedCallback (com.braintreepayments.api.interfaces.QueuedCallback)1 UnionPayCardBuilder (com.braintreepayments.api.models.UnionPayCardBuilder)1 JSONObject (org.json.JSONObject)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1