Search in sources :

Example 41 with Configuration

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

the class GooglePaymentTest method getTokenizationParameters_includesATokenizationKeyWhenPresent.

@Test(timeout = 5000)
public void getTokenizationParameters_includesATokenizationKeyWhenPresent() throws Exception {
    final BraintreeFragment fragment = getFragment(mActivityTestRule.getActivity(), TOKENIZATION_KEY, mBaseConfiguration.withAnalytics().build());
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            Bundle tokenizationParameters = GooglePayment.getTokenizationParameters(fragment).getParameters();
            assertEquals(TOKENIZATION_KEY, tokenizationParameters.getString("braintree:clientKey"));
            mLatch.countDown();
        }
    });
    mLatch.await();
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) BraintreeFragmentTestUtils.getFragmentWithConfiguration(com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration) BraintreeFragmentTestUtils.getMockFragmentWithConfiguration(com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration) Configuration(com.braintreepayments.api.models.Configuration) Bundle(android.os.Bundle) Test(org.junit.Test)

Example 42 with Configuration

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

the class GooglePaymentTest method getTokenizationParameters_returnsCorrectParametersInCallback.

@Test(timeout = 5000)
public void getTokenizationParameters_returnsCorrectParametersInCallback() throws Exception {
    String config = mBaseConfiguration.androidPay(mBaseConfiguration.androidPay().supportedNetworks(new String[] { "visa", "mastercard", "amex", "discover" })).build();
    final Configuration configuration = Configuration.fromJson(config);
    BraintreeFragment fragment = getFragment(mActivityTestRule.getActivity(), TOKENIZATION_KEY, config);
    GooglePayment.getTokenizationParameters(fragment, new TokenizationParametersListener() {

        @Override
        public void onResult(PaymentMethodTokenizationParameters parameters, Collection<Integer> allowedCardNetworks) {
            assertEquals("braintree", parameters.getParameters().getString("gateway"));
            assertEquals(configuration.getMerchantId(), parameters.getParameters().getString("braintree:merchantId"));
            assertEquals(configuration.getAndroidPay().getGoogleAuthorizationFingerprint(), parameters.getParameters().getString("braintree:authorizationFingerprint"));
            assertEquals("v1", parameters.getParameters().getString("braintree:apiVersion"));
            assertEquals(BuildConfig.VERSION_NAME, parameters.getParameters().getString("braintree:sdkVersion"));
            try {
                JSONObject metadata = new JSONObject(parameters.getParameters().getString("braintree:metadata"));
                assertNotNull(metadata);
                assertEquals(BuildConfig.VERSION_NAME, metadata.getString("version"));
                assertNotNull(metadata.getString("sessionId"));
                assertEquals("custom", metadata.getString("integration"));
                assertEquals("android", metadata.get("platform"));
            } catch (JSONException e) {
                fail("Failed to unpack json from tokenization parameters: " + e.getMessage());
            }
            assertEquals(4, allowedCardNetworks.size());
            assertTrue(allowedCardNetworks.contains(CardNetwork.VISA));
            assertTrue(allowedCardNetworks.contains(CardNetwork.MASTERCARD));
            assertTrue(allowedCardNetworks.contains(CardNetwork.AMEX));
            assertTrue(allowedCardNetworks.contains(CardNetwork.DISCOVER));
            mLatch.countDown();
        }
    });
    mLatch.await();
}
Also used : BraintreeFragmentTestUtils.getFragmentWithConfiguration(com.braintreepayments.api.BraintreeFragmentTestUtils.getFragmentWithConfiguration) BraintreeFragmentTestUtils.getMockFragmentWithConfiguration(com.braintreepayments.api.BraintreeFragmentTestUtils.getMockFragmentWithConfiguration) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) PaymentMethodTokenizationParameters(com.google.android.gms.wallet.PaymentMethodTokenizationParameters) JSONException(org.json.JSONException) TokenizationParametersListener(com.braintreepayments.api.interfaces.TokenizationParametersListener) Test(org.junit.Test)

Example 43 with Configuration

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

the class Ideal method fetchIssuingBanks.

/**
 * Makes a call to fetch the list of potential issuing banks with which a customer can pay.
 *
 * @param fragment {@link BraintreeFragment}
 * @param listener {@link BraintreeResponseListener} the callback to which a list of issuing banks will be provided.
 */
public static void fetchIssuingBanks(final BraintreeFragment fragment, final BraintreeResponseListener<List<IdealBank>> listener) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(final Configuration configuration) {
            Exception configException = checkIdealEnabled(configuration);
            if (configException != null) {
                fragment.postCallback(configException);
                return;
            }
            fragment.getBraintreeApiHttpClient().get("/issuers/ideal", new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    fragment.sendAnalyticsEvent("ideal.load.succeeded");
                    try {
                        List<IdealBank> banks = IdealBank.fromJson(configuration, responseBody);
                        if (listener != null) {
                            listener.onResponse(banks);
                        }
                    } catch (JSONException jsonException) {
                        failure(jsonException);
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.sendAnalyticsEvent("ideal.load.failed");
                    fragment.postCallback(exception);
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) IdealBank(com.braintreepayments.api.models.IdealBank) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException)

Example 44 with Configuration

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

the class Ideal method startPayment.

/**
 * Initiates the payment flow by opening a browser where the customer can authenticate with their bank.
 *
 * @param fragment {@link BraintreeFragment}
 * @param builder {@link IdealRequest} with the payment details.
 * @param listener {@link BraintreeResponseListener} the callback to which the {@link IdealResult} will be sent
 * with a status of `PENDING` before the flow starts. This result contains the iDEAL payment ID.
 */
public static void startPayment(final BraintreeFragment fragment, final IdealRequest builder, final BraintreeResponseListener<IdealResult> listener) {
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            fragment.sendAnalyticsEvent("ideal.start-payment.selected");
            Exception configException = checkIdealEnabled(configuration);
            if (configException != null) {
                fragment.postCallback(configException);
                fragment.sendAnalyticsEvent("ideal.start-payment.invalid-configuration");
                return;
            }
            String redirectUrl = URI.create(configuration.getIdealConfiguration().getAssetsUrl() + ASSET_SERVER_REDIRECT_PATH + fragment.getReturnUrlScheme() + "://").toString();
            fragment.getBraintreeApiHttpClient().post("/ideal-payments", builder.build(redirectUrl, configuration.getIdealConfiguration().getRouteId()), new HttpResponseCallback() {

                @Override
                public void success(String responseBody) {
                    try {
                        IdealResult idealResult = IdealResult.fromJson(responseBody);
                        BraintreeSharedPreferences.putString(fragment.getApplicationContext(), IDEAL_RESULT_ID, idealResult.getId());
                        if (listener != null) {
                            listener.onResponse(idealResult);
                        }
                        JSONObject responseJson = new JSONObject(responseBody);
                        String approvalUrl = responseJson.getJSONObject("data").getString("approval_url");
                        fragment.browserSwitch(BraintreeRequestCodes.IDEAL, approvalUrl);
                        fragment.sendAnalyticsEvent("ideal.webswitch.initiate.succeeded");
                    } catch (JSONException jsonException) {
                        failure(jsonException);
                    }
                }

                @Override
                public void failure(Exception exception) {
                    fragment.sendAnalyticsEvent("ideal.webswitch.initiate.failed");
                    fragment.postCallback(exception);
                }
            });
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) Configuration(com.braintreepayments.api.models.Configuration) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) HttpResponseCallback(com.braintreepayments.api.interfaces.HttpResponseCallback) InvalidArgumentException(com.braintreepayments.api.exceptions.InvalidArgumentException) ConfigurationException(com.braintreepayments.api.exceptions.ConfigurationException) JSONException(org.json.JSONException) IdealResult(com.braintreepayments.api.models.IdealResult)

Example 45 with Configuration

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

the class TokenizationClient method tokenize.

/**
 * Create a {@link PaymentMethodNonce} in the Braintree Gateway.
 * <p/>
 * On completion, returns the {@link PaymentMethodNonce} to {@link PaymentMethodNonceCallback}.
 * <p/>
 * If creation fails validation, {@link com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)}
 * will be called with the resulting {@link ErrorWithResponse}.
 * <p/>
 * If an error not due to validation (server error, network issue, etc.) occurs, {@link
 * com.braintreepayments.api.interfaces.BraintreeErrorListener#onError(Exception)} (Throwable)}
 * will be called with the {@link Exception} that occurred.
 *
 * @param paymentMethodBuilder {@link PaymentMethodBuilder} for the {@link PaymentMethodNonce}
 *        to be created.
 */
static void tokenize(final BraintreeFragment fragment, final PaymentMethodBuilder paymentMethodBuilder, final PaymentMethodNonceCallback callback) {
    paymentMethodBuilder.setSessionId(fragment.getSessionId());
    fragment.waitForConfiguration(new ConfigurationListener() {

        @Override
        public void onConfigurationFetched(Configuration configuration) {
            if (paymentMethodBuilder instanceof CardBuilder && configuration.getGraphQL().isFeatureEnabled(GraphQLConfiguration.TOKENIZE_CREDIT_CARDS_FEATURE)) {
                tokenizeGraphQL(fragment, (CardBuilder) paymentMethodBuilder, callback);
            } else {
                tokenizeRest(fragment, paymentMethodBuilder, callback);
            }
        }
    });
}
Also used : ConfigurationListener(com.braintreepayments.api.interfaces.ConfigurationListener) CardBuilder(com.braintreepayments.api.models.CardBuilder) GraphQLConfiguration(com.braintreepayments.api.models.GraphQLConfiguration) 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