Search in sources :

Example 31 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener in project braintree_android by braintree.

the class DataCollector method collectDeviceData.

/**
 * Collect device information for fraud identification purposes. This should be used in conjunction
 * with a non-aggregate fraud id.
 *
 * @param fragment {@link BraintreeFragment}
 * @param merchantId The fraud merchant id from Braintree.
 * @param listener listener to be called with the device data String to send to Braintree.
 */
public static void collectDeviceData(final BraintreeFragment fragment, final String merchantId, final BraintreeResponseListener<String> listener) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            final JSONObject deviceData = new JSONObject();
            try {
                String clientMetadataId = getPayPalClientMetadataId(fragment.getApplicationContext());
                if (!TextUtils.isEmpty(clientMetadataId)) {
                    deviceData.put(CORRELATION_ID_KEY, clientMetadataId);
                }
            } catch (JSONException ignored) {
            }
            if (configuration.getKount().isEnabled()) {
                final String id;
                if (merchantId != null) {
                    id = merchantId;
                } else {
                    id = configuration.getKount().getKountMerchantId();
                }
                try {
                    final String deviceSessionId = UUIDHelper.getFormattedUUID();
                    startDeviceCollector(fragment, id, deviceSessionId, new BraintreeResponseListener<String>() {

                        @Override
                        public void onResponse(String sessionId) {
                            try {
                                deviceData.put(DEVICE_SESSION_ID_KEY, deviceSessionId);
                                deviceData.put(FRAUD_MERCHANT_ID_KEY, id);
                            } catch (JSONException ignored) {
                            }
                            listener.onResponse(deviceData.toString());
                        }
                    });
                } catch (ClassNotFoundException | NoClassDefFoundError | NumberFormatException ignored) {
                    listener.onResponse(deviceData.toString());
                }
            } else {
                listener.onResponse(deviceData.toString());
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) BraintreeResponseListener(com.braintreepayments.api.interfaces.BraintreeResponseListener) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException)

Example 32 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener 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 33 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener 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)

Example 34 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener 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 35 with ConfigurationListener

use of com.braintreepayments.api.interfaces.ConfigurationListener 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)

Aggregations

ConfigurationListener (com.braintreepayments.api.interfaces.ConfigurationListener)37 Configuration (com.braintreepayments.api.models.Configuration)36 Test (org.junit.Test)21 InvalidArgumentException (com.braintreepayments.api.exceptions.InvalidArgumentException)15 JSONException (org.json.JSONException)12 UnexpectedException (com.braintreepayments.api.exceptions.UnexpectedException)10 HttpResponseCallback (com.braintreepayments.api.interfaces.HttpResponseCallback)10 SharedPreferencesHelper.writeMockConfiguration (com.braintreepayments.testutils.SharedPreferencesHelper.writeMockConfiguration)10 ConfigurationException (com.braintreepayments.api.exceptions.ConfigurationException)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 BraintreeFragmentTestUtils.getFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration)4 BraintreeFragmentTestUtils.getMockFragmentWithConfiguration (com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration)4 BraintreeException (com.braintreepayments.api.exceptions.BraintreeException)4 BraintreeHttpClient (com.braintreepayments.api.internal.BraintreeHttpClient)4 JSONObject (org.json.JSONObject)4 Bundle (android.os.Bundle)3 Intent (android.content.Intent)2 BraintreeResponseListener (com.braintreepayments.api.interfaces.BraintreeResponseListener)2 UnionPayConfiguration (com.braintreepayments.api.models.UnionPayConfiguration)2 Uri (android.net.Uri)1