VOOZH about

URL: https://www.geeksforgeeks.org/python/razorpay-integration-in-django/

⇱ Razorpay Integration in Django - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Razorpay Integration in Django

Last Updated : 9 Apr, 2026

Razorpay is a popular payment gateway in India that allows businesses to accept online payments via credit/debit cards, UPI, wallets, and net banking. Integrating Razorpay with Django enables secure and seamless payment processing for your web applications.

Project Setup with Razorpay in Django

Consider a project named 'dj_razorpay' having an app named 'payment', and create a Payment model to track transactions and apply migrations.

In payment/models.py:

Step 1: Get Razorpay Keys

Create an account on the Razorpay website by signing up with your email address and password. During registration, provide the required basic details such as your phone number. After the account is set up, complete the KYC verification by submitting your identity documents and business details.

Once verified, navigate to the Razorpay Dashboard to obtain the API Key ID and Key Secret, which are required for integration. For live transactions, you will also need to add your bank account details to enable settlements.

Note: After successful verification, you will see a similar user interface as below.

👁 Image

Inside the Razorpay settings screen, select 'Create a new key' to generate your API credentials. This will provide a Key ID and a Key Secret.

At this stage, payments operate in test mode, meaning no real transactions occur and only limited payment methods are available. To enable live transactions and unlock all payment options, complete the KYC verification process and add bank account details. The integration process remains identical for both test and live modes.

👁 Image

Add the 'Key Id' and 'Key Secret' to settings.py file.

RAZOR_KEY_ID = 'YOUR_KEY_ID'
RAZOR_KEY_SECRET = 'YOUR_KEY_SECRET'

Install razorpay's python package:

pip install razorpay

Razorpay Payment Flow

  • A Razorpay Order is created from the Django backend.
  • The Order ID and other required details are passed to the frontend.
  • The user clicks the payment button and completes the transaction using a preferred payment method.
  • Razorpay handles both payment success and failure scenarios.
  • In case of failure, Razorpay allows the user to retry the payment.
  • On successful payment, Razorpay sends a POST request to a callback URL on the Django server.
  • The server then verifies the payment signature to ensure authenticity and prevent tampering.
  • Once verified, the payment is captured, and a success page is rendered for the user.

Step 2: Views.py

In payment/views.py:

Note: It is necessary to capture the payment, otherwise it would be auto refunded to the payer.

Step 3: Map the views to the urls

In dj_razorpay/urls.py:

Step 4: Templates

Pass the Razorpay Order ID along with other options generated in the previous step. Include Razorpay's JavaScript in the template to render the payment window and initialize it with backend-provided options.

Add an event listener to the payment button so the payment window opens when the button is clicked. The payment button is rendered on the homepage. Two additional pages are required to handle payment success and payment failure.

Step 5: Testing

Run the Server:

python manage.py runserver

Visit development URL- http://127.0.0.1:8000

Comment