Home - Scripts - Website Development

  • 06 September 2025

How to Implement Razorpay Payment Gateway in CodeIgniter

by Shailender K. 3 minute read 7 views

Optimizing SEO titles, meta tags and Open Graph tags in Next.js improves visibility, improves click-through rates and boosts rankings in web development.

Key Points

  • Razorpay processes 10M+ monthly payments, ensuring faster and secure CodeIgniter transactions.
  • With 99.9% uptime, Razorpay keeps CodeIgniter apps reliable for payment processing.
  • Businesses see 25% faster checkouts, boosting satisfaction and improving overall conversions.

In today’s digital world, online payments are vital for website development, eCommerce platforms, and SaaS solutions. If you’re building an application with CodeIgniter, including a secure and reliable payment gateway is essential. Razorpay is one of the leading payment gateways in India, offering a smooth checkout experience, multiple payment options, and robust APIs.

This guide will walk you through each step of integrating Razorpay Payment Gateway into CodeIgniter and explain why it benefits businesses seeking custom web development solutions.

Why Razorpay?

  • Multiple Payment Options: Debit cards, credit cards, UPI, net banking, and wallets.
  • Seamless Checkout: Easy-to-use hosted checkout page.
  • Secure Transactions: PCI DSS compliant with fraud detection features.
  • Developer-Friendly APIs: Lightweight SDK with detailed documentation.

Whether you're a business owner working with website development services or planning to hire a PHP developer for your next project, Razorpay makes managing payments simple and secure.

Step 1: Get Razorpay API Keys

  • Log in to the Razorpay Dashboard.
  • Go to Settings → API Keys.
  • Generate your Key ID and Key Secret (you’ll need them in your CodeIgniter project).

Step 2: Install Razorpay PHP SDK

The simplest method to integrate Razorpay into CodeIgniter is through Composer.

                                        composer require razorpay/razorpay
                                    

This will add the Razorpay SDK to your vendor/ folder. If you’ve chosen custom web development for your project, this integration will provide flexibility and security when accepting payments.

Step 3: Create a Controller in CodeIgniter

Create a controller called Payment.php to manage order creation and verification.

                                        <?php
defined('BASEPATH') OR exit('No direct script access allowed');

use Razorpay\Api\Api;

class Payment extends CI_Controller {

    public function __construct() {
        parent::__construct();
        require APPPATH.'../vendor/autoload.php';
    }

    // Step 1: Create Razorpay Order
    public function create_order() {
        $api = new Api("YOUR_KEY_ID", "YOUR_KEY_SECRET");

        $orderData = [
            'receipt'         => uniqid(),
            'amount'          => 50000, // Amount in paise (50000 = INR 500)
            'currency'        => 'INR',
            'payment_capture' => 1
        ];

        $razorpayOrder = $api->order->create($orderData);

        $data['orderId'] = $razorpayOrder['id'];
        $data['amount'] = $orderData['amount'];
        $data['currency'] = $orderData['currency'];
        $data['key'] = "YOUR_KEY_ID";

        $this->load->view('checkout', $data);
    }

    // Step 2: Verify Payment
    public function verify() {
        $success = true;
        $error = "Payment Failed";

        if (!empty($_POST['razorpay_payment_id'])) {
            $api = new Api("YOUR_KEY_ID", "YOUR_KEY_SECRET");

            try {
                $attributes = [
                    'razorpay_order_id' => $_POST['razorpay_order_id'],
                    'razorpay_payment_id' => $_POST['razorpay_payment_id'],
                    'razorpay_signature' => $_POST['razorpay_signature']
                ];

                $api->utility->verifyPaymentSignature($attributes);

            } catch(SignatureVerificationError $e) {
                $success = false;
                $error = 'Razorpay Error : ' . $e->getMessage();
            }
        }

        if ($success) {
            echo "✅ Payment Successful";
            // Save payment details in the database
        } else {
            echo "❌ " . $error;
        }
    }
}
                                    

Step 4: Create Razorpay Checkout View

Now, create a view file called checkout.php to display the Razorpay payment form.

                                        <!DOCTYPE html>
<html>
<head>
    <title>Razorpay Payment</title>
</head>
<body>
    <h3>Pay with Razorpay</h3>

    <button id="rzp-button">Pay Now</button>

    <script src="https://checkout.razorpay.com/v1/checkout.js"></script>
    <script>
        var options = {
            "key": "<?php echo $key; ?>",
            "amount": "<?php echo $amount; ?>",
            "currency": "<?php echo $currency; ?>",
            "name": "Your Company Name",
            "description": "Test Transaction",
            "order_id": "<?php echo $orderId; ?>",
            "handler": function (response){
                var form = document.createElement('form');
                form.method = 'POST';
                form.action = "<?php echo base_url('payment/verify'); ?>";

                form.innerHTML = '<input type="hidden" name="razorpay_payment_id" value="'+response.razorpay_payment_id+'" />' +
                                 '<input type="hidden" name="razorpay_order_id" value="'+response.razorpay_order_id+'" />' +
                                 '<input type="hidden" name="razorpay_signature" value="'+response.razorpay_signature+'" />';

                document.body.appendChild(form);
                form.submit();
            },
            "theme": {
                "color": "#528FF0"
            }
        };
        var rzp1 = new Razorpay(options);
        document.getElementById('rzp-button').onclick = function(e){
            rzp1.open();
            e.preventDefault();
        }
    </script>
</body>
</html>
                                    

Step 5: Add Routes

Open application/config/routes.php and add:

                                        $route['payment/create'] = 'payment/create_order';
$route['payment/verify'] = 'payment/verify';
                                    

Testing the Integration

  • Visit http://yourdomain.com/payment/create.
  • Click on Pay Now.
  • Use Razorpay’s test card details.
  • On success, the request will be verified and confirmed.

Final Words

Integrating Razorpay Payment Gateway in CodeIgniter is simple with Razorpay’s PHP SDK. You just need to:

  • Generate API keys.
  • Create orders on the server.
  • Show Razorpay checkout on the client side.
  • Verify payment securely on the backend.

If you're planning a new project and need secure, scalable payment solutions, it’s best to hire a PHP developer with expertise in Razorpay integration. Professional teams offering website development and custom web development services can ensure your platform is reliable, secure, and future-proof.

Tech Stack & Version

Frontend

  • HTML5
  • CSS3
  • JavaScript 
  • Razorpay

Backend

  • CodeIgniter
  • PHP
  • MySQL

Deployment

  • AWS EC2
  • DigitalOcean
img

©2025Digittrix Infotech Private Limited , All rights reserved.