Laravel 8 Stripe Payment Gateway Integration Example

Hi Artisan,

In this tutorial, I will show you step by step Laravel 8 stripe integration example from scratch. From this tutorial, we will learn how to implement stripe payment gateway integration with your Laravel 8 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 8. I write step-by-step integration for the stripe payment gateway so that you can understand it better.

So let's start our stripe payment gateway integration in laravel 8 tutorial:

 

Step 1: Install Laravel 8

I am going to start it from step by step from scratch so, we need to get a fresh Laravel 8 application using bellow command, So open your terminal OR command prompt and run bellow 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 developer stripe account to get the key and secret and add that like below:

.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 it now:

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

 

Recommended: Laravel 8 Razorpay Payment Gateway Integration Example

 

Hope it can help you to integrate the stripe payment gateway in the Laravel 8 application.

 

#laravel #laravel-8x