Stripe Payment Gateway Integration In Laravel 12 Step By Step Guide With Example
If you're building a Laravel application and need to accept payments, Stripe is one of the best and most developer-friendly payment gateways available. In this blog, we will walk you through a complete integration of Stripe Payments in Laravel, using Bootstrap 5 for frontend styling, and including real customer billing details with card element UI provided by Stripe.js.
Before Moving must ensure that you have stripe account, if you don't have please create from here , https://dashboard.stripe.com/register?redirect=%2Ftest%2Fpayments
When you click on the above URL you have something like the below image
After signup you can see your dashboard like the below, just switch to the sandbox mode to get your stripe secret and publishable key
Table of Contents
- Introduction to Stripe Payments in Laravel
- Prerequisites
- Stripe Setup
- Step 1: Create the Payment Controller
- Step 2: Create the Route for Checkout
- Step 3: Blade Template for Stripe Payment
- Step 4: Adding Stripe Elements and JavaScript Logic
- Step 5: Submitting and Handling the Payment
- Bonus: Tips for Going Live
- Conclusion
Understanding Stripe's Requirements for Indian Businesses
Before integrating Stripe, it's crucial to understand the specific requirements for Indian businesses:
- Business Registration: Only registered Indian businesses (sole proprietorships, LLPs, or companies) can accept international payments. Individuals are not permitted.
- Import Export Code (IEC): To accept international payments, you must obtain an IEC from the Director General of Foreign Trade (DGFT).Stripe Support+3Stripe Support+3Stripe Support+3
- Purpose Code: You'll need to provide a Transaction Purpose Code that aligns with your business activities.Stripe Support+4Stripe Support+4Stripe Support+4
- Currency Restrictions: Domestic transactions must be in INR. For international transactions, ensure that the customer's card is issued outside India and that the billing address is located outside India.Stripe Support
For more detailed information, refer to Stripe's documentation on accepting international payments from Stripe accounts in India.
Introduction to Stripe Payments in Laravel
Stripe allows you to securely collect and process credit card information. Laravel makes it easy to integrate Stripe via HTTP requests. With just a few steps, you can create a payment intent, securely collect card details, and handle the payment status on the frontend.
Prerequisites
Before we begin, ensure you have the following:
- Laravel 8 or later installed
- Composer
- Stripe account (test keys)
- Basic knowledge of Laravel controllers, views, and routes
- Bootstrap 5 CDN
Stripe Setup
First, sign up at https://stripe.com. From your dashboard:
- Get your Publishable Key and Secret Key.
- Add the keys to your
.env
file:
Setting Up the Laravel Backend
We'll start by creating a controller to handle the payment process.
1. Create the Controller
Run the following Artisan command to create a new controller:
2. Implement the Controller Methods
In app/Http/Controllers/StripePaymentController.php
:
Creating the Payment Intent
The createPaymentIntent
method in the controller uses Laravel's HTTP client to send a POST request to Stripe's API, creating a Payment Intent with the specified amount and currency. The automatic_payment_methods[enabled]
parameter allows Stripe to handle the payment method selection automatically.
Designing the Frontend with Bootstrap 5
We'll create a simple payment form using Bootstrap 5.
In resources/views/stripe/checkout.blade.php
:
Implementing Stripe Elements and JavaScript Logic
We'll now add the JavaScript code to handle the payment process using Stripe Elements.
Add the routes in your routes/web.php
Handling Payment Confirmation and Errors
The JavaScript code handles the creation of the Payment Method and confirms the Payment Intent. It also provides real-time feedback to the user by displaying error messages if any issues occur during the payment process.
Testing the Integration
To test the integration:
- Ensure your
.env
file has the correct Stripe test keys. - Start your Laravel development server:
- Navigate to
http://localhost:8000/stripe/create-payment
to access the payment form.
when you visit the above URL you can see the image like the below one
- Use Stripe's test card numbers to simulate payments. For example:
- Card Number:
4242 4242 4242 4242 for USA
- Card Number:
4000 0035 6000 0008 for INDIA
- Expiry Date: Any future date
- CVC: Any 3-digit numberWikipedia+6Stripe Support+6Stripe Support+6
- ZIP: Any 5-digit number (for indian not required)
For more test card numbers and scenarios, refer to Stripe's testing documentation.
Download the project from github directly https://github.com/codehunger-team/how-to-integrate-stripe-payment-gateway-in-laravel-12.git
Conclusion
Integrating Stripe into your Laravel application without external packages is straightforward and allows for greater control over the payment process. By adhering to Indian regulations and using Stripe's APIs directly, you can create a secure and compliant payment system for your business.
Remember to switch your Stripe keys to live mode when you're ready to accept real payments, and ensure that all necessary business verifications and compliance requirements are met.