public static Intent getAppSwitchIntent(ContextInspector contextInspector, ConfigManager configManager, Request request, Recipe recipe) { Intent intent = createBaseIntent(recipe.getTargetIntentAction(), WALLET_APP_PACKAGE) .putExtra("version", recipe.getProtocol().getVersion()) .putExtra("app_guid", InstallationIdentifier.getInstallationGUID(contextInspector.getContext())) .putExtra("client_metadata_id", request.getClientMetadataId()) .putExtra("client_id", request.getClientId()) .putExtra("app_name", DeviceInspector.getApplicationInfoName(contextInspector.getContext())) .putExtra("environment", request.getEnvironment()) .putExtra("environment_url", EnvironmentManager.getEnvironmentUrl(request.getEnvironment())); if (request instanceof AuthorizationRequest) { AuthorizationRequest authorizationRequest = (AuthorizationRequest) request; intent.putExtra("scope", authorizationRequest.getScopeString()) .putExtra("response_type", "code") .putExtra("privacy_url", authorizationRequest.getPrivacyUrl()) .putExtra("agreement_url", authorizationRequest.getUserAgreementUrl()); } else { CheckoutRequest checkoutRequest = (CheckoutRequest) request; String webURL = checkoutRequest.getBrowserSwitchUrl(contextInspector.getContext(), configManager.getConfig()); intent.putExtra("response_type", "web") .putExtra("webURL", webURL); } return intent; }
public static Result parseAppSwitchResponse(ContextInspector contextInspector, Request request, Intent data) { Bundle bundle = data.getExtras(); if (request.validateV1V2Response(contextInspector, bundle)) { request.trackFpti(contextInspector.getContext(), TrackingPoint.Return, null); return processResponseIntent(bundle); } else { if (bundle.containsKey("error")) { request.trackFpti(contextInspector.getContext(), TrackingPoint.Error, null); return new Result(new WalletSwitchException(bundle.getString("error"))); } else { request.trackFpti(contextInspector.getContext(), TrackingPoint.Error, null); return new Result(new ResponseParsingException("invalid wallet response")); } } }
@Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(mApprovalUrl); dest.writeString(mTokenQueryParamKey); dest.writeString(mPairingId); }
public static Result parseBrowserSwitchResponse(ContextInspector contextInspector, Request request, Uri uri) { Result result = request.parseBrowserResponse(contextInspector, uri); switch (result.getResultType()) { case Error: request.trackFpti(contextInspector.getContext(), TrackingPoint.Error, null); break; case Cancel: request.trackFpti(contextInspector.getContext(), TrackingPoint.Cancel, null); break; case Success: request.trackFpti(contextInspector.getContext(), TrackingPoint.Return, null); break; } return result; } }
private static <T extends Request> T populateRequestData(BraintreeFragment fragment, T request) { PayPalConfiguration paypalConfiguration = fragment.getConfiguration().getPayPal(); String environment; switch (paypalConfiguration.getEnvironment()) { case "live": environment = EnvironmentManager.LIVE; break; case "offline": environment = EnvironmentManager.MOCK; break; default: environment = paypalConfiguration.getEnvironment(); break; } String clientId = paypalConfiguration.getClientId(); if (clientId == null && EnvironmentManager.MOCK.equals(environment)) { clientId = "FAKE-PAYPAL-CLIENT-ID"; } request.environment(environment) .clientId(clientId) .cancelUrl(fragment.getReturnUrlScheme(), "cancel") .successUrl(fragment.getReturnUrlScheme(), "success"); return request; }
@Test public void buildAppSwitchIntent_buildsIntentForProtocolV1() { Request request = mock(CheckoutRequest.class); when(request.getEnvironment()).thenReturn("test"); Intent intent = AppSwitchHelper.getAppSwitchIntent(mContextInspector, mConfigManager, request, getMockRecipe(1)); assertEquals("com.paypal.android.lib.authenticator.activity.v1.TouchActivity", intent.getAction()); assertEquals("com.paypal.android.p2pmobile", intent.getPackage()); assertEquals("1.0", intent.getStringExtra("version")); }
Recipe recipe = request.getRecipeToExecute(context, sConfigManager.getConfig()); request.trackFpti(context, TrackingPoint.SwitchToWallet, recipe.getProtocol()); return new PendingRequest(true, RequestTarget.wallet, request.getClientMetadataId(), AppSwitchHelper.getAppSwitchIntent(sContextInspector, sConfigManager, request, recipe)); } else { Intent intent = BrowserSwitchHelper.getBrowserSwitchIntent(sContextInspector, sConfigManager, request); if (intent != null) { return new PendingRequest(true, RequestTarget.browser, request.getClientMetadataId(), intent); } else { return new PendingRequest(false, RequestTarget.browser, request.getClientMetadataId(), null);
public static Intent getBrowserSwitchIntent(ContextInspector contextInspector, ConfigManager configManager, Request request) { OtcConfiguration configuration = configManager.getConfig(); try { String url = request.getBrowserSwitchUrl(contextInspector.getContext(), configuration); Recipe<?> recipe = request.getBrowserSwitchRecipe(configuration); for (String allowedBrowserPackage : recipe.getTargetPackagesInReversePriorityOrder()) { boolean canIntentBeResolved = Recipe.isValidBrowserTarget(contextInspector.getContext(), url, allowedBrowserPackage); if (canIntentBeResolved) { request.trackFpti(contextInspector.getContext(), TrackingPoint.SwitchToBrowser, recipe.getProtocol()); return Recipe.getBrowserIntent(contextInspector.getContext(), url, allowedBrowserPackage); } } } catch (CertificateException | UnsupportedEncodingException | NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | JSONException | BadPaddingException | InvalidEncryptionDataException | InvalidKeyException ignored) {} return null; }
Intent intent) { PayPalAccountBuilder paypalAccountBuilder = new PayPalAccountBuilder() .clientMetadataId(request.getClientMetadataId());
/** * Gets a {@link Result} from an {@link Intent} returned by either the PayPal Wallet app or * the browser. * * @param context * @param request the original {@link Request} that was used to get this {@link Result}. * @param data the {@link Intent} returned by either the PayPal Wallet app or the browser. * @return {@link Result} */ public static Result parseResponse(Context context, Request request, Intent data) { initService(context); if (data != null && data.getData() != null) { return BrowserSwitchHelper.parseBrowserSwitchResponse(sContextInspector, request, data.getData()); } else if (data != null && data.getExtras() != null && !data.getExtras().isEmpty()) { return AppSwitchHelper.parseAppSwitchResponse(sContextInspector, request, data); } else { request.trackFpti(context, TrackingPoint.Cancel, null); return new Result(); } }
@Test public void buildAppSwitchIntent_buildsIntentForProtocolV2() { Request request = mock(CheckoutRequest.class); when(request.getEnvironment()).thenReturn("test"); Intent intent = AppSwitchHelper.getAppSwitchIntent(mContextInspector, mConfigManager, request, getMockRecipe(2)); assertEquals("com.paypal.android.lib.authenticator.activity.v2.TouchActivity", intent.getAction()); assertEquals("com.paypal.android.p2pmobile", intent.getPackage()); assertEquals("2.0", intent.getStringExtra("version")); }
@Test public void buildAppSwitchIntent_buildsIntent() { Request request = mock(CheckoutRequest.class); when(request.getEnvironment()).thenReturn("test"); when(request.getClientMetadataId()).thenReturn("client-metadata-id"); when(request.getClientId()).thenReturn("client-id"); Intent intent = AppSwitchHelper.getAppSwitchIntent(mContextInspector, mConfigManager, request, getMockRecipe(2)); assertEquals("installation-guid", intent.getStringExtra("app_guid")); assertEquals("client-metadata-id", intent.getStringExtra("client_metadata_id")); assertEquals("client-id", intent.getStringExtra("client_id")); assertEquals("application-name", intent.getStringExtra("app_name")); assertEquals("test", intent.getStringExtra("environment")); assertEquals("test", intent.getStringExtra("environment_url")); }
@Test public void buildAppSwitchIntent_buildsIntentForProtocolV3() { Request request = mock(CheckoutRequest.class); when(request.getEnvironment()).thenReturn("test"); Intent intent = AppSwitchHelper.getAppSwitchIntent(mContextInspector, mConfigManager, request, getMockRecipe(3)); assertEquals("com.paypal.android.lib.authenticator.activity.v3.TouchActivity", intent.getAction()); assertEquals("com.paypal.android.p2pmobile", intent.getPackage()); assertEquals("3.0", intent.getStringExtra("version")); }
@Test public void parseAppSwitchResponse_parsesErrorAndReturnsResult() { Request request = mock(AuthorizationRequest.class); when(request.validateV1V2Response(any(ContextInspector.class), any(Bundle.class))) .thenReturn(true); Intent intent = new Intent() .putExtra("error", "there was an error"); Result result = AppSwitchHelper.parseAppSwitchResponse(mContextInspector, request, intent); verify(request).trackFpti(any(Context.class), eq(TrackingPoint.Return), isNull(Protocol.class)); assertTrue(result.getError() instanceof WalletSwitchException); assertEquals("there was an error", result.getError().getMessage()); }
private static void persistRequest(Context context, Request request) { Parcel parcel = Parcel.obtain(); request.writeToParcel(parcel, 0); BraintreeSharedPreferences.getSharedPreferences(context).edit() .putString(REQUEST_KEY, Base64.encodeToString(parcel.marshall(), 0)) .putString(REQUEST_TYPE_KEY, request.getClass().getSimpleName()) .apply(); }
@Test public void getCheckoutRequest_buildsWithOfflineStageUrl() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_offline_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); Request request = PayPal.getCheckoutRequest(fragment, null); assertEquals(EnvironmentManager.MOCK, request.getEnvironment()); }
@Test public void parseAppSwitchResponse_returnsErrorWhenResponseCouldNotBeParsed() { Request request = mock(AuthorizationRequest.class); when(request.validateV1V2Response(any(ContextInspector.class), any(Bundle.class))) .thenReturn(false); Intent intent = new Intent() .putExtra("braintree", "nonce"); Result result = AppSwitchHelper.parseAppSwitchResponse(mContextInspector, request, intent); verify(request).trackFpti(any(Context.class), eq(TrackingPoint.Error), isNull(Protocol.class)); assertTrue(result.getError() instanceof ResponseParsingException); assertEquals("invalid wallet response", result.getError().getMessage()); }
@Override public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeString(mPrivacyUrl); dest.writeString(mUserAgreementUrl); dest.writeSerializable(mScopes); dest.writeSerializable(mAdditionalPayloadAttributes); dest.writeString(mMsgGuid); dest.writeInt(mEncryptionKey.length); dest.writeByteArray(mEncryptionKey); }
@Test public void getBillingAgreementRequest_buildsWithLiveStageUrl() throws JSONException { Configuration configuration = Configuration.fromJson(stringFromFixture("configuration/with_live_paypal.json")); BraintreeFragment fragment = mMockFragmentBuilder.configuration(configuration).build(); Request request = PayPal.getBillingAgreementRequest(fragment, null); assertEquals(EnvironmentManager.LIVE, request.getEnvironment()); }
@Test public void parseAppSwitchResponse_parsesErrorForErrorResponses() { Request request = mock(AuthorizationRequest.class); when(request.validateV1V2Response(any(ContextInspector.class), any(Bundle.class))) .thenReturn(false); Intent intent = new Intent() .putExtra("error", "there was an error"); Result result = AppSwitchHelper.parseAppSwitchResponse(mContextInspector, request, intent); verify(request).trackFpti(any(Context.class), eq(TrackingPoint.Error), isNull(Protocol.class)); assertTrue(result.getError() instanceof WalletSwitchException); assertEquals("there was an error", result.getError().getMessage()); }