Skip to main content
The subscription system enables flexible, time-based billing for customers with support for multi-phase pricing, recurring and one-time charges, prorated billing, and automatic invoice generation.

Subscription model

Subscriptions are built on a hierarchical model:

Subscriptions

The top-level container represents a customer’s ongoing relationship with your service. Each subscription:
  • Belongs to a specific customer and account
  • Must have at least one subscription phase
  • Derives its start and end dates from its phases
  • Tracks billing cycles through phase transitions

Subscription phases

Time-bounded periods within a subscription that define different billing terms:
  • Start date — when this phase begins
  • End date — when this phase ends (optional for the final phase)
  • Multiple items — each phase can contain multiple subscription items
  • Validation — phases cannot overlap and must be sequential

Subscription phase items

Individual billable items within each phase:
  • Price reference — links to a specific product price
  • Quantity — how many units of this item (default: 1)
  • Price override — optional custom pricing that overrides the base price
  • Billing type — recurring or one-time charges
  • Validation — each phase must have at least one item, and all recurring items must share the same interval

Key features

Datetime precision

The subscription system uses full datetime fields (not just dates) for phase boundaries, enabling:
  • Hourly billing — cloud computing, parking, co-working spaces
  • Daily subscriptions — equipment rentals, temporary access
  • Precise transitions — exact moment when trials end and billing begins
  • No ambiguity — clear timezone-aware datetime boundaries

Automatic validation

Built-in validations prevent billing errors:
  • No overlaps — phases cannot overlap in time
  • No gaps — phases must connect seamlessly (end_date = next start_date)
  • Sequential integrity — phases automatically validated for proper ordering
  • Interval consistency — all recurring items in a phase must have matching intervals
  • Price constraints — overrides must be non-negative ($0 allowed for free trials)

Billing cycle anchors

Billing cycle anchors control when recurring charges occur. See Billing cycles for details.

Creating a subscription

Minimum requirements

To create a subscription, you need:
  1. Account — the business account that owns the subscription
  2. Customer — the customer receiving the subscription
  3. At least one phase with a start date
  4. At least one phase item linking to a price

Basic subscription

Multi-phase subscription

Validation rules

Subscription level

  • Must have at least one subscription phase
  • Cannot exist without phases at any time

Phase level

  • Must have at least one subscription phase item
  • Start date required on all phases
  • End date required if subsequent phases exist
  • Phases cannot overlap
  • Phases must be sequential without gaps — the end_date of one phase must exactly match the start_date of the next
  • Supports hourly, daily, weekly, monthly, and yearly precision

Phase item level

  • Must reference a valid price
  • Quantity must be a positive integer (minimum 1)
  • Price overrides must be non-negative (can be $0.00 for free trials)
  • All recurring items in a phase must have the same interval type and count

Sequential phases

When creating multi-phase subscriptions, phases must connect seamlessly:
Gaps between phases will fail validation. The end_date of one phase must exactly match the start_date of the next phase, down to the second.

Hourly subscription example

Interval consistency

All recurring items within a single subscription phase must have consistent billing intervals. This prevents ambiguity in invoice generation and billing calculations.

Valid configurations

Invalid configurations

Non-recurring items are exempt from interval consistency rules. One-time charges can coexist with any recurring interval type within the same phase.

Database structure

start_date and end_date are datetime fields (not date fields), enabling precise time-based billing for hourly and daily subscriptions.

Key methods

Pricing flexibility

Price overrides

Phase items can override the base price of a product:
  • Use case: promotional pricing, volume discounts, custom contracts, free trials
  • Validation: must be non-negative (>= 0), can be $0.00 for free trials
  • Fallback: uses the product’s base price if no override is set (nil)

Quantity support

Each phase item supports configurable quantities:
  • Use case: per-seat licensing, usage-based billing, bulk purchases
  • Calculation: quantity x (overridden_price_amount || price.amount)

Billing intervals

Phase items inherit billing behavior from their associated prices:
  • Recurring: monthly, weekly, yearly billing cycles
  • One-time: charges that appear only once per phase

Phase transitions

Subscriptions automatically transition between phases based on date ranges:
At any given time, the subscription has one active phase — the phase whose date range includes the current time. The active phase determines current billing amounts and drives invoice generation.

Real-world scenarios

Best practices

Always use Time.zone.parse() or Time.current for phase boundaries, not Date.parse() or Date.today. Datetime precision prevents ambiguity in phase transitions.
  1. Sequential phases — ensure the end_date of one phase exactly matches the start_date of the next
  2. Clear transition points — define specific datetimes for phase transitions, considering time zones
  3. Consistent intervals — all recurring items within a phase must have the same interval type and count
  4. Value progression — each phase should provide clear value changes to justify pricing changes
  5. Test your phases — validate that phases connect properly and intervals are consistent before deploying to production
  6. Grace periods — consider buffer time between phases for major pricing increases (but ensure no gaps in datetime terms)
  7. Communication — notify customers before phase transitions that affect pricing
  8. Price override validation — price overrides can be $0.00 (free trials) but not negative
  9. Last phase open-ended — the final phase can have end_date: nil for ongoing subscriptions

Billing cycles

Control when recurring charges occur with billing cycle anchors.

Invoice generation

How invoices are automatically generated from subscriptions.