Unlock Seamless Stripe Magic In Laravel 10

Abhishek Badar
Nerd For Tech
2 min readApr 4, 2024

--

Introduction

Integrating Stripe into your Laravel 10 project for handling payments and subscriptions is made straightforward with Laravel Cashier. This guide will walk you through the process from setup to implementation, enabling you to add secure online payment functionalities to your application.

Prerequisites

Ensure you have Laravel 10 installed, along with Composer. You’ll also need a Stripe account with API keys ready.

Installing Laravel Cashier

Start by requiring Cashier through Composer:

composer require laravel/cashier

Next, publish the Cashier migration:

php artisan vendor:publish --tag="cashier-migrations"

Run the migration to add Cashier’s tables to your database:

php artisan migrate

Configure your .env file with your Stripe keys:

STRIPE_KEY=your_stripe_key
STRIPE_SECRET=your_stripe_secret

Setting Up a Stripe Payment Model

In your User model, include the Billable trait provided by Cashier:


class User extends Authenticatable
{
use Notifiable, Billable;
}

Creating Subscriptions

To create a subscription, use the newSubscription method on a billable model instance. Here's how you can create a subscription named premium:

$user = Auth::user();
$user->newSubscription('premium', 'price_stripe_plan_id')->create($paymentMethod);

Replace 'price_stripe_plan_id' with your actual plan ID from Stripe.

Handling Stripe Webhooks

To handle webhooks for events like subscription renewals, first set up a route:

Route::post(
'/stripe/webhook',
'\Laravel\Cashier\Http\Controllers\WebhookController@handleWebhook'
);

Ensure you configure the webhook URL in your Stripe dashboard to point to this route and secure it accordingly.

Conclusion

With Laravel Cashier, integrating Stripe into your Laravel application is efficient and streamlined. By following the steps above, you’ve laid the foundation for handling subscriptions and payments in your application. Explore the Laravel Cashier and Stripe documentation for more advanced features and customization options.

--

--

Abhishek Badar
Nerd For Tech

Welcome to NomadNotes, where curiosity meets insightful storytelling.