Skip to content

Commit

Permalink
Merge v5'
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahkoop committed Oct 10, 2023
2 parents b42422b + 5ad6494 commit aa249e9
Show file tree
Hide file tree
Showing 23 changed files with 731 additions and 1,340 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
* Rename `PayPalDataCollector` to `DataCollector`
* Rename `PayPalDataCollectorRequest` to `DataCollectorRequest`
* Rename `PayPalDataCollectorCallback` to `DataCollectorCallback`
* Venmo
* Remove `VenmoListener`, `VenmoTokenizeAccountCallback`
* Add `VenmoLauncher`, `VenmoAuthChallenge`, `VenmoAuthChallengeCallback`,
`VenmoAuthChallengeResult`, `VenmoResult`, and
`VenmoAuthChallengeResultCallback`
* Rename `VenmoOnActivityResultCallback` to `VenmoResultCallback`
* Remove overload constructors, `setListener`, and `onActivityResult` from `VenmoClient`
* Change `VenmoClient#tokenizeVenmoAccount` parameters
* Add `VenmoClient#requestAuthChallenge`

## unreleased

Expand Down
42 changes: 25 additions & 17 deletions Demo/src/main/java/com/braintreepayments/demo/VenmoFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@
import com.braintreepayments.api.BraintreeClient;
import com.braintreepayments.api.VenmoAccountNonce;
import com.braintreepayments.api.VenmoClient;
import com.braintreepayments.api.VenmoLauncher;
import com.braintreepayments.api.VenmoLineItem;
import com.braintreepayments.api.VenmoListener;
import com.braintreepayments.api.VenmoPaymentMethodUsage;
import com.braintreepayments.api.VenmoRequest;

import java.util.ArrayList;

public class VenmoFragment extends BaseFragment implements VenmoListener {
public class VenmoFragment extends BaseFragment {

private ImageButton venmoButton;
private VenmoClient venmoClient;
private VenmoLauncher venmoLauncher;
private BraintreeClient braintreeClient;

@Nullable
Expand All @@ -35,14 +37,20 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
venmoButton = view.findViewById(R.id.venmo_button);
venmoButton.setOnClickListener(this::launchVenmo);

braintreeClient = getBraintreeClient();
venmoClient = new VenmoClient(this, braintreeClient);
venmoClient.setListener(this);
venmoLauncher = new VenmoLauncher(this, venmoAuthChallengeResult ->
venmoClient.tokenizeVenmoAccount(venmoAuthChallengeResult, this::handleVenmoResult));

return view;
}

private void handleVenmoResult(VenmoAccountNonce venmoAccountNonce) {
private void handleVenmoResult(VenmoAccountNonce nonce, Exception error) {
if (nonce != null) {
handleVenmoAccountNonce(nonce);
} else {
handleError(error);
}
}
private void handleVenmoAccountNonce(VenmoAccountNonce venmoAccountNonce) {
super.onPaymentMethodNonceCreated(venmoAccountNonce);

NavDirections action =
Expand All @@ -52,6 +60,10 @@ private void handleVenmoResult(VenmoAccountNonce venmoAccountNonce) {

public void launchVenmo(View v) {
getActivity().setProgressBarIndeterminateVisibility(true);
if (venmoClient == null) {
braintreeClient = getBraintreeClient();
venmoClient = new VenmoClient(braintreeClient);
}

FragmentActivity activity = getActivity();

Expand All @@ -74,22 +86,18 @@ public void launchVenmo(View v) {
lineItems.add(new VenmoLineItem(VenmoLineItem.KIND_DEBIT, "Two Items", 2, "10"));
venmoRequest.setLineItems(lineItems);

venmoClient.tokenizeVenmoAccount(activity, venmoRequest);
venmoClient.requestAuthChallenge(requireActivity(), venmoRequest, (venmoAuthChallenge, authError) -> {
if (authError != null) {
handleError(authError);
return;
}
venmoLauncher.launch(venmoAuthChallenge);
});
} else if (configuration.isVenmoEnabled()) {
showDialog("Please install the Venmo app first.");
} else {
showDialog("Venmo is not enabled for the current merchant.");
}
});
}

@Override
public void onVenmoSuccess(@NonNull VenmoAccountNonce venmoAccountNonce) {
handleVenmoResult(venmoAccountNonce);
}

@Override
public void onVenmoFailure(@NonNull Exception error) {
handleError(error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.json.JSONException;
import org.json.JSONObject;

class VenmoActivityResultContract extends ActivityResultContract<VenmoIntentData, VenmoResult> {
class VenmoActivityResultContract extends ActivityResultContract<VenmoAuthChallenge, VenmoAuthChallengeResult> {

static final String VENMO_PACKAGE_NAME = "com.venmo";
static final String APP_SWITCH_ACTIVITY = "controller.SetupMerchantActivity";
Expand All @@ -31,7 +31,7 @@ class VenmoActivityResultContract extends ActivityResultContract<VenmoIntentData

@NonNull
@Override
public Intent createIntent(@NonNull Context context, VenmoIntentData input) {
public Intent createIntent(@NonNull Context context, VenmoAuthChallenge input) {
Intent venmoIntent = getVenmoIntent()
.putExtra(EXTRA_MERCHANT_ID, input.getProfileId())
.putExtra(EXTRA_ACCESS_TOKEN, input.getConfiguration().getVenmoAccessToken())
Expand Down Expand Up @@ -60,17 +60,17 @@ public Intent createIntent(@NonNull Context context, VenmoIntentData input) {
}

@Override
public VenmoResult parseResult(int resultCode, @Nullable Intent intent) {
public VenmoAuthChallengeResult parseResult(int resultCode, @Nullable Intent intent) {
if (resultCode == AppCompatActivity.RESULT_OK) {
if (intent == null) {
return new VenmoResult(null, null, null, new BraintreeException("An unknown Android error occurred with the activity result API."));
return new VenmoAuthChallengeResult(null, null, null, new BraintreeException("An unknown Android error occurred with the activity result API."));
}
String paymentContextId = intent.getStringExtra(EXTRA_RESOURCE_ID);
String nonce = intent.getStringExtra(EXTRA_PAYMENT_METHOD_NONCE);
String venmoUsername = intent.getStringExtra(EXTRA_USERNAME);
return new VenmoResult(paymentContextId, nonce, venmoUsername, null);
return new VenmoAuthChallengeResult(paymentContextId, nonce, venmoUsername, null);
} else if (resultCode == AppCompatActivity.RESULT_CANCELED) {
return new VenmoResult(null, null, null, new UserCanceledException("User canceled Venmo."));
return new VenmoAuthChallengeResult(null, null, null, new UserCanceledException("User canceled Venmo."));
}
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions Venmo/src/main/java/com/braintreepayments/api/VenmoApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void onResult(String responseBody, Exception httpError) {
});
}

void createNonceFromPaymentContext(String paymentContextId, final VenmoOnActivityResultCallback callback) {
void createNonceFromPaymentContext(String paymentContextId, final VenmoResultCallback callback) {
JSONObject params = new JSONObject();
try {
params.put("query", "query PaymentContext($id: ID!) { node(id: $id) { ... on VenmoPaymentContext { paymentMethodId userName payerInfo { firstName lastName phoneNumber email externalId userName " +
Expand Down Expand Up @@ -114,7 +114,7 @@ public void onResult(String responseBody, Exception httpError) {
}
}

void vaultVenmoAccountNonce(String nonce, final VenmoOnActivityResultCallback callback) {
void vaultVenmoAccountNonce(String nonce, final VenmoResultCallback callback) {
VenmoAccount venmoAccount = new VenmoAccount();
venmoAccount.setNonce(nonce);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.braintreepayments.api;

class VenmoIntentData {

private final Configuration configuration;
private final String profileId;
private final String paymentContextId;
private final String sessionId;
private final String integrationType;

VenmoIntentData(Configuration configuration, String profileId, String paymentContextId, String sessionId, String integrationType) {
/**
* Used to request Venmo authentication via {@link VenmoLauncher#launch(VenmoAuthChallenge)}
*/
public class VenmoAuthChallenge {

private Configuration configuration;
private String profileId;
private String paymentContextId;
private String sessionId;
private String integrationType;

VenmoAuthChallenge(Configuration configuration, String profileId, String paymentContextId, String sessionId, String integrationType) {
this.configuration = configuration;
this.profileId = profileId;
this.paymentContextId = paymentContextId;
Expand All @@ -28,11 +31,11 @@ String getPaymentContextId() {
return paymentContextId;
}

public String getSessionId() {
String getSessionId() {
return sessionId;
}

public String getIntegrationType() {
String getIntegrationType() {
return integrationType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.braintreepayments.api;

import androidx.annotation.Nullable;

/**
* Callback to handle result from {@link VenmoClient#tokenizeVenmoAccount(VenmoAuthChallengeResult, VenmoResultCallback)}
*/
public interface VenmoAuthChallengeCallback {
void onVenmoAuthChallenge(@Nullable VenmoAuthChallenge venmoAuthChallenge,
@Nullable Exception error);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.braintreepayments.api;

import androidx.annotation.Nullable;

public class VenmoAuthChallengeResult {

private final Exception error;
private final String paymentContextId;
private final String venmoAccountNonce;
private final String venmoUsername;

VenmoAuthChallengeResult(@Nullable String paymentContextId, @Nullable String venmoAccountNonce,
@Nullable String venmoUsername, @Nullable Exception error) {
this.paymentContextId = paymentContextId;
this.venmoAccountNonce = venmoAccountNonce;
this.venmoUsername = venmoUsername;
this.error = error;
}

Exception getError() {
return error;
}

String getPaymentContextId() {
return paymentContextId;
}

String getVenmoAccountNonce() {
return venmoAccountNonce;
}

String getVenmoUsername() {
return venmoUsername;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.braintreepayments.api;

public interface VenmoAuthChallengeResultCallback {

void onVenmoResult(VenmoAuthChallengeResult venmoAuthChallengeResult);
}
Loading

0 comments on commit aa249e9

Please sign in to comment.