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

Update StripeIntentResult's Status logic #1275

Merged
merged 2 commits into from
Jul 31, 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 @@ -4,38 +4,24 @@

import com.stripe.android.model.PaymentIntent;

public final class PaymentIntentResult implements StripeIntentResult<PaymentIntent> {
@NonNull private final PaymentIntent paymentIntent;
@Status private final int status;
public final class PaymentIntentResult extends StripeIntentResult<PaymentIntent> {

private PaymentIntentResult(@NonNull Builder builder) {
this.paymentIntent = builder.mPaymentIntent;
this.status = builder.mStatus;
}

@NonNull
@Override
public PaymentIntent getIntent() {
return paymentIntent;
}

@Override
public int getStatus() {
return status;
super(builder.mPaymentIntent, builder.mStatus);
}

static final class Builder implements ObjectBuilder<PaymentIntentResult> {
private PaymentIntent mPaymentIntent;
@Status private int mStatus;

@NonNull
public Builder setPaymentIntent(@NonNull PaymentIntent paymentIntent) {
Builder setPaymentIntent(@NonNull PaymentIntent paymentIntent) {
mPaymentIntent = paymentIntent;
return this;
}

@NonNull
public Builder setStatus(@Status int status) {
Builder setStatus(@Status int status) {
mStatus = status;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,19 @@ public void start(@NonNull Data data) {
final Intent intent = new Intent(mActivity, PaymentRelayActivity.class)
.putExtra(StripeIntentResultExtras.CLIENT_SECRET,
data.stripeIntent != null ? data.stripeIntent.getClientSecret() : null)
.putExtra(StripeIntentResultExtras.AUTH_EXCEPTION, data.exception)
.putExtra(StripeIntentResultExtras.AUTH_STATUS, data.status);
.putExtra(StripeIntentResultExtras.AUTH_EXCEPTION, data.exception);
mActivity.startActivityForResult(intent, mRequestCode);
}

public static final class Data {
@Nullable final StripeIntent stripeIntent;
@Nullable final Exception exception;
@StripeIntentResult.Status final int status;

/**
* Use when payment authentication completed or can be bypassed.
*/
Data(@NonNull StripeIntent stripeIntent) {
this.stripeIntent = stripeIntent;
this.status = StripeIntentResult.Status.SUCCEEDED;
this.exception = null;
}

Expand All @@ -53,13 +50,12 @@ public static final class Data {
*/
Data(@NonNull Exception exception) {
this.stripeIntent = null;
this.status = StripeIntentResult.Status.FAILED;
this.exception = exception;
}

@Override
public int hashCode() {
return ObjectUtils.hash(stripeIntent, exception, status);
return ObjectUtils.hash(stripeIntent, exception);
}

@Override
Expand All @@ -69,8 +65,7 @@ public boolean equals(@Nullable Object obj) {

private boolean typedEquals(@NonNull Data data) {
return ObjectUtils.equals(stripeIntent, data.stripeIntent) &&
ObjectUtils.equals(exception, data.exception) &&
ObjectUtils.equals(status, data.status);
ObjectUtils.equals(exception, data.exception);
}
}
}
41 changes: 4 additions & 37 deletions stripe/src/main/java/com/stripe/android/SetupIntentResult.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
package com.stripe.android;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.stripe.android.model.SetupIntent;
import com.stripe.android.utils.ObjectUtils;

public class SetupIntentResult implements StripeIntentResult<SetupIntent> {
@NonNull private final SetupIntent mSetupIntent;
@Status private final int mStatus;
public class SetupIntentResult extends StripeIntentResult<SetupIntent> {

private SetupIntentResult(@NonNull Builder builder) {
mSetupIntent = builder.mSetupIntent;
mStatus = builder.mStatus;
}

@NonNull
@Override
public SetupIntent getIntent() {
return mSetupIntent;
}

@Override
public int getStatus() {
return mStatus;
super(builder.mSetupIntent, builder.mStatus);
}

static final class Builder implements ObjectBuilder<SetupIntentResult> {
private SetupIntent mSetupIntent;
@Status private int mStatus;

@NonNull
public Builder setSetupIntent(@NonNull SetupIntent setupIntent) {
Builder setSetupIntent(@NonNull SetupIntent setupIntent) {
mSetupIntent = setupIntent;
return this;
}

@NonNull
public Builder setStatus(@Status int status) {
Builder setStatus(@Status int status) {
mStatus = status;
return this;
}
Expand All @@ -47,21 +31,4 @@ public SetupIntentResult build() {
return new SetupIntentResult(this);
}
}

@Override
public boolean equals(@Nullable Object obj) {
return this == obj || (obj instanceof SetupIntentResult &&
typedEquals((SetupIntentResult) obj));
}

private boolean typedEquals(@NonNull SetupIntentResult setupIntentResult) {
return ObjectUtils.equals(mSetupIntent, setupIntentResult.mSetupIntent)
&& ObjectUtils.equals(mStatus, setupIntentResult.mStatus);
}

@Override
public int hashCode() {
return ObjectUtils.hash(mSetupIntent, mStatus);

}
}
66 changes: 62 additions & 4 deletions stripe/src/main/java/com/stripe/android/StripeIntentResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import android.app.Activity;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.stripe.android.model.ConfirmPaymentIntentParams;
import com.stripe.android.model.StripeIntent;
import com.stripe.android.utils.ObjectUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;

/**
* A model representing the result of a {@link StripeIntent} confirmation or authentication attempt
Expand All @@ -18,18 +21,73 @@
* {@link #getIntent()} represents a {@link StripeIntent} retrieved after
* confirmation/authentication succeeded or failed.
*/
public interface StripeIntentResult<T extends StripeIntent> {
public abstract class StripeIntentResult<T extends StripeIntent> {
@NonNull private final T mStripeIntent;
@Status private final int mStatus;

@NonNull T getIntent();
public StripeIntentResult(@NonNull T stripeIntent, @Status int status) {
mStripeIntent = stripeIntent;
mStatus = calculateStatus(Objects.requireNonNull(stripeIntent.getStatus()), status);
}

@StripeIntentResult.Status
private static int calculateStatus(@NonNull StripeIntent.Status stripeIntentStatus,
@StripeIntentResult.Status int authStatus) {
if (authStatus != StripeIntentResult.Status.UNKNOWN) {
return authStatus;
}

switch (stripeIntentStatus) {
case RequiresAction:
case Canceled: {
return StripeIntentResult.Status.CANCELED;
}
case RequiresPaymentMethod: {
return StripeIntentResult.Status.FAILED;
}
case Succeeded:
case RequiresCapture:
case RequiresConfirmation: {
return StripeIntentResult.Status.SUCCEEDED;
}
case Processing:
default: {
return StripeIntentResult.Status.UNKNOWN;
}
}
}

@Status int getStatus();
@NonNull
public final T getIntent() {
return mStripeIntent;
}

public final int getStatus() {
return mStatus;
}

@Override
public final boolean equals(@Nullable Object obj) {
return this == obj || (obj instanceof StripeIntentResult &&
typedEquals((StripeIntentResult) obj));
}

private boolean typedEquals(@NonNull StripeIntentResult setupIntentResult) {
return ObjectUtils.equals(mStripeIntent, setupIntentResult.mStripeIntent)
&& ObjectUtils.equals(mStatus, setupIntentResult.mStatus);
}

@Override
public final int hashCode() {
return ObjectUtils.hash(mStripeIntent, mStatus);
}

/**
* Values that indicate the outcome of confirmation and payment authentication.
*/
@Retention(RetentionPolicy.SOURCE)
@IntDef({Status.UNKNOWN, Status.SUCCEEDED, Status.FAILED, Status.CANCELED, Status.TIMEDOUT})
@interface Status {
public @interface Status {
int UNKNOWN = 0;

/**
Expand Down
24 changes: 20 additions & 4 deletions stripe/src/main/java/com/stripe/android/model/StripeIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,33 @@ public String toString() {
}

/**
* See https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status
* <ul>
* <li>
* <a href="https://stripe.com/docs/payments/intents#intent-statuses">
* The Intent State Machine - Intent statuses</a>
* </li>
*
* <li>
* <a href="https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status">
* PaymentIntent.status API reference</a>
* </li>
*
* <li>
* <a href="https://stripe.com/docs/api/setup_intents/object#setup_intent_object-status">
* SetupIntent.status API reference</a>
* </li>
* </ul>
*/
enum Status {
Canceled("canceled"),
Processing("processing"),
RequiresAction("requires_action"),
RequiresAuthorization("requires_authorization"),
RequiresCapture("requires_capture"),
RequiresConfirmation("requires_confirmation"),
RequiresPaymentMethod("requires_payment_method"),
Succeeded("succeeded");
Succeeded("succeeded"),

// only applies to Payment Intents
RequiresCapture("requires_capture");

@NonNull
public final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
Expand All @@ -13,8 +14,6 @@
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.stripe.android.StripeIntentResult;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -41,9 +40,9 @@ public PaymentAuthWebView(@NonNull Context context, @Nullable AttributeSet attrs
configureSettings();
}

void init(@NonNull PaymentAuthWebViewClient.Listener listener, @NonNull ProgressBar progressBar,
void init(@NonNull Activity activity, @NonNull ProgressBar progressBar,
@NonNull String clientSecret, @NonNull String returnUrl) {
setWebViewClient(new PaymentAuthWebViewClient(listener, progressBar, clientSecret,
setWebViewClient(new PaymentAuthWebViewClient(activity, progressBar, clientSecret,
returnUrl));
}

Expand All @@ -66,11 +65,11 @@ static class PaymentAuthWebViewClient extends WebViewClient {
@NonNull private final String mClientSecret;
@Nullable private final Uri mReturnUrl;
@NonNull private final ProgressBar mProgressBar;
@NonNull private final Listener mListener;
@NonNull private final Activity mActivity;

PaymentAuthWebViewClient(@NonNull Listener listener, @NonNull ProgressBar progressBar,
PaymentAuthWebViewClient(@NonNull Activity activity, @NonNull ProgressBar progressBar,
@NonNull String clientSecret, @Nullable String returnUrl) {
mListener = listener;
mActivity = activity;
mClientSecret = clientSecret;
mReturnUrl = returnUrl != null ? Uri.parse(returnUrl) : null;
mProgressBar = progressBar;
Expand Down Expand Up @@ -154,11 +153,7 @@ private boolean isPredefinedReturnUrl(@NonNull Uri uri) {
}

private void onAuthCompleted() {
mListener.onAuthCompleted(StripeIntentResult.Status.SUCCEEDED);
}

interface Listener {
void onAuthCompleted(@StripeIntentResult.Status int status);
mActivity.finish();
}
}
}
Loading