Laravel Cashier Subscription Billing

By Evytor Dailyβ€’August 7, 2025β€’Programming / Developer

🎯 Summary

Laravel Cashier provides an elegant, fluent interface to manage subscription billing services like Stripe and Paddle. This comprehensive guide walks you through setting up Cashier, defining subscription plans, handling trials, managing subscriptions, and implementing advanced features like webhooks and customized billing logic. If you are looking to integrate subscription billing into your Laravel application, Cashier is the way to go! Let's dive in!

Setting Up Laravel Cashier πŸ”§

Before you can start using Cashier, you need to install it and configure your billing provider credentials. This section covers the initial setup steps.

Installation

Install Cashier via Composer:

composer require laravel/cashier 

Configuration

Next, configure your billing provider (e.g., Stripe) by setting the appropriate API keys in your .env file:

STRIPE_KEY=your_stripe_public_key STRIPE_SECRET=your_stripe_secret_key 

Publish Cashier's migrations:

php artisan vendor:publish --tag="cashier-migrations" php artisan migrate 

Defining Subscription Plans βœ…

Cashier allows you to define different subscription plans with varying prices and features. This section shows you how to create and manage these plans.

Creating Plans in Stripe

First, create your subscription plans in your Stripe dashboard. Note the plan IDs.

Referencing Plans in Your Application

Reference these plan IDs in your application, typically within your models or configuration files.

// In your User model public function subscribeToBasic() {     return $this->newSubscription('default', 'price_YOURBASICPLANID')                 ->create($paymentMethod); } 

Handling Trials and Subscriptions πŸ€”

Trials are a great way to attract new subscribers. Cashier simplifies the process of offering and managing trial periods.

Offering Trial Periods

You can easily offer trial periods by specifying the trialDays option when creating a subscription:

$user->newSubscription('default', 'price_YOURPLANID')      ->trialDays(14)      ->create($paymentMethod); 

Checking Subscription Status

Cashier provides convenient methods to check the status of a user's subscription:

if ($user->subscribed('default')) {     // User is subscribed to the 'default' plan }  if ($user->onTrial('default')) {     // User is on trial for the 'default' plan } 

Managing Subscriptions πŸ“ˆ

Once a user is subscribed, you'll need to manage their subscription, including cancellations, renewals, and updates.

Cancelling Subscriptions

Users can easily cancel their subscriptions:

$user->subscription('default')->cancel(); 

Resuming Subscriptions

Subscriptions can be resumed before the end of the billing cycle:

$user->subscription('default')->resume(); 

Updating Subscriptions

To switch a user to a different subscription plan, use the swap method:

$user->subscription('default')->swap('price_NEWPLANID'); 

Advanced Features and Webhooks πŸ’‘

Cashier also supports advanced features like handling webhooks for events such as failed payments and subscription cancellations.

Handling Webhooks

Configure Stripe webhooks to notify your application of events. Cashier provides a middleware to verify webhook signatures.

// In your routes/web.php Route::post('/stripe/webhook',     '\Laravel\Cashier\Http\Controllers\WebhookController@handleWebhook'); 

Customizing Billing Logic

You can customize Cashier's behavior by overriding methods in your models or creating custom event listeners.

Troubleshooting Common Issues

Encountering issues with Laravel Cashier? Here are some common problems and their solutions:

Issue: Payment Intent Requires Action

Solution: Implement 3D Secure authentication flow.

     $payment = $subscription->latestPayment();      if ($payment->requiresPaymentMethod()) {         return redirect()->route('cashier.payment', [             $payment->id,              'redirect' => route('your.redirect.route'),         ]);     }     

Issue: Webhook Not Being Processed

Solution: Ensure your webhook URL is correctly configured in Stripe and your server can receive POST requests.

php artisan route:list | grep stripe.webhook 

Issue: Subscription Not Active After Payment

Solution: Verify the subscription status in Stripe and ensure your application correctly reflects the changes.

     if ($user->subscribed('default')) {         // Subscription is active     }     

Code Sandbox Example

Here's a simplified interactive example using a CodeSandbox embed, demonstrating a basic subscription setup. This allows you to experiment with the code directly and see how it behaves in a real environment.

To run it locally, you will need NodeJS and NPM installed. Clone the repository, and then run the following commands:

 git clone https://github.com/example/laravel-cashier-sandbox.git cd laravel-cashier-sandbox npm install composer install php artisan serve 

Make sure to set your Stripe API keys correctly in the .env file for the application to work correctly.

Keywords

Laravel, Cashier, Subscription Billing, Stripe, Paddle, PHP, Framework, SaaS, Payments, Webhooks, Trials, Subscriptions, API, Configuration, Models, Composer, Migrations, Payment Intents, Webhook Handling, Subscription Management

Popular Hashtags

#Laravel, #Cashier, #SubscriptionBilling, #Stripe, #PHP, #WebDevelopment, #SaaS, #Payments, #Webhooks, #Coding, #Programming, #Developer, #LaravelFramework, #SubscriptionModel, #SoftwareDevelopment

Frequently Asked Questions

What is Laravel Cashier?

Laravel Cashier provides an expressive, fluent interface for managing subscription billing with services like Stripe and Paddle.

How do I install Cashier?

You can install Cashier via Composer using the command composer require laravel/cashier.

How do I define subscription plans?

Define your subscription plans in your billing provider's dashboard (e.g., Stripe) and reference the plan IDs in your application.

How do I handle trials with Cashier?

Use the trialDays method when creating a new subscription to offer a trial period.

Can I customize Cashier's behavior?

Yes, you can customize Cashier by overriding methods in your models or creating custom event listeners.

How can I integrate other services like payment gateways with Cashier?

While Cashier is primarily designed for Stripe and Paddle, you can extend it to work with other payment gateways by creating custom drivers or utilizing webhooks to handle payment confirmations and subscription events. Make sure to check out our other article on Laravel Payment Integrations for a deeper dive.

Is it possible to set up metered billing with Laravel Cashier?

Yes, with Cashier, you can manage metered billing by tracking usage and applying charges accordingly. This typically involves setting up webhooks to monitor usage events and updating subscriptions based on those events. You may also find helpful information in our guide on Advanced Billing Strategies.

A clean, modern illustration depicting a Laravel application interface with a prominent 'Subscription' dashboard. The dashboard shows interactive charts representing subscriber growth and revenue trends. Include icons representing payment methods (Stripe, Paddle) and secure lock symbols to convey billing security. The color scheme should be professional and inviting, using Laravel's brand colors subtly.