Implementing Credit-Based Billing Systems in Laravel
Overview
Modern SaaS pricing has shifted from feature-gating to consumption-based models. This approach, popular in AI-driven tools, allows users to pay for specific usage—like tokens or

Prerequisites
To follow this guide, you should be comfortable with
Key Libraries & Tools
- Laravel Cashier: Manages subscriptions and webhooks.
- Stripe: Handles the actual payment processing.
- Prism: A package used here to interact with the AnthropicAI provider.
- PHP: Used to define static plan limits and pricing.
Code Walkthrough
1. Database Schema
Add fields to your users table to track the current balance and reset dates.
Schema::table('users', function (Blueprint $table) {
$table->integer('credits_remaining')->default(100);
$table->integer('credits_limit')->default(100);
$table->timestamp('credits_reset_at')->nullable();
});
2. Credit Middleware
Validate that a user has enough credits before they hit expensive API endpoints.
public function handle(Request $request, Closure $next)
{
if ($request->user()->credits_remaining < 1) {
return back()->with('error', 'Insufficient credits.');
}
return $next($request);
}
3. Deduction Logic
Wrap the deduction in a service to keep your controllers clean and ensure transactions are logged.
public function deduct(User $user, int $amount)
{
$user->decrement('credits_remaining', $amount);
$user->creditTransactions()->create([
'amount' => $amount,
'type' => 'usage'
]);
}
Syntax Notes
This implementation uses PHP Enums to store plan details, making it easy to call $plan->creditLimit() anywhere in the app. It also relies on Laravel Artisan Commands to automate monthly resets by comparing the credits_reset_at timestamp with the current date.
Practical Examples
This system is ideal for AI Content Generators where each API call to models like
Tips & Gotchas
Avoid over-reliance on third-party wallet packages for simple credit needs; they can create dependency hell during invoice.payment_succeeded) to trigger credit resets, ensuring users only get their new balance once payment clears.