I stand up for children in need. Please join me in helping this family.

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

Generating Unique Lowercase Handles in Laravel Models

This blog post explains how to generate unique, lowercase handles in Laravel models. It covers defining the model, creating a unique key method, using model events for automatic handle assignment, and allowing user overrides for customization.

Building a Smooth Accordion Component with Alpine.js

Learn to create an interactive and accessible accordion component using Alpine.js. This tutorial covers basic implementation, styling enhancements, smooth transitions, and keyboard navigation. Build a user-friendly accordion interface with minimal JavaScript.

Keyboard Shortcuts with Alpine.js

Use Alpine.js to quickly, and very easily, implement key bindings into your website. I wrote an example to build a simple search component.