Laravel Cashier – Subscription billing with Stripe

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:

  1. Subscription Management: Easily create and manage user subscriptions.
  2. Plan Management: Integrate with Stripe plans to manage different subscription tiers.
  3. Handling Coupons: Support for applying and managing coupons.
  4. Invoice Management: Automatically generate and manage invoices for subscribers.
  5. Webhook Support: Handle webhooks for events like subscription updates, cancellations, etc.
  6. Payment Methods: Easily manage payment methods and user cards.

Installation

To get started with Laravel Cashier, follow these steps:

  1. Install Laravel Cashier: Use Composer to install Cashier:
  2. Configure Stripe: In your .env file, add your Stripe API keys:
  3. Publish Cashier Configuration: Publish the Cashier configuration file:
  4. Run Migrations: Cashier comes with migration files that need to be run to set up the necessary database tables:
  5. Add Billable Trait: In your User model (app/Models/User.php), add the Billable trait:

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.

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:

3. Cancelling Subscriptions:

You can cancel a subscription at any time:

To cancel at the end of the billing period:

Managing Payment Methods

1. Adding Payment Methods:

When creating a subscription, you can collect payment methods directly through Stripe’s elements.

2. Retrieving Payment Methods:

You can retrieve the payment methods associated with a user:

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.

Generating Invoices

1. Viewing Invoices:

Cashier allows you to retrieve a user’s invoices easily:

2. Paying Invoices:

You can allow users to pay outstanding invoices:

Applying Coupons

You can apply coupons when creating a subscription:

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top