mirror of
https://github.com/anikeen-com/id.git
synced 2026-11-04 22:59:41 +00:00
trial_days, trial_ends_at and trial_once when creating subscriptions (app and user token) and per subscription of checkout sessions. isTrial() on subscriptions, the subscription.trial_will_end event type, and the trial_already_used error code in the docs.
91 lines
2.7 KiB
PHP
91 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Anikeen\Id\Concerns;
|
|
|
|
use Anikeen\Id\Enums\SubscriptionStatus;
|
|
|
|
/**
|
|
* The state of a subscription, derived from its fields the same way Anikeen ID derives `status`
|
|
* (so it also works with responses that only carry the fields).
|
|
*/
|
|
trait HasSubscriptionStatus
|
|
{
|
|
/**
|
|
* One of the SubscriptionStatus constants: draft, trial, active, paused, cancelling, expired
|
|
* or cancelled.
|
|
*/
|
|
public function status(): string
|
|
{
|
|
$active = (bool) $this->active;
|
|
$started = !empty($this->starts_at);
|
|
|
|
return match (true) {
|
|
(bool) $this->cancelled => SubscriptionStatus::CANCELLED,
|
|
!$active && !$started => SubscriptionStatus::DRAFT,
|
|
$active && !empty($this->cancels_at) => SubscriptionStatus::CANCELLING,
|
|
$active && !$this->autorenew => SubscriptionStatus::PAUSED,
|
|
$active && !empty($this->trial_ends_at) && strtotime($this->trial_ends_at) > time() => SubscriptionStatus::TRIAL,
|
|
$active => SubscriptionStatus::ACTIVE,
|
|
!$this->autorenew && $started => SubscriptionStatus::EXPIRED,
|
|
default => SubscriptionStatus::INACTIVE,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Not checked out or confirmed yet (third-party apps: send the user to confirmationUrl()).
|
|
*/
|
|
public function isDraft(): bool
|
|
{
|
|
return $this->status() === SubscriptionStatus::DRAFT;
|
|
}
|
|
|
|
/**
|
|
* Running and paid: active, in trial, paused or with a scheduled cancellation. Grant access
|
|
* while this is true.
|
|
*/
|
|
public function isActive(): bool
|
|
{
|
|
return in_array($this->status(), [SubscriptionStatus::TRIAL, SubscriptionStatus::ACTIVE, SubscriptionStatus::PAUSED, SubscriptionStatus::CANCELLING], true);
|
|
}
|
|
|
|
/**
|
|
* In its free trial: running, nothing billed until trial_ends_at.
|
|
*/
|
|
public function isTrial(): bool
|
|
{
|
|
return $this->status() === SubscriptionStatus::TRIAL;
|
|
}
|
|
|
|
/**
|
|
* Paused: runs until the paid period ends, then expires (resume() to continue).
|
|
*/
|
|
public function isPaused(): bool
|
|
{
|
|
return $this->status() === SubscriptionStatus::PAUSED;
|
|
}
|
|
|
|
/**
|
|
* Running until a scheduled cancellation takes effect.
|
|
*/
|
|
public function isCancelling(): bool
|
|
{
|
|
return $this->status() === SubscriptionStatus::CANCELLING;
|
|
}
|
|
|
|
/**
|
|
* Paused and ran out: resume() starts a new period.
|
|
*/
|
|
public function isExpired(): bool
|
|
{
|
|
return $this->status() === SubscriptionStatus::EXPIRED;
|
|
}
|
|
|
|
/**
|
|
* Cancelled for good.
|
|
*/
|
|
public function isCancelled(): bool
|
|
{
|
|
return $this->status() === SubscriptionStatus::CANCELLED;
|
|
}
|
|
}
|