SFelixAlejandro

SFelixAlejandro

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!

Showing Posts 1 to 1

stocks29

stocks29

Awesome! I look forward to trying this.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New
danschultzer
I’ve moved the strategies out of PowAssent and created a multi-provider framework for Elixir. This way the strategies can be used in any ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
mat-hek
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn: This is possible because it now builds on a very stab...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews