Razorpay Integration in Django
Last Updated :
24 Sep, 2024
Payments are an integral part of the online ecosystem, be it at e-commerce stores to process orders or a simple donation to someone. Integrating a payment gateway into your website can be a tedious process. Let us have a look at how you can integrate Razorpay into a Django website.
What are we building?
We will build a basic replica of Buy Me Coffee , uhh Buy Me Chai where users can give donations to us.
The integration process though remains the same for whatever you are building. Let's break down the whole process into simple steps.
Step 1: Initiate the Django project
Make sure you are done with the django installation. Create a new project called dj_razorpay and then start a new app called payment . Add "payment" to the list of installed_apps in the settings.py file. Also, apply the migrations.
INSTALLED_APPS = [
...
'payment',
...
]
Integrating payment systems like Razorpay is crucial for building e-commerce apps. To learn more about advanced integrations and develop feature-rich Django applications, consider the Complete Django Web Development Course - Basics to Advance .
Step 2: Get Razorpay Keys
First, create an account on Razorpay's website , you simply have to signup with your email and password and add some basic information such as your phone number.

Inside the setting screen, click on Create a new key option your key will be generated. You will get Key ID and Key Secret. Currently our payments will be in "test mode" i.e no real transaction would happen and the payment options are also limited. To accept real money and unlock more payment options you need to complete your KYC and provide your bank details. The integration process remains the same irrespective of the mode.
Note: You will be shown the Key Secret only once, so immediately copy it somewhere.

Now add the Key Id and Key Secret to settings.py file.
RAZOR_KEY_ID = YOUR_KEY_ID
RAZOR_KEY_SECRET = YOUR_KEY_SECRET
Before we move ahead let's install razorpay's python package.
pip install razorpay
Step 3: Views.py
- This is the main step of the process. First, we need to understand how payment works in Razorpay.
- Create a Razor Order from our Django Server.
- Pass Order Id and Other Options to the Frontend.
- The user clicks the payment button and pays with one of the payment methods.
- Razorpay handles Payment Success and Failure.
- On Failure, Razorpay facilitates retry of the payments.
- On Success, Razorpay makes a post request to a callback URL on our server.
- Verify the payment signature, to confirm that payment is authentic and isn't tampered with.
- Once verified, capture the payment and render the success page.
See code comments for more details.
Note: Amount in Razorpay works in subunits of currency i.e Rs 500 would become 50000 paise.
Python
from django.shortcuts import render
import razorpay
from django.conf import settings
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponseBadRequest
# authorize razorpay client with API Keys.
razorpay_client = razorpay.Client(
auth=(settings.RAZOR_KEY_ID, settings.RAZOR_KEY_SECRET))
def homepage(request):
currency = 'INR'
amount = 20000 # Rs. 200
# Create a Razorpay Order
razorpay_order = razorpay_client.order.create(dict(amount=amount,
currency=currency,
payment_capture='0'))
# order id of newly created order.
razorpay_order_id = razorpay_order['id']
callback_url = 'paymenthandler/'
# we need to pass these details to frontend.
context = {}
context['razorpay_order_id'] = razorpay_order_id
context['razorpay_merchant_key'] = settings.RAZOR_KEY_ID
context['razorpay_amount'] = amount
context['currency'] = currency
context['callback_url'] = callback_url
return render(request, 'index.html', context=context)
# we need to csrf_exempt this url as
# POST request will be made by Razorpay
# and it won't have the csrf token.
@csrf_exempt
def paymenthandler(request):
# only accept POST request.
if request.method == "POST":
try:
# get the required parameters from post request.
payment_id = request.POST.get('razorpay_payment_id', '')
razorpay_order_id = request.POST.get('razorpay_order_id', '')
signature = request.POST.get('razorpay_signature', '')
params_dict = {
'razorpay_order_id': razorpay_order_id,
'razorpay_payment_id': payment_id,
'razorpay_signature': signature
}
# verify the payment signature.
result = razorpay_client.utility.verify_payment_signature(
params_dict)
if result is not None:
amount = 20000 # Rs. 200
try:
# capture the payemt
razorpay_client.payment.capture(payment_id, amount)
# render success page on successful caputre of payment
return render(request, 'paymentsuccess.html')
except:
# if there is an error while capturing payment.
return render(request, 'paymentfail.html')
else:
# if signature verification fails.
return render(request, 'paymentfail.html')
except:
# if we don't find the required parameters in POST data
return HttpResponseBadRequest()
else:
# if other than POST request is made.
return HttpResponseBadRequest()
Note: It is necessary to capture the payment, otherwise it would be auto refunded to the payer.
Now map the above views to the urls in the urls.py.
Python
# dj_razorpay/urls.py
from django.contrib import admin
from django.urls import path
from payment import views
urlpatterns = [
path('', views.homepage, name='index'),
path('paymenthandler/', views.paymenthandler, name='paymenthandler'),
path('admin/', admin.site.urls),
]
Step 4: Frontend
We need to pass the Razorpay order id and other options mentioned in the previous step. First load the Razorpay's javascript code that would render the payment window and initialize it with the options received from the backend. Add an event listener to the payment button so the payment window gets opened once it is clicked.
Here we are rendering the payment button on the homepage itself. We would also need another two pages for payment success and failure.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>GFG</title>
<style>
* {
box-sizing: border-box;
padding: 0px;
margin: 0px;
font-family: cursive;
}
html,
body {
height: 100%;
}
body {
background-color: #f1f5f8;
display: flex;
justify-content: center;
align-items: center;
}
.card {
background-color: white;
padding: 25px;
border: 1px solid #bbbbbb;
border-radius: 5px;
box-shadow: 1px 1px 10px 0px rgb(0 0 0 / 25%);
}
.title {
text-align: center;
letter-spacing: 1px;
}
.muted {
color: #8e7f7f;
display: block;
margin-bottom: 10px;
text-align: center;
}
.btn_container {
padding: 20px;
text-align: center;
}
.btn {
border-radius: 4px;
cursor: pointer;
padding: 4px 8px;
background-color: #ffaaa7;
color: white;
font-size: 1.2em;
font-weight: 600;
letter-spacing: 1px;
}
</style>
</head>
<body>
<div class="card">
<h1 class="title">Buy Me a Chai ☕</h1>
<small class="muted"
>If you like my work, you can support me by donating ₹200</small
>
<div class="btn_container">
<!-- Payment Button -->
<button class="btn" id="pay-btn">Donate❤️</button>
</div>
</div>
</body>
<!-- Razorpay's Javascript code. -->
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
// Enter the Key ID generated from the Dashboard
key: "{{ razorpay_merchant_key }}",
// Amount is in currency subunits.
// Default currency is INR. Hence,
// 50000 refers to 50000 paise
amount: "{{ razorpay_amount }}",
currency: "{{ currency }}",
// Your/store name.
name: "Dj Razorpay",
// Pass the `id` obtained in the response of Step 1
order_id: "{{ razorpay_order_id }}",
callback_url: "{{ callback_url }}",
};
// initialise razorpay with the options.
var rzp1 = new Razorpay(options);
// add event listener to the payment button.
document.getElementById("pay-btn").onclick = function (e) {
rzp1.open();
e.preventDefault();
};
</script>
</html>
Step 5: Testing
Now let's fire up the server and check if everything is working fine!
python manage.py runserver
We successfully received the payment!! You can check these payments in Razorpay's dashboard.
Similar Reads
Navigation in Django
Navigation refers to how users move through different sections of a web application. In Django, navigation is handled by combining URLs, views, and templates. By defining URL patterns, associating them with views, and linking them in templates, we can create a seamless experience for users as they m
4 min read
Django Migrations | Python
Prerequisite: Django Models No such table? - The class defined in product/models.py is the mere idea of what our database is going to look like but it didn't create any table in the database. We can assume class Phone as conceptual schema. Before the creation of any table, if we try to access the ta
2 min read
ProgrammingError in Django
This article will elucidate the 'django.db.utils.ProgrammingError' through a code example and explore various solution approaches to resolve the error. What is 'django.db.utils.ProgrammingError' ?The 'django.db.utils.ProgrammingError' is an exception in Django, a popular web framework for Python. Th
5 min read
Swagger Integration with Python Django
Integrating Swagger with Django REST Framework can be quite useful for documenting and testing your API. One popular tool for integrating Swagger with Django REST Framework is drf-yasg (Yet Another Swagger Generator). In this article, we will see how to integrate Swagger with the Django REST framewo
2 min read
Render Model in Django Admin Interface
Rendering model in admin refers to adding the model to the admin interface so that data can be manipulated easily using admin interface. Django's ORM provides a predefined admin interface that can be used to manipulate data by performing operations such as INSERT, SEARCH, SELECT, CREATE, etc. as in
2 min read
Django Installation and Setup
Installing and setting up Django is a straightforward process. Below are the step-by-step instructions to install Django and set up a new Django project on your system.Prerequisites: Before installing Django, make sure you have Python installed on your system. How to Install Django?To Install Django
2 min read
Django vs Ruby On Rails
When you're building a website or web application, picking the right framework can be a big decision. Two popular choices are Django and Ruby on Rails. Django uses Python, while Ruby on Rails uses Ruby. Each has its own way of doing things, and they both have their strengths. This article will break
6 min read
Django REST Framework Installation
Django REST Framework can be installed via pip package similar to Django installation. Since Django REST Framework is a wrapper over default Django Framework, to install it, Django should be installed already. Now to install rest framework follow the below process. Prerequisites Python Pip Django Ho
1 min read
Middleware in Django
Middleware is a series of processing layers present between the web server and the view (controller in some frameworks), allowing the user to intercept, modify, and add behavior to these requests and responses.What is Middleware in Django?In Python Django, middleware is a framework that provides a m
9 min read
Nestest Serializer in Django Framework
In web development, creating and consuming APIs (Application Programming Interfaces) is commonplace. Django Rest Framework (DRF) serves as a robust toolkit for building APIs in Django-based web applications. Within DRF, a pivotal concept is serializers. In this article, we will delve into the concep
4 min read