Implementing Payment System in your Laravel 11 App using Stripe
Stripe is a payment processing platform to help applications securely accept and manage online payments. Its developer-friendly API makes it a top choice in websites. In this Laravel 11 Stripe integration tutorial, we will use this platform to aid our payments in web applications.
Table Of Content
1 Prerequisites
1.) PHP version of >= 8.2
2.) Composer
3.) Mysql
2 Introduction
This guide is an ideal starting point to understand how to integrate Stripe payment gateway in Laravel 11 and how you can integrate Stripe into an existing web application.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('payments', function (Blueprint $table) {
$table->id();
$table->string('charge_id');
$table->string('transaction_id');
$table->string('amount');
$table->string('card_id');
$table->string('card_last_four');
$table->string('card_exp_month');
$table->string('card_exp_year');
$table->string('postal_code');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('payments');
}
};
Run the migration:
php artisan migrate
Create the Payment model in app/Models/Payment.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Payment extends Model
{
protected $fillable = [
'charge_id',
'transaction_id',
'amount',
'card_id',
'card_last_four',
'card_exp_month',
'card_exp_year',
'postal_code',
];
}
7 Create Controller ( StripeController )
Create StripeController:
php artisan make:controller StripeController
In your controller, you can use the index() and createCharge() methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Payment;
use Stripe;
class StripeController extends Controller
{
public function index()
{
return view('checkout');
}
public function createCharge(Request $request)
{
Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$charge= Stripe\Charge::create ([
"amount" => $request->amount * 100,
"currency" => "cad",
"source" => $request->stripeToken,
"description" => "Get Sample Code - Stripe Test Card Payment"
]);
$payment=new Payment;
$payment->charge_id=$charge->id;
$payment->transaction_id=$charge->balance_transaction;
$payment->amount=number_format(($charge->amount)/100,2);
$payment->card_id=$charge->source->id;
$payment->card_last_four=$charge->source->last4;
$payment->card_exp_month=$charge->source->exp_month;
$payment->card_exp_year=$charge->source->exp_year;
$payment->postal_code=$charge->source->address_zip;
$payment->save();
return redirect('stripe')->with('success', 'Card Payment Successful!');
}
}
?>
8 Define a Route
Add routes in routes/web.php:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\StripeController;
Route::get('/', function () {
return view('welcome');
});
Route::get('stripe', [StripeController::class, 'index']);
Route::post('stripe/create-charge', [StripeController::class, 'createCharge'])->name('stripe.create-charge');
9 Create Blade File
Create a Blade view checkout.blade.php(resources/views/checkout.blade.php) to show Payment Form: