Laravel Cashier is a robust package for managing subscription billing with Stripe. It simplifies the process of handling subscriptions, invoicing, and payment management in Laravel applications. Cashier provides an expressive and fluent interface for working with Stripe’s billing services, making it easier for developers to integrate subscription features into their applications.
Key Features of Laravel Cashier:
- Subscription Management: Easily create and manage user subscriptions.
- Plan Management: Integrate with Stripe plans to manage different subscription tiers.
- Handling Coupons: Support for applying and managing coupons.
- Invoice Management: Automatically generate and manage invoices for subscribers.
- Webhook Support: Handle webhooks for events like subscription updates, cancellations, etc.
- Payment Methods: Easily manage payment methods and user cards.
Installation
To get started with Laravel Cashier, follow these steps:
- Install Laravel Cashier: Use Composer to install Cashier:
1composer require laravel/cashier - Configure Stripe: In your
.env
file, add your Stripe API keys:
12STRIPE_KEY=your-stripe-keySTRIPE_SECRET=your-stripe-secret - Publish Cashier Configuration: Publish the Cashier configuration file:
1php artisan vendor:publish --provider="Laravel\Cashier\CashierServiceProvider" - Run Migrations: Cashier comes with migration files that need to be run to set up the necessary database tables:
1php artisan migrate - Add Billable Trait: In your
User
model (app/Models/User.php
), add theBillable
trait:
123456use Laravel\Cashier\Billable;class User extends Authenticatable{use Billable;}
Subscription Management
1. Creating Subscriptions:
To create a subscription for a user, you typically do this after collecting their payment information via Stripe Elements or Checkout.
1 2 |
$user = User::find(1); $user->newSubscription('main', 'plan-id')->create($request->paymentMethod); |
In this example, 'main'
is the subscription name, and 'plan-id'
is the ID of the plan you’ve defined in Stripe.
2. Updating Subscriptions:
You can update a user’s subscription plan:
1 |
$user->subscription('main')->swap('new-plan-id'); |
3. Cancelling Subscriptions:
You can cancel a subscription at any time:
1 |
$user->subscription('main')->cancel(); |
To cancel at the end of the billing period:
1 |
$user->subscription('main')->cancelAtEndOfPeriod(); |
Managing Payment Methods
1. Adding Payment Methods:
When creating a subscription, you can collect payment methods directly through Stripe’s elements.
1 |
$user->updateDefaultPaymentMethod($request->paymentMethod); |
2. Retrieving Payment Methods:
You can retrieve the payment methods associated with a user:
1 |
$paymentMethods = $user->paymentMethods(); |
Handling Webhooks
To handle Stripe webhooks, you can use Cashier’s built-in commands. Set up a route for your webhook and listen for events like subscription updates, cancellations, etc.
1 2 3 |
Route::post('/stripe/webhook', function () { \Laravel\Cashier\Http\Controllers\WebhookController::class . '@handleWebhook'; }); |
Generating Invoices
1. Viewing Invoices:
Cashier allows you to retrieve a user’s invoices easily:
1 |
$invoices = $user->invoices(); |
2. Paying Invoices:
You can allow users to pay outstanding invoices:
1 |
$user->charge($invoice->amount_due); |
Applying Coupons
You can apply coupons when creating a subscription:
1 |
$user->newSubscription('main', 'plan-id')->withCoupon('coupon-id')->create($request->paymentMethod); |
Conclusion
Laravel Cashier is a powerful package that makes it easy to integrate subscription billing with Stripe into your Laravel applications. It abstracts many of the complexities of handling payments and subscriptions, allowing developers to focus on building great user experiences.
Additional Considerations
- Testing: Make sure to test your integration using Stripe’s test mode and test cards.
- Webhook Configuration: Ensure you set up and configure your webhooks correctly to handle subscription events.