Integrate Razorpay Payment Gateway In Laravel 12 Without Any Package With Example

image
image
image
image
image
image
image
image
Integrate Razorpay Payment Gateway In Laravel 12  Without Any Package With Example

Integrate Razorpay Payment Gateway In Laravel 12 Without Any Package With Example

Want to integrate Razorpay in your Laravel 12 app without using any package? Follow this simple guide to securely create Razorpay orders, handle frontend payments, and verify the signature after payment.


Ok, so before getting started you need to open you account first on razorpay and have to collect, your key and secret which we will put in .env files, I just a added a photo for from you will get those



✅ Steps We’ll Cover


  1. Create a new controller with Razorpay logic
  2. Create a Blade view for payment
  3. Define routes for payment and verification
  4. Start your Laravel server and test the integration
  5. GitHub repo and contact info


Step 1: Controller Code – app/Http/Controllers/PaymentController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class PaymentController extends Controller
{
public function createOrder(Request $request)
{
$apiKey = env('RAZORPAY_API_KEY');
$apiSecret = env('RAZORPAY_API_SECRET');

$response = Http::withBasicAuth($apiKey, $apiSecret)->post('https://api.razorpay.com/v1/orders', [
'amount' => 50000, // ₹500.00 in paise
'currency' => 'INR',
'receipt' => 'rcptid_11',
'payment_capture' => 1
]);

$order = $response->json();

return view('payment.razorpay', [
'order_id' => $order['id'],
'amount' => $order['amount'],
'apiKey' => $apiKey
]);
}

public function verifySignature(Request $request)
{
$signature = $request->razorpay_signature;
$orderId = $request->razorpay_order_id;
$paymentId = $request->razorpay_payment_id;

$generatedSignature = hash_hmac('sha256', "$orderId|$paymentId", env('RAZORPAY_API_SECRET'));

if (hash_equals($generatedSignature, $signature)) {
return response()->json(['status' => 'success', 'message' => 'Payment verified successfully!']);
} else {
return response()->json(['status' => 'error', 'message' => 'Signature mismatch! Payment failed.']);
}
}
}


Step 2: View Code – resources/views/payment/razorpay.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Pay with Razorpay</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<style>
body {
font-family: 'Segoe UI', sans-serif;
background: #f7f9fc;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
.payment-card {
background: #fff;
padding: 2rem 3rem;
border-radius: 12px;
box-shadow: 0 0 20px rgba(0,0,0,0.05);
text-align: center;
}
.payment-card h2 {
margin-bottom: 1rem;
color: #333;
}
.payment-card button {
background-color: #528FF0;
color: white;
padding: 12px 25px;
border: none;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
}
#message {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="payment-card">
<h2>Complete Your Payment</h2>
<p>Amount: ₹{{ number_format($amount / 100, 2) }}</p>
<button id="rzp-button">Pay Now</button>
<p id="message"></p>
</div>

<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
let options = {
"key": "{{ $apiKey }}",
"amount": "{{ $amount }}",
"currency": "INR",
"name": "CodeHunger",
"description": "Laravel Razorpay Payment",
"order_id": "{{ $order_id }}",
"handler": function (response) {
fetch("{{ route('payment.verify') }}", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRF-TOKEN": document.querySelector('meta[name=\"csrf-token\"]').content
},
body: JSON.stringify(response)
})
.then(res => res.json())
.then(data => {
const msg = document.getElementById("message");
msg.innerText = data.message;
msg.style.color = data.status === 'success' ? 'green' : 'red';
});
},
"theme": {
"color": "#528FF0"
}
};

const rzp = new Razorpay(options);

document.getElementById('rzp-button').onclick = function(e) {
rzp.open();
e.preventDefault();
};
</script>
</body>
</html>


Step 3: Routes – routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PaymentController;

Route::get('/pay', [PaymentController::class, 'createOrder'])->name('payment.form');
Route::post('/verify-payment', [PaymentController::class, 'verifySignature'])->name('payment.verify');

Step 4: Run Cache Clear Command


PHP artisan route:cache


Step 5: Run Laravel Server and Visit Payment Page

php artisan serve

Now open your browser and go to:

http://127.0.0.1:8000/pay




5️⃣ Full Source Code

👉 Get the complete working code on GitHub:

🔗 https://github.com/codehunger-team/integrate-razorpay-payment-gateway-in-laravel-12.git


💼 Need Help With Integration?

If you want us to implement this in your project, we’d love to help!

📧 Contact Us at CodeHunger