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 AddPaymentMethodActivityStarter.Result #1583

Merged
merged 1 commit into from
Sep 18, 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 @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -66,5 +67,8 @@ public interface Args extends Parcelable {

public interface Result extends Parcelable {
String EXTRA = "extra_activity_result";

@NonNull
Bundle toBundle();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class AddPaymentMethodActivity extends StripeActivity {

public static final String TOKEN_ADD_PAYMENT_METHOD_ACTIVITY = "AddPaymentMethodActivity";

/**
* @deprecated use {@link AddPaymentMethodActivityStarter.Result}
*/
@Deprecated
public static final String EXTRA_NEW_PAYMENT_METHOD = "new_payment_method";

@Nullable private AddPaymentMethodView mAddPaymentMethodView;
Expand Down Expand Up @@ -152,7 +156,9 @@ private void attachPaymentMethodToCustomer(@NonNull final PaymentMethod paymentM

private void finishWithPaymentMethod(@NonNull PaymentMethod paymentMethod) {
setCommunicatingProgress(false);
setResult(RESULT_OK, new Intent().putExtra(EXTRA_NEW_PAYMENT_METHOD, paymentMethod));
setResult(RESULT_OK, new Intent()
.putExtra(EXTRA_NEW_PAYMENT_METHOD, paymentMethod)
.putExtras(new AddPaymentMethodActivityStarter.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;

Expand Down Expand Up @@ -181,4 +182,75 @@ public AddPaymentMethodActivityStarter.Args build() {
}
}
}

/**
* The result of a {@link AddPaymentMethodActivity}.
*
* <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
static Result fromIntent(@NonNull Intent intent) {
return intent.getParcelableExtra(EXTRA);
}

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

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

@NonNull
@Override
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 Objects.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 Objects.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 @@ -28,7 +28,6 @@
import java.util.List;

import static com.stripe.android.PaymentSession.TOKEN_PAYMENT_SESSION;
import static com.stripe.android.view.AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD;

/**
* <p>An activity that allows a customer to select from their attach payment methods,
Expand Down Expand Up @@ -143,9 +142,11 @@ public boolean onSupportNavigateUp() {
private void onPaymentMethodCreated(@Nullable Intent data) {
initLoggingTokens();

if (data != null && data.hasExtra(EXTRA_NEW_PAYMENT_METHOD)) {
final PaymentMethod paymentMethod =
data.getParcelableExtra(EXTRA_NEW_PAYMENT_METHOD);
if (data != null) {
final AddPaymentMethodActivityStarter.Result result =
AddPaymentMethodActivityStarter.Result.fromIntent(data);
final PaymentMethod paymentMethod = result != null ?
result.paymentMethod : null;
onAddedPaymentMethod(paymentMethod);
} else {
fetchCustomerPaymentMethods();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private Result(@NonNull Parcel parcel) {
}

@NonNull
@Override
public Bundle toBundle() {
final Bundle bundle = new Bundle();
bundle.putParcelable(EXTRA, this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.stripe.android.view;

import android.content.Intent;
import android.os.Bundle;

import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.PaymentMethodFixtures;
import com.stripe.android.utils.ParcelUtils;

import java.util.Objects;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
Expand All @@ -25,4 +31,19 @@ public void testParceling() {
ParcelUtils.create(args, AddPaymentMethodActivityStarter.Args.CREATOR);
assertEquals(args, createdArgs);
}

@Test
public void testResultParceling() {
final Bundle bundle =
new AddPaymentMethodActivityStarter
.Result(PaymentMethodFixtures.CARD_PAYMENT_METHOD)
.toBundle();
final Intent intent = new Intent().putExtras(bundle);
final AddPaymentMethodActivityStarter.Result result =
Objects.requireNonNull(AddPaymentMethodActivityStarter.Result.fromIntent(intent));
assertEquals(
PaymentMethodFixtures.CARD_PAYMENT_METHOD,
result.paymentMethod
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,8 @@ public void addFpx_whenServerReturnsSuccessAndUpdatesCustomer_finishesWithIntent
final Intent intent = mShadowActivity.getResultIntent();

assertTrue(mActivity.isFinishing());
assertTrue(intent.hasExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD));
final PaymentMethod paymentMethod =
intent.getParcelableExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD);
assertNotNull(paymentMethod);

final PaymentMethod paymentMethod = getPaymentMethodFromIntent(intent);
assertEquals(expectedPaymentMethod, paymentMethod);
}

Expand Down Expand Up @@ -250,10 +248,8 @@ public void addCardData_whenServerReturnsSuccessAndUpdatesCustomer_finishesWithI
final Intent intent = mShadowActivity.getResultIntent();

assertTrue(mActivity.isFinishing());
assertTrue(intent.hasExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD));
final PaymentMethod paymentMethod =
intent.getParcelableExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD);
assertNotNull(paymentMethod);

final PaymentMethod paymentMethod = getPaymentMethodFromIntent(intent);
assertEquals(expectedPaymentMethod, paymentMethod);
}

Expand Down Expand Up @@ -366,10 +362,18 @@ private void verifyFinishesWithIntent() {
final Intent intent = mShadowActivity.getResultIntent();

assertTrue(mActivity.isFinishing());
assertTrue(intent.hasExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD));
final PaymentMethod newPaymentMethod =
intent.getParcelableExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD);
assertNotNull(newPaymentMethod);
assertEquals(expectedPaymentMethod, newPaymentMethod);

final PaymentMethod paymentMethod = getPaymentMethodFromIntent(intent);
assertEquals(expectedPaymentMethod, paymentMethod);
}

@NonNull
private static PaymentMethod getPaymentMethodFromIntent(@NonNull Intent intent) {
final AddPaymentMethodActivityStarter.Result result =
AddPaymentMethodActivityStarter.Result.fromIntent(intent);
assertNotNull(result);
final PaymentMethod paymentMethod = result.paymentMethod;
assertNotNull(paymentMethod);
return paymentMethod;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ public void onActivityResult_withValidPaymentMethod_refreshesPaymentMethods() {
assertNotNull(paymentMethod);

final Intent resultIntent = new Intent()
.putExtra(AddPaymentMethodActivity.EXTRA_NEW_PAYMENT_METHOD, paymentMethod);

.putExtras(new AddPaymentMethodActivityStarter.Result(paymentMethod).toBundle());
mPaymentMethodsActivity.onActivityResult(
AddPaymentMethodActivityStarter.REQUEST_CODE, RESULT_OK, resultIntent
);
Expand Down