com.braintreepayments.api.interfaces.BraintreeErrorListener Java Examples

The following examples show how to use com.braintreepayments.api.interfaces.BraintreeErrorListener. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThreeDSecureVerificationTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Test(timeout = 10000)
public void performVerification_failsWithATokenizationKey() throws InterruptedException {
    String clientToken = new TestClientTokenBuilder().build();
    BraintreeFragment fragment = getFragment(TOKENIZATION_KEY, clientToken);
    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());
            mCountDownLatch.countDown();
        }
    });
    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(THREE_D_SECURE_VERIFICATON)
            .expirationDate("12/20");

    ThreeDSecure.performVerification(fragment, cardBuilder, TEST_AMOUNT);

    mCountDownLatch.await();
}
 
Example #2
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Test
public void startActivityForResult_postsExceptionWhenNotAttached() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, TOKENIZATION_KEY);
    mAppCompatActivity.getSupportFragmentManager().beginTransaction().detach(fragment).commit();
    mAppCompatActivity.getSupportFragmentManager().executePendingTransactions();
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertEquals("BraintreeFragment is not attached to an Activity. Please ensure it is attached and try again.",
                    error.getMessage());
            mCalled.set(true);
        }
    });

    fragment.startActivityForResult(new Intent(), 1);

    assertTrue(mCalled.get());
}
 
Example #3
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Test
public void postCallback_ErrorWithResponseIsPostedToListeners() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, TOKENIZATION_KEY);
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertTrue(error instanceof ErrorWithResponse);
            assertEquals(422, ((ErrorWithResponse) error).getStatusCode());
            mCalled.set(true);
        }
    });

    fragment.postCallback(new ErrorWithResponse(422, ""));

    assertTrue(mCalled.get());
}
 
Example #4
Source File: CardTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Ignore("Sample merchant account is not set up for CVV verification")
@Test(timeout = 10000)
public void tokenize_callsErrorCallbackForInvalidCvv() throws Exception {
    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(VISA)
            .expirationDate("08/20")
            .cvv("200");

    final CountDownLatch countDownLatch = new CountDownLatch(1);
    BraintreeFragment fragment = setupBraintreeFragment(new TestClientTokenBuilder().withCvvVerification().build());
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertEquals("CVV verification failed",
                    ((ErrorWithResponse) error).errorFor("creditCard").getFieldErrors().get(0).getMessage());
            countDownLatch.countDown();
        }
    });

    Card.tokenize(fragment, cardBuilder);

    countDownLatch.await();
}
 
Example #5
Source File: CardTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Ignore("Sample merchant account is not set up for postal code verification")
@Test(timeout = 10000)
public void tokenize_callsErrorCallbackForInvalidPostalCode() throws Exception {
    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(VISA)
            .expirationDate("08/20")
            .postalCode("20000");

    final CountDownLatch countDownLatch = new CountDownLatch(1);
    BraintreeFragment fragment = setupBraintreeFragment(
            new TestClientTokenBuilder().withPostalCodeVerification().build());
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertEquals("Postal code verification failed",
                    ((ErrorWithResponse) error).errorFor("creditCard").errorFor("billingAddress")
                            .getFieldErrors().get(0).getMessage());
            countDownLatch.countDown();
        }
    });

    Card.tokenize(fragment, cardBuilder);

    countDownLatch.await();
}
 
Example #6
Source File: CardTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Test
public void tokenize_whenInvalidCountryCode_callsErrorCallbackWithDetailedError() throws Exception {
    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(VISA)
            .expirationDate("08/20")
            .countryCode("ABC");

    final CountDownLatch countDownLatch = new CountDownLatch(1);
    BraintreeFragment fragment = setupBraintreeFragment(new TestClientTokenBuilder().build());
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertEquals("Country code (alpha3) is not an accepted country",
                    ((ErrorWithResponse) error).errorFor("creditCard").errorFor("billingAddress")
                            .getFieldErrors().get(0).getMessage());
            countDownLatch.countDown();
        }
    });

    Card.tokenize(fragment, cardBuilder);

    countDownLatch.await();
}
 
Example #7
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Test
public void removeListener_noErrorCallbacksReceived() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, 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, ""));
}
 
Example #8
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 6 votes vote down vote up
@Test
public void addListener_flushesErrorWithResponseCallback() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, 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());
}
 
Example #9
Source File: UnionPayTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test(timeout = 10000)
public void enroll_whenSmsCodeRequiredFalse_onSmsCodeSentReturnsFalse() throws InterruptedException {
    mCountDownLatch = new CountDownLatch(2);
    String cardNumber = UNIONPAY_SMS_NOT_REQUIRED;
    final UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder()
            .cardNumber(cardNumber)
            .expirationMonth("12")
            .expirationYear(ExpirationDateHelper.validExpirationYear())
            .mobileCountryCode("62")
            .mobilePhoneNumber("11111111111");

    mBraintreeFragment.addListener(new UnionPayListener() {
        @Override
        public void onCapabilitiesFetched(UnionPayCapabilities capabilities) {
            assertTrue(capabilities.isUnionPay());
            assertTrue(capabilities.isSupported());
            UnionPay.enroll(mBraintreeFragment, unionPayCardBuilder);
            mCountDownLatch.countDown();
        }

        @Override
        public void onSmsCodeSent(String enrollmentId, boolean smsCodeRequired) {
            assertNotNull(enrollmentId);
            assertFalse(smsCodeRequired);
            mCountDownLatch.countDown();
        }
    });

    mBraintreeFragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            fail("Not expecting error");
        }
    });

    UnionPay.fetchCapabilities(mBraintreeFragment, cardNumber);
    mCountDownLatch.await();
}
 
Example #10
Source File: ThreeDSecureVerificationTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test(timeout = 10000)
public void performVerification_doesALookupAndReturnsACardWhenThereIsAMPILookupError() throws InterruptedException {
    BraintreeFragment fragment = getFragment();
    fragment.addListener(new PaymentMethodNonceCreatedListener() {
        @Override
        public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
            assertIsANonce(paymentMethodNonce.getNonce());
            CardNonce cardNonce = (CardNonce) paymentMethodNonce;

            assertEquals("85", cardNonce.getLastTwo());
            assertFalse(cardNonce.getThreeDSecureInfo().isLiabilityShifted());
            assertFalse(cardNonce.getThreeDSecureInfo().isLiabilityShiftPossible());
            assertTrue(((CardNonce) paymentMethodNonce).getThreeDSecureInfo().wasVerified());

            mCountDownLatch.countDown();
        }
    });

    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            fail(error.getMessage());
        }
    });

    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(THREE_D_SECURE_MPI_LOOKUP_ERROR)
            .expirationDate("12/20");

    ThreeDSecure.performVerification(fragment, cardBuilder, TEST_AMOUNT);

    mCountDownLatch.await();
}
 
Example #11
Source File: ThreeDSecureVerificationTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test(timeout = 10000)
public void performVerification_doesALookupAndReturnsACardWhenThereIsALookupError() throws InterruptedException {
    BraintreeFragment fragment = getFragment();
    fragment.addListener(new PaymentMethodNonceCreatedListener() {
        @Override
        public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
            assertIsANonce(paymentMethodNonce.getNonce());
            CardNonce cardNonce = (CardNonce) paymentMethodNonce;

            assertEquals("77", cardNonce.getLastTwo());
            assertFalse(cardNonce.getThreeDSecureInfo().isLiabilityShifted());
            assertFalse(cardNonce.getThreeDSecureInfo().isLiabilityShiftPossible());
            assertTrue(((CardNonce) paymentMethodNonce).getThreeDSecureInfo().wasVerified());

            mCountDownLatch.countDown();
        }
    });

    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            fail(error.getMessage());
        }
    });

    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(THREE_D_SECURE_LOOKUP_ERROR)
            .expirationDate("12/20");

    ThreeDSecure.performVerification(fragment, cardBuilder, TEST_AMOUNT);

    mCountDownLatch.await();
}
 
Example #12
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test
public void startActivityForResult_doesNotPostExceptionWhenAttached() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, TOKENIZATION_KEY);
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            fail("onError was called");
        }
    });

    fragment.startActivityForResult(new Intent(), 1);

    assertFalse(mCalled.get());
}
 
Example #13
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test
public void postCallback_exceptionIsPostedToListeners() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, TOKENIZATION_KEY);
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertEquals("Error!", error.getMessage());
            mCalled.set(true);
        }
    });

    fragment.postCallback(new Exception("Error!"));

    assertTrue(mCalled.get());
}
 
Example #14
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test
public void addListener_flushesExceptionCallbacks() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, TOKENIZATION_KEY);
    fragment.postCallback(new Exception("Error!"));

    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertEquals("Error!", error.getMessage());
            mCalled.set(true);
        }
    });

    assertTrue(mCalled.get());
}
 
Example #15
Source File: UnionPayTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test(timeout = 10000)
public void enroll_whenIsUnionPayFalse_willError() throws InterruptedException {
    String cardNumber = CardNumber.VISA;
    final UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder()
            .cardNumber(cardNumber)
            .expirationMonth("12")
            .expirationYear(ExpirationDateHelper.validExpirationYear())
            .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();
}
 
Example #16
Source File: CardTest.java    From braintree_android with MIT License 5 votes vote down vote up
private void assertTokenizationSuccessful(String authorization, CardBuilder cardBuilder) throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    BraintreeFragment fragment = setupBraintreeFragment(authorization);
    fragment.addListener(new PaymentMethodNonceCreatedListener() {
        @Override
        public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
            CardNonce cardNonce = (CardNonce) paymentMethodNonce;

            assertNotNull(cardNonce.getNonce());
            assertEquals("Visa", cardNonce.getCardType());
            assertEquals("1111", cardNonce.getLastFour());
            assertEquals("11", cardNonce.getLastTwo());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getPrepaid());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getHealthcare());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getDebit());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getDurbinRegulated());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getCommercial());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getPayroll());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getIssuingBank());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getCountryOfIssuance());
            assertEquals(BinData.UNKNOWN, cardNonce.getBinData().getProductId());
            assertFalse(cardNonce.getThreeDSecureInfo().wasVerified());

            countDownLatch.countDown();
        }
    });
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            throw new RuntimeException(error);
        }
    });

    Card.tokenize(fragment, cardBuilder);

    countDownLatch.await();
}
 
Example #17
Source File: CardTest.java    From braintree_android with MIT License 5 votes vote down vote up
@Test(timeout = 10000)
public void tokenize_failsWithTokenizationKeyAndValidateTrue() throws Exception {
    CardBuilder cardBuilder = new CardBuilder()
            .cardNumber(VISA)
            .expirationDate("08/20")
            .validate(true);
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    BraintreeFragment fragment = setupBraintreeFragment(TOKENIZATION_KEY);
    fragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            assertTrue(error instanceof AuthorizationException);

            if (mRequestProtocol.equals(GRAPHQL)) {
                assertEquals("You are unauthorized to perform input validation with the provided authentication credentials.",
                        error.getMessage());
            } else {
                assertEquals("Tokenization key authorization not allowed for this endpoint. Please use an " +
                        "authentication method with upgraded permissions", error.getMessage());
            }

            countDownLatch.countDown();
        }
    });

    Card.tokenize(fragment, cardBuilder);

    countDownLatch.await();
}
 
Example #18
Source File: PaymentMethodTest.java    From braintree_android with MIT License 5 votes vote down vote up
@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();
}
 
Example #19
Source File: BraintreeFragment.java    From braintree_android with MIT License 5 votes vote down vote up
/**
 * Removes a previously added listener.
 *
 * @param listener the listener to remove.
 */
public <T extends BraintreeListener> void removeListener(T listener) {
    if (listener instanceof ConfigurationListener) {
        mConfigurationListener = null;
    }

    if (listener instanceof BraintreeCancelListener) {
        mCancelListener = null;
    }

    if (listener instanceof PaymentMethodNoncesUpdatedListener) {
        mPaymentMethodNoncesUpdatedListener = null;
    }

    if (listener instanceof PaymentMethodNonceCreatedListener) {
        mPaymentMethodNonceCreatedListener = null;
    }

    if (listener instanceof PaymentMethodNonceDeletedListener) {
        mPaymentMethodNonceDeletedListener = null;
    }

    if (listener instanceof BraintreePaymentResultListener) {
        mBraintreePaymentResultListener = null;
    }

    if (listener instanceof BraintreeErrorListener) {
        mErrorListener = null;
    }

    if (listener instanceof UnionPayListener) {
        mUnionPayListener = null;
    }

    if (listener instanceof AmericanExpressListener) {
        mAmericanExpressListener = null;
    }
}
 
Example #20
Source File: BraintreeFragment.java    From braintree_android with MIT License 5 votes vote down vote up
/**
 * Adds a listener.
 *
 * @param listener the listener to add.
 */
public <T extends BraintreeListener> void addListener(T listener) {
    if (listener instanceof ConfigurationListener) {
        mConfigurationListener = (ConfigurationListener) listener;
    }

    if (listener instanceof BraintreeCancelListener) {
        mCancelListener = (BraintreeCancelListener) listener;
    }

    if (listener instanceof PaymentMethodNoncesUpdatedListener) {
        mPaymentMethodNoncesUpdatedListener = (PaymentMethodNoncesUpdatedListener) listener;
    }

    if (listener instanceof PaymentMethodNonceCreatedListener) {
        mPaymentMethodNonceCreatedListener = (PaymentMethodNonceCreatedListener) listener;
    }

    if (listener instanceof PaymentMethodNonceDeletedListener) {
        mPaymentMethodNonceDeletedListener = (PaymentMethodNonceDeletedListener) listener;
    }

    if (listener instanceof BraintreePaymentResultListener) {
        mBraintreePaymentResultListener = (BraintreePaymentResultListener) listener;
    }

    if (listener instanceof BraintreeErrorListener) {
        mErrorListener = (BraintreeErrorListener) listener;
    }

    if (listener instanceof UnionPayListener) {
        mUnionPayListener = (UnionPayListener) listener;
    }

    if (listener instanceof AmericanExpressListener) {
        mAmericanExpressListener = (AmericanExpressListener) listener;
    }

    flushCallbacks();
}
 
Example #21
Source File: DropInResultUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void fetchDropInResult_resetsBraintreeListenersWhenResultIsReturned()
        throws InvalidArgumentException, InterruptedException {
    BraintreeFragment fragment = setupFragment(new BraintreeUnitTestHttpClient()
            .configuration(new TestConfigurationBuilder().build())
            .successResponse(BraintreeUnitTestHttpClient.GET_PAYMENT_METHODS,
                    stringFromFixture("responses/get_payment_methods_two_cards_response.json")));
    BraintreeErrorListener errorListener = new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {}
    };
    fragment.addListener(errorListener);
    PaymentMethodNoncesUpdatedListener paymentMethodListener = new PaymentMethodNoncesUpdatedListener() {
        @Override
        public void onPaymentMethodNoncesUpdated(List<PaymentMethodNonce> paymentMethodNonces) {}
    };
    fragment.addListener(paymentMethodListener);
    DropInResult.DropInResultListener listener = new DropInResult.DropInResultListener() {
        @Override
        public void onError(Exception exception) {
            fail("onError called");
        }

        @Override
        public void onResult(DropInResult result) {
            assertEquals(PaymentMethodType.VISA, result.getPaymentMethodType());
            mCountDownLatch.countDown();
        }
    };

    DropInResult.fetchDropInResult(mActivity, base64EncodedClientTokenFromFixture("client_token.json"), listener);

    mCountDownLatch.await();
    List<BraintreeListener> listeners = fragment.getListeners();
    assertEquals(2, listeners.size());
    assertTrue(listeners.contains(errorListener));
    assertTrue(listeners.contains(paymentMethodListener));
}
 
Example #22
Source File: DropInResultUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void fetchDropInResult_resetsBraintreeListenersWhenErrorIsPosted()
        throws InvalidArgumentException, InterruptedException {
    BraintreeFragment fragment = setupFragment(new BraintreeUnitTestHttpClient()
            .configuration(new TestConfigurationBuilder().build())
            .errorResponse(BraintreeUnitTestHttpClient.GET_PAYMENT_METHODS, 404,
                    "No payment methods found"));
    BraintreeErrorListener errorListener = new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {}
    };
    fragment.addListener(errorListener);
    PaymentMethodNoncesUpdatedListener paymentMethodListener = new PaymentMethodNoncesUpdatedListener() {
        @Override
        public void onPaymentMethodNoncesUpdated(List<PaymentMethodNonce> paymentMethodNonces) {}
    };
    fragment.addListener(paymentMethodListener);
    DropInResult.DropInResultListener listener = new DropInResult.DropInResultListener() {
        @Override
        public void onError(Exception exception) {
            assertEquals("No payment methods found", exception.getMessage());
            mCountDownLatch.countDown();
        }

        @Override
        public void onResult(DropInResult result) {
            fail("onResult called");
        }
    };

    DropInResult.fetchDropInResult(mActivity, base64EncodedClientTokenFromFixture("client_token.json"), listener);

    mCountDownLatch.await();
    List<BraintreeListener> listeners = fragment.getListeners();
    assertEquals(2, listeners.size());
    assertTrue(listeners.contains(errorListener));
    assertTrue(listeners.contains(paymentMethodListener));
}
 
Example #23
Source File: DropInResultUnitTest.java    From braintree-android-drop-in with MIT License 4 votes vote down vote up
@Test
public void fetchDropInResult_resetsBraintreeListenersWhenPayWithGoogleResultIsReturned()
        throws InvalidArgumentException, InterruptedException {
    BraintreeSharedPreferences.getSharedPreferences(mActivity)
            .edit()
            .putString(DropInResult.LAST_USED_PAYMENT_METHOD_TYPE,
                    PaymentMethodType.GOOGLE_PAYMENT.getCanonicalName())
            .commit();
    googlePaymentReadyToPay(true);
    BraintreeFragment fragment = setupFragment(new BraintreeUnitTestHttpClient()
            .configuration(new TestConfigurationBuilder().build()));
    BraintreeErrorListener errorListener = new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {}
    };
    fragment.addListener(errorListener);
    PaymentMethodNoncesUpdatedListener paymentMethodListener = new PaymentMethodNoncesUpdatedListener() {
        @Override
        public void onPaymentMethodNoncesUpdated(List<PaymentMethodNonce> paymentMethodNonces) {}
    };
    fragment.addListener(paymentMethodListener);
    DropInResult.DropInResultListener listener = new DropInResult.DropInResultListener() {
        @Override
        public void onError(Exception exception) {
            fail("onError called");
        }

        @Override
        public void onResult(DropInResult result) {
            assertEquals(PaymentMethodType.GOOGLE_PAYMENT, result.getPaymentMethodType());
            mCountDownLatch.countDown();
        }
    };

    DropInResult.fetchDropInResult(mActivity, base64EncodedClientTokenFromFixture("client_token.json"), listener);

    mCountDownLatch.await();
    List<BraintreeListener> listeners = fragment.getListeners();
    assertEquals(2, listeners.size());
    assertTrue(listeners.contains(errorListener));
    assertTrue(listeners.contains(paymentMethodListener));
}
 
Example #24
Source File: BraintreeFragmentUnitTest.java    From braintree_android with MIT License 4 votes vote down vote up
@Test
public void addAndRemoveListenersAddAndRemoveAllListeners() throws InvalidArgumentException {
    BraintreeFragment fragment = BraintreeFragment.newInstance(mAppCompatActivity, TOKENIZATION_KEY);
    ConfigurationListener configurationListener = new ConfigurationListener() {
        @Override
        public void onConfigurationFetched(Configuration configuration) {}
    };
    BraintreeErrorListener braintreeErrorListener = new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {}
    };
    PaymentMethodNoncesUpdatedListener paymentMethodNoncesUpdatedListener = new PaymentMethodNoncesUpdatedListener() {
        @Override
        public void onPaymentMethodNoncesUpdated(List<PaymentMethodNonce> paymentMethodNonces) {}
    };
    PaymentMethodNonceCreatedListener paymentMethodNonceCreatedListener = new PaymentMethodNonceCreatedListener() {
        @Override
        public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {}
    };
    BraintreeCancelListener braintreeCancelListener = new BraintreeCancelListener() {
        @Override
        public void onCancel(int requestCode) {}
    };
    UnionPayListener unionPayListener = new UnionPayListener() {
        @Override
        public void onCapabilitiesFetched(UnionPayCapabilities capabilities) {}

        @Override
        public void onSmsCodeSent(String enrollmentId, boolean smsCodeRequired) {}
    };
    AmericanExpressListener americanExpressListener = new AmericanExpressListener() {
        @Override
        public void onRewardsBalanceFetched(AmericanExpressRewardsBalance rewardsBalance) {}
    };
    BraintreePaymentResultListener braintreePaymentResultListener = new BraintreePaymentResultListener() {
        @Override
        public void onBraintreePaymentResult(BraintreePaymentResult result) {}
    };

    fragment.addListener(configurationListener);
    fragment.addListener(braintreeErrorListener);
    fragment.addListener(paymentMethodNoncesUpdatedListener);
    fragment.addListener(paymentMethodNonceCreatedListener);
    fragment.addListener(braintreeCancelListener);
    fragment.addListener(unionPayListener);
    fragment.addListener(americanExpressListener);
    fragment.addListener(braintreePaymentResultListener);

    assertEquals(8, fragment.getListeners().size());

    fragment.removeListener(configurationListener);
    fragment.removeListener(braintreeErrorListener);
    fragment.removeListener(paymentMethodNoncesUpdatedListener);
    fragment.removeListener(paymentMethodNonceCreatedListener);
    fragment.removeListener(braintreeCancelListener);
    fragment.removeListener(unionPayListener);
    fragment.removeListener(americanExpressListener);
    fragment.removeListener(braintreePaymentResultListener);

    assertEquals(0, fragment.getListeners().size());
}