Razorpay is the most popular payment gateway in India. It supports UPI, cards, netbanking, wallets, and EMI. Here's how to integrate it into your PHP website.
Why Razorpay?
- 2% transaction fee (competitive)
- Supports all Indian payment methods
- Excellent documentation
- Fast settlement (T+2 days)
- Easy integration
Step 1: Create Razorpay Account
- Sign up at razorpay.com
- Complete KYC (PAN, bank details, business proof)
- Wait 1-3 days for activation
- Get your Key ID and Key Secret from Dashboard → Settings → API Keys
Step 2: Install Razorpay PHP SDK
Via Composer:
composer require razorpay/razorpay
Or download from GitHub and include manually.
Step 3: Create Payment Page
<?php
require 'vendor/autoload.php';
use Razorpay\Api\Api;
$api = new Api('YOUR_KEY_ID', 'YOUR_KEY_SECRET');
$order = $api->order->create([
'amount' => 499900, // amount in paise (₹4999)
'currency' => 'INR',
'receipt' => 'order_rcptid_' . time()
]);
?>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<button id="payBtn">Pay ₹4999</button>
<script>
document.getElementById('payBtn').onclick = function() {
var options = {
key: 'YOUR_KEY_ID',
amount: <?= $order['amount'] ?>,
currency: 'INR',
name: 'Digital Dynamix',
description: 'Course Enrollment',
order_id: '<?= $order['id'] ?>',
handler: function(response) {
window.location.href = '/payment-success.php?payment_id=' + response.razorpay_payment_id;
},
prefill: {
name: 'Customer Name',
email: 'customer@example.com'
},
theme: { color: '#4f46e5' }
};
var rzp = new Razorpay(options);
rzp.open();
};
</script>
Step 4: Verify Payment on Server
Never trust client-side success. Always verify on the backend:
<?php
$payment_id = $_GET['payment_id'];
$payment = $api->payment->fetch($payment_id);
if ($payment['status'] === 'captured') {
// Payment successful — grant access
} else {
// Payment failed
}
?>
Step 5: Test in Sandbox
Razorpay provides test mode with fake cards. Test before going live:
- Test card: 4111 1111 1111 1111
- Any future expiry, any CVV
Step 6: Go Live
- Switch to live keys
- Set up webhook URL (Razorpay Dashboard → Webhooks)
- Test with ₹1 real transaction
Security Best Practices
- Never expose Key Secret in frontend
- Always verify signature on server
- Use HTTPS everywhere
- Log all transactions
- Handle failures gracefully
Want help integrating payments? Contact Digital Dynamix.