How to do payments with stripe checkoutLaravel
Introduction
Today we learn how we made stripe checkout in larvel. I am already install laravel 8 .
Payments gateways are very useful components of any e-commerce store. One of the popular payment gateways is Stripe. it's becoming more popular nowadays.
Stripe's simple definition is :
We bring together everything that’s required to build websites and apps that accept payments and send payouts globally. Stripe’s products power payments for online and in-person retailers, subscription businesses, software platforms and marketplaces, and everything in between. ~ Stripe
To begin this laravel tutorial, I hope you already have fresh laravel repo.
Step 1 Setup Laravel project
First install laravel 8 with following command-:
composer create-project laravel/laravel:^8.0 laravel-stripe
Here 'laravel-stripe' is our project folder in which our project is installed.
After that g0 to folder and run project like this-:
cd laravel-stripe
php artisan serve
Step 2 Stripe Configuration with Laravel
Laravel provide package to integrate stripe in laravel. To install stripe package in laravel run following command.
composer require stripe/stripe-php
It will install required package in laravel for stripe.
After that you can run you laravel project with command
php artisan serve
Step 3 Configure sandbox stripe account
Stripe provide us sandbox account , which is testing environment . It is same as live environment . In sandbox account we test our payment before it goes to live.
For integration stripe we need stripe keys which -:
STRIPE_SECRET_KEY = your-stripe-secret
STRIPE_PUBLISHABLE_KEY = your-stripe-key
First to sign-up/sign-in to stripe with you gmail and add required data in account.
When account is created you will see on right side for test option. Select test option as we are using in test mode .
From this account you will get Publishable-key and Secret-key .
Add this keys in .env file of laravel.
Step 4 Integration stripe in laravel
Create routes for the files , as you see i am already created the routes for it .
First route is for products which is get on cart page.
Second is for chechout , when customer hit checkout button
Third is for succes url , when payment is made successfully . Then it will redirect to that success page.
Last is for if any payment is faild. Then it will redirect to that page.
After that i created ProductController, in which all logic is written . You can create controller with this command-:
php artisan make:controller ProductController
As you can see when we hit checkout button then i will redirect to stripe checkout page after yopu fill STRIPE_SECRET_KEY in .env file.
You can see stripe checkout page like this -:
After fill the correct details when you click pay button , if when payment is made then it will redirect to success page which is design like this you can customize it .
Here is code after payment is successfully made then it will redirect to this .
In case if payment is fail then it will redirect to fail page also you can design it accoding to your need .
Here is code is if payment is failed it will redirect to fail route.
You can also check if paymeny made successfully or fails in stripe account
Conclusion
With this you can integrate stripe checkout with laravel.