Integrate Payment Gateway - RazorPay with Laravel

Integrate Payment Gateway - RazorPay with Laravel

A guide on integrating RazorPay in Laravel Application

Aanchal Goyal's photo
Aanchal Goyal
·Nov 18, 2022·

3 min read

Play this article

Table of contents

Introduction

In this tutorial, you will learn how to integrate RazorPay in your Laravel 8 application. Read on to know the essential steps for the integration.

Steps to Integrate RazorPay with Laravel

Let’s get started. Follow the steps below, and you are good to go!

Step 1 - Install the Laravel 8 Application

Go to the directory you want to install your project in the terminal and run the below command to install new Laravel project

composer create-project laravel/laravel RazorpayIntegrationLaravel

Step 2 - Connect Database to an Application

In this step, open your project folder in vs. code and open .env and add your database name, user, and password like this

Screenshot 2022-04-13 at 10.57.54 PM.png

Step 3 - Add RazorPay Credentials

If you already have Razorpay account, login into the Razorpay dashboard or create a new Razorpay account. Then go to settings from the left sidebar, and you will see the API KEYS tab.

Open that tab and generate a new key. Copy those keys, and paste them into your .env

Screenshot 2022-04-14 at 5.32.25 PM.jpg

Screenshot 2022-04-14 at 5.49.33 PM.png

Step 4 - Install the Composer Package of RazorPay

Now, could you install the composer package of Razorpay? To install that, get the root directory of your project in the terminal and run the below command.

composer requires razorpay/razorpay

Step 5 - Create Route

Open web.php and create a new route.

Screenshot 2022-04-13 at 10.59.12 PM.png

Step 6 - Create Migration and Model

Now, you need to create migration for new table payments to store the response of RazorPay API. Also, create a model Payment for the same. Run this below command

php artisan make:model Payment —migration

Copy this code in the migration file and run php artisan migrate

Screenshot 2022-04-13 at 10.58.37 PM.png

Screenshot 2022-04-13 at 11.07.57 PM.png

Step 7 - Create Controller

Now, create a controller using this command and write the below code. In the controller, we will write the logic for Razorpay API call and store the response in our database.

Screenshot 2022-04-13 at 11.00.09 PM.png

Step 8 - Create View File

Create Laravel blade file and add the below code in that file and call that file with the help of the view function from the controller.

Screenshot 2022-04-13 at 11.01.09 PM.png

Now run this below command and open this URL ( 127.0.0.1:8000/product )

PHP artisan serve

Conclusion

This was a tutorial on integrating Razorpay into your Laravel 8 application. You can try it out and let us know how it went!

 
Share this