diff --git a/stripe/src/main/java/com/stripe/android/model/PaymentIntent.java b/stripe/src/main/java/com/stripe/android/model/PaymentIntent.java index 19db58f1e2d..1033b0304d4 100644 --- a/stripe/src/main/java/com/stripe/android/model/PaymentIntent.java +++ b/stripe/src/main/java/com/stripe/android/model/PaymentIntent.java @@ -675,7 +675,7 @@ private static Type fromCode(@Nullable String typeCode) { } } - enum CancellationReason { + public enum CancellationReason { Duplicate("duplicate"), Fraudulent("fraudulent"), RequestedByCustomer("requested_by_customer"), @@ -691,7 +691,7 @@ enum CancellationReason { } @Nullable - static CancellationReason fromCode(@Nullable String code) { + private static CancellationReason fromCode(@Nullable String code) { for (CancellationReason cancellationReason : values()) { if (cancellationReason.code.equals(code)) { return cancellationReason; diff --git a/stripe/src/main/java/com/stripe/android/model/SetupIntent.java b/stripe/src/main/java/com/stripe/android/model/SetupIntent.java index 3752fef4e63..5b3cfe3d538 100644 --- a/stripe/src/main/java/com/stripe/android/model/SetupIntent.java +++ b/stripe/src/main/java/com/stripe/android/model/SetupIntent.java @@ -28,9 +28,9 @@ public final class SetupIntent extends StripeModel implements StripeIntent { private static final String FIELD_ID = "id"; private static final String FIELD_OBJECT = "object"; + private static final String FIELD_CANCELLATION_REASON = "cancellation_reason"; private static final String FIELD_CREATED = "created"; private static final String FIELD_CLIENT_SECRET = "client_secret"; - private static final String FIELD_CUSTOMER = "customer"; private static final String FIELD_DESCRIPTION = "description"; private static final String FIELD_LAST_SETUP_ERROR = "last_setup_error"; private static final String FIELD_LIVEMODE = "livemode"; @@ -44,9 +44,9 @@ public final class SetupIntent extends StripeModel implements StripeIntent { @Nullable private final String mId; @Nullable private final String mObjectType; + @Nullable private final CancellationReason mCancellationReason; private final long mCreated; @Nullable private final String mClientSecret; - @Nullable private final String mCustomerId; @Nullable private final String mDescription; private final boolean mLiveMode; @Nullable private final Map mNextAction; @@ -60,9 +60,9 @@ public final class SetupIntent extends StripeModel implements StripeIntent { private SetupIntent(@NonNull Builder builder) { mId = builder.mId; mObjectType = builder.mObjectType; + mCancellationReason = builder.mCancellationReason; mCreated = builder.mCreated; mClientSecret = builder.mClientSecret; - mCustomerId = builder.mCustomerId; mDescription = builder.mDescription; mLiveMode = builder.mLiveMode; mNextAction = builder.mNextAction; @@ -89,6 +89,14 @@ public String getId() { return mId; } + /** + * @return Reason for cancellation of this SetupIntent. + */ + @Nullable + public CancellationReason getCancellationReason() { + return mCancellationReason; + } + /** * @return Time at which the object was created. Measured in seconds since the Unix epoch. */ @@ -103,7 +111,7 @@ public long getCreated() { @Deprecated @Nullable public String getCustomerId() { - return mCustomerId; + return null; } /** @@ -275,7 +283,9 @@ public static SetupIntent fromJson(@Nullable JSONObject jsonObject) { .setObjectType(optString(jsonObject, FIELD_OBJECT)) .setCreated(jsonObject.optLong(FIELD_CREATED)) .setClientSecret(optString(jsonObject, FIELD_CLIENT_SECRET)) - .setCustomerId(optString(jsonObject, FIELD_CUSTOMER)) + .setCancellationReason(CancellationReason.fromCode( + optString(jsonObject, FIELD_CANCELLATION_REASON) + )) .setDescription(optString(jsonObject, FIELD_DESCRIPTION)) .setLiveMode(jsonObject.optBoolean(FIELD_LIVEMODE)) .setPaymentMethodId(optString(jsonObject, FIELD_PAYMENT_METHOD)) @@ -298,7 +308,7 @@ private boolean typedEquals(@NonNull SetupIntent setupIntent) { && ObjectUtils.equals(mObjectType, setupIntent.mObjectType) && ObjectUtils.equals(mClientSecret, setupIntent.mClientSecret) && ObjectUtils.equals(mCreated, setupIntent.mCreated) - && ObjectUtils.equals(mCustomerId, setupIntent.mCustomerId) + && ObjectUtils.equals(mCancellationReason, setupIntent.mCancellationReason) && ObjectUtils.equals(mDescription, setupIntent.mDescription) && ObjectUtils.equals(mLastSetupError, setupIntent.mLastSetupError) && ObjectUtils.equals(mLiveMode, setupIntent.mLiveMode) @@ -312,7 +322,7 @@ private boolean typedEquals(@NonNull SetupIntent setupIntent) { @Override public int hashCode() { - return ObjectUtils.hash(mId, mObjectType, mCustomerId, mClientSecret, mCreated, + return ObjectUtils.hash(mId, mObjectType, mCancellationReason, mClientSecret, mCreated, mDescription, mLastSetupError, mLiveMode, mStatus, mPaymentMethodId, mPaymentMethodTypes, mNextAction, mNextActionType, mUsage); } @@ -320,9 +330,9 @@ public int hashCode() { private static final class Builder implements ObjectBuilder { @Nullable private String mId; @Nullable private String mObjectType; + @Nullable private CancellationReason mCancellationReason; private long mCreated; @Nullable private String mClientSecret; - @Nullable private String mCustomerId; @Nullable private String mDescription; private boolean mLiveMode; @Nullable private Map mNextAction; @@ -357,8 +367,8 @@ Builder setClientSecret(@Nullable String clientSecret) { } @NonNull - Builder setCustomerId(@Nullable String customerId) { - mCustomerId = customerId; + Builder setCancellationReason(@Nullable CancellationReason cancellationReason) { + mCancellationReason = cancellationReason; return this; } @@ -605,4 +615,27 @@ private static Type fromCode(@Nullable String typeCode) { } } } + + public enum CancellationReason { + Duplicate("duplicate"), + RequestedByCustomer("requested_by_customer"), + Abandoned("abandoned"); + + @NonNull private final String code; + + CancellationReason(@NonNull String code) { + this.code = code; + } + + @Nullable + private static CancellationReason fromCode(@Nullable String code) { + for (CancellationReason cancellationReason : values()) { + if (cancellationReason.code.equals(code)) { + return cancellationReason; + } + } + + return null; + } + } } diff --git a/stripe/src/test/java/com/stripe/android/model/PaymentIntentTest.java b/stripe/src/test/java/com/stripe/android/model/PaymentIntentTest.java index 2a758969f57..6516e022478 100644 --- a/stripe/src/test/java/com/stripe/android/model/PaymentIntentTest.java +++ b/stripe/src/test/java/com/stripe/android/model/PaymentIntentTest.java @@ -170,6 +170,8 @@ public void getLastPaymentError_parsesCorrectly() { @Test public void testCanceled() { + assertEquals(PaymentIntent.Status.Canceled, + PaymentIntentFixtures.CANCELLED.getStatus()); assertEquals(PaymentIntent.CancellationReason.Abandoned, PaymentIntentFixtures.CANCELLED.getCancellationReason()); assertEquals(1567091866L, diff --git a/stripe/src/test/java/com/stripe/android/model/SetupIntentFixtures.java b/stripe/src/test/java/com/stripe/android/model/SetupIntentFixtures.java index aedeb40b8ac..54cdfba02b3 100644 --- a/stripe/src/test/java/com/stripe/android/model/SetupIntentFixtures.java +++ b/stripe/src/test/java/com/stripe/android/model/SetupIntentFixtures.java @@ -7,7 +7,7 @@ public class SetupIntentFixtures { @NonNull - public static final String SI_NEXT_ACTION_REDIRECT_JSON = "{\n" + + static final String SI_NEXT_ACTION_REDIRECT_JSON = "{\n" + " \"id\": \"seti_1EqTSZGMT9dGPIDGVzCUs6dV\",\n" + " \"object\": \"setup_intent\",\n" + " \"cancellation_reason\": null,\n" + @@ -32,7 +32,7 @@ public class SetupIntentFixtures { "}"; @NonNull - public static final SetupIntent SI_WITH_LAST_PAYMENT_ERROR = Objects.requireNonNull( + static final SetupIntent SI_WITH_LAST_PAYMENT_ERROR = Objects.requireNonNull( SetupIntent.fromString("{\n" + "\t\"id\": \"seti_1EqTSZGMT9dGPIDGVzCUs6dV\",\n" + "\t\"object\": \"setup_intent\",\n" + @@ -103,6 +103,34 @@ public class SetupIntentFixtures { "}") ); + static final SetupIntent CANCELLED = Objects.requireNonNull(SetupIntent.fromString("{\n" + + " \"id\": \"seti_1FCoS9CRMbs6FrXfxFQOp8Mm\",\n" + + " \"object\": \"setup_intent\",\n" + + " \"application\": null,\n" + + " \"cancellation_reason\": \"abandoned\",\n" + + " \"client_secret\": \"seti_1FCoS9CRMbs6FrXfxFQOp8Mm_secret_FiEwNDtwMi\",\n" + + " \"created\": 1567088301,\n" + + " \"customer\": \"cus_FWhpaTLIPWLhpJ\",\n" + + " \"description\": null,\n" + + " \"last_setup_error\": null,\n" + + " \"livemode\": false,\n" + + " \"metadata\": {},\n" + + " \"next_action\": null,\n" + + " \"on_behalf_of\": null,\n" + + " \"payment_method\": \"pm_1F1wa2CRMbs6FrXfm9XfWrGS\",\n" + + " \"payment_method_options\": {\n" + + " \"card\": {\n" + + " \"request_three_d_secure\": \"automatic\"\n" + + " }\n" + + " },\n" + + " \"payment_method_types\": [\n" + + " \"card\"\n" + + " ],\n" + + " \"status\": \"canceled\",\n" + + " \"usage\": \"off_session\"\n" + + "}" + )); + @NonNull public static final SetupIntent SI_NEXT_ACTION_REDIRECT = Objects.requireNonNull(SetupIntent.fromString(SI_NEXT_ACTION_REDIRECT_JSON)); diff --git a/stripe/src/test/java/com/stripe/android/model/SetupIntentTest.java b/stripe/src/test/java/com/stripe/android/model/SetupIntentTest.java index 85b71e05015..bbc8d684d85 100644 --- a/stripe/src/test/java/com/stripe/android/model/SetupIntentTest.java +++ b/stripe/src/test/java/com/stripe/android/model/SetupIntentTest.java @@ -64,4 +64,12 @@ public void getLastSetupError_parsesCorrectly() { lastSetupError.message ); } + + @Test + public void testCanceled() { + assertEquals(SetupIntent.Status.Canceled, + SetupIntentFixtures.CANCELLED.getStatus()); + assertEquals(SetupIntent.CancellationReason.Abandoned, + SetupIntentFixtures.CANCELLED.getCancellationReason()); + } }