Stripe Integration with Laravel
Stripe Integration with Laravel
15 January 2021
Stripe is a payment gateway that offers easy and most efficient ways to process payments through online, leading technology, and plenty of ecommerce tools to make things simple for ecommerce store owners trying to run a business and make money. It has several advantages comparing to other payment gateways:
- Stripe has a cloud-based infrastructure designed for reliability, scalability and security.
- Stripe charges a low fee for successful payments as compared to other payment gateways.
- Stripe offers a highly optimized security-focused program that is designed to allow users to collect credit card information without storing anything on the servers.
- Stripe offers the best payment processing APIs. Which includes a simple interface, great documentation and overall ease-of-use.
- There is no setup cost for registering on Stripe and it enables users to stay on your site when making a payment, instead of being redirected to another site.
Prerequisites
For integration of stripe we will need the following:
- Laravel
- PHP 7.1 or more
- Laravel server to host the application
Create Stripe Account
The first step involves the creation of a stripe account on the website. Once we complete the registration. We will get the test account credentials. Which will consist of the client Id and client secret key. We will then use this client id and client secret key in our application.We can register on stripe through this link: https://dashboard.stripe.com/register
Stripe Configuration With Laravel
Now we need to configure the laravel with stripe. We will add the stripe package to our composer file. The package name is:
composer require stripe/stripe-php
Once the package is installed then we need to update the package.json with the following command:
cartalyst/stripe-laravel: 7.0.*
Setup App Service Provider For Laravel Stripe Integration
Now we need to register the stripe package in the laravel provider. For this we need to update the config/app.php file
‘providers’ => [ Stripe\Laravel\StripeServiceProvider::class], ‘aliases’ => [ ‘Stripe’ => Stripe\Laravel\Facades\Stripe::class]
Set Up Private And Secret Credentials
We need to add the stripe key and secret in our environment file:
STRIPE_KEY = pk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx STRIPE_SECRET = sk_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
We will get this stripe credentials i.e. stripe key and secret from the stripe account that we have created.
To save the credentials we have added in the environment files we need to run the following command:
php artisan config:cache
Create Migrations
We can create a table for stripe customers to save the customer payment details with the timestamp. Command to create the migration is:
php artisan make:migration create_customer_stripe_payment_table
Setup Stripe Payment Controller
We need to create the payment controller to setup the data and configure the methods:
php artisan make:controller StripeController
After creating the controller we build the actual logic for stripe payment in this controller:
In this way we can add the customer data and redirect the user to the official stripe page using the Stripe key and secret.
Route Call
We also need to create the route:
Route::post('/charge', 'StripeController@charge');
Conclusion
This is the overview of how to integrate the stripe with laravel. We can have many other payment gateways to be integrated such as paypal, google pay, square etc. But as compared to the other payment gateways stripe is user friendly and free of cost to be used. It is much more secure than other payment gateways.