In this blog, We will see how can we add Razorpay payment gateway in the PHP Laravel website.
What is a Payment Gateway
An online payment gateway (PG) is a tunnel that connects your bank account to the platform where you need to transfer your money. A PG is a software that authorises you to conduct an online transaction through different payment modes like net banking, credit card, debit card, UPI or the many online wallets that are available these days.
How Razorpay works ?
Razorpay Payment gateway has a very straightforward way of accepting payments and getting paid. It is one of the easiest payment gateway available out there to integrate into your website.
Razorpay integration steps
- Generate API Keys
- Install Razorpay library
- Create web routes
- Make payment page
- New Stylesheet file
- Make Payment Controller
- Create Checkout blade page
- Create functions for transaction verification
1 . Generate API Keys
Log into your Dashboard with appropriate credentials. Select the mode (Test or Live) for which you want to generate the API key.
Navigate to Settings → API Keys → Generate Key to generate the key for the selected mode.
Key Id and
Key Secret appear in a pop-out window as shown below:
2 . Install Razorpay library
Razorpay provides a library for developers for creating an order. Install the library using the below command in the terminal.
If you want to install the latest library of Razorpay.
You can check here – click here
3 . Create web routes
Create routes for payment page initiate, initiate order, and complete order. Create web routes under routes → web.php file.
4 . Make payment page
Make payment page where user fill their details which use for creating an order. In our demo, We put Username, Email, Contact Number, Address and Amount fields in payment form. You can use fields as per your need in your website.
Create payment page under resources → views → payment-initiate.blade.php and copy the below code in your payment page.
5 . New Stylesheet file
Make a new stylesheet file for payment page look nice.
Copy below css code and paste it in public → css → style.css file.
6 . Make Payment Controller
Make a new controller with the name of PaymentController.php where we create an order and return a response to initiate the payment.
We save Razorpay Id and Key in controller.
7 . Create Checkout blade page
On this page, we provide the order details to the Razorpay function to show the transaction page. When payment is done successfully than the handler function holds the payment data and then we send this data to the controller for the payment verification process.
Create a checkout page under resources → views → payment-page.blade.php and copy the below code on your payment page.
8 . Create functions for transaction verification
After creating the checkout page. Let’s verify the transaction data which comes through the hander function in checkout page. Razorpay provide payment Id, order Id, and signature. Signature is use for verify the payment id and order id not intercepted in between this process.
Add these two functions in PaymentController.php to verify the payment.
In this tutorial, I send the user to success or failed page according to Signature verification. You can change it according to your need.
You can see the process of payment below
I hope, now you can integrate the Razorpay into your project easily. If you don’t understand something in this blog or you got an error in code, You can watch a Razorpay integration video tutorial which will help you.
You can download code from here – click here
I would like to hear your thoughts or suggestions in the comment section.