Laravel 9 Stripe Payment Gateway Integration Example

Hi Artisan,

In this tutorial, I will show you step by step Laravel 9 stripe payment gateway integration example from scratch. From this tutorial, you will learn how to implement a stripe payment gateway on your Laravel 9 application.

Before using stripe in your application, we need to create a stripe developer account and need to get an API key and secret from there to make a payment system in Laravel. Having got that credentials from the stripe, we will use stripe/stripe-php composer package for the stripe payment gateway in Laravel 9. 

You know that stripe payment gateway is a very popular and secure internet payment gateway system that helps to accept payments worldwide. Stripe provides a really cool development interface to start and we don’t have to pay any charges to learn it provides a free developer account, before starting to code in your app.

There are many payment gateways and stripe is one of them you know. So I am here to show you how to integrate the stripe payment gateway in the Laravel 9 application. 

laravel-9-stripe-payment-gateway-integration-example

 

Step 1: Install Laravel 9

I am going to start it step by step from scratch so, we need to get a fresh Laravel 8 application using below command, So open your terminal OR command prompt and run below command:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Install stripe-php Package

In this step, we need to install stripe-php before using it via the composer package manager, so one your terminal and fire bellow command:

composer require stripe/stripe-php

 

Step 3: Set Stripe API Key and SECRET

Now, we need to set the stripe key and secret. so first you can go on the Stripe website and create a developer stripe account to get the key and secret and add that like below:

laravel-9-stripe-payment-integration

.env

STRIPE_KEY=your_stripe_key
STRIPE_SECRET=your_stripe_secret

 

And update config/services.php file like

config/services.php

'stripe' => [
     'secret' => env('STRIPE_SECRET'),
 ],

 

Step 4: Add Routes

In this step, we need to create a route to process the stripe integration in Laravel. 

routes/web.php

use Illuminate\Support\Facades\Route;
   
use App\Http\Controllers\StripeController;
   
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
   
Route::get('stripe', [StripeController::class, 'stripe']);
Route::post('stripe', [StripeController::class, 'stripePost'])->name('stripe.post');

 

Step 5: Add Controller

In this step, now we have created a new controller as StripeController and written both methods on it like as bellow, So let's create both controllers:

app/Http/Controllers/StripeController.php

namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Session;
use Stripe;
    
class StripeController extends Controller
{
    /**
     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
    public function stripe()
    {
        return view('stripe');
    }
   
    /**
     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
    public function stripePost(Request $request)
    {
        Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
        Stripe\Charge::create ([
                "amount" => 100 * 100,
                "currency" => "usd",
                "source" => $request->stripeToken,
                "description" => "This payment is tested purpose from codecheef.org"
        ]);
   
        Session::flash('success', 'Payment successful!');
           
        return back();
    }
}

 

Step 6: Create Blade File

In this step, we have to create a file like stripe.blade.php. Then add the following code into the stripe.blade.php file:

resources/views/stripe.blade.php

 

All are set to go. You can test it now. Take the testing credentials from below and test them now:

Card No: 4242424242424242
Month: any future month
Year: any future Year
CVV: 123

 

Then you will see the output in your developer accounts like:

laravel-stripe-example

Recommended: Laravel 9 Razorpay Payment Gateway Integration Example

 

Hope it can help you to integrate the stripe payment gateway into the Laravel 9 application.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js