Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Activity from VenmoClient #865

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
`VenmoClient#tokenize`
* Change parameters of `VenmoIsReadyToPayCallback` and add `VenmoReadinessResult`
* Add `VenmoClient#createPaymentAuthRequest`
* Move `showVenmoInGooglePlayStore` to `VenmoLauncher`
* GooglePay
* Remove `GooglePayListener` and `GooglePayRequestPaymentCallback`
* Add `GooglePayLauncher`, `GooglePayPaymentAuthRequest`,
Expand Down
38 changes: 10 additions & 28 deletions Venmo/src/main/java/com/braintreepayments/api/VenmoClient.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.braintreepayments.api;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentActivity;

/**
* Used to create and tokenize Venmo accounts. For more information see the <a
* href="https://developer.paypal.com/braintree/docs/guides/venmo/overview">documentation</a>
*/
public class VenmoClient {

static final String VENMO_PACKAGE_NAME = "com.venmo";
private final BraintreeClient braintreeClient;
private final VenmoApi venmoApi;
private final VenmoSharedPrefsWriter sharedPrefsWriter;
Expand Down Expand Up @@ -53,31 +48,18 @@ public VenmoClient(@NonNull Context context, @NonNull String authorization) {
}

/**
* Launches an Android Intent pointing to the Venmo app on the Google Play Store
*
* @param activity used to open the Venmo's Google Play Store
*/
public void showVenmoInGooglePlayStore(@NonNull FragmentActivity activity) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"https://play.google.com/store/apps/details?id=" + VENMO_PACKAGE_NAME));
activity.startActivity(intent);
}


/**
* Start the Pay With Venmo flow. This will return a {@link VenmoPaymentAuthRequestParams} that will be
* used to authenticate the user by switching to the Venmo app in {@link
* VenmoLauncher#launch(VenmoPaymentAuthRequest.ReadyToLaunch)}
* Start the Pay With Venmo flow. This will return a {@link VenmoPaymentAuthRequestParams} that
* will be used to authenticate the user by switching to the Venmo app in
* {@link VenmoLauncher#launch(VenmoPaymentAuthRequest.ReadyToLaunch)}
* <p>
* If the Venmo app is not available, {@link AppSwitchNotAvailableException} will be sent to
* {@link VenmoPaymentAuthRequestCallback#onVenmoPaymentAuthRequest(VenmoPaymentAuthRequest)}
*
* @param activity Android FragmentActivity
* @param context Android Context
* @param request {@link VenmoRequest}
* @param callback {@link VenmoPaymentAuthRequestCallback}
*/
public void createPaymentAuthRequest(@NonNull final FragmentActivity activity,
public void createPaymentAuthRequest(@NonNull final Context context,
@NonNull final VenmoRequest request,
@NonNull VenmoPaymentAuthRequestCallback callback) {
braintreeClient.sendAnalyticsEvent(VenmoAnalytics.TOKENIZE_STARTED);
Expand All @@ -92,7 +74,7 @@ public void createPaymentAuthRequest(@NonNull final FragmentActivity activity,
new VenmoPaymentAuthRequest.Failure(new AppSwitchNotAvailableException("Venmo is not enabled")));
return;
}
if (!deviceInspector.isVenmoAppSwitchAvailable(activity)) {
if (!deviceInspector.isVenmoAppSwitchAvailable(context)) {
braintreeClient.sendAnalyticsEvent(VenmoAnalytics.APP_SWITCH_FAILED);
callbackPaymentAuthFailure(callback,
new VenmoPaymentAuthRequest.Failure(new AppSwitchNotAvailableException("Venmo is not installed")));
Expand All @@ -119,7 +101,7 @@ public void createPaymentAuthRequest(@NonNull final FragmentActivity activity,
venmoApi.createPaymentContext(request, venmoProfileId,
(paymentContextId, exception) -> {
if (exception == null) {
createPaymentAuthRequest(activity, request, configuration,
createPaymentAuthRequest(context, request, configuration,
braintreeClient.getAuthorization(), finalVenmoProfileId,
paymentContextId, callback);
} else {
Expand All @@ -130,7 +112,7 @@ public void createPaymentAuthRequest(@NonNull final FragmentActivity activity,
}

private void createPaymentAuthRequest(
final FragmentActivity activity,
final Context context,
final VenmoRequest request,
final Configuration configuration,
Authorization authorization,
Expand All @@ -140,7 +122,7 @@ private void createPaymentAuthRequest(
) {
boolean isClientTokenAuth = (authorization instanceof ClientToken);
boolean shouldVault = request.getShouldVault() && isClientTokenAuth;
sharedPrefsWriter.persistVenmoVaultOption(activity, shouldVault);
sharedPrefsWriter.persistVenmoVaultOption(context, shouldVault);
VenmoPaymentAuthRequestParams params =
new VenmoPaymentAuthRequestParams(configuration, venmoProfileId, paymentContextId,
braintreeClient.getSessionId(), braintreeClient.getIntegrationType());
Expand All @@ -149,7 +131,7 @@ private void createPaymentAuthRequest(

/**
* After successfully authenticating a Venmo user account via {@link
* VenmoClient#createPaymentAuthRequest(FragmentActivity, VenmoRequest, VenmoPaymentAuthRequestCallback)},
* VenmoClient#createPaymentAuthRequest(Context, VenmoRequest, VenmoPaymentAuthRequestCallback)},
* this method should be invoked to tokenize the account to retrieve a
* {@link VenmoAccountNonce}.
*
Expand Down
20 changes: 18 additions & 2 deletions Venmo/src/main/java/com/braintreepayments/api/VenmoLauncher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.braintreepayments.api;


import android.content.Intent;
import android.net.Uri;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.ActivityResultRegistry;
import androidx.annotation.NonNull;
Expand All @@ -17,6 +21,7 @@ public class VenmoLauncher {
@VisibleForTesting
ActivityResultLauncher<VenmoPaymentAuthRequestParams> activityLauncher;
private static final String VENMO_SECURE_RESULT = "com.braintreepayments.api.Venmo.RESULT";
static final String VENMO_PACKAGE_NAME = "com.venmo";

/**
* Used to launch the Venmo authentication flow to tokenize a Venmo account. This class must be
Expand Down Expand Up @@ -56,10 +61,21 @@ public VenmoLauncher(@NonNull FragmentActivity activity,
* Launches the Venmo authentication flow by switching to the Venmo app.
*
* @param venmoPaymentAuthRequest the result of
* {@link VenmoClient#createPaymentAuthRequest(FragmentActivity,
* VenmoRequest, VenmoPaymentAuthRequestCallback)}
* {@link VenmoClient#createPaymentAuthRequest(android.content.Context, VenmoRequest, VenmoPaymentAuthRequestCallback)}
*/
public void launch(VenmoPaymentAuthRequest.ReadyToLaunch venmoPaymentAuthRequest) {
activityLauncher.launch(venmoPaymentAuthRequest.getRequestParams());
}

/**
* Launches an Android Intent pointing to the Venmo app on the Google Play Store
*
* @param activity used to open the Venmo's Google Play Store
*/
public void showVenmoInGooglePlayStore(@NonNull ComponentActivity activity) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(
"https://play.google.com/store/apps/details?id=" + VENMO_PACKAGE_NAME));
activity.startActivity(intent);
}
}
Loading
Loading