static void getConfiguration(final BraintreeFragment fragment, final @NonNull ConfigurationListener listener, final @NonNull BraintreeResponseListener<Exception> errorListener) { final String configUrl = Uri.parse(fragment.getAuthorization().getConfigUrl()) .buildUpon() .appendQueryParameter("configVersion", "3") fragment.getAuthorization().getBearer()); if (cachedConfig != null) { listener.onConfigurationFetched(cachedConfig);
private void stubConfigurationFromGateway(final String responseString) { BraintreeHttpClient fakeClient = new BraintreeHttpClient(mBraintreeFragment.getAuthorization()) { @Override public void get(String path, HttpResponseCallback callback) { if (path.contains(mBraintreeFragment.getAuthorization().getConfigUrl())) { callback.success(responseString); } } }; when(mBraintreeFragment.getHttpClient()).thenReturn(fakeClient); } }
@Override public void get(String path, HttpResponseCallback callback) { if (path.contains(mBraintreeFragment.getAuthorization().getConfigUrl())) { callback.success(responseString); } } };
static void onActivityResult(final BraintreeFragment fragment, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { fragment.sendAnalyticsEvent("pay-with-venmo.app-switch.success"); String nonce = data.getStringExtra(EXTRA_PAYMENT_METHOD_NONCE); if (shouldVault(fragment.getApplicationContext()) && fragment.getAuthorization() instanceof ClientToken) { vault(fragment, nonce); } else { String venmoUsername = data.getStringExtra(EXTRA_USERNAME); VenmoAccountNonce venmoAccountNonce = new VenmoAccountNonce(nonce, venmoUsername, venmoUsername); fragment.postCallback(venmoAccountNonce); } } else if (resultCode == Activity.RESULT_CANCELED) { fragment.sendAnalyticsEvent("pay-with-venmo.app-switch.canceled"); } }
@Before public void setup() throws InvalidArgumentException { clearSharedPreferences(RuntimeEnvironment.application); ConfigurationManager.sFetchingConfiguration = false; mTokenizationKey = Authorization.fromString(TestTokenizationKey.TOKENIZATION_KEY); mCountDownLatch = new CountDownLatch(1); mBraintreeFragment = mock(BraintreeFragment.class); when(mBraintreeFragment.getAuthorization()).thenReturn(mTokenizationKey); when(mBraintreeFragment.getApplicationContext()).thenReturn(RuntimeEnvironment.application); when(mBraintreeFragment.getHttpClient()).thenReturn(new BraintreeHttpClient(mTokenizationKey)); }
@Test public void getTokenizationKey_returnsTokenizationKey() throws InvalidArgumentException { BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY); assertEquals(TOKENIZATION_KEY, fragment.getAuthorization().getBearer()); }
@Override public void success(String responseBody) { try { Configuration configuration = Configuration.fromJson(responseBody); cacheConfiguration(fragment.getApplicationContext(), configUrl + fragment.getAuthorization().getBearer(), configuration); sFetchingConfiguration = false; listener.onConfigurationFetched(configuration); } catch (final JSONException e) { sFetchingConfiguration = false; errorListener.onResponse(e); } }
@Override public void onConfigurationFetched(Configuration configuration) { if (configuration.getCardConfiguration().isFraudDataCollectionEnabled()) { HashMap<String,String> additionalProperties = new HashMap<>(); additionalProperties.put("rda_tenant", "bt_card"); additionalProperties.put("mid", configuration.getMerchantId()); if (fragment.getAuthorization() instanceof ClientToken) { String customerId = ((ClientToken)fragment.getAuthorization()).getCustomerId(); if (customerId != null) { additionalProperties.put("cid", customerId); } } PayPalDataCollectorRequest request = new PayPalDataCollectorRequest() .setApplicationGuid(InstallationIdentifier.getInstallationGUID(fragment.getApplicationContext())) .setClientMetadataId(paymentMethodNonce.getNonce()) .setDisableBeacon(true) .setAdditionalData(additionalProperties); PayPalDataCollector.getClientMetadataId(fragment.getApplicationContext(), request); } } });
.put(OFFER_CREDIT_KEY, request.shouldOfferCredit()); if (fragment.getAuthorization() instanceof ClientToken) { parameters.put(AUTHORIZATION_FINGERPRINT_KEY, fragment.getAuthorization().getBearer()); } else { parameters.put(TOKENIZATION_KEY, fragment.getAuthorization().getBearer());
private void flushAnalyticsEvents() { if (getConfiguration() != null && getConfiguration().toJson() != null && getConfiguration().getAnalytics().isEnabled()) { Intent intent = new Intent(mContext, AnalyticsIntentService.class) .putExtra(AnalyticsIntentService.EXTRA_AUTHORIZATION, getAuthorization().toString()) .putExtra(AnalyticsIntentService.EXTRA_CONFIGURATION, getConfiguration().toJson()); try { getApplicationContext().startService(intent); } catch (RuntimeException e) { AnalyticsSender.send(getApplicationContext(), mAuthorization, getHttpClient(), getConfiguration().getAnalytics().getUrl(), false); } } }
final PaymentMethodNonce paymentMethodNonce) { boolean usesClientToken = fragment.getAuthorization() instanceof ClientToken;
private static void tokenizeGraphQL(final BraintreeFragment fragment, final CardBuilder cardBuilder, final PaymentMethodNonceCallback callback) { fragment.sendAnalyticsEvent("card.graphql.tokenization.started"); String payload; try { payload = cardBuilder.buildGraphQL(fragment.getApplicationContext(), fragment.getAuthorization()); } catch (BraintreeException e) { callback.failure(e); return; } fragment.getGraphQLHttpClient().post(payload, new HttpResponseCallback() { @Override public void success(String responseBody) { try { callback.success(parsePaymentMethodNonces(responseBody, cardBuilder.getResponsePaymentMethodType())); fragment.sendAnalyticsEvent("card.graphql.tokenization.success"); } catch (JSONException e) { callback.failure(e); } } @Override public void failure(Exception exception) { fragment.sendAnalyticsEvent("card.graphql.tokenization.failure"); callback.failure(exception); } }); }
@Config(sdk = 21) @Test public void tokenize_tokenizesCardsWithGraphQLWhenEnabled() throws BraintreeException { BraintreeFragment fragment = new MockFragmentBuilder() .configuration(new TestConfigurationBuilder() .graphQL() .build()) .build(); CardBuilder cardBuilder = new CardBuilder(); TokenizationClient.tokenize(fragment, cardBuilder, null); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verifyZeroInteractions(fragment.getHttpClient()); verify(fragment.getGraphQLHttpClient()).post(captor.capture(), any(HttpResponseCallback.class)); assertEquals(cardBuilder.buildGraphQL(fragment.getApplicationContext(), fragment.getAuthorization()), captor.getValue()); }
@Test(timeout = 1000) public void getConfiguration_takesClientTokenIntoAccountForCache() throws InvalidArgumentException, InterruptedException { ClientToken clientToken = (ClientToken) Authorization.fromString( stringFromFixture("client_token_with_authorization_fingerprint_options.json")); when(mBraintreeFragment.getAuthorization()).thenReturn(clientToken); writeMockConfiguration(RuntimeEnvironment.application, clientToken.getConfigUrl(), clientToken.getAuthorizationFingerprint(), stringFromFixture("configuration/configuration.json"), System.currentTimeMillis()); ConfigurationManager.getConfiguration(mBraintreeFragment, new ConfigurationListener() { @Override public void onConfigurationFetched(Configuration configuration) { assertEquals(stringFromFixture("configuration/configuration.json"), configuration.toJson()); mCountDownLatch.countDown(); } }, new BraintreeResponseListener<Exception>() { @Override public void onResponse(Exception e) { fail(e.getMessage()); } }); mCountDownLatch.await(); }
@Override public void onConfigurationFetched(Configuration configuration) { fragment.sendAnalyticsEvent("pay-with-venmo.selected"); String venmoProfileId = profileId; if (TextUtils.isEmpty(venmoProfileId)) { venmoProfileId = configuration.getPayWithVenmo().getMerchantId(); } String exceptionMessage = ""; if (!configuration.getPayWithVenmo().isAccessTokenValid()) { exceptionMessage = "Venmo is not enabled"; } else if (!Venmo.isVenmoInstalled(fragment.getApplicationContext())) { exceptionMessage = "Venmo is not installed"; } if (!TextUtils.isEmpty(exceptionMessage)) { fragment.postCallback(new AppSwitchNotAvailableException(exceptionMessage)); fragment.sendAnalyticsEvent("pay-with-venmo.app-switch.failed"); } else { persistVenmoVaultOption(vault && fragment.getAuthorization() instanceof ClientToken, fragment.getApplicationContext()); fragment.startActivityForResult(getLaunchIntent(configuration.getPayWithVenmo(), venmoProfileId, fragment), BraintreeRequestCodes.VENMO); fragment.sendAnalyticsEvent("pay-with-venmo.app-switch.started"); } } });
@VisibleForTesting @Deprecated static AuthorizationRequest getAuthorizationRequest(BraintreeFragment fragment) { return populateRequestData(fragment, new AuthorizationRequest(fragment.getApplicationContext())) .privacyUrl(fragment.getConfiguration().getPayPal().getPrivacyUrl()) .userAgreementUrl(fragment.getConfiguration().getPayPal().getUserAgreementUrl()) .withScopeValue(SCOPE_FUTURE_PAYMENTS) .withScopeValue(SCOPE_EMAIL) .withAdditionalPayloadAttribute(PAYLOAD_CLIENT_TOKEN_KEY, fragment.getAuthorization().toString()); }
private BraintreeFragment getSetupFragment() { String configuration = mBaseConfiguration.googlePayment(mBaseConfiguration.googlePayment() .environment("sandbox") .supportedNetworks(new String[]{"visa", "mastercard", "amex", "discover"})) .paypal(new TestConfigurationBuilder.TestPayPalConfigurationBuilder(true) .clientId("paypal-client-id")) .withAnalytics() .build(); BraintreeFragment fragment = new MockFragmentBuilder() .configuration(configuration) .build(); try { when(fragment.getAuthorization()).thenReturn(Authorization.fromString("sandbox_tokenization_key")); } catch (InvalidArgumentException e) { throw new RuntimeException(e); } return fragment; } }