@Override public String getReturnUrlScheme() { return getApplicationContext().getPackageName().toLowerCase(Locale.ROOT) .replace("_", "") + ".braintree"; }
private static void launchWebView(BraintreeFragment fragment, ThreeDSecureLookup threeDSecureLookup) { Intent intent = new Intent(fragment.getApplicationContext(), ThreeDSecureWebViewActivity.class) .putExtra(ThreeDSecureWebViewActivity.EXTRA_THREE_D_SECURE_LOOKUP, threeDSecureLookup); fragment.startActivityForResult(intent, BraintreeRequestCodes.THREE_D_SECURE); }
/** * Collect PayPal device information for fraud identification purposes. * * @param fragment {@link BraintreeFragment} * @param listener listener to be called with the device data String to send to Braintree. */ public static void collectPayPalDeviceData(final BraintreeFragment fragment, final BraintreeResponseListener<String> listener) { final JSONObject deviceData = new JSONObject(); try { String clientMetadataId = getPayPalClientMetadataId(fragment.getApplicationContext()); if (!TextUtils.isEmpty(clientMetadataId)) { deviceData.put(CORRELATION_ID_KEY, clientMetadataId); } } catch (JSONException ignored) {} listener.onResponse(deviceData.toString()); }
private static boolean isManifestValid(BraintreeFragment fragment) { return ManifestValidator.isUrlSchemeDeclaredInAndroidManifest(fragment.getApplicationContext(), fragment.getReturnUrlScheme(), BraintreeBrowserSwitchActivity.class); }
@Override public void onConfigurationFetched(Configuration configuration) { fragment.sendAnalyticsEvent("android-pay.change-masked-wallet"); Intent intent = new Intent(fragment.getApplicationContext(), AndroidPayActivity.class) .putExtra(EXTRA_ENVIRONMENT, GooglePayment.getEnvironment(configuration.getAndroidPay())) .putExtra(EXTRA_GOOGLE_TRANSACTION_ID, androidPayCardNonce.getGoogleTransactionId()) .putExtra(EXTRA_CART, androidPayCardNonce.getCart()) .putExtra(EXTRA_REQUEST_TYPE, CHANGE_PAYMENT_METHOD); fragment.startActivityForResult(intent, BraintreeRequestCodes.ANDROID_PAY); } });
private String getResultIdFromPrefs() { return BraintreeSharedPreferences.getString(mBraintreeFragment.getApplicationContext(), IDEAL_RESULT_ID); } }
private void putResultIdInPrefs(String resultId) { BraintreeSharedPreferences.putString(mBraintreeFragment.getApplicationContext(), IDEAL_RESULT_ID, resultId); }
@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); } });
@Test public void getContext_returnsContext() throws InvalidArgumentException { BraintreeFragment fragment = BraintreeFragment.newInstance(mActivity, TOKENIZATION_KEY); assertEquals(mActivity.getApplicationContext(), fragment.getApplicationContext()); }
@VisibleForTesting static BillingAgreementRequest getBillingAgreementRequest(BraintreeFragment fragment, String redirectUrl) { BillingAgreementRequest request = populateRequestData(fragment, new BillingAgreementRequest()) .approvalURL(redirectUrl); if (redirectUrl != null) { Uri uri = Uri.parse(redirectUrl); String pairingId = uri.getQueryParameter("ba_token"); if (pairingId != null) { request.pairingId(fragment.getApplicationContext(), pairingId); } } return request; }
@VisibleForTesting static CheckoutRequest getCheckoutRequest(BraintreeFragment fragment, String redirectUrl) { CheckoutRequest request = populateRequestData(fragment, new CheckoutRequest()) .approvalURL(redirectUrl); if (redirectUrl != null) { Uri uri = Uri.parse(redirectUrl); String pairingId = uri.getQueryParameter("token"); if (pairingId != null) { request.pairingId(fragment.getApplicationContext(), pairingId); } } return request; }
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"); } }
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); } } }
@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)); }
@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 handleApproval(Request request, PayPalApprovalCallback paypalApprovalCallback) { PendingRequest pendingRequest = PayPalOneTouchCore.getStartIntent(fragment.getApplicationContext(), request); if (pendingRequest.isSuccess() && pendingRequest.getRequestTarget() == RequestTarget.wallet) { sendAnalyticsForPayPal(fragment, request, true, RequestTarget.wallet); fragment.startActivityForResult(pendingRequest.getIntent(), BraintreeRequestCodes.PAYPAL); } else if (pendingRequest.isSuccess() && pendingRequest.getRequestTarget() == RequestTarget.browser) { sendAnalyticsForPayPal(fragment, request, true, RequestTarget.browser); fragment.browserSwitch(BraintreeRequestCodes.PAYPAL, pendingRequest.getIntent()); } else { sendAnalyticsForPayPal(fragment, request, false, null); } } };
@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()); }
@Test public void performAppSwitch_persistsIfVaultFalse() throws InvalidArgumentException { Configuration configuration = getConfigurationFromFixture(); Authorization clientToken = Authorization.fromString(stringFromFixture("base_64_client_token.txt")); disableSignatureVerification(); BraintreeFragment fragment = new MockFragmentBuilder() .context(VenmoInstalledContextFactory.venmoInstalledContext(true, RuntimeEnvironment.application)) .configuration(configuration) .authorization(clientToken) .build(); Venmo.authorizeAccount(fragment, false); SharedPreferences prefs = SharedPreferencesHelper.getSharedPreferences(fragment.getApplicationContext()); assertFalse(prefs.getBoolean("com.braintreepayments.api.Venmo.VAULT_VENMO_KEY", true)); }
@Test public void performAppSwitch_persistsIfVaultTrue() throws InvalidArgumentException { Configuration configuration = getConfigurationFromFixture(); Authorization clientToken = Authorization.fromString(stringFromFixture("base_64_client_token.txt")); disableSignatureVerification(); BraintreeFragment fragment = new MockFragmentBuilder() .context(VenmoInstalledContextFactory.venmoInstalledContext(true, RuntimeEnvironment.application)) .configuration(configuration) .authorization(clientToken) .build(); Venmo.authorizeAccount(fragment, true); SharedPreferences prefs = SharedPreferencesHelper.getSharedPreferences(fragment.getApplicationContext()); assertTrue(prefs.getBoolean("com.braintreepayments.api.Venmo.VAULT_VENMO_KEY", false)); }
@Test public void performAppSwitch_persistsFalseIfTokenizationKeyUsed() throws InvalidArgumentException { Configuration configuration = getConfigurationFromFixture(); Authorization tokenizationkey = Authorization.fromString("sandbox_tk_abcd"); disableSignatureVerification(); BraintreeFragment fragment = new MockFragmentBuilder() .context(VenmoInstalledContextFactory.venmoInstalledContext(true, RuntimeEnvironment.application)) .configuration(configuration) .authorization(tokenizationkey) .sessionId("session-id") .build(); Venmo.authorizeAccount(fragment, true); SharedPreferences prefs = SharedPreferencesHelper.getSharedPreferences(fragment.getApplicationContext()); assertFalse(prefs.getBoolean("com.braintreepayments.api.Venmo.VAULT_VENMO_KEY", true)); }