Search in sources :

Example 6 with BraintreeException

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

the class AndroidPay method requestAndroidPay.

/**
 * @deprecated Android Pay is deprecated, use {@link GooglePayment} instead. For more information see the
 * <a href="https://developers.braintreepayments.com/guides/pay-with-google/overview">documentation</a>
 *
 * Launch an Android Pay masked wallet request. This method will show the payment instrument
 * chooser to the user.
 *
 * @param fragment The current {@link BraintreeFragment}.
 * @param cart The cart representation with price, currency code, and optionally items.
 * @param shippingAddressRequired {@code true} if this request requires a shipping address, {@code false} otherwise.
 * @param phoneNumberRequired {@code true} if this request requires a phone number, {@code false} otherwise.
 * @param allowedCountries ISO 3166-2 country codes that shipping is allowed to.
 */
@Deprecated
public static void requestAndroidPay(final BraintreeFragment fragment, @NonNull final Cart cart, final boolean shippingAddressRequired, final boolean phoneNumberRequired, final ArrayList<CountrySpecification> allowedCountries) {
    fragment.sendAnalyticsEvent("android-pay.selected");
    if (!validateManifest(fragment.getApplicationContext())) {
        fragment.postCallback(new BraintreeException("AndroidPayActivity was not found in the Android manifest, " + "or did not have a theme of R.style.bt_transparent_activity"));
        fragment.sendAnalyticsEvent("android-pay.failed");
        return;
    }
    if (cart == null) {
        fragment.postCallback(new BraintreeException("Cannot pass null cart to performMaskedWalletRequest"));
        fragment.sendAnalyticsEvent("android-pay.failed");
        return;
    }
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fragment.sendAnalyticsEvent("android-pay.started");
            Intent intent = new Intent(fragment.getApplicationContext(), AndroidPayActivity.class).putExtra(EXTRA_ENVIRONMENT, GooglePayment.getEnvironment(configuration.getAndroidPay())).putExtra(EXTRA_MERCHANT_NAME, configuration.getAndroidPay().getDisplayName()).putExtra(EXTRA_CART, cart).putExtra(EXTRA_TOKENIZATION_PARAMETERS, GooglePayment.getTokenizationParameters(fragment)).putIntegerArrayListExtra(EXTRA_ALLOWED_CARD_NETWORKS, GooglePayment.getAllowedCardNetworks(fragment)).putExtra(EXTRA_SHIPPING_ADDRESS_REQUIRED, shippingAddressRequired).putExtra(EXTRA_PHONE_NUMBER_REQUIRED, phoneNumberRequired).putParcelableArrayListExtra(EXTRA_ALLOWED_COUNTRIES, allowedCountries).putExtra(EXTRA_REQUEST_TYPE, AUTHORIZE);
            fragment.startActivityForResult(intent, BraintreeRequestCodes.ANDROID_PAY);
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) Intent(android.content.Intent)

Example 7 with BraintreeException

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

the class PayPalUnitTest method requestOneTimePayment_postsExceptionWhenNoAmountIsSet.

@Test
public void requestOneTimePayment_postsExceptionWhenNoAmountIsSet() {
    BraintreeFragment fragment = mMockFragmentBuilder.build();
    PayPal.requestOneTimePayment(fragment, new PayPalRequest());
    ArgumentCaptor<Exception> captor = ArgumentCaptor.forClass(Exception.class);
    verify(fragment).postCallback(captor.capture());
    assertTrue(captor.getValue() instanceof BraintreeException);
    assertEquals("An amount must be specified for the Single Payment flow.", captor.getValue().getMessage());
}
Also used : BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with BraintreeException

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

the class UnionPayUnitTest method enroll_sendsAnalyticsEventOnFailure.

@Test
public void enroll_sendsAnalyticsEventOnFailure() throws JSONException {
    UnionPayCardBuilder unionPayCardBuilder = new UnionPayCardBuilder().cardNumber("some-card-number");
    BraintreeFragment braintreeFragment = new MockFragmentBuilder().configuration(mConfigurationWithUnionPay).errorResponse(new BraintreeException()).build();
    UnionPay.enroll(braintreeFragment, unionPayCardBuilder);
    verify(braintreeFragment).sendAnalyticsEvent("union-pay.enrollment-failed");
}
Also used : UnionPayCardBuilder(com.braintreepayments.api.models.UnionPayCardBuilder) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with BraintreeException

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

the class GooglePayment method requestPayment.

/**
 * Launch a Google Payments request. This method will show the payment instrument chooser to the user.
 *
 * @param fragment The current {@link BraintreeFragment}.
 * @param request The {@link GooglePaymentRequest} containing options for the transaction.
 */
public static void requestPayment(final BraintreeFragment fragment, @NonNull final GooglePaymentRequest request) {
    fragment.sendAnalyticsEvent("google-payment.selected");
    if (!validateManifest(fragment.getApplicationContext())) {
        fragment.postCallback(new BraintreeException("GooglePaymentActivity was not found in the Android " + "manifest, or did not have a theme of R.style.bt_transparent_activity"));
        fragment.sendAnalyticsEvent("google-payment.failed");
        return;
    }
    if (request == null || request.getTransactionInfo() == null) {
        fragment.postCallback(new BraintreeException("Cannot pass null TransactionInfo to requestPayment"));
        fragment.sendAnalyticsEvent("google-payment.failed");
        return;
    }
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            PaymentDataRequest.Builder paymentDataRequest = PaymentDataRequest.newBuilder().setTransactionInfo(request.getTransactionInfo()).addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_CARD).addAllowedPaymentMethod(WalletConstants.PAYMENT_METHOD_TOKENIZED_CARD).setPaymentMethodTokenizationParameters(getTokenizationParameters(fragment));
            CardRequirements.Builder cardRequirements = CardRequirements.newBuilder().addAllowedCardNetworks(getAllowedCardNetworks(fragment));
            if (request.getAllowPrepaidCards() != null) {
                cardRequirements.setAllowPrepaidCards(request.getAllowPrepaidCards());
            }
            if (request.getBillingAddressFormat() != null) {
                cardRequirements.setBillingAddressFormat(request.getBillingAddressFormat());
            }
            if (request.isBillingAddressRequired() != null) {
                cardRequirements.setBillingAddressRequired(request.isBillingAddressRequired());
            }
            paymentDataRequest.setCardRequirements(cardRequirements.build());
            if (request.isEmailRequired() != null) {
                paymentDataRequest.setEmailRequired(request.isEmailRequired());
            }
            if (request.isPhoneNumberRequired() != null) {
                paymentDataRequest.setPhoneNumberRequired(request.isPhoneNumberRequired());
            }
            if (request.isShippingAddressRequired() != null) {
                paymentDataRequest.setShippingAddressRequired(request.isShippingAddressRequired());
            }
            if (request.getShippingAddressRequirements() != null) {
                paymentDataRequest.setShippingAddressRequirements(request.getShippingAddressRequirements());
            }
            if (request.isUiRequired() != null) {
                paymentDataRequest.setUiRequired(request.isUiRequired());
            }
            fragment.sendAnalyticsEvent("google-payment.started");
            Intent intent = new Intent(fragment.getApplicationContext(), GooglePaymentActivity.class).putExtra(EXTRA_ENVIRONMENT, getEnvironment(configuration.getAndroidPay())).putExtra(EXTRA_PAYMENT_DATA_REQUEST, paymentDataRequest.build());
            fragment.startActivityForResult(intent, BraintreeRequestCodes.GOOGLE_PAYMENT);
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) AndroidPayConfiguration(com.braintreepayments.api.models.AndroidPayConfiguration) Configuration(com.braintreepayments.api.models.Configuration) MetadataBuilder(com.braintreepayments.api.models.MetadataBuilder) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) Intent(android.content.Intent)

Example 10 with BraintreeException

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

the class PayPal method requestOneTimePayment.

private static void requestOneTimePayment(final BraintreeFragment fragment, final PayPalRequest paypalRequest, final boolean isBillingAgreement, final PayPalApprovalHandler handler) {
    final HttpResponseCallback callback = new HttpResponseCallback() {

        @Override
        public void success(String responseBody) {
            final PayPalPaymentResource paypalPaymentResource;
            try {
                paypalPaymentResource = PayPalPaymentResource.fromJson(responseBody);
            } catch (JSONException e) {
                fragment.postCallback(e);
                return;
            }
            String redirectUrl = Uri.parse(paypalPaymentResource.getRedirectUrl()).buildUpon().appendQueryParameter(USER_ACTION_KEY, paypalRequest.getUserAction()).toString();
            Request request;
            if (isBillingAgreement) {
                request = getBillingAgreementRequest(fragment, redirectUrl);
            } else {
                request = getCheckoutRequest(fragment, redirectUrl);
            }
            startPayPal(fragment, request, handler);
        }

        @Override
        public void failure(Exception e) {
            fragment.postCallback(e);
        }
    };
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (!configuration.isPayPalEnabled()) {
                fragment.postCallback(new BraintreeException("PayPal is not enabled"));
                return;
            }
            if (!isManifestValid(fragment)) {
                fragment.sendAnalyticsEvent("paypal.invalid-manifest");
                fragment.postCallback(new BraintreeException("BraintreeBrowserSwitchActivity missing, " + "incorrectly configured in AndroidManifest.xml or another app defines the same browser " + "switch url as this app. See " + "https://developers.braintreepayments.com/guides/client-sdk/android/v2#browser-switch " + "for the correct configuration"));
                return;
            }
            try {
                persistPayPalRequest(fragment.getApplicationContext(), paypalRequest);
                createPaymentResource(fragment, paypalRequest, isBillingAgreement, callback);
            } catch (JSONException | ErrorWithResponse | BraintreeException ex) {
                fragment.postCallback(ex);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) PayPalPaymentResource(com.braintreepayments.api.models.PayPalPaymentResource) PayPalConfiguration(com.braintreepayments.api.models.PayPalConfiguration) Configuration(com.braintreepayments.api.models.Configuration) Request(com.paypal.android.sdk.onetouch.core.Request) PayPalRequest(com.braintreepayments.api.models.PayPalRequest) BillingAgreementRequest(com.paypal.android.sdk.onetouch.core.BillingAgreementRequest) AuthorizationRequest(com.paypal.android.sdk.onetouch.core.AuthorizationRequest) CheckoutRequest(com.paypal.android.sdk.onetouch.core.CheckoutRequest) PendingRequest(com.paypal.android.sdk.onetouch.core.sdk.PendingRequest) JSONException(org.json.JSONException) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) BraintreeException(com.braintreepayments.api.exceptions.BraintreeException) JSONException(org.json.JSONException) BrowserSwitchException(com.braintreepayments.api.exceptions.BrowserSwitchException)

Aggregations

BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)12 JSONException (org.json.JSONException)4 Test (org.junit.Test)4 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)3 Configuration (com.braintreepayments.api.models.Configuration)3 PayPalRequest (com.braintreepayments.api.models.PayPalRequest)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Intent (android.content.Intent)2 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)2 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)2 Context (android.content.Context)1 Resources (android.content.res.Resources)1 NotFoundException (android.content.res.Resources.NotFoundException)1 Uri (android.net.Uri)1 BrowserSwitchException (com.braintreepayments.api.exceptions.BrowserSwitchException)1 ErrorWithResponse (com.braintreepayments.api.exceptions.ErrorWithResponse)1 AndroidPayConfiguration (com.braintreepayments.api.models.AndroidPayConfiguration)1 MetadataBuilder (com.braintreepayments.api.models.MetadataBuilder)1 PayPalConfiguration (com.braintreepayments.api.models.PayPalConfiguration)1 PayPalPaymentResource (com.braintreepayments.api.models.PayPalPaymentResource)1