Introduction
Stripe is one of the most popular payment gateways for handling secure online transactions. In this blog post, we will walk through a step-by-step guide to integrating Stripe Checkout into a Laravel application. By the end of this post, you'll have a working Stripe card checkout form that can be used to collect payments from your users.
Why Use Stripe Checkout in Laravel?
Stripe provides a developer-friendly API, secure payment handling, and a beautiful UI for collecting card details. Laravel, with its elegant syntax and support for service providers, makes it easy to plug in Stripe and start accepting payments with minimal effort.
Steps
01: Install Stripe PHP SDK
First, install the official Stripe PHP package using Composer.
composer require stripe/stripe-php
02: Add Stripe Keys in .env
Add your Stripe publishable and secret keys in your Laravel .env
file. Replace the placeholders with your actual keys.
STRIPE_KEY=pk_test_XXXXXXXXXXXXXXXX
STRIPE_SECRET=sk_test_XXXXXXXXXXXXXXXX
03: Configure services.php
Add Stripe configuration to config/services.php
:
'stripe' => [
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
04: Create CheckoutController
Let's create a controller to handle the payment view and processing:
php artisan make:controller CheckoutController
In CheckoutController.php
:
use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\Charge;
public function show()
{
return view('checkout');
}
public function process(Request $request)
{
Stripe::setApiKey(config('services.stripe.secret'));
Charge::create([
'amount' => 2000, // $20 in cents
'currency' => 'usd',
'description' => 'Test Payment',
'source' => $request->stripeToken,
]);
return back()->with('success', 'Payment successful!');
}
05: Create Routes
use App\Http\Controllers\CheckoutController;
Route::get('/checkout', [CheckoutController::class, 'show']);
Route::post('/checkout', [CheckoutController::class, 'process']);
06: Build Stripe Checkout Form
Create a Blade view at resources/views/checkout.blade.php
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Stripe Checkout</title>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
@if(session('success'))
<p style="color: green">{{ session('success') }}</p>
@endif
<form action="/checkout" method="POST" id="payment-form">
@csrf
<input type="text" name="name" placeholder="Name" required><br><br>
<div id="card-element"></div><br>
<button type="submit">Pay $20</button>
</form>
<script>
const stripe = Stripe("{{ config('services.stripe.key') }}");
const elements = stripe.elements();
const card = elements.create('card');
card.mount('#card-element');
const form = document.getElementById('payment-form');
form.addEventListener('submit', async (e) => {
e.preventDefault();
const {token, error} = await stripe.createToken(card);
if (error) {
alert(error.message);
} else {
const hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
form.submit();
}
});
</script>
</body>
</html>
07: Test Payment
Use a test card like 4242 4242 4242 4242
with any future expiry date, CVC, and ZIP to test the checkout.
Conclusion:
Integrating Stripe with Laravel is straightforward using the official PHP SDK. With just a few lines of code, you can securely accept payments directly on your site. Stripe’s robust APIs and Laravel’s flexibility make them a powerful combo for e-commerce or SaaS applications.