mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-24 21:15:48 +00:00
Update how we deal with failed or in progress subscriptions.
This commit is contained in:
committed by
Cody Henthorne
parent
b4fe5bdcc6
commit
8a00caabd7
@@ -4,9 +4,71 @@ import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class ActiveSubscription {
|
||||
|
||||
private enum Status {
|
||||
/**
|
||||
* The subscription is currently in a trial period and it’s safe to provision your product for your customer.
|
||||
* The subscription transitions automatically to active when the first payment is made.
|
||||
*/
|
||||
TRIALING("trialing"),
|
||||
|
||||
/**
|
||||
* The subscription is in good standing and the most recent payment was successful. It’s safe to provision your product for your customer.
|
||||
*/
|
||||
ACTIVE("active"),
|
||||
|
||||
/**
|
||||
* Payment failed when you created the subscription. A successful payment needs to be made within 23 hours to activate the subscription.
|
||||
*/
|
||||
INCOMPLETE("incomplete"),
|
||||
|
||||
/**
|
||||
* The initial payment on the subscription failed and no successful payment was made within 23 hours of creating the subscription.
|
||||
* These subscriptions don’t bill customers. This status exists so you can track customers that failed to activate their subscriptions.
|
||||
*/
|
||||
INCOMPLETE_EXPIRED("incomplete_expired"),
|
||||
|
||||
/**
|
||||
* Payment on the latest invoice either failed or wasn’t attempted.
|
||||
*/
|
||||
PAST_DUE("past_due"),
|
||||
|
||||
/**
|
||||
* The subscription has been canceled. During cancellation, automatic collection for all unpaid invoices is disabled (auto_advance=false).
|
||||
*/
|
||||
CANCELED("canceled"),
|
||||
|
||||
/**
|
||||
* The latest invoice hasn’t been paid but the subscription remains in place.
|
||||
* The latest invoice remains open and invoices continue to be generated but payments aren’t attempted.
|
||||
*/
|
||||
UNPAID("unpaid");
|
||||
|
||||
private final String status;
|
||||
|
||||
Status(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
private static Status getStatus(String status) {
|
||||
for (Status s : Status.values()) {
|
||||
if (Objects.equals(status, s.status)) {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unknown status " + status);
|
||||
}
|
||||
|
||||
static boolean isPaymentFailed(String status) {
|
||||
Status s = getStatus(status);
|
||||
return s == INCOMPLETE || s == INCOMPLETE_EXPIRED;
|
||||
}
|
||||
}
|
||||
|
||||
private final Subscription activeSubscription;
|
||||
|
||||
@JsonCreator
|
||||
@@ -22,6 +84,14 @@ public final class ActiveSubscription {
|
||||
return activeSubscription != null && activeSubscription.isActive();
|
||||
}
|
||||
|
||||
public boolean isInProgress() {
|
||||
return activeSubscription != null && !isActive() && !isFailedPayment();
|
||||
}
|
||||
|
||||
public boolean isFailedPayment() {
|
||||
return activeSubscription != null && !isActive() && isFailedPayment();
|
||||
}
|
||||
|
||||
public static final class Subscription {
|
||||
private final int level;
|
||||
private final String currency;
|
||||
@@ -30,6 +100,7 @@ public final class ActiveSubscription {
|
||||
private final boolean isActive;
|
||||
private final long billingCycleAnchor;
|
||||
private final boolean willCancelAtPeriodEnd;
|
||||
private final String status;
|
||||
|
||||
@JsonCreator
|
||||
public Subscription(@JsonProperty("level") int level,
|
||||
@@ -38,7 +109,8 @@ public final class ActiveSubscription {
|
||||
@JsonProperty("endOfCurrentPeriod") long endOfCurrentPeriod,
|
||||
@JsonProperty("active") boolean isActive,
|
||||
@JsonProperty("billingCycleAnchor") long billingCycleAnchor,
|
||||
@JsonProperty("cancelAtPeriodEnd") boolean willCancelAtPeriodEnd)
|
||||
@JsonProperty("cancelAtPeriodEnd") boolean willCancelAtPeriodEnd,
|
||||
@JsonProperty("status") String status)
|
||||
{
|
||||
this.level = level;
|
||||
this.currency = currency;
|
||||
@@ -47,6 +119,7 @@ public final class ActiveSubscription {
|
||||
this.isActive = isActive;
|
||||
this.billingCycleAnchor = billingCycleAnchor;
|
||||
this.willCancelAtPeriodEnd = willCancelAtPeriodEnd;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public int getLevel() {
|
||||
@@ -89,5 +162,21 @@ public final class ActiveSubscription {
|
||||
public boolean willCancelAtPeriodEnd() {
|
||||
return willCancelAtPeriodEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Stripe status of this subscription (see https://stripe.com/docs/billing/subscriptions/overview#subscription-statuses)
|
||||
*/
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public boolean isInProgress() {
|
||||
return !isActive() &&
|
||||
!Status.isPaymentFailed(getStatus());
|
||||
}
|
||||
|
||||
public boolean isFailedPayment() {
|
||||
return Status.isPaymentFailed(getStatus());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user