Hello developers! In this guide, we'll see how to integrate a stripe payment gateway in laravel 11 with step by step guide. Here, we'll learn laravel 11 stripe payment gateway integration. we'll use the stripe/stripe-php composer package in laravel 11.
Stripe is a payment service provider that lets merchants accept credit and debit cards or other payments. Stripe Payments, is best suited for businesses that make most of their sales online.
Also, you can pay via debit card, credit card, and payment links. Stripe supports recurring payments like subscription based modal.
In this step, we'll install the laravel 11 application using the following composer command.
composer create-project laravel/laravel example-app
Next, we'll install the stripe package using the following composer command. The Stripe PHP library provides convenient access to the Stripe API from applications written in the PHP language.
composer require stripe/stripe-php
Now, we'll configure the stripe payment gateway as below in the images and code. If you don't have a Stripe account. create an account on Stripe.
Now, open the .env file and add the below code.
STRIPE_KEY=pk_test_xxxxxx
STRIPE_SECRET=sk_test_xxxxxx
Then, we'll create a StripePaymentController using the following command and define the login of the Stripe payment gateway.
php artisan make:controller StripePaymentController
app/Http/Controllers/StripePaymentController.php
<?php
namespace App\Http\Controllers;
use Stripe;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
class StripePaymentController extends Controller
{
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function index(): View
{
return view('stripe');
}
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function stripe(Request $request): RedirectResponse
{
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
Stripe\Charge::create ([
"amount" => 10 * 100,
"currency" => "usd",
"source" => $request->stripeToken,
"description" => "Stripe Test Payment"
]);
return back()->with('success', 'Payment has been successful');
}
}
Now, we'll define the route to the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StripePaymentController;
Route::controller(StripePaymentController::class)->group(function(){
Route::get('stripe', 'index');
Route::post('stripe', 'stripe')->name('stripe.post');
});
Then, we'll create a stripe.blade.php file for getting payment from the users. Also, we'll add jQuery code to create a card element.
resources/views/stripe.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Integrate Stripe Payment Gateway in Laravel 11 - techsolutionstuff.com</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css">
#card-element{
height: 50px;
padding-top: 16px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 offset-md-2">
<div class="card mt-5">
<h3 class="card-header p-3">Integrate Stripe Payment Gateway in Laravel 11 - techsolutionstuff.com<</h3>
<div class="card-body">
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsession
<form id='checkout-form' method='post' action="{{ route('stripe.post') }}">
@csrf
<strong>Name:</strong>
<input type="input" class="form-control" name="name" placeholder="Enter Name">
<input type='hidden' name='stripeToken' id='stripe-token-id'>
<br>
<div id="card-element" class="form-control" ></div>
<button
id='pay-btn'
class="btn btn-success mt-3"
type="button"
style="margin-top: 20px; width: 100%;padding: 7px;"
onclick="createToken()">PAY $10
</button>
<form>
</div>
</div>
</div>
</div>
</div>
</body>
<script src="https://js.stripe.com/v3/"></script>
<script type="text/javascript">
var stripe = Stripe('{{ env('STRIPE_KEY') }}')
var elements = stripe.elements();
var cardElement = elements.create('card');
cardElement.mount('#card-element');
function createToken() {
document.getElementById("pay-btn").disabled = true;
stripe.createToken(cardElement).then(function(result) {
if(typeof result.error != 'undefined') {
document.getElementById("pay-btn").disabled = false;
alert(result.error.message);
}
/* creating token success */
if(typeof result.token != 'undefined') {
document.getElementById("stripe-token-id").value = result.token.id;
document.getElementById('checkout-form').submit();
}
});
}
</script>
</html>
Now, run the laravel 11 application using the following command.
php artisan serve
Testing card details:
Card Name: Test
Card Number: 4242424242424242
Month: 07
Year: 2027
CVV: 123
References:
You might also like: