DEV Community

Aleson França
Aleson França

Posted on

Laravel Cashier: Managing Subscriptions And One-Time Charges Easily

Laravel Cashier is a powerful package that simplifies integration with payment providers like Stripe and Paddle, making it easier to implement recurring payments and manage subscriptions. In this post, we will explore the main features of Cashier with practical examples to help you get started.

Installation and Configuration

To begin, install the Cashier package via Composer:
composer require laravel/cashier

After installation, publish the configuration by running:
php artisan vendor:publish --tag="cashier-migrations"

php artisan migrate

Next, add your Stripe credentials to the .env file:
STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret

Then, configure the Billable trait in your user model:

<?php

use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use Billable;
}
Enter fullscreen mode Exit fullscreen mode

What is the Billable Trait?

The Billable trait is provided by Laravel Cashier and adds subscription and payment management capabilities to the model. It implements several useful methods for handling subscription plans, invoices, one-time charges, and payment methods.

Some key features added by the Billable trait include:

  • Creating and managing subscriptions(newSubscription, subscribed, subscription)
  • Cancelling and resuming subscriptions(cancel, resume)
  • Registering and updating payment methods
  • Generating invoices and checking payment status

By adding use Billable to your model, you automatically enable these features without needing to implement them manually.

Creating Subscriptions

To create a subscription for a user, you can use the newSubscription method:

<?php

$user = User::find(1);

$user->newSubscription('default', 'price_id')->create(
    $paymentMethod
);
Enter fullscreen mode Exit fullscreen mode

Where price_id is the identifier of the plan registered in Stripe.

Managing Subscriptions

You can check if a user is subscribed to a specific plan:

<?php

if ($user->subscribed('default')) {
    // The user has an active subscription
}
Enter fullscreen mode Exit fullscreen mode

To cancel subscriptions:

<?php

$user->subscription('default')->cancel();
Enter fullscreen mode Exit fullscreen mode

To resume a canceled subscription:

<?php

$user->subscription('default')->resume();
Enter fullscreen mode Exit fullscreen mode

One-Time Charges

In addition to subscriptions, Cashier allows one-time charges:

<?php
$user->charge(5000, $paymentMethod); // 5000 = $50.00
Enter fullscreen mode Exit fullscreen mode

Conclusion

Laravel Cashier greatly simplifies the integration with payment services, making subscription and one-time charge management much more accessible for Laravel developers. With its intuitive API, you can quickly implement a recurring payment system without worrying about the complex details of payment gateway integration.

If you enjoyed this content, share it and leave a comment! 🚀

Top comments (0)