Stripe Payment Gateway Integration In Laravel 12 - Step-by-Step Guide

Learn how to integrate Stripe Payment Gateway in Laravel with this step-by-step guide. Includes installation, API keys, payment form, and testing with Stripe's test mode.

Muhammad Ishaq
Muhammad Ishaq
10 Aug 2025
4-5 min read
Stripe Payment Gateway Integration In Laravel 12 - Step-by-Step Guide

If you’re developing an application in Laravel 12 that requires online payments, integrating Stripe is one of the most secure and developer-friendly options. Stripe allows you to accept credit cards, debit cards, and multiple payment methods globally.

In this guide, you’ll learn how to integrate Stripe with Laravel 12 from scratch - including installation, configuration, payment processing, and testing.


Why Use Stripe in Laravel 12?

Stripe is widely used for its:

  • Developer-friendly API

  • Support for multiple currencies and payment methods

  • Advanced security (PCI compliance)

  • Simple test environment for developers

Whether you’re building an e-commerce platform, SaaS, or donation system, Stripe can handle it.


Step 1: Install Stripe PHP SDK in Laravel 12

Open your terminal in your Laravel 12 project folder and run:

composer require stripe/stripe-php

This installs the official Stripe PHP SDK, which lets Laravel communicate with Stripe’s API.


Step 2: Create a Stripe Account and Get API Keys

  1. Go to Stripe’s website and sign up (free).

  2. Navigate to Developers → API Keys in the dashboard.

  3. Copy your:

    Publishable Key (pk_test_xxxxxx)

    Secret Key (sk_test_xxxxxx)

  4. Add them to your Laravel .env file:

STRIPE_KEY=pk_test_xxxxxx
STRIPE_SECRET=sk_test_xxxxxx

Step 3: Configure Stripe in Laravel 12

In Laravel 12, we’ll add Stripe configuration to config/services.php.

Open config/services.php and add:


'stripe' => [

    'secret' => env('STRIPE_SECRET'),

],

This way, you don’t expose sensitive API keys in your code.


Step 4: Create Payment Routes

In routes/web.php, add:

use App\Http\Controllers\StripeController;
use Illuminate\Support\Facades\Route;

Route::get('/stripe', [StripeController::class, 'index'])->name('stripe.index');
Route::post('/stripe', [StripeController::class, 'processPayment'])->name('stripe.process');

Step 5: Create StripeController

Run:

php artisan make:controller StripeController

Open app/Http/Controllers/StripeController.php and add:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\Charge;
use Illuminate\Support\Facades\Session;

class StripeController extends Controller
{

    // Show the payment form

    public function index()
    {
        return view('stripe');
    }


    // Handle payment processing

    public function processPayment(Request $request)
    {
        $request->validate([
            'stripeToken' => 'required',
        ]);

        Stripe::setApiKey(config('services.stripe.secret'));

        Charge::create([
            "amount" => 100 * 100, // 100 INR in cents
            "currency" => "INR",
            "source" => $request->stripeToken,
            "description" => "Laravel 12 Stripe Payment Example",
        ]);

        Session::flash('success', 'Payment successful!');

        return back();
    }

}

Step 6: Create the Payment Form (Blade File)

Create a file:
resources/views/stripe.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 12 Stripe Payment Gateway Integration</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>      
</head>

<body>

<div class="container mt-5">
    <h3 class="text-center">Stripe Payment Gateway Integration in Laravel 12</h3>

    @if (Session::has('success'))
        <div class="alert alert-success text-center">
            {{ Session::get('success') }}
        </div>
    @endif

    <form role="form" action="{{ route('stripe.process') }}" method="post" 
          class="require-validation" 
          data-stripe-publishable-key="{{ env('STRIPE_KEY') }}" 
          id="payment-form">

        @csrf
 
       <div class="form-group">
            <label>Name on Card</label>
            <input type="text" class="form-control" required>
        </div>

        <div class="form-group">
            <label>Card Number</label>
            <input type="text" class="form-control card-number" autocomplete="off" required>
        </div>

        <div class="form-group">
            <label>CVC</label>
            <input type="text" class="form-control card-cvc" placeholder="ex. 311" required>
        </div>
 
        <div class="row">
            <div class="col">
                <label>Expiration Month</label>
                <input type="text" class="form-control card-expiry-month" placeholder="MM" required>
            </div>

            <div class="col">
                <label>Expiration Year</label>
                <input type="text" class="form-control card-expiry-year" placeholder="YYYY" required>
            </div>
        </div>
 
        <button class="btn btn-primary btn-block mt-3" type="submit">Pay Now</button>

    </form>
</div>

<script src="https://js.stripe.com/v2/"></script>
<script>

$(function() {

    var $form = $(".require-validation");

    $form.on('submit', function(e) {

        if (!$form.data('cc-on-file')) {

            e.preventDefault();

            Stripe.setPublishableKey($form.data('stripe-publishable-key'));

            Stripe.createToken({

                number: $('.card-number').val(),

                cvc: $('.card-cvc').val(),

                exp_month: $('.card-expiry-month').val(),

                exp_year: $('.card-expiry-year').val()

            }, stripeResponseHandler);

        }

    });

 

    function stripeResponseHandler(status, response) {

        if (response.error) {

            alert(response.error.message);

        } else {

            var token = response.id;

            $form.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");

            $form.get(0).submit();

        }

    }

});

</script>

</body>

</html>

Step 7: Test the Payment

Stripe provides test card details for development.
Use the following:

  • Card Number: 4242 4242 4242 4242

  • Expiry Date: Any future date (e.g., 04 / 2026)

  • CVC: Any 3 digits (e.g., 123)

  • When you submit the form, the payment will be processed in test mode and appear in your Stripe dashboard.


Conclusion

You’ve successfully integrated Stripe Payment Gateway with Laravel 12. This example uses a basic one-time charge, but Stripe also supports:

  • Subscriptions

  • Webhooks for real-time payment updates

  • Refunds and partial payments

  • Multiple payment methods

With Laravel 12 and Stripe, you can build a secure, scalable, and global-ready payment system.