Skip to main content

How to Integrate Stripe Payment Gateway in Laravel

How to Integrate Stripe Payment Gateway in Laravel

Stripe is one of the most popular and secure payment gateways for handling online payments. Laravel makes Stripe integration easy using its clean syntax and powerful ecosystem. In this tutorial, you’ll learn how to integrate Stripe payments in a Laravel application step by step.

Prerequisites

  • A Laravel application
  • A Stripe account
  • Stripe API keys (Test mode)

Step 1: Install Stripe PHP Library

Install the official Stripe PHP SDK using Composer:

composer require stripe/stripe-php

Step 2: Configure Stripe API Keys

Add your Stripe keys to the .env file:

STRIPE_KEY=pk_test_your_public_key
STRIPE_SECRET=sk_test_your_secret_key
Tip: Always use test keys during development to avoid real charges.

Step 3: Update Services Configuration

Open config/services.php and add the Stripe configuration:

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

Step 4: Create Payment Route

Define routes for displaying the payment form and processing the payment:

use App\Http\Controllers\StripeController;

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

Step 5: Create Payment Controller

Create a controller to handle Stripe payments:

use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\Charge;

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

public function payment(Request $request)
{
    Stripe::setApiKey(config('services.stripe.secret'));

    Charge::create([
        'amount' => $request->amount * 100,
        'currency' => 'usd',
        'source' => $request->stripeToken,
        'description' => 'Stripe Payment'
    ]);

    return back()->with('success', 'Payment successful!');
}

Step 6: Create Stripe Payment Form

Create a Blade file to collect payment details:

<form action="{{ route('stripe.payment') }}" method="POST">
    @csrf
    <input type="number" name="amount" placeholder="Enter Amount" required>

    <script
        src="https://checkout.stripe.com/checkout.js"
        class="stripe-button"
        data-key="{{ config('services.stripe.key') }}"
        data-name="Laravel Stripe Payment"
        data-description="Test Payment"
        data-currency="usd">
    </script>
</form>

Step 7: Handle Success Message

Display success messages after payment completion:

@if(session('success'))
    <div class="alert alert-success">
        {{ session('success') }}
    </div>
@endif

Common Issues

  • Invalid Stripe API keys
  • Incorrect currency or amount
  • Missing CSRF token
Pro Tip: For modern applications, consider using Stripe Payment Intents instead of Charges.

Conclusion

Integrating Stripe payment gateway in Laravel is simple and efficient. By following this guide, you can securely accept online payments and scale your application with confidence using Stripe’s powerful APIs.

Comments

Popular posts from this blog

How to Use L5-Swagger for API Documentation in Laravel

Integrating Swagger in Laravel: Annotations, JSON, and YAML What is Swagger? Swagger (OpenAPI) is a powerful tool for generating interactive API documentation. It helps developers understand and test your API easily. ✅ Step-by-Step Guide to Setup L5-Swagger 1. Install L5-Swagger Package composer require "darkaonline/l5-swagger" 2. Publish Config & View Files This command publishes the config file to config/l5-swagger.php : php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider" 3. Configure Swagger (Optional) Edit the file config/l5-swagger.php to update: API Title Base Path Directories to scan for annotations 4. Add Swagger Annotations Add these before your controller class: /** * @OA\Info( * version="1.0.0", * title="Your API Title", * description=...

How to Send Emails in Node.js using Nodemailer and Ethereal

How to Send Email in Node.js using Nodemailer Email functionality is essential in modern web applications. Whether you're sending confirmation emails, password resets, or notifications, Node.js with Nodemailer makes this simple. In this blog, we'll walk through setting up email sending using Node.js , Express , and Ethereal Email for testing. ๐Ÿงพ Prerequisites Node.js installed Basic knowledge of Express.js Internet connection ๐Ÿ“ Project Structure project-folder/ ├── index.js ├── .env ├── package.json └── app/ └── controller/ └── emailSendController.js ๐Ÿ“ฆ Step 1: Install Dependencies npm init -y npm install express nodemailer dotenv npm install --save-dev nodemon ๐Ÿ” Configure nodemon (Optional but Recommended) Update your package.json with a custom start script: "scripts": { "start": "nodemon index.js" } ๐Ÿ” Step 2: Create a .env File Create a .env file a...

How to Integrate RevenueCat Webhook in Laravel

How to Integrate RevenueCat Webhook in Laravel If you're using RevenueCat to manage in-app subscriptions for your mobile app (iOS, Android, etc.), it’s important to connect it with your backend. Laravel makes it easy to handle incoming webhooks for real-time events like purchases, renewals, cancellations, and more. ๐Ÿ“Œ What is RevenueCat? RevenueCat is a powerful subscription management platform for mobile apps. It simplifies in-app purchases across iOS, Android, and Stripe. With webhooks, it notifies your server in real-time when important events happen, such as a new purchase or a cancellation. ๐Ÿ”— Webhook Events from RevenueCat Common webhook events include: INITIAL_PURCHASE RENEWAL CANCELLATION BILLING_ISSUE EXPIRATION SUBSCRIBER_ALIAS ๐Ÿ”— For a full list of event payloads, visit: RevenueCat Sample Events ✅ Step-by-Step Guide to Integration 1. Create the Route // routes/ap...