/**
   * Create a new instance of {@link BraintreeFragment} using the client token and add it to the
   * {@link Activity}'s {@link FragmentManager}.
   *
   * @param activity The {@link Activity} to add the {@link Fragment} to.
   * @param authorization The tokenization key or client token to use.
   * @return {@link BraintreeFragment}
   * @throws InvalidArgumentException If the tokenization key or client token is not valid or cannot
   *     be parsed.
   */
  public static BraintreeFragment newInstance(Activity activity, String authorization)
      throws InvalidArgumentException {
    FragmentManager fm = activity.getFragmentManager();

    String integrationType = "custom";
    try {
      if (Class.forName("com.braintreepayments.api.BraintreePaymentActivity")
          .isInstance(activity)) {
        integrationType = "dropin";
      }
    } catch (ClassNotFoundException ignored) {
    }

    BraintreeFragment braintreeFragment = (BraintreeFragment) fm.findFragmentByTag(TAG);
    if (braintreeFragment == null) {
      braintreeFragment = new BraintreeFragment();
      Bundle bundle = new Bundle();

      try {
        Authorization auth = Authorization.fromString(authorization);
        bundle.putParcelable(EXTRA_AUTHORIZATION_TOKEN, auth);
      } catch (InvalidArgumentException e) {
        throw new InvalidArgumentException("Tokenization Key or client token was invalid.");
      }

      bundle.putString(EXTRA_INTEGRATION_TYPE, integrationType);
      braintreeFragment.setArguments(bundle);
      fm.beginTransaction().add(braintreeFragment, TAG).commit();
    }

    return braintreeFragment;
  }
Exemplo n.º 2
0
  @Override
  protected void onAuthorizationFetched() {
    try {
      mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);
    } catch (InvalidArgumentException e) {
      onError(e);
    }

    setProgressBarIndeterminateVisibility(false);
    mPurchaseButton.setEnabled(true);
  }