- Simplified Billing: Laravel Cashier simplifies the integration with Stripe and Paddle, abstracting away the complexities of handling subscriptions, payments, and invoices.
- Subscription Management: Cashier provides an easy way to manage subscriptions, including creating, updating, canceling, and resuming subscriptions.
- Payment Handling: It handles payment processing, including capturing payments, handling payment failures, and managing refunds.
- Invoice Generation: Cashier can generate invoices for your customers, making it easy to track and manage billing history.
- Trial Periods and Coupons: It supports trial periods and coupons, allowing you to offer discounts and promotions to your customers.
- Webhooks: Cashier integrates with webhooks, allowing you to handle events such as subscription cancellations, payment failures, and invoice creation.
Are you looking to simplify your subscription billing process? If yes, then Laravel Cashier is here to help you out. In this article, we will dive deep into the world of Laravel Cashier, a powerful package that simplifies subscription billing in your Laravel applications. We'll explore its features, benefits, and how it can streamline your billing processes. Whether you're building a SaaS platform, a membership site, or any application with recurring payments, Laravel Cashier can be a game-changer. So, let's dive in and discover how you can leverage Laravel Cashier to create a robust and efficient billing portal.
What is Laravel Cashier?
Laravel Cashier provides an expressive, fluent interface to Stripe's and Paddle's subscription billing services. It handles almost all of the boilerplate subscription billing code you are dreading writing. In addition to basic subscription management, Cashier can handle coupons, swapping subscriptions, subscription "quantities", cancellation grace periods, and even generate invoice PDFs.
Why Use Laravel Cashier?
Setting Up Laravel Cashier
Before we start using Laravel Cashier, we need to set it up in our Laravel application. Here's how you can do it:
Installation
You can install Laravel Cashier using Composer:
composer require laravel/cashier
Configuration
After installing Cashier, you need to configure it by adding your Stripe or Paddle API keys to your .env file:
STRIPE_KEY=your_stripe_key
STRIPE_SECRET=your_stripe_secret
PADDLE_VENDOR_ID=your_paddle_vendor_id
PADDLE_API_KEY=your_paddle_api_key
Database Migrations
Cashier requires a few database columns to store subscription information. You can run the following command to add these columns to your users table:
php artisan migrate
Billable Model
Next, you need to add the Billable trait to your User model:
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use Billable;
// ...
}
Creating Subscriptions
Now that we have set up Laravel Cashier, let's create a subscription for a user. Here's how you can do it:
Retrieving Plans
First, you need to retrieve the available plans from Stripe or Paddle. You can do this using the Stripe::products() or Paddle::products() methods:
use Stripe\Stripe;
$plans = Stripe::products()->all();
Subscribing a User
Once you have the plans, you can subscribe a user to a plan using the newSubscription() method:
$user = User::find(1);
$user->newSubscription('default', 'plan_id')->create($paymentMethod);
In this example, default is the name of the subscription, plan_id is the ID of the plan, and $paymentMethod is the payment method token.
Handling Trials
If you want to offer a trial period, you can use the trialDays() method:
$user->newSubscription('default', 'plan_id')
->trialDays(14)
->create($paymentMethod);
This will give the user a 14-day trial period before the subscription starts.
Managing Subscriptions
Laravel Cashier provides several methods for managing subscriptions. Let's take a look at some of the most common ones:
Checking Subscription Status
You can check the status of a subscription using the subscribed() method:
$user = User::find(1);
if ($user->subscribed('default')) {
// User is subscribed to the 'default' plan
}
Canceling Subscriptions
You can cancel a subscription using the cancel() method:
$user = User::find(1);
$user->subscription('default')->cancel();
This will cancel the subscription immediately. If you want to cancel the subscription at the end of the current period, you can use the cancelNow() method:
$user->subscription('default')->cancelNow();
Resuming Subscriptions
You can resume a canceled subscription using the resume() method:
$user = User::find(1);
$user->subscription('default')->resume();
Swapping Plans
You can swap a user to a different plan using the swap() method:
$user = User::find(1);
$user->subscription('default')->swap('new_plan_id');
Handling Webhooks
Webhooks are a crucial part of subscription billing. They allow you to handle events such as subscription cancellations, payment failures, and invoice creation. Laravel Cashier provides an easy way to handle webhooks.
Defining Webhook Routes
First, you need to define a webhook route in your routes/web.php file:
Route::post('/stripe/webhook', '\Laravel\Cashier\Http\Controllers\WebhookController@handleWebhook');
Handling Events
Next, you need to create a webhook handler to handle the events. You can do this by creating a new class that extends the Laravel\Cashier\Http\Controllers\WebhookController class:
use Laravel\Cashier\Http\Controllers\WebhookController;
class StripeWebhookController extends WebhookController
{
public function handleInvoicePaymentFailed($payload)
{
// Handle the invoice payment failed event
}
}
In this example, we're handling the invoice.payment_failed event. You can handle other events by creating methods with the corresponding names.
Building a Billing Portal
Now that we have covered the basics of Laravel Cashier, let's build a simple billing portal for our users. A billing portal allows users to manage their subscriptions, update their payment methods, and view their billing history.
Creating the Routes
First, we need to create the routes for our billing portal. Add the following routes to your routes/web.php file:
Route::get('/billing', 'BillingController@index')->name('billing.index');
Route::post('/billing/update-payment-method', 'BillingController@updatePaymentMethod')->name('billing.update-payment-method');
Route::get('/billing/invoices', 'BillingController@invoices')->name('billing.invoices');
Route::get('/billing/invoices/{invoice}', 'BillingController@downloadInvoice')->name('billing.download-invoice');
Creating the Controller
Next, we need to create the BillingController to handle the requests. Create a new controller using the following command:
php artisan make:controller BillingController
Here's the code for the BillingController:
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class BillingController extends Controller
{
public function index()
{
$user = auth()->user();
return view('billing.index', [
'subscription' => $user->subscription('default'),
'paymentMethods' => $user->paymentMethods(),
]);
}
public function updatePaymentMethod(Request $request)
{
$user = auth()->user();
$user->updateDefaultPaymentMethod($request->payment_method);
return redirect()->route('billing.index')->with('success', 'Payment method updated successfully.');
}
public function invoices()
{
$user = auth()->user();
$invoices = $user->invoices();
return view('billing.invoices', [
'invoices' => $invoices,
]);
}
public function downloadInvoice($invoiceId)
{
$user = auth()->user();
return $user->downloadInvoice($invoiceId, [
'vendor' => 'Your Company',
'product' => 'Your Product',
]);
}
}
Creating the Views
Finally, we need to create the views for our billing portal. Create the following views in your resources/views/billing directory:
index.blade.phpinvoices.blade.php
Here's the code for the index.blade.php view:
@extends('layouts.app')
@section('content')
<h1>Billing</h1>
@if (session('success'))
{{ session('success') }}
@endif
<h2>Subscription</h2>
@if ($subscription)
@if ($subscription->active())
<p>Your subscription is active.</p>
<form action="{{ route('billing.cancel') }}" method="POST">
@csrf
<button type="submit">Cancel Subscription</button>
</form>
@else
<p>Your subscription is canceled.</p>
<form action="{{ route('billing.resume') }}" method="POST">
@csrf
<button type="submit">Resume Subscription</button>
</form>
@endif
@else
<p>You don't have an active subscription.</p>
<a href="{{ route('subscribe') }}">Subscribe Now</a>
@endif
<h2>Payment Method</h2>
<form action="{{ route('billing.update-payment-method') }}" method="POST">
@csrf
<label for="payment_method">Payment Method:</label>
<input type="text" name="payment_method" id="payment_method">
<button type="submit">Update Payment Method</button>
</form>
<h2>Invoices</h2>
<a href="{{ route('billing.invoices') }}">View Invoices</a>
@endsection
Here's the code for the invoices.blade.php view:
@extends('layouts.app')
@section('content')
<h1>Invoices</h1>
<ul>
@foreach ($invoices as $invoice)
<li>
<a href="{{ route('billing.download-invoice', $invoice->id) }}">
Invoice #{{ $invoice->id }} - {{ $invoice->date()->toFormattedDateString() }} - {{ $invoice->total() }}
</a>
</li>
@endforeach
</ul>
@endsection
Conclusion
In conclusion, Laravel Cashier simplifies subscription billing in your Laravel applications by providing an expressive and fluent interface to Stripe and Paddle. It handles subscription management, payment processing, invoice generation, and webhooks. By following the steps outlined in this article, you can set up Laravel Cashier, create subscriptions, manage subscriptions, handle webhooks, and build a billing portal for your users. With Laravel Cashier, you can focus on building your application without worrying about the complexities of subscription billing. So, go ahead and leverage Laravel Cashier to create a robust and efficient billing portal for your application.
Lastest News
-
-
Related News
OSSC Celtic SC Vs Cavaliers: Watch Live!
Alex Braham - Nov 9, 2025 40 Views -
Related News
Lamar Jackson Vs. Josh Allen: Who's The MVP? Reddit Weighs In
Alex Braham - Nov 9, 2025 61 Views -
Related News
Puerto Rico's Soccer Journey: The National Team Story
Alex Braham - Nov 14, 2025 53 Views -
Related News
Rockets Scoreboard & NYT Coverage: Game Insights
Alex Braham - Nov 9, 2025 48 Views -
Related News
UCSF Infectious Disease Faculty: Research & Expertise
Alex Braham - Nov 15, 2025 53 Views