Laravel UPI Payment Integration with Razorpay
Learn how to set up UPI payments in a Laravel application using Razorpay's API. This tutorial covers installing Razorpay's PHP SDK, setting up routes, and creating views to handle UPI transactions. Follow the steps below to get started with UPI payments in Laravel.
Step 1: Install the Razorpay PHP SDK
To begin, open your terminal and install the Razorpay PHP SDK via Composer:
composer require razorpay/razorpay
Step 2: Configure Razorpay API Keys
After installing the SDK, go to your Razorpay Dashboard and get your API Key ID and API Key Secret. Add them to your .env
file:
RAZORPAY_KEY_ID=your_key_id
RAZORPAY_KEY_SECRET=your_key_secret
Step 3: Create a Payment Controller
In Laravel, create a controller to manage payment requests. Run the following Artisan command to create it:
php artisan make:controller PaymentController
Then, open PaymentController.php
and add methods to initiate and verify the payment. Here’s the code for the controller:
<?php
namespace App\Http\Controllers;
use Razorpay\Api\Api;
use Illuminate\Http\Request;
class PaymentController extends Controller
{
private $razorpayId;
private $razorpayKey;
public function __construct()
{
$this->razorpayId = env('RAZORPAY_KEY_ID');
$this->razorpayKey = env('RAZORPAY_KEY_SECRET');
}
// Initiate Payment
public function initiatePayment()
{
$api = new Api($this->razorpayId, $this->razorpayKey);
$order = $api->order->create([
'amount' => 5000, // Amount in paise (e.g., 5000 paise = ₹50)
'currency' => 'INR',
'payment_capture' => 1, // Auto-capture payment
]);
return view('payment.initiate', ['orderId' => $order->id, 'razorpayId' => $this->razorpayId]);
}
// Verify Payment
public function verifyPayment(Request $request)
{
$api = new Api($this->razorpayId, $this->razorpayKey);
try {
$attributes = [
'razorpay_order_id' => $request->input('razorpay_order_id'),
'razorpay_payment_id' => $request->input('razorpay_payment_id'),
'razorpay_signature' => $request->input('razorpay_signature'),
];
$api->utility->verifyPaymentSignature($attributes);
return redirect('/')->with('success', 'Payment successful.');
} catch (\Exception $e) {
return redirect('/')->with('error', 'Payment verification failed.');
}
}
}
Step 4: Define Routes for Payment
Add the following routes in your web.php
file to handle payment initiation and verification:
use App\Http\Controllers\PaymentController;
Route::get('/payment', [PaymentController::class, 'initiatePayment']);
Route::post('/payment/verify', [PaymentController::class, 'verifyPayment']);
Step 5: Create a Payment View
Create a Blade view in resources/views/payment/initiate.blade.php
to display the payment interface:
<!DOCTYPE html>
<html>
<head>
<title>UPI Payment</title>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
</head>
<body>
<h2>Pay with UPI</h2>
<button id="payButton">Pay ₹50 with UPI</button>
<script>
document.getElementById('payButton').onclick = function () {
var options = {
"key": "{{ $razorpayId }}",
"amount": "5000", // Amount in paise (₹50)
"currency": "INR",
"name": "Your Company Name",
"order_id": "{{ $orderId }}",
"handler": function (response) {
fetch("/payment/verify", {
method: "POST",
headers: { "Content-Type": "application/json", "X-CSRF-TOKEN": "{{ csrf_token() }}" },
body: JSON.stringify(response)
}).then(res => res.json())
.then(data => alert(data.success ? "Payment successful!" : "Payment failed"));
}
};
var rzp1 = new Razorpay(options);
rzp1.open();
};
</script>
</body>
</html>
Conclusion
With these steps, you can easily set up UPI payments in Laravel using Razorpay. Once testing is complete, switch to live credentials in the Razorpay dashboard. This setup enables seamless UPI transactions in your Laravel application.
No comments:
Post a Comment