com.braintreepayments.api.BraintreeFragment Java Examples

The following examples show how to use com.braintreepayments.api.BraintreeFragment. 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: AddCardActivityPowerMockTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Before
public void setup() throws NoSuchFieldException, IllegalAccessException {
    mFragment = mock(BraintreeFragment.class);

    mActivity = Robolectric.buildActivity(AddCardUnitTestActivity.class).get();

    setField(BaseActivity.class, mActivity, "mBraintreeFragment", mFragment);

    EditCardView editCardView = mock(EditCardView.class);
    when(editCardView.getCardForm()).thenReturn(mock(CardForm.class));
    setField(AddCardActivity.class, mActivity, "mEditCardView", editCardView);

    mockStatic(ThreeDSecure.class);
    doNothing().when(ThreeDSecure.class);
    ThreeDSecure.performVerification(any(BraintreeFragment.class), any(ThreeDSecureRequest.class));
}
 
Example #2
Source File: DropInResultUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void fetchDropInResult_clearsListenersWhenErrorIsPosted()
        throws InvalidArgumentException, InterruptedException {
    BraintreeFragment fragment = setupFragment(new BraintreeUnitTestHttpClient()
            .configuration(new TestConfigurationBuilder().build())
            .errorResponse(BraintreeUnitTestHttpClient.GET_PAYMENT_METHODS, 404,
                    "No payment methods found"));
    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();
    assertEquals(0, fragment.getListeners().size());
}
 
Example #3
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void doesNotSendAnalyticsEventTwiceWhenRecreated() {
    BraintreeFragment fragment = mock(BraintreeFragment.class);
    setup(fragment);

    Bundle bundle = new Bundle();
    mActivityController.saveInstanceState(bundle)
            .pause()
            .stop()
            .destroy();

    mActivityController = Robolectric.buildActivity(DropInUnitTestActivity.class);
    mActivity = (DropInUnitTestActivity) mActivityController.get();
    mActivity.braintreeFragment = fragment;
    mActivityController.setup(bundle);
    mActivity.braintreeFragment.onAttach(mActivity);
    mActivity.braintreeFragment.onResume();

    verify(mActivity.braintreeFragment, times(1)).sendAnalyticsEvent("appeared");
}
 
Example #4
Source File: DropInResultUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void fetchDropInResult_clearsListenersWhenResultIsReturned()
        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")));
    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(0, listeners.size());
}
 
Example #5
Source File: AddCardActivityUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void nonFormFieldError_callsFinishWithError() {
    setup(mock(BraintreeFragment.class));

    ErrorWithResponse error = new ErrorWithResponse(422, "{\n" +
            "  \"error\": {\n" +
            "    \"message\": \"Error message\"\n" +
            "  }\n" +
            "}");

    mActivity.onError(error);

    assertTrue(mActivity.isFinishing());
    assertEquals(RESULT_FIRST_USER, mShadowActivity.getResultCode());
    Exception actualException = (Exception) mShadowActivity.getResultIntent()
            .getSerializableExtra(DropInActivity.EXTRA_ERROR);
    assertEquals(error.getClass(), actualException.getClass());
    assertEquals("Error message", actualException.getMessage());
}
 
Example #6
Source File: DropInActivityPowerMockTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void onPaymentMethodSelected_withPayPalRequestWithoutAmount_startsRequestBillingAgreement() {
    PayPalRequest paypalRequest = new PayPalRequest()
            .billingAgreementDescription("Peter Pipers Pepper Pickers");
    DropInRequest dropInRequest = new DropInRequest()
            .paypalRequest(paypalRequest);
    mActivity.setDropInRequest(dropInRequest);
    mockStatic(PayPal.class);
    doNothing().when(PayPal.class);
    PayPal.requestBillingAgreement(any(BraintreeFragment.class), any(PayPalRequest.class));

    mActivity.onPaymentMethodSelected(PaymentMethodType.PAYPAL);

    verifyStatic();
    PayPal.requestBillingAgreement(eq(mActivity.braintreeFragment), any(PayPalRequest.class));
}
 
Example #7
Source File: DropInActivityPowerMockTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void onPaymentMethodSelected_withPayPalRequestAndAmount_startsRequestOneTimePayment() {
    PayPalRequest paypalRequest = new PayPalRequest("10")
            .currencyCode("USD");
    DropInRequest dropInRequest = new DropInRequest()
            .paypalRequest(paypalRequest);
    mActivity.setDropInRequest(dropInRequest);
    mockStatic(PayPal.class);
    doNothing().when(PayPal.class);
    PayPal.requestOneTimePayment(any(BraintreeFragment.class), any(PayPalRequest.class));

    mActivity.onPaymentMethodSelected(PaymentMethodType.PAYPAL);

    verifyStatic();
    PayPal.requestOneTimePayment(eq(mActivity.braintreeFragment), any(PayPalRequest.class));
}
 
Example #8
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void clickingVaultedPaymentMethod_whenCard_sendsAnalyticEvent() {
    PaymentMethodNonce nonce = mock(CardNonce.class);
    ArrayList<PaymentMethodNonce> nonces = new ArrayList<>();
    nonces.add(nonce);

    BraintreeFragment fragment = mock(BraintreeFragment.class);

    setup(fragment);

    mActivity.onPaymentMethodNoncesUpdated(nonces);
    RecyclerView recyclerView = mActivity.findViewById(R.id.bt_vaulted_payment_methods);
    recyclerView.measure(0, 0);
    recyclerView.layout(0, 0, 100, 10000);
    recyclerView.findViewHolderForAdapterPosition(0).itemView.callOnClick();

    verify(fragment).sendAnalyticsEvent("vaulted-card.select");
}
 
Example #9
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void clickingVaultedPaymentMethod_whenPayPal_doesNotSendAnalyticEvent() {
    PaymentMethodNonce nonce = mock(PayPalAccountNonce.class);
    ArrayList<PaymentMethodNonce> nonces = new ArrayList<>();
    nonces.add(nonce);

    BraintreeFragment fragment = mock(BraintreeFragment.class);

    setup(fragment);

    mActivity.onPaymentMethodNoncesUpdated(nonces);
    RecyclerView recyclerView = mActivity.findViewById(R.id.bt_vaulted_payment_methods);
    recyclerView.measure(0, 0);
    recyclerView.layout(0, 0, 100, 10000);
    recyclerView.findViewHolderForAdapterPosition(0).itemView.callOnClick();

    verify(fragment, never()).sendAnalyticsEvent("vaulted-card.select");
}
 
Example #10
Source File: AddCardActivityPowerMockTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void performsThreeDSecureVerificationWhenRequestedEnableAndAmountPresent()
        throws NoSuchFieldException, IllegalAccessException, JSONException {
    DropInRequest dropInRequest = new DropInRequest()
            .amount("1.00")
            .requestThreeDSecureVerification(true);
    Configuration configuration = mock(Configuration.class);
    when(configuration.isThreeDSecureEnabled()).thenReturn(true);
    setConfiguration(dropInRequest, configuration);

    mActivity.onPaymentMethodNonceCreated(CardNonce.fromJson(
            stringFromFixture("responses/visa_credit_card_response.json")));

    verifyStatic();
    ThreeDSecure.performVerification(any(BraintreeFragment.class), any(ThreeDSecureRequest.class));
}
 
Example #11
Source File: Braintree.java    From react-native-braintree-android with MIT License 6 votes vote down vote up
@ReactMethod
public void setup(final String token, final Callback successCallback, final Callback errorCallback) {
  try {
    this.mBraintreeFragment = BraintreeFragment.newInstance(getCurrentActivity(), token);
    this.mBraintreeFragment.addListener(new PaymentMethodNonceCreatedListener() {
      @Override
      public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
        nonceCallback(paymentMethodNonce.getNonce());
      }
    });
    this.setToken(token);
    successCallback.invoke(this.getToken());
  } catch (InvalidArgumentException e) {
    errorCallback.invoke(e.getMessage());
  }
}
 
Example #12
Source File: FlutterBraintreeCustom.java    From FlutterBraintree with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flutter_braintree_custom);
    try {
        Intent intent = getIntent();
        braintreeFragment = BraintreeFragment.newInstance(this, intent.getStringExtra("authorization"));
        String type = intent.getStringExtra("type");
        if (type.equals("tokenizeCreditCard")) {
            tokenizeCreditCard();
        } else if (type.equals("requestPaypalNonce")) {
            requestPaypalNonce();
        } else {
            throw new Exception("Invalid request type: " + type);
        }
    } catch (Exception e) {
        Intent result = new Intent();
        result.putExtra("error", e);
        setResult(2, result);
        finish();
        return;
    }
}
 
Example #13
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 6 votes vote down vote up
@Test
public void onActivityResult_vaultedPaymentEditedReturnsToDropIn() {
    setup(mock(BraintreeFragment.class));

    PayPalAccountNonce paypalNonce = mock(PayPalAccountNonce.class);
    when(paypalNonce.getDescription()).thenReturn("paypal-nonce");

    ArrayList<Parcelable> paymentMethodNonces = new ArrayList<Parcelable>();
    paymentMethodNonces.add(paypalNonce);

    assertEquals(0, ((ViewSwitcher) mActivity.findViewById(R.id.bt_loading_view_switcher)).getDisplayedChild());

    mActivity.onActivityResult(DELETE_PAYMENT_METHOD_NONCE_CODE, RESULT_OK, new Intent()
            .putExtra("com.braintreepayments.api.EXTRA_PAYMENT_METHOD_NONCES", paymentMethodNonces));

    assertEquals(1, ((ViewSwitcher) mActivity.findViewById(R.id.bt_loading_view_switcher)).getDisplayedChild());
}
 
Example #14
Source File: AddCardActivityPowerMockTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void skipsThreeDSecureWhenAmountMissing()
        throws NoSuchFieldException, IllegalAccessException, JSONException {
    DropInRequest dropInRequest = new DropInRequest()
            .requestThreeDSecureVerification(true);
    Configuration configuration = mock(Configuration.class);
    when(configuration.isThreeDSecureEnabled()).thenReturn(true);
    setConfiguration(dropInRequest, configuration);

    mActivity.onPaymentMethodNonceCreated(CardNonce.fromJson(
            stringFromFixture("responses/visa_credit_card_response.json")));

    verifyStatic(never());
    ThreeDSecure.performVerification(any(BraintreeFragment.class), any(ThreeDSecureRequest.class));
}
 
Example #15
Source File: VisaCheckoutActivity.java    From braintree_android with MIT License 5 votes vote down vote up
@Override
protected void onAuthorizationFetched() {
    try {
        mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
    } catch (InvalidArgumentException e) {
        onError(e);
    }

    VisaCheckout.createProfileBuilder(mBraintreeFragment, this);
}
 
Example #16
Source File: AddCardActivityPowerMockTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void skipsThreeDSecureWhenRequestedButNotEnabled()
        throws NoSuchFieldException, IllegalAccessException, JSONException {
    DropInRequest dropInRequest = new DropInRequest()
            .amount("1.00")
            .requestThreeDSecureVerification(true);
    Configuration configuration = mock(Configuration.class);
    setConfiguration(dropInRequest, configuration);

    mActivity.onPaymentMethodNonceCreated(CardNonce.fromJson(
            stringFromFixture("responses/visa_credit_card_response.json")));

    verifyStatic(never());
    ThreeDSecure.performVerification(any(BraintreeFragment.class), any(ThreeDSecureRequest.class));
}
 
Example #17
Source File: LocalPaymentsActivity.java    From braintree_android with MIT License 5 votes vote down vote up
@Override
protected void onAuthorizationFetched() {
    if (!Settings.SANDBOX_ENV_NAME.equals(Settings.getEnvironment(this))) {
        onError(new Exception("To use this feature, enable the \"Sandbox\" environment."));
        return;
    }

    try {
        mBraintreeFragment = BraintreeFragment.newInstance(this, Settings.getLocalPaymentsTokenizationKey(this));
        mIdealButton.setEnabled(true);
    } catch (InvalidArgumentException e) {
        onError(e);
    }
}
 
Example #18
Source File: PayPalTwoFactorAuthActivity.java    From braintree_android with MIT License 5 votes vote down vote up
@Override
protected void onAuthorizationFetched() {
    try {
        if (!Settings.SANDBOX_INDIA_ENV_NAME.equals(Settings.getEnvironment(this))) {
            onError(new Exception("To use feature, enable the \"Sandbox India\" environment."));
            return;
        }

        mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
        mBillingAgreementButton.setEnabled(true);
    } catch (InvalidArgumentException e) {
        onError(e);
    }
}
 
Example #19
Source File: GooglePaymentActivity.java    From braintree_android with MIT License 5 votes vote down vote up
@Override
protected void onAuthorizationFetched() {
    try {
        mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
    } catch (InvalidArgumentException e) {
        onError(e);
    }
}
 
Example #20
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void onVaultEditButtonClick_sendsAnalyticEvent() {
    setup(mock(BraintreeFragment.class));

    mActivity.onVaultEditButtonClick(null);

    verify(mActivity.mBraintreeFragment).sendAnalyticsEvent("manager.appeared");
}
 
Example #21
Source File: AddCardActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void setsTitleToCardDetailsWhenStarted() {
    setup(mock(BraintreeFragment.class));

    assertEquals(RuntimeEnvironment.application.getString(R.string.bt_card_details),
            mActivity.getSupportActionBar().getTitle());
}
 
Example #22
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void vaultEditButton_whenVaultManagerDisabled_isInvisible() {
    setup(mock(BraintreeFragment.class));

    List<PaymentMethodNonce> nonceList = new ArrayList<>();
    nonceList.add(mock(CardNonce.class));

    mActivity.setDropInRequest(new DropInRequest()
            .vaultManager(false));

    mActivity.onPaymentMethodNoncesUpdated(nonceList);

    assertEquals(View.INVISIBLE, mActivity.findViewById(R.id.bt_vault_edit_button).getVisibility());
}
 
Example #23
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void vaultEditButton_whenVaultManagerEnabled_isVisible() {
    setup(mock(BraintreeFragment.class));

    List<PaymentMethodNonce> nonceList = new ArrayList<>();
    nonceList.add(mock(CardNonce.class));

    mActivity.setDropInRequest(new DropInRequest()
            .vaultManager(true));

    mActivity.onPaymentMethodNoncesUpdated(nonceList);

    assertEquals(View.VISIBLE, mActivity.findViewById(R.id.bt_vault_edit_button).getVisibility());
}
 
Example #24
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void downForMaintenanceExceptionExitsActivityWithError() {
    setup(mock(BraintreeFragment.class));

    assertExceptionIsReturned("server-unavailable",
            new DownForMaintenanceException("Exception"));
}
 
Example #25
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void unexpectedExceptionExitsActivityWithError() {
    setup(mock(BraintreeFragment.class));

    assertExceptionIsReturned("server-error",
            new UnexpectedException("Exception"));
}
 
Example #26
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void serverExceptionExitsActivityWithError() {
    setup(mock(BraintreeFragment.class));

    assertExceptionIsReturned("server-error",
            new ServerException("Exception"));
}
 
Example #27
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void upgradeRequiredExceptionExitsActivityWithError() {
    setup(mock(BraintreeFragment.class));

    assertExceptionIsReturned("developer-error",
            new UpgradeRequiredException("Exception"));
}
 
Example #28
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void authorizationExceptionExitsActivityWithError() {
    setup(mock(BraintreeFragment.class));

    assertExceptionIsReturned("developer-error",
            new AuthorizationException("Access denied"));
}
 
Example #29
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void authenticationExceptionExitsActivityWithError() {
    setup(mock(BraintreeFragment.class));

    assertExceptionIsReturned("developer-error",
            new AuthenticationException("Access denied"));
}
 
Example #30
Source File: DropInActivityUnitTest.java    From braintree-android-drop-in with MIT License 5 votes vote down vote up
@Test
public void onActivityResult_nonceFromAddCardActivity_doesNotSendVaultAnalyticEvent() throws JSONException {
    setup(mock(BraintreeFragment.class));

    DropInResult result = new DropInResult()
            .paymentMethodNonce(CardNonce.fromJson(
                    stringFromFixture("responses/visa_credit_card_response.json")));
    Intent data = new Intent()
            .putExtra(DropInResult.EXTRA_DROP_IN_RESULT, result);

    mActivity.onActivityResult(1, RESULT_OK, data);

    verify(mActivity.braintreeFragment, never()).sendAnalyticsEvent("vaulted-card.appear");
}