Introducing a new way to review your PRs.

Read the announcement

ndolestudio / httpsms

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API with end to end encryption supported.

View on GitHub

22 documents · Updated Nov 18, 2026

docs / subscription-billing-and-entitlements

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 needResult
Change a plan or subscription detailsA Lemon Squeezy customer portal URL
Cancel a subscriptionA cancellation request sent to Lemon Squeezy
Review paymentsSubscription invoice records fetched from Lemon Squeezy
Download an invoiceA 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_created
  • subscription_updated
  • subscription_cancelled
  • subscription_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.

EventLocal account effect
CreatedStore the subscription ID, plan, status, and renewal time. Clear any end time.
UpdatedFor an active subscription, refresh the ID, plan, status, end time, and renewal time. Non-active updates do not change the account.
CancelledKeep the subscription and plan until the end time. Store the cancellation status and clear the renewal time.
ExpiredRemove 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 typeCurrent free-plan limitChecked before
Message volume200 messages per billing cycleMessage send actions
Contacts200Contact creation
Send schedules1Schedule creation
Phone API keys1API-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.

EndpointPurpose
GET /v1/billing/usageRead the current sent and received message totals
GET /v1/billing/usage-historyRead 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

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.

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

TriggerConsequenceGuard
Invalid Lemon Squeezy signatureThe webhook is rejected and no subscription state changes.Verify X-Signature before event parsing.
Unknown provider variantThe account is mapped to the free plan.Add each provider variant to the plan mapping before release.
Missing webhook user ID and unmatched emailThe lifecycle event cannot update an account.Keep provider custom user_id data and account email aligned.
Subscription update is not activeThe 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 cycleA 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 limitThe 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 listInvoice generation is rejected during validation.Validate the invoice ID against the authenticated subscription.
Contact count plus requested batch exceeds the plan limitContact creation is rejected.Check the full requested count before creating contacts.

Key files

FileWhy to read it
api/…/lemonsqueezy_service.goUnderstand provider identity lookup, plan mapping, and lifecycle event creation.
api/…/lemonsqueezy_handler.goUnderstand the provider webhook boundary and supported event names.
api/…/user_listener.goSee how internal subscription events reach account state updates.
api/…/user.goSee the subscription fields, plan limits, and billing anchor rule.
api/…/billing_service.goUnderstand message entitlement checks, usage reads, and usage alerts.
api/…/entitlement_service.goUnderstand count limits for contacts, schedules, and phone API keys.
api/…/contact_handler.goSee how contact creation checks the account's plan limit.
api/…/gorm_billing_usage_repository.goUnderstand billing-cycle records and message count persistence.
api/…/billing_handler.goSee the API surface for current usage and usage history.
api/…/user_service.goSee subscription management, payment lookup, and invoice generation.