Skip to main content

Crate larust_cashier_stripe

Crate larust_cashier_stripe 

Source
Expand description

The Stripe backend of Larust’s Cashier umbrella (Laravel’s laravel/ cashier) - subscription billing. One backend crate per payment provider, deliberately, not one trait every provider implements. Stripe Checkout Sessions, PayPal Subscriptions, and Square Subscriptions don’t actually share enough structure (webhook signing scheme, subscription/price object shape, …) for a shared interface to be anything but either leaky or lowest-common-denominator thin - the same conclusion Laravel’s own ecosystem already reached by shipping laravel/cashier (Stripe) and laravel/cashier-paddle as two entirely separate packages rather than one package behind an interface. A future larust-cashier-paypal/larust-cashier-square crate is the same shape as this one, named the same way, re-exported alongside it through larust_support::cashier::paypal/::square the way this crate is re-exported through larust_support::cashier::stripe (see this module’s own closing doc comment below) - an app can enable more than one backend feature at once (“install multiple Cashier methods under the same umbrella”) with zero extra plumbing, since they’re just ordinary independent Cargo features already.

Stripe is the one real exception to this project’s usual “no required paid service” posture ([larust-scout]’s Meilisearch, [larust-reverb]’s WebSocket server, [larust-db]’s embedded redb, all self-hostable) - there is no self-hostable, pure-Rust subscription-billing engine to stand in for it, and real Laravel Cashier itself is exactly as Stripe-specific today (Paddle support was dropped from current Cashier releases). An honest, unavoidable exception, not a quiet departure from that posture.

§The shape of this crate

Laravel’s Billable trait is usually mixed into the User model itself, backed by columns (stripe_id, …) added directly to that model’s own table. This crate instead owns two small tables of its own - cashier_customers (user id → Stripe customer id) and cashier_subscriptions (a local mirror of each user’s current Stripe subscription) - the same “a new optional feature owns its own tables, never reaches into an app’s existing schema” shape [larust-sanctum]/[larust-passport]/[larust-pennant] already establish. Every function here takes a plain user_id: i64 rather than requiring an Authenticatable impl - this crate has no request-time “current billable user” extractor to resolve, so there’s nothing that actually needs the trait; an app calls these with user.id, the same call-site shape [larust-passport]’s own AuthorizeRequest.user_id already uses.

cashier_subscriptions is a mirror, not the source of truth - Stripe owns real subscription state, and this crate’s own understanding of it only ever updates in response to a verified webhook event (see webhook::handle_webhook). subscription::cancel_subscription/ subscription::resume_subscription/subscription::swap_subscription call Stripe’s API directly but do not update the local mirror themselves - the next customer.subscription.updated webhook does that, same “eventually consistent, not synchronously updated” tolerance [larust-scout]’s own indexing calls already document. [subscription:: subscription_for]/subscription::subscribed read the mirror only, never calling Stripe - fast, and correct as of the last webhook Stripe actually delivered.

See each submodule’s own doc comment for the full design of its own piece: customer (Stripe customer creation + the local id mapping), checkout (Stripe Checkout Session creation for starting a subscription), subscription (cancel/resume/swap + reading the local mirror), webhook (signature verification + keeping the mirror in sync).

Re-exported through larust_support::cashier::stripe (see crates/ larust-support/src/lib.rs) so generated apps depend only on larust-support, never on this crate directly. Gated behind its own cashier-stripe Cargo feature (not a bare cashier feature) - each Cashier backend is independently selectable, by design (see this module’s own opening doc comment).

§Deliberately out of scope

  • No Checkout UI, consent screen, or customer portal. Your own app registers whatever route redirects to checkout::checkout_url’s returned URL and whatever route receives Stripe’s webhook POST - the same “framework provides the mechanism, the app owns the actual route” split [larust-sanctum]/[larust-passport]/ [larust-socialite] already establish.
  • One subscription item per subscription. [subscription:: swap_subscription] replaces a subscription’s one price wholesale - no multi-item carts, quantities beyond 1, or metered/usage billing.
  • No invoice listing, payment-method management, or tax calculation. webhook::WebhookEvent::InvoicePaymentFailed is surfaced so your own app can react (send a dunning email, say) - this crate doesn’t otherwise model invoices at all.
  • No trial-period helpers. A trial is configured on the Stripe Price/subscription itself (trial_period_days) - Stripe’s own status of "trialing" already flows straight through subscription::subscription_for/subscription::subscribed with no special-casing needed here.

Re-exports§

pub use checkout::checkout_url;
pub use customer::create_customer;
pub use customer::customer_for;
pub use subscription::cancel_subscription;
pub use subscription::resume_subscription;
pub use subscription::subscribed;
pub use subscription::subscription_for;
pub use subscription::swap_subscription;
pub use subscription::Subscription;
pub use webhook::handle_webhook;
pub use webhook::WebhookEvent;

Modules§

checkout
Starting a subscription - Laravel’s $user->newSubscription($type, $price)->checkout(...). This crate doesn’t render anything itself: checkout_url returns the URL Stripe’s own hosted Checkout page lives at, and your own route redirects the browser there.
customer
Maps a local user_id to a Stripe customer - Laravel Cashier’s own $user->stripe_id, stored in this crate’s own cashier_customers table instead of a column on the app’s users table (see this crate’s own doc comment for why). One row per user, created once and reused by every later create_customer call for the same user.
subscription
Reading a user’s current subscription, and the three Stripe-side management actions Cashier itself offers: cancel, resume, and swap price.
webhook
Verifying and handling Stripe webhook events - the only thing that ever writes to this crate’s own cashier_subscriptions mirror (see this crate’s own doc comment for why). Your own app registers a route (POST /stripe/webhook, say) that reads the raw request body and the Stripe-Signature header and hands both to handle_webhook - the same “framework provides the mechanism, the app owns the actual route” split larust-sanctum/larust-passport already establish.

Enums§

CashierError
Every error this crate can produce. Stripe covers both a transport failure (network error) and Stripe itself returning a non-2xx response - callers that need to distinguish those two specifically can match on the message, but in practice every caller in this crate’s own design just surfaces it as a failure either way, the same way a database connection failure and a constraint violation both just become AppError::Internal elsewhere in this codebase.