Search in sources :

Example 1 with ErrorWithResponse

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

the class UnionPayTest method enroll_whenIsUnionPayFalse_willError.

@Test(timeout = 10000)
public void enroll_whenIsUnionPayFalse_willError() throws InterruptedException {
    String cardNumber = CardNumber.VISA;
    final UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder().cardNumber(cardNumber).expirationMonth("12").expirationYear("2019").mobileCountryCode("62").mobilePhoneNumber("11111111111");
    mBraintreeFragment.addListener(new UnionPayListener() {

        @Override
        public void onCapabilitiesFetched(UnionPayCapabilities capabilities) {
            assertFalse(capabilities.isUnionPay());
            UnionPay.enroll(mBraintreeFragment, unionPayCardBuilder);
        }

        @Override
        public void onSmsCodeSent(String enrollmentId, boolean smsCodeRequired) {
            fail("Not expecting onSmsCodeSent");
        }
    });
    mBraintreeFragment.addListener(new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            assertTrue(error instanceof ErrorWithResponse);
            assertEquals("UnionPay Enrollment is invalid", error.getMessage());
            mCountDownLatch.countDown();
        }
    });
    UnionPay.fetchCapabilities(mBraintreeFragment, cardNumber);
    mCountDownLatch.await();
}
Also used : UnionPayListener(com.braintreepayments.api.interfaces.UnionPayListener) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder) UnionPayCapabilities(com.braintreepayments.api.models.UnionPayCapabilities) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) Test(org.junit.Test)

Example 2 with ErrorWithResponse

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

the class ThreeDSecure method onActivityResult.

protected static void onActivityResult(BraintreeFragment fragment, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        ThreeDSecureAuthenticationResponse authenticationResponse;
        Uri resultUri = data.getData();
        if (resultUri != null) {
            authenticationResponse = ThreeDSecureAuthenticationResponse.fromJson(resultUri.getQueryParameter("auth_response"));
        } else {
            authenticationResponse = data.getParcelableExtra(ThreeDSecureWebViewActivity.EXTRA_THREE_D_SECURE_RESULT);
        }
        if (authenticationResponse.isSuccess()) {
            fragment.postCallback(authenticationResponse.getCardNonce());
        } else if (authenticationResponse.getException() != null) {
            fragment.postCallback(new BraintreeException(authenticationResponse.getException()));
        } else {
            fragment.postCallback(new ErrorWithResponse(422, authenticationResponse.getErrors()));
        }
    }
}
Also used : ThreeDSecureAuthenticationResponse(com.braintreepayments.api.models.ThreeDSecureAuthenticationResponse) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) Uri(android.net.Uri)

Example 3 with ErrorWithResponse

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

the class CardUnitTest method mockFailureCallback.

private void mockFailureCallback() {
    mockStatic(TokenizationClient.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((PaymentMethodNonceCallback) invocation.getArguments()[2]).failure(new ErrorWithResponse(422, ""));
            return null;
        }
    }).when(TokenizationClient.class);
    TokenizationClient.tokenize(any(BraintreeFragment.class), any(PaymentMethodBuilder.class), any(PaymentMethodNonceCallback.class));
}
Also used : Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) PaymentMethodBuilder(com.braintreepayments.api.models.PaymentMethodBuilder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) PaymentMethodNonceCallback(com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)

Example 4 with ErrorWithResponse

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

the class BraintreeFragmentUnitTest method addListener_flushesErrorWithResponseCallback.

@Test
public void addListener_flushesErrorWithResponseCallback() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    fragment.postCallback(new ErrorWithResponse(422, ""));
    fragment.addListener(new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            assertTrue(error instanceof ErrorWithResponse);
            assertEquals(422, ((ErrorWithResponse) error).getStatusCode());
            mCalled.set(true);
        }
    });
    assertTrue(mCalled.get());
}
Also used : ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) JSONException(org.json.JSONException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with ErrorWithResponse

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

the class BraintreeFragmentUnitTest method removeListener_noErrorCallbacksReceived.

@Test
public void removeListener_noErrorCallbacksReceived() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY);
    BraintreeErrorListener listener = new BraintreeErrorListener() {

        @Override
        public void onError(Exception error) {
            fail("Listener was called");
        }
    };
    fragment.addListener(listener);
    fragment.removeListener(listener);
    fragment.postCallback(new Exception());
    fragment.postCallback(new ErrorWithResponse(400, ""));
}
Also used : ErrorWithResponse(com.braintreepayments.api.exceptions.ErrorWithResponse) BraintreeErrorListener(com.braintreepayments.api.interfaces.BraintreeErrorListener) JSONException(org.json.JSONException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ErrorWithResponse (com.braintreepayments.api.exceptions.ErrorWithResponse)10 Test (org.junit.Test)7 BraintreeErrorListener (com.braintreepayments.api.interfaces.BraintreeErrorListener)5 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)4 JSONException (org.json.JSONException)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 PaymentMethodNonceCallback (com.braintreepayments.api.interfaces.PaymentMethodNonceCallback)2 UnionPayCardBuilder (com.braintreepayments.api.models.UnionPayCardBuilder)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)2 Uri (android.net.Uri)1 AuthorizationException (com.braintreepayments.api.exceptions.AuthorizationException)1 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)1 UnionPayListener (com.braintreepayments.api.interfaces.UnionPayListener)1 CardBuilder (com.braintreepayments.api.models.CardBuilder)1 PaymentMethodBuilder (com.braintreepayments.api.models.PaymentMethodBuilder)1 ThreeDSecureAuthenticationResponse (com.braintreepayments.api.models.ThreeDSecureAuthenticationResponse)1 UnionPayCapabilities (com.braintreepayments.api.models.UnionPayCapabilities)1 TestClientTokenBuilder (com.braintreepayments.api.test.TestClientTokenBuilder)1