Django payment integration | Razorpay | Full Guide
Let's face it. Integrating a payment gateway to a back-end is actually quite easy. No, I am serious! You find it hard because you are probably confused about the payment flow. No worries after reading this article you will be able to ingrate the payment system and get payments from any kind of website you are making with Django.
First, create an account on the Razorpay site or log in if you already have an account. Be sure that you are in test mode. Go to settings → API keys. Generate New API keys. You will get two API keys. One is a key-id which you can show publicly and another key is a secret key. Copy the keys somewhere else. I am assuming that you have some knowledge about Django like creating projects, starting new apps, URLs, views, etc.
Steps to get a successful payment :
- Create an order object in your views and send the order_id, amount, and your Razorpay public key id to the frontend.
- Frontend template file will have some javascript code that will get the order_id, payment, and key id and generate the payment. User will see a pop-up where they can select multiple options for their payment like UPI, net banking, card, etc. After they make a successful payment javascript will send some data to your server via the POST method.
- Back to your server, you will get the incoming data and validate them. Razorpay gives us a nice little function in order to validate the data. If the data is valid you return a success page or any other response you want like FileResponse or JsonResponse, your choice. But if the data is invalid you will return a failure page or redirect the user back to the payment page, again your choice.
Coding Part:
I hope you understood the steps mentioned above. Didn’t understand a single thing. Don’t worry nobody does actually. It will all be clear in the coding part.
- First, we need to create an order object. We want to create an order object when the user clicks on the payment button. Later in the article, you will see that I have used fetch API for that. Because I don’t want to create an object every time the user just visits the URL. Fetch API gets the data in JSON format. That’s why I have returned a JSON response.
On the 4th line, I have imported razorpay. So we need to install that first. Go to your terminal and type:
pip install razorpay
On the 13th line, we have created the order object. And then we gathered the data into context and send it as a JSON response.
Add this view function to your urls.py file as any other normal URL path. Also, you should have another view where you want the payment button to be. You could want the payment button on your product’s description page, checkout page, donation page, etc. In my case, I want it on my home page. So I create another view which just returns a home page.
Now my app’s urls.py file looks like this:
2. We are half done with our backend. Time to code the frontend templates. I have only one template → home.html inside my templates folder, which looks like this:
The file was not fitted into my screen so I have screenshotted only the essential part. Explanation → on the homepage we have a button. we want that an order object should be created and returned from the backend when the user clicks on the button. So we have used the famous fetch API. Fetch API first sends a GET request to our payment view. Then with the response fetch API creates a payment object. After the user pays the amount fetch API will send data via POST method to the callback_url (see line 28). We haven’t coded the best part yet.
3. Last but not least we should validate the incoming data in our server and return something. Edit your payment function in the views.py file. Views.py should look something like this :
Code the payment_failed.html and payment_success.html page whatever you want.
Now you will be able to: Go to http://127.0.0.1:8000/ → click on the payment button → a pop-up will come → choose your payment method and pay → on a successful payment you will get payment_success page.
If you find articles like this valuable and want to support this type of content, consider signing up to Medium, You will get unlimited access to all articles from thousands of authors. I appreciate your support. Made with ❤️ by Sk Soyeb Akhter.