Subscription billing and entitlements
Subscription billing connects Lemon Squeezy subscription state to the account record that controls product access. It also tracks message use in billing cycles so the API can enforce plan limits, show usage, and provide access to subscription invoices. See Accounts and billing for the wider account and billing model.
Billing provider integration
Lemon Squeezy is the external source for subscription actions and payment records. The application keeps the subscription identity and current state on the user account, while it asks Lemon Squeezy for customer-facing subscription links and invoice data.
Subscription management
An authenticated user can open the provider's customer portal to update a subscription or request cancellation. These actions start in the API, but the account is changed only when the provider sends the matching lifecycle event. The routes are exposed by api/…/user_handler.go.
| User need | Result |
|---|---|
| Change a plan or subscription details | A Lemon Squeezy customer portal URL |
| Cancel a subscription | A cancellation request sent to Lemon Squeezy |
| Review payments | Subscription invoice records fetched from Lemon Squeezy |
| Download an invoice | A provider-generated PDF returned by the API |
References: api/…/user_handler.go• L178-231 api/…/user_handler.go• L267-339 api/…/user_service.go• L61-126 api/…/user_service.go• L390-424
Webhook intake
The provider sends subscription changes to POST /lemonsqueezy/event. The endpoint checks the provider signature before it accepts the event. It then maps the provider event to an internal event so the rest of the system can update the account without depending on the provider payload.
The supported provider events are:
subscription_createdsubscription_updatedsubscription_cancelledsubscription_expired
The event can identify an account with custom user_id data. If that data is not present, the system finds the account by the email in the provider event. Provider variant names become the local subscription names used by entitlement checks. An unknown variant falls back to the free plan.
The diagram shows how a provider callback becomes an account change and then affects access decisions.
flowchart LR
A[Lemon Squeezy subscription event] --> B[Verify signature]
B --> C[Map provider event and plan]
C --> D[Dispatch internal subscription event]
D --> E[Update user subscription state]
E --> F[Apply plan limits to product actions]
References: api/…/lemonsqueezy_handler.go• L41-100 api/…/lemonsqueezy_handler_validator.go• L32-45 api/…/lemonsqueezy_service.go• L42-59 api/…/lemonsqueezy_service.go• L61-189 api/…/lemonsqueezy_service.go• L192-234
Subscription lifecycle
Subscription lifecycle events are the handoff between billing-provider state and local account state. Internal event listeners apply each accepted event to the user record, which makes the local subscription name and status available to message and resource checks.
Lifecycle states
The account stores the provider subscription ID together with its plan, status, renewal time, and end time. The fields that are present depend on the lifecycle state.
| Event | Local account effect |
|---|---|
| Created | Store the subscription ID, plan, status, and renewal time. Clear any end time. |
| Updated | For an active subscription, refresh the ID, plan, status, end time, and renewal time. Non-active updates do not change the account. |
| Cancelled | Keep the subscription and plan until the end time. Store the cancellation status and clear the renewal time. |
| Expired | Remove the subscription details and return the account to the free plan. |
Cancellation therefore does not immediately remove paid access. Expiration is the event that returns the account to free-plan state. The internal event payloads and account updates are defined by api/pkg/events and api/…/user_service.go.
Event dispatch
The provider service creates one internal event for each supported lifecycle change. The user listener consumes that event and performs the matching account update. This separation keeps webhook parsing and account persistence as separate stages.
References: api/…/user_subscription_created_event.go• L9-20 api/…/user_subscription_updated_event.go• L9-21 api/…/user_subscription_canceled_event.go• L9-20 api/…/user_subscription_expired_event.go• L9-21 api/…/user_listener.go• L87-152 api/…/user_service.go• L367-498
Entitlement checks
An entitlement is the permission to perform an action under the account's current plan. The system uses two kinds of checks: message-volume checks and count checks for selected account resources.
Message entitlements
Before the system sends a message, the billing service loads the account's current usage and compares the requested message count with the plan limit. A single send checks one message. A bulk send checks the whole batch before it queues the messages. The same gate is used by supported message entry points such as API sends and integration sends.
If the request would pass the limit, the action continues. If it would exceed the limit, the API returns a payment-required response with an upgrade message. The first limit breach also sends a usage-limit email, with a short cache period to avoid repeated notices.
Resource entitlements
Resource entitlements limit how many instances of some account resources a user can create. They are separate from message volume. The current plan-based count limits apply to contacts, free-plan send schedules, and phone API keys. Contact limits use the account plan's general limit, while paid plans and resources without a listed limit are allowed by this check.
The resource check can be enabled or disabled by the entitlement setting. When it is disabled, or when a resource has no configured limit, the request is allowed without a count query.
| Entitlement type | Current free-plan limit | Checked before |
|---|---|---|
| Message volume | 200 messages per billing cycle | Message send actions |
| Contacts | 200 | Contact creation |
| Send schedules | 1 | Schedule creation |
| Phone API keys | 1 | API-key creation |
References: api/…/billing_service.go• L51-118 api/…/entitlement_service.go• L16-110 api/…/user.go• L17-71 api/…/contact.go• L13-14 api/…/message_handler.go• L95-103 api/…/message_handler.go• L140-148 api/…/bulk_message_handler.go• L101-110 api/…/message_send_schedule_handler.go• L95-110 api/…/contact_handler.go• L313-321
Billing usage
Billing usage is the local record of sent and received messages for one account and one billing cycle. It supports both enforcement and the usage views shown to users, while the provider remains responsible for subscription payments.
Usage cycle
Each usage record has a start time, end time, sent count, received count, and total cost field. The current record is found from the current UTC time. A paid account uses its subscription renewal day as the cycle anchor. A free account uses the account creation day. If an anchor day does not exist in a month, the cycle uses the last day of that month.
The total used for message entitlement is the sum of sent and received messages. A request is allowed when the current total plus the requested count is less than or equal to the plan limit.
Usage persistence
Message events update usage after the system observes an API-sent message or a phone-received message. The repository increments the matching cycle in a transaction. If no cycle exists, it creates one with the event's timestamp and records the first message in that cycle.
Account deletion removes that account's billing usage records. This keeps usage data aligned with the account lifecycle.
References: api/…/billing_usage.go• L9-29 api/…/user.go• L131-139 api/…/billing_usage_repository.go• L10-25 api/…/gorm_billing_usage_repository.go• L37-139 api/…/gorm_billing_usage_repository.go• L165-217 api/…/billing_listener.go• L16-89
Usage access
Authenticated clients can read the current usage record or page through completed usage records. The current record is created on demand when no record covers the current time. History returns only records whose cycle has ended and sorts them from newest to oldest.
| Endpoint | Purpose |
|---|---|
GET /v1/billing/usage | Read the current sent and received message totals |
GET /v1/billing/usage-history | Read completed billing-cycle records with paging |
References: api/…/billing_handler.go• L39-112 api/…/billing_usage_history_request.go• L9-33 api/…/billing_responses.go• L7-17 api/…/billing_service.go• L120-134
Invoice access
Invoice access uses the subscription ID stored on the local account to query Lemon Squeezy. Accounts without a subscription ID receive an empty payment list rather than a locally stored invoice record.
Payment history
The authenticated user can request subscription payments from the provider. The returned records represent the purchase and renewal invoices for that subscription. Before the application generates a PDF, it checks that the requested invoice ID belongs to the authenticated user's provider subscription.
Invoice generation
The user supplies invoice details such as name, address, location, and notes. The service asks Lemon Squeezy to generate the invoice and then returns the provider's download content as a PDF attachment. The application does not persist the invoice file.
References: api/…/user_handler.go• L267-339 api/…/user_handler_validator.go• L113-127 api/…/user_payment_invoice_request.go• L8-45 api/…/user_service.go• L61-126
Related system boundaries
Billing state affects more than the billing endpoints. Message send paths use message entitlements before they queue work, while account and dashboard views use the stored subscription and usage values to present the user's current access.
- Identity and user preferences describes the account that owns subscription and usage state.
- Bulk and scheduled messaging describes batch sends and scheduled work that must respect message or resource entitlements.
- Web dashboard describes the client surface that reads billing and account state.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep Lemon Squeezy variant mapping aligned with the local subscription names and their message limits.
- Keep provider webhook event names aligned with the internal subscription event types and listener handlers.
- Preserve the account lookup path for webhook events when provider custom data is absent.
- Keep billing-cycle anchor rules aligned with subscription renewal fields and usage record boundaries.
- Keep contact creation aligned with the plan limit and keep all message entry points behind the same count-aware entitlement check so bulk sends cannot bypass the single-send rule.
- Keep invoice ownership validation tied to the authenticated user's subscription ID before provider invoice generation.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Invalid Lemon Squeezy signature | The webhook is rejected and no subscription state changes. | Verify X-Signature before event parsing. |
| Unknown provider variant | The account is mapped to the free plan. | Add each provider variant to the plan mapping before release. |
| Missing webhook user ID and unmatched email | The lifecycle event cannot update an account. | Keep provider custom user_id data and account email aligned. |
| Subscription update is not active | The local account keeps its prior subscription state. | Treat active provider updates as the state that can refresh paid access. |
| Usage record is missing for the current cycle | A new cycle record is created on demand. | Use the account's billing anchor when creating the record. |
| Message count plus requested batch exceeds the plan limit | The message action returns payment required and is not queued. | Check the full requested count before dispatch. |
| Requested invoice is not in the user's provider payment list | Invoice generation is rejected during validation. | Validate the invoice ID against the authenticated subscription. |
| Contact count plus requested batch exceeds the plan limit | Contact creation is rejected. | Check the full requested count before creating contacts. |
Key files
| File | Why to read it |
|---|---|
| api/…/lemonsqueezy_service.go | Understand provider identity lookup, plan mapping, and lifecycle event creation. |
| api/…/lemonsqueezy_handler.go | Understand the provider webhook boundary and supported event names. |
| api/…/user_listener.go | See how internal subscription events reach account state updates. |
| api/…/user.go | See the subscription fields, plan limits, and billing anchor rule. |
| api/…/billing_service.go | Understand message entitlement checks, usage reads, and usage alerts. |
| api/…/entitlement_service.go | Understand count limits for contacts, schedules, and phone API keys. |
| api/…/contact_handler.go | See how contact creation checks the account's plan limit. |
| api/…/gorm_billing_usage_repository.go | Understand billing-cycle records and message count persistence. |
| api/…/billing_handler.go | See the API surface for current usage and usage history. |
| api/…/user_service.go | See subscription management, payment lookup, and invoice generation. |