Search in sources :

Example 81 with Configuration

use of com.braintreepayments.api.models.Configuration in project braintree_android by braintree.

the class PaymentMethod method getPaymentMethodNonces.

/**
 * Retrieves the current list of {@link PaymentMethodNonce}s for the current customer.
 * <p/>
 * When finished, the {@link java.util.List} of {@link PaymentMethodNonce}s will be sent to {@link
 * PaymentMethodNoncesUpdatedListener#onPaymentMethodNoncesUpdated(List)}.
 *
 * @param fragment {@link BraintreeFragment}
 * @param defaultFirst when {@code true} the customer's default payment method will be first in the list, otherwise
 *        payment methods will be ordered my most recently used.
 */
public static void getPaymentMethodNonces(final BraintreeFragment fragment, boolean defaultFirst) {
    final Uri uri = Uri.parse(TokenizationClient.versionedPath(TokenizationClient.PAYMENT_METHOD_ENDPOINT)).buildUpon().appendQueryParameter("default_first", String.valueOf(defaultFirst)).appendQueryParameter("session_id", fragment.getSessionId()).build();
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fragment.getHttpClient().get(uri.toString(), new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    try {
                        fragment.postCallback(PaymentMethodNonce.parsePaymentMethodNonces(responseBody));
                        fragment.sendAnalyticsEvent("get-payment-methods.succeeded");
                    } catch (JSONException e) {
                        fragment.postCallback(e);
                        fragment.sendAnalyticsEvent("get-payment-methods.failed");
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.postCallback(exception);
                    fragment.sendAnalyticsEvent("get-payment-methods.failed");
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) Uri(android.net.Uri) JSONException(org.json.JSONException)

Example 82 with Configuration

use of com.braintreepayments.api.models.Configuration in project braintree_android by braintree.

the class UnionPay method fetchCapabilities.

/**
 * Fetches the capabilities of a card. If the card needs to be enrolled use {@link
 * UnionPay#enroll(BraintreeFragment, UnionPayCardBuilder)}.
 * <p/>
 * On completion, returns the {@link UnionPayCapabilities} to
 * {@link com.braintreepayments.api.interfaces.UnionPayListener#onCapabilitiesFetched(UnionPayCapabilities)}
 * <p/>
 * On error, an exception will be passed back to
 * {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 *
 * @param fragment {@link BraintreeFragment}
 * @param cardNumber The card number to check for Union Pay capabilities.
 */
public static void fetchCapabilities(final BraintreeFragment fragment, final String cardNumber) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (!configuration.getUnionPay().isEnabled()) {
                fragment.postCallback(new ConfigurationException("UnionPay is not enabled"));
                return;
            }
            String fetchCapabilitiesUrl = Uri.parse(UNIONPAY_CAPABILITIES_PATH).buildUpon().appendQueryParameter("creditCard[number]", cardNumber).build().toString();
            fragment.getHttpClient().get(fetchCapabilitiesUrl, new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    fragment.postCallback(UnionPayCapabilities.fromJson(responseBody));
                    fragment.sendAnalyticsEvent("union-pay.capabilities-received");
                }

                @Override
                public void failure(Exception exception) {
                    fragment.postCallback(exception);
                    fragment.sendAnalyticsEvent("union-pay.capabilities-failed");
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) Configuration(com.braintreepayments.api.models.Configuration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Example 83 with Configuration

use of com.braintreepayments.api.models.Configuration in project braintree_android by braintree.

the class UnionPay method enroll.

/**
 * Enrolls a Union Pay card. Only call this method if the card needs to be enrolled. Check {@link
 * UnionPay#fetchCapabilities(BraintreeFragment, String)} if your card needs to be enrolled.
 * <p/>
 * On completion, returns a enrollmentId to
 * {@link com.braintreepayments.api.interfaces.UnionPayListener#onSmsCodeSent(String, boolean)}
 * This enrollmentId needs to be applied to {@link UnionPayCardBuilder} along with the SMS code
 * collected from the merchant before invoking {@link UnionPay#tokenize(BraintreeFragment, UnionPayCardBuilder)}
 * <p/>
 * On error, an exception will be passed back to
 * {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 *
 * @param fragment {@link BraintreeFragment}
 * @param unionPayCardBuilder {@link UnionPayCardBuilder}
 */
public static void enroll(final BraintreeFragment fragment, final UnionPayCardBuilder unionPayCardBuilder) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            UnionPayConfiguration unionPayConfiguration = configuration.getUnionPay();
            if (!unionPayConfiguration.isEnabled()) {
                fragment.postCallback(new ConfigurationException("UnionPay is not enabled"));
                return;
            }
            try {
                JSONObject enrollmentPayloadJson = unionPayCardBuilder.buildEnrollment();
                fragment.getHttpClient().post(UNIONPAY_ENROLLMENT_PATH, enrollmentPayloadJson.toString(), new HttpResponseCallback() {

                    @Override
                    public void success(String responseBody) {
                        try {
                            JSONObject response = new JSONObject(responseBody);
                            String enrollmentId = response.getString(UNIONPAY_ENROLLMENT_ID_KEY);
                            boolean smsCodeRequired = response.getBoolean(UNIONPAY_SMS_REQUIRED_KEY);
                            fragment.postUnionPayCallback(enrollmentId, smsCodeRequired);
                            fragment.sendAnalyticsEvent("union-pay.enrollment-succeeded");
                        } catch (JSONException e) {
                            failure(e);
                        }
                    }

                    @Override
                    public void failure(Exception exception) {
                        fragment.postCallback(exception);
                        fragment.sendAnalyticsEvent("union-pay.enrollment-failed");
                    }
                });
            } catch (JSONException exception) {
                fragment.postCallback(exception);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) UnionPayConfiguration(com.braintreepayments.api.models.UnionPayConfiguration) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Example 84 with Configuration

use of com.braintreepayments.api.models.Configuration in project braintree_android by braintree.

the class BraintreeFragment method sendAnalyticsEvent.

public void sendAnalyticsEvent(final String eventFragment) {
    final AnalyticsEvent request = new AnalyticsEvent(mContext, getSessionId(), mIntegrationType, eventFragment);
    waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (configuration.getAnalytics().isEnabled()) {
                mAnalyticsDatabase.addEvent(request);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) AnalyticsEvent(com.braintreepayments.api.internal.AnalyticsEvent) Configuration(com.braintreepayments.api.models.Configuration)

Aggregations

Configuration (com.braintreepayments.api.models.Configuration)84 Test (org.junit.Test)66 ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)36 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)35 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)19 JSONException (org.json.JSONException)19 Authorization (com.braintreepayments.api.models.Authorization)14 Intent (android.content.Intent)13 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)13 TestConfigurationBuilder (com.braintreepayments.testutils.TestConfigurationBuilder)12 AuthorizationRequest (com.paypal.android.sdk.onetouch.core.AuthorizationRequest)11 BillingAgreementRequest (com.paypal.android.sdk.onetouch.core.BillingAgreementRequest)11 CheckoutRequest (com.paypal.android.sdk.onetouch.core.CheckoutRequest)11 JSONObject (org.json.JSONObject)11 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)10 Request (com.paypal.android.sdk.onetouch.core.Request)10 Bundle (android.os.Bundle)9 BraintreeFragmentTestUtils.getMockFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration)6 BraintreeFragmentTestUtils.getFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration)5