Skip to content
Steven Roland

Laravel Cashier: Simplifying Subscription Billing with Stripe

Laravel Cashier provides an expressive, fluent interface to Stripe's subscription billing services. It handles almost all of the boilerplate subscription billing code you dread writing, allowing you to focus on building your application. Let's explore how to use Cashier and some of its key features.

Getting Started

First, install Cashier via Composer:

composer require laravel/cashier

Next, add the Billable trait to your User model:

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}

Creating Subscriptions

Creating a subscription is straightforward with Cashier:

$user = User::find(1);
$user->newSubscription('default', 'price_monthly')->create($paymentMethod);

This creates a new subscription for the user with the 'default' name and 'price_monthly' Stripe price ID.

Checking Subscription Status

You can easily check a user's subscription status:

if ($user->subscribed('default')) {
    // User has an active subscription
}

if ($user->onTrial('default')) {
    // User is on trial
}

if ($user->subscription('default')->cancelled()) {
    // Subscription is cancelled
}

Handling Payment Failures

Cashier makes it easy to handle failed payments:

use Laravel\Cashier\Subscription;

Subscription::failed(function ($subscription) {
    // Notify user of failed payment
});

Generating Invoices

Cashier can generate PDF invoices for your customers:

$invoice = $user->invoices()->first();

return $invoice->download();

Suggested Usages

  • SaaS Applications: Implement tiered pricing plans for your software service.

  • Content Subscriptions: Offer premium content access through subscriptions.

  • Membership Sites: Manage member access and recurring payments.

  • API Access: Charge for API usage with metered billing.

  • Digital Downloads: Sell digital products with one-time charges.

Metered Billing

For usage-based billing, Cashier supports metered subscriptions:

$user->newSubscription('default', 'price_metered')
    ->meteredPlan()
    ->create($paymentMethod);

// Report usage
$user->subscription('default')->reportUsage(5);

Conclusion

Laravel Cashier simplifies the complex world of subscription billing, allowing you to focus on building great products. With its intuitive API and seamless Stripe integration, implementing a robust billing system has never been easier.

Remember to always refer to the official Laravel Cashier documentation for the most up-to-date information and best practices.

Support My Work

If you enjoy my content, consider supporting me through Buy Me a Coffee or GitHub Sponsors.

Buy Me A Coffee
or

More posts

Getting Started with Laravel Dusk for Browser Testing

Laravel Dusk simplifies browser testing with an easy-to-use API. Set up Dusk, write tests for page interactions, handle dynamic content, and capture screenshots. Use it for form submissions, user flows, and cross-browser compatibility.