How to Integrate Stripe Payment Gateway in Laravel 10
In this tutorial, we will see how to integrate stripe payment gateway in Laravel 10, I assume that you already have a stripe account and you have created a new fresh Laravel application.
Create the controller
First, we create a new controller 'PaymentController' Inside we have two functions that handle payment and redirect the user to a success page.
NB: do not forget to get your secret key from the stripe dashboard.
<?php
namespace App\Http\Controllers;
use Stripe\Stripe;
use Illuminate\Http\Request;
use Stripe\Checkout\Session;
class PaymentController extends Controller
{
//
public function pay(Request $request)
{
Stripe::setApiKey('YOUR API SECRET KEY');
$checkout_session = Session::create([
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'product_data' => [
'name' => 'T-shirt',
],
'unit_amount' => $request->price * 100,
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => route('pay.success')
]);
return redirect($checkout_session->url);
}
public function success()
{
return view('success');
}
}
Add routes
Next, we add routes in 'web.php'.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PaymentController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function() {
return view('welcome');
});
Route::post('pay', [PaymentController::class, 'pay'])->name('pay.order');
Route::get('success', [PaymentController::class, 'success'])->name('pay.success');
Update the welcome page
Next, let's update the welcome page inside we add a form that has only an input that takes the amount to pay and a submit button.
After the form is submitted, the user is redirected to the stripe payment form.
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<title>Laravel Stripe Payment</title>
</head>
<body>
<div class="container">
<div class="row my-4">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="my-3">
<form action="{{route('pay.order')}}" method="post">
@csrf
<input type="hidden" name="price" value="300">
<button type="submit" class="btn btn-primary">
Pay now
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
-->
</body>
</html>
Create the success page
Next, let's create a new file inside views 'success.blade.php' here we display a success message to the user after payment.
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>Laravel Shopping Cart</title>
</head>
<body>
<div class="container">
<div class="row my-5">
<div class="col-md-6 mx-auto">
<div class="card">
<div class="card-body">
<h1>Thanks for your order!</h1>
<p>
We appreciate your business!
If you have any questions, please conact us.
</p>
</div>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript; choose one of the two! -->
<!-- Option 1: Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<!-- Option 2: Separate Popper and Bootstrap JS -->
<!--
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
-->
</body>
</html>