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

Create PaymentMethodsActivityStarter.Result #1421

Merged
merged 1 commit into from
Aug 22, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ import android.view.View
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView

import com.stripe.android.CustomerSession
import com.stripe.android.StripeError
import com.stripe.android.model.Customer
import com.stripe.android.model.PaymentMethod
import com.stripe.android.view.AddPaymentMethodActivityStarter
import com.stripe.android.view.PaymentMethodsActivity
import com.stripe.android.view.PaymentMethodsActivityStarter
import com.stripe.example.R
import com.stripe.example.controller.ErrorDialogHandler
import com.stripe.example.service.ExampleEphemeralKeyProvider

import java.lang.ref.WeakReference

/**
Expand Down Expand Up @@ -61,11 +57,10 @@ class CustomerSessionActivity : AppCompatActivity() {

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == AddPaymentMethodActivityStarter.REQUEST_CODE &&
if (requestCode == PaymentMethodsActivityStarter.REQUEST_CODE &&
resultCode == Activity.RESULT_OK && data != null) {
val paymentMethod = data
.getParcelableExtra<PaymentMethod>(PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT)

val paymentMethod =
PaymentMethodsActivityStarter.Result.fromIntent(data)?.paymentMethod
if (paymentMethod?.card != null) {
selectedSourceTextView.text = buildCardString(paymentMethod.card!!)
}
Expand Down
6 changes: 4 additions & 2 deletions stripe/src/main/java/com/stripe/android/PaymentSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ public boolean handlePaymentData(int requestCode, int resultCode, @NonNull Inten
} else if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case PaymentMethodsActivityStarter.REQUEST_CODE: {
final PaymentMethod paymentMethod =
data.getParcelableExtra(PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT);
final PaymentMethodsActivityStarter.Result result =
PaymentMethodsActivityStarter.Result.fromIntent(data);
final PaymentMethod paymentMethod = result != null ?
result.paymentMethod : null;
if (paymentMethod != null) {
persistPaymentMethod(paymentMethod);
mPaymentSessionData.setPaymentMethod(paymentMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ final Intent newIntent() {
}

public interface Args extends Parcelable {
String EXTRA = "EXTRA_ARGS";
String EXTRA = "extra_activity_args";
}

public interface Result extends Parcelable {
String EXTRA = "extra_activity_result";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public class PaymentMethodsActivity extends AppCompatActivity {
private static final String STATE_SELECTED_PAYMENT_METHOD_ID =
"state_selected_payment_method_id";

/**
* @deprecated use {@link PaymentMethodsActivityStarter.Result#fromIntent(Intent)}
* to retrieve the result of this activity from an intent.
*/
@Deprecated
public static final String EXTRA_SELECTED_PAYMENT = "selected_payment";

public static final String TOKEN_PAYMENT_METHODS_ACTIVITY = "PaymentMethodsActivity";

private boolean mCommunicating;
Expand Down Expand Up @@ -128,9 +134,7 @@ private void onPaymentMethodCreated(@Nullable Intent data) {
// If the added Payment Method is not reusable, it also can't be attached to a
// customer, so immediately return to the launching host with the new
// Payment Method.
setResult(RESULT_OK,
new Intent().putExtra(EXTRA_SELECTED_PAYMENT, paymentMethod));
finish();
finishWithPaymentMethod(paymentMethod);
} else {
// Refresh the list of Payment Methods with the new Payment Method.
getCustomerPaymentMethods(paymentMethod != null ? paymentMethod.id : null);
Expand Down Expand Up @@ -217,7 +221,14 @@ private void setSelectionAndFinish() {
return;
}

setResult(RESULT_OK, new Intent().putExtra(EXTRA_SELECTED_PAYMENT, paymentMethod));
finishWithPaymentMethod(paymentMethod);
}

private void finishWithPaymentMethod(@NonNull PaymentMethod paymentMethod) {
setResult(RESULT_OK, new Intent()
.putExtra(EXTRA_SELECTED_PAYMENT, paymentMethod)
.putExtras(new PaymentMethodsActivityStarter.Result(paymentMethod).toBundle())
);
finish();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
Expand All @@ -18,6 +19,14 @@
import java.util.Objects;
import java.util.Set;

/**
* A class to start {@link PaymentMethodsActivity}. Arguments for the activity can be specified
* with {@link Args} and constructed with {@link Args.Builder}.
*
* <p>The result data is a {@link Result} instance, obtained using
* {@link Result#fromIntent(Intent)}}. The result will be returned with request code
* {@link #REQUEST_CODE}.</p>
*/
public final class PaymentMethodsActivityStarter
extends ActivityStarter<PaymentMethodsActivity, PaymentMethodsActivityStarter.Args> {
public static final int REQUEST_CODE = 6000;
Expand Down Expand Up @@ -166,4 +175,74 @@ public Args build() {
}
}
}

/**
* The result of a {@link PaymentMethodsActivity}.
*
* <p>Retrieve in <code>#onActivityResult()</code> using {@link #fromIntent(Intent)}.
*/
public static final class Result implements ActivityStarter.Result {
@NonNull public final PaymentMethod paymentMethod;

/**
* @return the {@link Result} object from the given <code>Intent</code>
*/
@Nullable
public static Result fromIntent(@NonNull Intent intent) {
return intent.getParcelableExtra(EXTRA);
}

public Result(@NonNull PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}

private Result(@NonNull Parcel parcel) {
this.paymentMethod = Objects.requireNonNull(
parcel.<PaymentMethod>readParcelable(PaymentMethod.class.getClassLoader())
);
}

@NonNull
public Bundle toBundle() {
final Bundle bundle = new Bundle();
bundle.putParcelable(EXTRA, this);
return bundle;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeParcelable(paymentMethod, flags);
}

@Override
public int hashCode() {
return ObjectUtils.hash(paymentMethod);
}

@Override
public boolean equals(@Nullable Object obj) {
return super.equals(obj) || (obj instanceof Result && typedEquals((Result) obj));
}

private boolean typedEquals(@NonNull Result other) {
return ObjectUtils.equals(paymentMethod, other.paymentMethod);
}

public static final Creator<Result> CREATOR = new Creator<Result>() {
@Override
public Result createFromParcel(Parcel in) {
return new Result(in);
}

@Override
public Result[] newArray(int size) {
return new Result[size];
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ public void handlePaymentData_whenPaymentMethodRequest_notifiesListenerAndFetche
// We have already tested the functionality up to here.
reset(mPaymentSessionListener);

final PaymentMethodsActivityStarter.Result result =
new PaymentMethodsActivityStarter.Result(PaymentMethodFixtures.CARD_PAYMENT_METHOD);
final boolean handled = paymentSession.handlePaymentData(
PaymentMethodsActivityStarter.REQUEST_CODE, RESULT_OK,
new Intent().putExtra(PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT,
PaymentMethod.fromString(PaymentMethodTest.PM_CARD_JSON)));
new Intent().putExtras(result.toBundle()));
assertTrue(handled);

verify(mPaymentSessionListener)
.onPaymentSessionDataChanged(mPaymentSessionDataArgumentCaptor.capture());
final PaymentSessionData data = mPaymentSessionDataArgumentCaptor.getValue();
assertNotNull(data);
assertEquals(PaymentMethod.fromString(PaymentMethodTest.PM_CARD_JSON),
data.getPaymentMethod());
assertEquals(PaymentMethodFixtures.CARD_PAYMENT_METHOD, data.getPaymentMethod());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import static android.app.Activity.RESULT_OK;
import static com.stripe.android.PaymentSession.EXTRA_PAYMENT_SESSION_ACTIVE;
import static com.stripe.android.view.PaymentMethodsActivity.EXTRA_SELECTED_PAYMENT;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -234,11 +234,9 @@ public void onSaveMenuItem_finishedWithExpectedResult() {
assertEquals(RESULT_OK, mShadowActivity.getResultCode());
final Intent intent = mShadowActivity.getResultIntent();
assertNotNull(intent);
assertTrue(intent.hasExtra(EXTRA_SELECTED_PAYMENT));

final PaymentMethod selectedPaymentMethod =
intent.getParcelableExtra(EXTRA_SELECTED_PAYMENT);
assertNotNull(selectedPaymentMethod);
assertEquals(PAYMENT_METHODS.get(0), selectedPaymentMethod);
final PaymentMethodsActivityStarter.Result result =
Objects.requireNonNull(PaymentMethodsActivityStarter.Result.fromIntent(intent));
assertEquals(PAYMENT_METHODS.get(0), result.paymentMethod);
}
}