Consent

This site uses third party services that need your consent.

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.

    More posts

    Understanding Laravel Breeze

    Explore Laravel Breeze, a lightweight authentication package offering flexibility with Blade, Livewire, and Inertia. Compare it with Laravel Jetstream for your project needs.

    The Art of Being: More Than Just Looking Nice

    Inspired by a quote from "Eleanor & Park," this post explores the idea of being art rather than just looking nice. It challenges readers to embrace their uniqueness and focus on evoking emotions rather than conforming to beauty standards.