Mastering Subscription Management with Laravel Cashier: A Comprehensive Guide

Bilal Awan
3 min readJan 17, 2024

--

Introduction:

In the fast-evolving landscape of web development, building robust and scalable subscription-based applications requires a reliable and efficient payment processing system. Laravel Cashier, an elegant billing system integrated into the Laravel framework, has emerged as a go-to solution for developers looking to streamline subscription management.

What is Laravel Cashier?

Laravel Cashier is an extension of the Laravel framework that simplifies the implementation of subscription billing services. Developed by Taylor Otwell, the creator of Laravel, Cashier provides a clean and expressive syntax for handling subscription-related tasks, allowing developers to focus on building their application’s unique features rather than dealing with complex billing logic.

Key Features of Laravel Cashier:

Subscription Management:
Laravel Cashier offers a straightforward API for handling subscription creation, modification, and cancellation.
Developers can define multiple subscription plans with different billing intervals and features.

Payment Gateway Integration:
Cashier seamlessly integrates with popular payment gateways such as Stripe and Braintree, providing flexibility in choosing the right service for your application.

Proration and Coupons:
Cashier automates proration, adjusting subscription charges when users upgrade or downgrade their plans mid-cycle.
The system supports coupon codes for discounts and promotional offers.

Invoice Handling:
Automatic generation and sending of invoices to users simplify the billing process. The cashier handles failed payments, retries, and notifies users of any issues.

Multi-Currency Support:
With support for multiple currencies, developers can create applications for a global audience.

Getting Started with Laravel Cashier

Installation:

To begin using Laravel Cashier, first install the package through Composer:

composer require laravel/cashier

After installation, configure your chosen payment gateway (e.g., Stripe or Braintree) credentials in your Laravel application.

Setting Up Models:

Cashier requires specific database columns to be present in your User model. Run the following Artisan command to generate the necessary migrations:

php artisan cashier:install

Defining Subscription Plans:

Define your subscription plans using the plans method in your model. This method should return an array of plans with their respective details:

public static function plans()
{
return [
'monthly' => [
'price' => 9.99,
'options' => [
'limit' => 10,
],
],
'yearly' => [
'price' => 99.99,
'options' => [
'limit' => 100,
],
],
];
}

Managing Subscriptions in Laravel Cashier

Subscribing Users:

To subscribe a user to a plan, simply call the newSubscription method on the user instance:

$user->newSubscription('monthly')->create($paymentMethod);

Canceling Subscriptions:

Canceling a subscription is as simple as calling the cancel method:

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

Proration and Upgrades/Downgrades:

Cashier automatically handles proration when users upgrade or downgrade their subscription plans. Users are billed only for the remaining value of their current plan.

$user->subscription('monthly')->swap('yearly');

Handling Webhooks:

Laravel Cashier relies on webhooks to listen for events from the payment gateway (e.g., subscription renewals or cancellations). Configure your gateway’s webhook URL to handle these events.

Conclusion:

Laravel Cashier empowers developers to build subscription-based applications with ease, providing a clean and elegant API for managing billing-related tasks. By abstracting away the complexities of payment processing, Cashier allows developers to focus on creating exceptional user experiences and unique features for their applications. Whether you’re building a SaaS platform or a content subscription service, Laravel Cashier is a valuable tool in your toolkit for creating seamless and scalable subscription management systems.

For more in-depth information and detailed documentation, refer to the official Laravel Cashier documentation.

--

--

Bilal Awan

I have a strong passion for turning ideas into reality through code.