Schema for subscriptions (master record).
Subscriptions are controlled internally by PhoenixKit, NOT by payment providers. This allows using any payment provider (even those without subscription APIs) and provides full control over subscription lifecycle.
Status Lifecycle
trialing -> active -> [past_due -> active] -> cancelled
-> paused -> active
-> cancelledtrialing- Free trial period activeactive- Subscription is active and paidpast_due- Payment failed, in grace periodpaused- Subscription temporarily paused by usercancelled- Subscription ended
Renewal Process
Renewals are handled by Oban workers:
SubscriptionRenewalWorkerruns daily, checks subscriptions near period end- Creates invoice for the subscription
- Charges saved payment method via provider
- On success: extends
current_period_end - On failure: sets status to
past_due, incrementsrenewal_attempts
Grace Period (Dunning)
When payment fails:
- Status changes to
past_due grace_period_endis set (configurable days)SubscriptionDunningWorkerretries payment- After max attempts or grace period end: subscription cancelled
Summary
Functions
Changeset for activating a subscription after successful payment.
Returns true if the subscription is currently active (can use service).
Changeset for cancelling a subscription.
Returns true if the subscription is cancelled.
Returns true if the subscription will be cancelled at period end.
Creates a changeset for creating a new subscription.
Returns the number of days remaining in the current period.
Returns true if grace period has expired.
Returns true if the subscription is past due (payment failed).
Changeset for marking subscription as past_due.
Changeset for pausing a subscription.
Returns true if the subscription is paused.
Returns true if renewal is due (period end is near or past).
Changeset for resuming a paused subscription.
Returns true if we should attempt renewal (within 24 hours of period end).
Changeset for starting a trial.
Returns true if the subscription is in trial period.
Functions
Changeset for activating a subscription after successful payment.
Returns true if the subscription is currently active (can use service).
Changeset for cancelling a subscription.
Returns true if the subscription is cancelled.
Returns true if the subscription will be cancelled at period end.
Creates a changeset for creating a new subscription.
Returns the number of days remaining in the current period.
Returns true if grace period has expired.
Returns true if the subscription is past due (payment failed).
Changeset for marking subscription as past_due.
Changeset for pausing a subscription.
Returns true if the subscription is paused.
Returns true if renewal is due (period end is near or past).
Changeset for resuming a paused subscription.
Returns true if we should attempt renewal (within 24 hours of period end).
Changeset for starting a trial.
Returns true if the subscription is in trial period.