AshBilling - Declarative Stripe billing and subscriptions for Ash Framework

Hi everyone!

I’d like to introduce AshBilling, an open-source Elixir library I’ve been working on to simplify Stripe billing and subscription management in applications built with the Ash Framework.

The goal is to make billing feel like a natural part of your Ash domain, rather than a separate integration that requires its own synchronization and authorization infrastructure.

The problem

When integrating Stripe subscriptions into an Ash application, creating a Checkout Session is usually the easy part.

The work tends to grow around it:

  • Storing and associating Stripe customer IDs with Ash resources.

  • Synchronizing subscriptions when Stripe sends webhooks.

  • Handling duplicate deliveries, retries, and out-of-order events.

  • Keeping subscription state consistent in PostgreSQL.

  • Connecting subscription entitlements to Ash authorization policies.

  • Managing cancellations, plan changes, and the billing portal.

Without a billing integration, an application typically needs to implement and maintain these pieces individually.

AshBilling brings them together through Ash resource extensions, a provider API, webhook synchronization, and native policy checks.

Using AshBilling

1. Create a Stripe Checkout Session

{:ok, session} =
  AshBilling.create_checkout_session(user,
    price: "price_pro_monthly",
    success_url: "https://example.com/billing/success",
    cancel_url: "https://example.com/billing/cancel"
  )

# Redirect the user to session.url

AshBilling can associate Stripe customer and subscription information with your existing Ash resources as webhook events are processed.

2. Authorize actions with subscription policies

In an Ash resource that already defines :generate and :export actions:

policies do
  policy action(:generate) do
    authorize_if AshBilling.Checks.Subscribed
  end

  policy action(:export) do
    authorize_if {AshBilling.Checks.HasPrice,
      price: "price_pro_monthly"}
  end
end

Subscribed checks for an eligible active subscription, while HasPrice requires a specific price.

As of v0.4.1, HasPrice also validates its configuration during Ash policy compilation. Missing or invalid price identifiers are rejected, and runtime checks fail closed.

The policy checks use locally synchronized subscription state, so webhook configuration remains an important part of the integration.

3. Manage subscriptions

The public API also supports common billing operations:

AshBilling.create_portal_session(user,
  return_url: "https://example.com/settings"
)

AshBilling.cancel_subscription(subscription)

AshBilling.resume_subscription(subscription)

AshBilling.swap_plan(subscription, "price_enterprise")

These operations interact with the configured billing provider. Subscription state is synchronized through the corresponding billing events.

Webhook handling and resilience

Stripe webhooks may be delivered more than once or arrive out of order.

AshBilling includes signature verification, event deduplication, retry-aware error handling, and synchronization safeguards.

Two deduplication approaches are available:

  • In-memory ETS: suitable for development and single-node use.

  • PostgreSQL-backed resources: persistent event tracking with transaction-scoped advisory locks and row-level fencing for coordinating concurrent processing across application nodes.

For multi-node deployments, the PostgreSQL-backed approach requires proper resource configuration, migrations, and a compatible shared Repo setup.

AshBilling.WebhookEvent is an internal resource and should not be exposed through public APIs.

These mechanisms help protect billing synchronization, but applications must still handle external side-effect idempotency, authorization, and operational recovery appropriately.

Current status

AshBilling v0.4.1 includes stricter authorization configuration checks, regression tests, and clearer documentation for PostgreSQL-backed webhook deduplication.

The project currently focuses on Stripe and Ash 3.x, with the aim of keeping the integration declarative while making its operational boundaries explicit.

Links:

  • GitHub

I’d be interested in feedback from anyone building subscription-based applications with Ash, particularly around the resource DSL, authorization policies, and webhook integration.

Thanks for taking a look!

12 Likes