Search in sources :

Example 6 with AuthorizationException

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

the class PaymentMethodTest method getPaymentMethodNonces_failsWithATokenizationKey.

@Test(timeout = 10000)
public void getPaymentMethodNonces_failsWithATokenizationKey() throws InterruptedException, InvalidArgumentException {
    final CountDownLatch latch = new CountDownLatch(1);
    BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    getInstrumentation().waitForIdleSync();
    fragment.addListener(new PaymentMethodNoncesUpdatedListener() {

        @Override
        public void onPaymentMethodNoncesUpdated(List<PaymentMethodNonce> paymentMethodNonces) {
            fail("getPaymentMethodNonces succeeded");
        }
    });
    fragment.addListener(new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            assertTrue(error instanceof AuthorizationException);
            assertEquals("Tokenization key authorization not allowed for this endpoint. Please use an authentication method with upgraded permissions", error.getMessage());
            latch.countDown();
        }
    });
    tokenize(fragment, new CardBuilder().cardNumber(VISA).expirationMonth("04").expirationYear(validExpirationYear()));
    PaymentMethod.getPaymentMethodNonces(fragment);
    latch.await();
}
Also used : PaymentMethodNoncesUpdatedListener(com.braintreepayments.api.interfaces.PaymentMethodNoncesUpdatedListener) CardBuilder(com.braintreepayments.api.models.CardBuilder) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) CountDownLatch(java.util.concurrent.CountDownLatch) PaymentMethodNonce(com.braintreepayments.api.models.PaymentMethodNonce) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 7 with AuthorizationException

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

the class BraintreeHttpClientTest method getRequestSslCertificateSuccessfulInSandbox.

@Test(timeout = 5000)
public void getRequestSslCertificateSuccessfulInSandbox() throws InterruptedException, InvalidArgumentException {
    BraintreeHttpClient httpClient = new BraintreeHttpClient(TokenizationKey.fromString(TOKENIZATION_KEY));
    httpClient.setBaseUrl("https://api.sandbox.braintreegateway.com");
    httpClient.get("/", new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertTrue(exception instanceof AuthorizationException);
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 8 with AuthorizationException

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

the class BraintreeHttpClientTest method getRequestSslCertificateSuccessfulInProduction.

@Test(timeout = 5000)
public void getRequestSslCertificateSuccessfulInProduction() throws InterruptedException, InvalidArgumentException {
    BraintreeHttpClient httpClient = new BraintreeHttpClient(TokenizationKey.fromString(TOKENIZATION_KEY));
    httpClient.setBaseUrl("https://api.braintreegateway.com");
    httpClient.get("/", new HttpResponseCallback() {

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

        @Override
        public void failure(Exception exception) {
            assertTrue(exception instanceof AuthorizationException);
            mCountDownLatch.countDown();
        }
    });
    mCountDownLatch.await();
}
Also used : AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) JSONException(org.json.JSONException) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 9 with AuthorizationException

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

the class BraintreeGraphQLHttpClient method parseResponse.

@Override
protected String parseResponse(HttpURLConnection connection) throws Exception {
    String response = super.parseResponse(connection);
    JSONArray errors = new JSONObject(response).optJSONArray(ErrorWithResponse.GRAPHQL_ERRORS_KEY);
    if (errors != null) {
        for (int i = 0; i < errors.length(); i++) {
            JSONObject error = errors.getJSONObject(i);
            JSONObject extensions = error.optJSONObject("extensions");
            if (extensions == null) {
                throw new UnexpectedException("An unexpected error occurred");
            }
            if (Json.optString(extensions, "legacyCode", "").equals("50000")) {
                throw new AuthorizationException(error.getString("message"));
            } else if (!Json.optString(extensions, "errorType", "").equals("user_error")) {
                throw new UnexpectedException("An unexpected error occurred");
            }
        }
        throw ErrorWithResponse.fromGraphQLJson(response);
    }
    return response;
}
Also used : UnexpectedException(com.braintreepayments.api.exceptions.UnexpectedException) JSONObject(org.json.JSONObject) AuthorizationException(com.braintreepayments.api.exceptions.AuthorizationException) JSONArray(org.json.JSONArray)

Aggregations

AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)9 Test (org.junit.Test)8 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)5 JSONException (org.json.JSONException)4 BraintreeErrorListener (com.braintreepayments.api.interfaces.BraintreeErrorListener)3 CardBuilder (com.braintreepayments.api.models.CardBuilder)3 Intent (android.content.Intent)2 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)2 Authorization (com.braintreepayments.api.models.Authorization)2 Configuration (com.braintreepayments.api.models.Configuration)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 AppSwitchNotAvailableException (com.braintreepayments.api.exceptions.AppSwitchNotAvailableException)1 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)1 PaymentMethodNoncesUpdatedListener (com.braintreepayments.api.interfaces.PaymentMethodNoncesUpdatedListener)1 PaymentMethodNonce (com.braintreepayments.api.models.PaymentMethodNonce)1 TestClientTokenBuilder (com.braintreepayments.api.test.TestClientTokenBuilder)1 HttpURLConnection (java.net.HttpURLConnection)1