Razozpay Integration with Django

po

Po

Posted on January 4, 2021

Razozpay Integration with Django

To make use of razorpay in our application we are going to use this razorpay python package. We need to install it first.

pip install razorpay
Enter fullscreen mode Exit fullscreen mode

Once signup is complete, Make sure you are in test mode and not live mode. Visit Razorpay Dashboard and create new api keys.

The api keys are shown just once so, make sure you save it. Ok so, you have already put it in your settings file,good. Not Good !!! Secrets should remain secrets, either we should put them as environment variables or in a json file which we won't push to Github.
Lets create an url, upon visiting which we should be provided with a Button, When we click on the button then we should be provided us with payment gateway screen.
But for this lets create our view first.

import os
import razorpay

from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt


def payment(request):
    amount = 100 #100 here means 1 dollar,1 rupree if currency INR
    client = razorpay.Client(auth=(os.getenv('razorpaykey'), os.getenv('razorpaysecret')))
    response = client.order.create({'amount':amount,'currency':'USD','payment_capture':1})
    print(response)
    context = {'response':response}
    return render(request,"payments/payment.html",context)


@csrf_exempt
def payment_success(request):
    if request.method =="POST":
        print(request.POST)
        return HttpResponse("Done payment hurrey!")
Enter fullscreen mode Exit fullscreen mode

This will be our HTML file and it will be responsible for getting a razozpay button which when click will generate the payment form.
Alt Text

{% if response %}
<form action="/success/" method="POST">
    <script
        src="https://checkout.razorpay.com/v1/checkout.js"
        data-key="your_test_api_key"
        data-amount={{response.amount}}
        data-currency={{response.currency}}
        data-order_id={{response.id}}
        data-buttontext="Pay with Razorpay"
        data-name="ILoveDjango3000"
        data-description=""
        data-image=""
        data-prefill.name={{request.user.username}}
        data-prefill.email={{request.user.email}}
        data-theme.color="#F37254"
    ></script>
    <input type="hidden" custom="Hidden Element" name="hidden">
    </form>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

Hook up the views i.e. payment and payment_success with url of your choice, Make sure to update the HTML file form-action with your success url. One more thing is once payment is success, Razorpay will send a POST request to our application at success url. Since the form is not having csrf_token we can exempt csrf_token. It is advised that, we should make use of webhooks and then exempt csrf_token.

This is an e.g. of my urls.py file:

from django.urls import path

from apps.payments.views import payment,payment_success

urlpatterns = [
    path('payment/',payment,name="payment"),
    path('success/',payment_success,name="payment-success"),
]

Enter fullscreen mode Exit fullscreen mode

This is a minimal configuration to get Razorpay to work with django. There are still things which needs to be improved or integrated e.g. Subscription, invoices, refunds, routes, and most important security and signature verification etc.

I am planning to update more payment options e.g. paypal and instamojo :
https://www.ilovedjango3000.com/blog/payment-in-django/

💖 💪 🙅 🚩
po
Po

Posted on January 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Razozpay Integration with Django
django Razozpay Integration with Django

January 4, 2021