Search in sources :

Example 21 with HttpResponseCallback

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

the class Ideal method startPayment.

/**
 * Initiates the payment flow by opening a browser where the customer can authenticate with their bank.
 *
 * @param fragment {@link BraintreeFragment}
 * @param builder {@link IdealRequest} with the payment details.
 * @param listener {@link BraintreeResponseListener} the callback to which the {@link IdealResult} will be sent
 * with a status of `PENDING` before the flow starts. This result contains the iDEAL payment ID.
 */
public static void startPayment(final BraintreeFragment fragment, final IdealRequest builder, final BraintreeResponseListener<IdealResult> listener) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fragment.sendAnalyticsEvent("ideal.start-payment.selected");
            Exception configException = checkIdealEnabled(configuration);
            if (configException != null) {
                fragment.postCallback(configException);
                fragment.sendAnalyticsEvent("ideal.start-payment.invalid-configuration");
                return;
            }
            String redirectUrl = URI.create(configuration.getIdealConfiguration().getAssetsUrl() + ASSET_SERVER_REDIRECT_PATH + fragment.getReturnUrlScheme() + "://").toString();
            fragment.getBraintreeApiHttpClient().post("/ideal-payments", builder.build(redirectUrl, configuration.getIdealConfiguration().getRouteId()), new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    try {
                        IdealResult idealResult = IdealResult.fromJson(responseBody);
                        BraintreeSharedPreferences.putString(fragment.getApplicationContext(), IDEAL_RESULT_ID, idealResult.getId());
                        if (listener != null) {
                            listener.onResponse(idealResult);
                        }
                        JSONObject responseJson = new JSONObject(responseBody);
                        String approvalUrl = responseJson.getJSONObject("data").getString("approval_url");
                        fragment.browserSwitch(BraintreeRequestCodes.IDEAL, approvalUrl);
                        fragment.sendAnalyticsEvent("ideal.webswitch.initiate.succeeded");
                    } catch (JSONException jsonException) {
                        failure(jsonException);
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.sendAnalyticsEvent("ideal.webswitch.initiate.failed");
                    fragment.postCallback(exception);
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException) IdealResult(com.braintreepayments.api.models.IdealResult)

Example 22 with HttpResponseCallback

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

the class TokenizationClient method tokenizeGraphQL.

private static void tokenizeGraphQL(final BraintreeFragment fragment, final CardBuilder cardBuilder, final PaymentMethodNonceCallback callback) {
    fragment.sendAnalyticsEvent("card.graphql.tokenization.started");
    String payload;
    try {
        payload = cardBuilder.buildGraphQL(fragment.getApplicationContext(), fragment.getAuthorization());
    } catch (BraintreeException e) {
        callback.failure(e);
        return;
    }
    fragment.getGraphQLHttpClient().post(payload, new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            try {
                callback.success(parsePaymentMethodNonces(responseBody, cardBuilder.getResponsePaymentMethodType()));
                fragment.sendAnalyticsEvent("card.graphql.tokenization.success");
            } catch (JSONException e) {
                callback.failure(e);
            }
        }

        @Override
        public void failure(Exception exception) {
            fragment.sendAnalyticsEvent("card.graphql.tokenization.failure");
            callback.failure(exception);
        }
    });
}
Also used : BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException)

Example 23 with HttpResponseCallback

use of com.braintreepayments.api.interfaces.HttpResponseCallback 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 24 with HttpResponseCallback

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

the class ConfigurationManager method getConfiguration.

static void getConfiguration(final BraintreeFragment fragment, @NonNull final ConfigurationListener listener, @NonNull final BraintreeResponseListener<Exception> errorListener) {
    final String configUrl = Uri.parse(fragment.getAuthorization().getConfigUrl()).buildUpon().appendQueryParameter("configVersion", "3").build().toString();
    Configuration cachedConfig = getCachedConfiguration(fragment.getApplicationContext(), configUrl + fragment.getAuthorization().getBearer());
    if (cachedConfig != null) {
        listener.onConfigurationFetched(cachedConfig);
    } else {
        sFetchingConfiguration = true;
        fragment.getHttpClient().get(configUrl, new HttpResponseCallback() {

            @Override
            public void success(String responseBody) {
                try {
                    Configuration configuration = Configuration.fromJson(responseBody);
                    cacheConfiguration(fragment.getApplicationContext(), configUrl + fragment.getAuthorization().getBearer(), configuration);
                    sFetchingConfiguration = false;
                    listener.onConfigurationFetched(configuration);
                } catch (final JSONException e) {
                    sFetchingConfiguration = false;
                    errorListener.onResponse(e);
                }
            }

            @Override
            public void failure(final Exception exception) {
                sFetchingConfiguration = false;
                errorListener.onResponse(exception);
            }
        });
    }
}
Also used : Configuration(com.braintreepayments.api.models.Configuration) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException)

Example 25 with HttpResponseCallback

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

the class HttpClientTest method postsErrorWhenPathIsNull.

@Test(timeout = 1000)
public void postsErrorWhenPathIsNull() throws InterruptedException {
    HttpClient httpClient = new HttpClient();
    final CountDownLatch countDownLatch = new CountDownLatch(2);
    httpClient.get(null, new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            fail("Request was successful");
        }

        @Override
        public void failure(Exception exception) {
            assertEquals(IllegalArgumentException.class, exception.getClass());
            assertEquals("Path cannot be null", exception.getMessage());
            countDownLatch.countDown();
        }
    });
    httpClient.post(null, "{}", new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            fail("Request was successful");
        }

        @Override
        public void failure(Exception exception) {
            assertEquals(IllegalArgumentException.class, exception.getClass());
            assertEquals("Path cannot be null", exception.getMessage());
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) DownForMaintenanceException(com.braintreepayments.api.exceptions.DownForMaintenanceException) AuthenticationException(com.braintreepayments.api.exceptions.AuthenticationException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) UnprocessableEntityException(com.braintreepayments.api.exceptions.UnprocessableEntityException) UpgradeRequiredException(com.braintreepayments.api.exceptions.UpgradeRequiredException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ServerException(com.braintreepayments.api.exceptions.ServerException) RateLimitException(com.braintreepayments.api.exceptions.RateLimitException) SSLException(javax.net.ssl.SSLException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.Test)

Aggregations

HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)54 Test (org.junit.Test)36 JSONException (org.json.JSONException)26 IOException (java.io.IOException)22 JSONObject (org.json.JSONObject)22 InvocationOnMock (org.mockito.invocation.InvocationOnMock)20 Answer (org.mockito.stubbing.Answer)20 CountDownLatch (java.util.concurrent.CountDownLatch)18 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)16 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)14 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 Configuration (com.braintreepayments.api.models.Configuration)13 MalformedURLException (java.net.MalformedURLException)11 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)10 IdealBank (com.braintreepayments.api.models.IdealBank)9 Matchers.anyString (org.mockito.Matchers.anyString)8 List (java.util.List)7 SSLException (javax.net.ssl.SSLException)7