Blog

Home / Blog

Laravel stripe integration example

Learn how to easily integrate Stripe payment gateway into your Laravel application with this comprehensive example, complete with step-by-step instructions and code snippets. Streamline your e-commerce platform and start accepting secure online payments today with this straightforward Laravel Stripe integration example.

Stripe payment gateway integration in laravel 9

Stripe payment gateway integration in Laravel 9 allows for secure and seamless online transactions, enabling businesses to accept various payment methods with ease.
By incorporating Stripe in Laravel 9, developers can customize payment processes, manage subscriptions, and enhance the overall user experience for customers.

Laravel is a popular PHP framework that is widely used for building web applications. One of the many features of Laravel is its ease of integration with various payment gateways. Stripe is one such payment gateway that is widely used for processing online payments. In this article, we will walk you through a step-by-step guide on how to integrate Stripe with a Laravel application.

Setting up your Stripe account

The first step in integrating Stripe with Laravel is to create a Stripe account. Go to the Stripe website and sign up for a new account. Once you have signed up, you will be provided with API keys that you will need to integrate with your Laravel application.

Install the Laravel Cashier package

Laravel Cashier is a package that provides a simple and fluent interface for interacting with Stripe. You can install Laravel Cashier using Composer by running the following command in your terminal:

```
composer require laravel/cashier
```

This will download and install the Laravel Cashier package in your Laravel application.

Configure your Stripe API keys

Next, you need to configure your Stripe API keys in your Laravel application. Open the `.env` file in your Laravel project and add your Stripe API keys like this:

```
STRIPE_KEY=your_stripe_key
STRIPE_SECRET=your_stripe_secret
```

Replace `your_stripe_key` and `your_stripe_secret` with the API keys provided to you by Stripe when you signed up.

Create a migration for the user subscription table

Laravel Cashier requires a `subscriptions` table in your database to store subscription information for users. You can create this table by running the following command in your terminal:

```
php artisan make:migration create_subscriptions_table
```

Open the migration file that is generated and define the schema for the `subscriptions` table like this:

```php
Schema::create('subscriptions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('stripe_id')->collation('utf8mb4_bin');
$table->string('stripe_plan');
$table->integer('quantity');
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamps();
});
```

Run the migration to create the `subscriptions` table in your database by running the following command in your terminal:

```
php artisan migrate
```

Set up the routes and controller for handling subscriptions

Next, you need to set up routes and a controller for handling subscriptions in your Laravel application. Create a new controller by running the following command in your terminal:

```
php artisan make:controller SubscriptionController
```

Open the controller file that is generated and define the methods for subscribing and handling subscription cancellations like this:

```php
use IlluminateHttpRequest;
use StripeStripe;
use StripeCustomer;
use IlluminateSupportFacadesAuth;
use LaravelCashierSubscription;

public function subscribe(Request $request)
{
$user = Auth::user();

$user->newSubscription('default', 'monthly')->create($request->stripeToken);

return redirect()->back();
}

public function cancelSubscription(Request $request)
{
$user = Auth::user();

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

return redirect()->back();
}
```

Define the routes for subscribing and cancelling subscriptions in your `routes/web.php` file like this:

```php
Route::post('/subscribe', 'SubscriptionController@subscribe')->name('subscribe');

Route::post('/cancel-subscription', 'SubscriptionController@cancelSubscription')->name('cancelSubscription');
```

Create the views for subscribing and cancelling subscriptions

Finally, you need to create the views for subscribing and cancelling subscriptions in your Laravel application. Create a new blade file for subscribing to a subscription and add a form for collecting payment details like this:

```html


@csrf







```

Create a new blade file for cancelling a subscription and add a form for handling subscription cancellations like this:

```html

@csrf


```

That's it! You have successfully integrated Stripe with your Laravel application. Users can now subscribe to a monthly plan and cancel their subscription using your Laravel application.

In conclusion, integrating Stripe with Laravel is a straightforward process that can be easily accomplished by following the steps outlined in this article. Laravel Cashier simplifies the integration process and provides an efficient way of handling subscriptions and payments in your Laravel application. With this integration, you can offer a seamless payment experience to your users and manage subscriptions effectively.

Payment gateway integration services

Streamline your online payment process with our expert payment gateway integration services. Securely accept payments and increase conversion rates with ease.