Skip to content

Commit

Permalink
Add support for cancellation_reason attribute to SetupIntent (#1450)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshafrir-stripe authored Aug 29, 2019
1 parent 2a709b5 commit 58e1e8e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ private static Type fromCode(@Nullable String typeCode) {
}
}

enum CancellationReason {
public enum CancellationReason {
Duplicate("duplicate"),
Fraudulent("fraudulent"),
RequestedByCustomer("requested_by_customer"),
Expand All @@ -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;
Expand Down
53 changes: 43 additions & 10 deletions stripe/src/main/java/com/stripe/android/model/SetupIntent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<String, Object> mNextAction;
Expand All @@ -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;
Expand All @@ -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.
*/
Expand All @@ -103,7 +111,7 @@ public long getCreated() {
@Deprecated
@Nullable
public String getCustomerId() {
return mCustomerId;
return null;
}

/**
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -312,17 +322,17 @@ 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);
}

private static final class Builder implements ObjectBuilder<SetupIntent> {
@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<String, Object> mNextAction;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -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" +
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 58e1e8e

Please sign in to comment.