PhoenixKit.Modules.Billing.Subscription (phoenix_kit v1.7.33)

Copy Markdown View Source

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
                   -> cancelled
  • trialing - Free trial period active
  • active - Subscription is active and paid
  • past_due - Payment failed, in grace period
  • paused - Subscription temporarily paused by user
  • cancelled - Subscription ended

Renewal Process

Renewals are handled by Oban workers:

  1. SubscriptionRenewalWorker runs daily, checks subscriptions near period end
  2. Creates invoice for the subscription
  3. Charges saved payment method via provider
  4. On success: extends current_period_end
  5. On failure: sets status to past_due, increments renewal_attempts

Grace Period (Dunning)

When payment fails:

  1. Status changes to past_due
  2. grace_period_end is set (configurable days)
  3. SubscriptionDunningWorker retries payment
  4. 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

activate_changeset(subscription, period_end)

Changeset for activating a subscription after successful payment.

active?(arg1)

Returns true if the subscription is currently active (can use service).

cancel_changeset(subscription, immediately \\ false)

Changeset for cancelling a subscription.

cancelled?(arg1)

Returns true if the subscription is cancelled.

cancelling?(arg1)

Returns true if the subscription will be cancelled at period end.

changeset(subscription, attrs)

Creates a changeset for creating a new subscription.

days_remaining(subscription)

Returns the number of days remaining in the current period.

grace_period_expired?(subscription)

Returns true if grace period has expired.

past_due?(arg1)

Returns true if the subscription is past due (payment failed).

past_due_changeset(subscription, grace_period_end)

Changeset for marking subscription as past_due.

pause_changeset(subscription)

Changeset for pausing a subscription.

paused?(arg1)

Returns true if the subscription is paused.

renewal_due?(arg1)

Returns true if renewal is due (period end is near or past).

resume_changeset(subscription)

Changeset for resuming a paused subscription.

should_renew?(arg1)

Returns true if we should attempt renewal (within 24 hours of period end).

trial_changeset(subscription, trial_end)

Changeset for starting a trial.

trialing?(arg1)

Returns true if the subscription is in trial period.