Quick Summary:
In this modern technology world, the combination of Laravel and VueJS is unbeatable and very powerful for making any high-end application run smoothly. There is an increasing need for e-commerce/content-driven websites. You now need a payment gateway to make easy transactions. So here is how to integrate the Razorpay payment gateway into your latest versions of Laravel along with VueJS.
Without further ado, let’s get started with the Razorpay Payment Gateway Integration in Laravel and VueJS 2.
First, you need the latest Laravel version and Node dependencies installed to get started; run the commands below one by one to install the required stuff.
composer create-project laravel/laravel razorpay-demo cd razorpay-demo composer require laravel/ui php artisan ui vue npm install npm run dev
Note: If you face this error
“Cannot find module ‘webpack/lib/rules/DescriptionDataMatcherRulePlugin’”
Run this command
Then in a separate terminal, run the Laravel server by using the following command
Your app.js file will be compiled and saved to public/js/app.js. Make sure to add a root element with the app’s ID to your HTML scaffolding. Otherwise, Vue won’t know where to mount the components.
// resources/views/app.blade.php
Hire full stack developer from us and ensure smooth payment integration to provide reliable and secure user experience.
The following are the ways to set up Razorpay Laravel integration with Vue js.
// resources/js/app.js
import App from './App.vue'; const app = new Vue({ el: '#app', template: "", components: { App, }, });
This file contains the Payment data for now; in your real-time application, you need to pass the user information and amount on the “pay” button click.
// resources/js/App.vue
Create an Account from here: https://razorpay.com
Get id and secret from here: https://dashboard.razorpay.com/app/keys
Open .env file and add RazorPay key and secret to it
// .env
Use the below command to install RazorPay package
Create config file to access .env values and use the below code.
// config/values.php
Use the below command to create a controller.
// app/Http/Controllers/RazorPayController.php
order->create([ 'receipt' => '123', 'amount' => $request->amount * 100, 'currency' => 'INR' ]); // Creates Razorpay order $data = [ "key" => Config("values.razorpayKey"), "amount" => $request->amount * 100, "order_id" => $orderData['id'], ]; return response()->json($data, 200); } function verify(Request $request) { $success = true; $error = "Payment Failed!"; if (empty($request->razorpay_payment_id) === false) { $api = new Api(Config("values.razorpayKey"), Config("values.razorpaySecret")); try { $attributes = [ 'razorpay_order_id' => $request->razorpay_order_id, 'razorpay_payment_id' => $request->razorpay_payment_id, 'razorpay_signature' => $request->razorpay_signature ]; $api->utility->verifyPaymentSignature($attributes); } catch (SignatureVerificationError $e) { $success = false; $error = 'Razorpay Error : ' . $e->getMessage(); } } if ($success === true) { // Update database with success data // Redirect to success page return redirect('/'); } else { // Update database with error data // Redirect to payment page with error // Pass $error along with route return redirect('/'); } } }
Moving further in our tutorial: Razorpay payment gateway integration. Let’s add routes for displaying the Razorpay page and verifying requests. Open the routes/web.php file and use the below code.
// routes/web.php
use App\Http\Controllers\RazorPayController; Route::view('/pay/razorpay', 'razorpay'); Route::post('/pay/verify', [RazorPayController::class, 'verify']);
Now, add an API route to access through VueJS. For that use the following code.
// routes/api.php
use App\Http\Controllers\RazorPayController; Route::post('payment/razorpay', [RazorPayController::class, 'razorpay'])->name('paymentRazorpay');
This is the Razorpay Laravel and Vue js gateway. Users enter their credentials and pay using various methods. Here, you can enter your company/personal details like name, description, and logo to show users who they are paying.
// resources/views/razorpay.blade.php
You can also prefill user data if you have already; you need to add this under options.
The user interface will look something like this.
Now it’s time to check the payment transaction; for that, visit RazorPay Dashboard.
Here is the reference image:
The entire source code is available here: razorpay-payment-gateway-integration-example. Use the below command to clone the repository and play around with it.
The tutorial on Razorpay Laravel and Vue js payment gateway integration has helped you to generate secure transactions. Moreover, you can visit VueJS and Laravel tutorials pages and explore more about both technologies individually. Each tutorial will provide you with a github repository to play around with the code.
Bacancy has a skilled team of developers who are experts in both backend and frontend development. Our dedicated full-stack developers analyze the project requirements and problems from both front-end and back-end perspectives, providing the optimum solution.
Your Success Is Guaranteed !
We accelerate the release of digital product and guaranteed their success
We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.