Razorpay Integration in Django
Payments are an integral part of the online ecosystem, be it at e-commerce stores to process the 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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
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', ... ]
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.
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.
Step 4: Frontend
Here we are rendering the payment button on the homepage itself. We would also need another two pages for payment success and failure.
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.