Configuration in Stripe
Stripe allows you to set up different tiered pricing models:
• Usage-based: The final price is based on the last tier reached, requiring a metering event in Stripe.
• Per tier: Each usage tier has a fixed price assigned.
• Volume: A single price applies to all units consumed within the range.
In your Stripe account, you must:
1. Create a price (price_id) with the desired pricing model.
2. Define unit ranges and their costs in Stripe’s pricing panel.
3. Create a Meter Event and associate it with the price_id.
Creating the Subscription in Laravel
To start a subscription with tiered pricing in Laravel, you can use the following code after integrating Stripe Elements:To start a subscription with tiered pricing in Laravel, you can use the following code after integrating Stripe Elements
public function processSubscription(Request $request)
{
$partner = auth('partner')->user();
$paymentMethod = $request->input('payment_method');
$partner->createOrGetStripeCustomer();
$partner->addPaymentMethod($paymentMethod);
$price_id = 'price_1...'; //Price ID
$partner->newSubscription('default')
->meteredPrice($price_id)
->create($paymentMethod);
return redirect()->route('subscription.form')->with('success', 'Subscription started!');
}
Report usage to stripe
For Stripe to correctly calculate invoices in “usage-based” models, you must periodically send usage information (consumption). This is done through usageRecords.
In Laravel, you can report it using a Command that runs at your desired frequency (e.g., every hour or at the end of the day).
$subscription = $user->subscriptions()->first();
if ($subscription && $subscription->items->first()) {
$user->reportMeterEvent('meter_id', quantity: 10);
}
It is important to understand that Stripe accumulates the amounts you send in each usage record event. This means that if you are charging based on the number of users, you only need to report newly added users instead of sending the total each time.
The same applies if, instead of “users,” you are measuring tokens, messages, or any other metric—you only need to send the actual usage increments, and Stripe will handle the cumulative count for billing.
Schedule this command in your app/Console/Kernel.php or execute it in your workflow, and Stripe will automatically bill the accumulated usage in each billing cycle.
Conclusion
Setting up tiered pricing in Stripe with Laravel is straightforward if you properly define the tiers in Stripe and report usage correctly. This approach allows for automatic billing based on the number of users without the need to manually manage subscriptions.