PayU is a payment gateway used for secured payment transactions, PayU is available in 200+ countries across the globe that’s the reason it has a number of customers, and also it provides the best services to their customers and it gives service at a reasonable cost. The blog contains different steps so that we can understand how to use PayU using Django, we are going to add design templates with payment logic and PayU.
Different steps required to implement PayU using Django are given below
To integrate PayU with Django, you need to install paywix with the latest version 1.3.0 The following command is used to install PayU in Django.
pip install paywix
In the PayU dashboard as we are now developing gateway, we need to switch live mode to test mode as shown in the below image.
To setup connectivity you need different keys of PayU, using PayU you require a merchant key and merchant salt key, and to get these keys you need to create your account and get it and then you need to add these details to settings.py of your project and to get the credentials you need to follow the below steps.
PAYU_CONFIG = { "merchant_key": *********, "merchant_salt":*********, , "mode": "test", }
After adding keys to Django settings you need to create templates to design a payment gateway.to use templates in your project add templates in Django setting as shown below
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR+'/templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
After that, you need to create a folder in the Django project as shown below
<html> <head> <title>Loading...</title> </head> <body> <h1>Hello</h1> <a href="{% url 'payu_demo' %}"> Pay_demo</a> </body> </html>
<html> <head> <title>Loading...</title> </head> <body onload="document.payuForm.submit()"> <form action={{ posted.action }} method="post" name="payuForm"> {% csrf_token %} <input type="hidden" name="key" value="{{posted.key}}" /> <input type="hidden" name="hash_string" value="{{ posted.hash_string }}" /> <input type="hidden" name="hash" value="{{ posted.hashh }}" /> <input type="hidden" name="posted" value="{{ posted }}" /> <input type="hidden" name="txnid" value="{{ posted.txnid }}" /> <input type="hidden" name="amount" value="{{ posted.amount|default:'' }}" /></td> <input type="hidden" name="firstname" id="firstname" value="{{ posted.firstname|default:'' }}" /></td> <input type="hidden" name="email" id="email" value="{{ posted.email|default:'' }}" /></td> <input type="hidden" name="phone" value="{{ posted.phone|default:'' }}" /></td> <textarea type="hidden" name="productinfo" style="display:none;">{{ posted.productinfo|default:'' }}</textarea> </td> <input type="hidden" name="surl" value="{{ posted.surl }}" size="64" /></td> <input type="hidden" name="furl" value="{{ posted.furl }}" size="64" /></td> <input type="hidden" name="service_provider" value="{{posted.service_provider}}" size="64" /> <input type="hidden" name="lastname" id="lastname" value="{{ posted.lastname }}" /></td> <input type="hidden" name="address1" value="{{ posted.address1 }}" /></td> <input type="hidden" name="address2" value="{{ posted.address2 }}" /></td> <input type="hidden" name="city" value="{{ posted.city }}" /></td> <input type="hidden" name="state" value="{{ posted.state }}" /></td> <input type="hidden" name="country" value="{{ posted.country }}" /></td> <input type="hidden" name="zipcode" value="{{ posted.zipcode }}" /></td> <input type="hidden" name="udf1" value="{{ posted.udf1 }}" /></td> <input type="hidden" name="udf2" value="{{ posted.udf2 }}" /></td> <input type="hidden" name="udf3" value="{{ posted.udf3 }}" /></td> <input type="hidden" name="udf4" value="{{ posted.udf4 }}" /></td> <input type="hidden" name="udf5" value="{{ posted.udf5 }}" /></td> </form> </body> </html>
Creating a view for checkout is a simple task. In this task, we have to send data html template only by using render method and need to send user details and hash key to template that we have created for payment gateway design.
class PayuDemoAPiView(GenericAPIView): """ Class for creating API view for Payment. """ def get(self, request): """ Function for Payment. """ data = {'amount': '10', 'firstname': test, 'email': 'test@gmail.com', 'phone': '1122334455', 'productinfo': 'test', 'lastname': 'test', 'address1': 'test', 'address2': 'test', 'city': 'test', 'state': 'test', 'country': 'test', 'zipcode': 'tes' } data.update({"txnid": "123456789"}) payu_data = payu.transaction(**data) return render(request, 'payu_checkout.html', {"posted": payu_data})
Hask hay is generated by using the PayU transaction method and then we can pass it to the checkout template.
Handling success is an important step in the payment process. In this process after the transaction successfully completed then we need to show all transaction details and for that, we need to call API view of a successful transaction.
PAYU_CONFIG = { "merchant_key": "********", "merchant_salt": "********", "mode": "test", "success_url": "http://127.0.0.1:8000/success", }
class PayuSuccessAPiView(GenericAPIView): """ Class for creating API view for Payment Success. """ serializer_class = TransactionDetailsSerializer def post(self, request): """ Function for Payment Success. """ serializer = self.get_serializer(data=request.data) data = {k: v[0] for k, v in dict(request.data).items()} if serializer.is_valid(): serializer.save() response = payu.verify_transaction(data) return JsonResponse(response)
After successful payment, we have also stored payment transaction details to the database using serializers as shown in the above code.
Handling failure is the last step in the transaction process. In this process after the transaction failed then we need to call api view of transaction failure.
PAYU_CONFIG = { "merchant_key": "********", "merchant_salt": "********", "mode": "test", "failure_url": "http://127.0.0.1:8000/failure", }
class PayuFailureAPiView(GenericAPIView): """ Class for creating API view for Payment Failure. """ @csrf_exempt def post(self, request): """ Function for Payment Failure. """ data = {k: v[0] for k, v in dict(request.data).items()} response = payu.verify_transaction(data) return JsonResponse(response)
Here, we have also added the PayU method to .verify the transaction to check whether transactions are failed or not
This blog covered how to design a payment gateway with good UI and how we can add different fields in it, also how to use templates in Django, and it also gives us ideas about how to create checkout and generate hash keys and also covered how to handle transaction success and failure.
view.py
from django.http import JsonResponse from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt from django.conf import settings from paywix.payu import Payu from rest_framework.generics import GenericAPIView from .serializers import TransactionDetailsSerializer payu_config = settings.PAYU_CONFIG merchant_key = payu_config.get('merchant_key') merchant_salt = payu_config.get('merchant_salt') surl = payu_config.get('success_url') furl = payu_config.get('failure_url') mode = payu_config.get('mode') payu = Payu(merchant_key, merchant_salt, surl, furl, mode) def home(request): print("request", request) return render(request, 'home.html') class PayuDemoAPiView(GenericAPIView): """ Class for creating API view for Payment. """ def get(self, request): """ Function for Payment. """ data = {'amount': '10', 'firstname': 'rishikesh', 'email': 'rishidevkate@gmail.com', 'phone': '7276034203', 'productinfo': 'test', 'lastname': 'test', 'address1': 'test', 'address2': 'test', 'city': 'test', 'state': 'test', 'country': 'test', 'zipcode': 'tes' } data.update({"txnid": "123456789"}) payu_data = payu.transaction(**data) return render(request, 'payu_checkout.html', {"posted": payu_data}) class PayuSuccessAPiView(GenericAPIView): """ Class for creating API view for Payment Success. """ serializer_class = TransactionDetailsSerializer def post(self, request): """ Function for Payment Success. """ serializer = self.get_serializer(data=request.data) data = {k: v[0] for k, v in dict(request.data).items()} if serializer.is_valid(): serializer.save() response = payu.verify_transaction(data) return JsonResponse(response) class PayuFailureAPiView(GenericAPIView): """ Class for creating API view for Payment Failure. """ @csrf_exempt def post(self, request): """ Function for Payment Failure. """ data = {k: v[0] for k, v in dict(request.data).items()} response = payu.verify_transaction(data) return JsonResponse(response)
templates/checkout.html
<html> <head> <title>Loading...</title> </head> <body onload="document.payuForm.submit()"> <form action={{ posted.action }} method="post" name="payuForm"> {% csrf_token %} <input type="hidden" name="key" value="{{posted.key}}" /> <input type="hidden" name="hash_string" value= "{{ posted.hash_string }}" /> <input type="hidden" name="hash" value="{{ posted.hashh }}" /> <input type="hidden" name="posted" value="{{ posted }}" /> <input type="hidden" name="txnid" value="{{ posted.txnid }}" /> <input type="hidden" name="amount" value="{{ posted.amount |default:'' }}" /></td> <input type="hidden" name="firstname" id="firstname" value="{{ posted.firstname|default:'' }}" /></td> <input type="hidden" name="email" id="email" value= "{{ posted.email|default:'' }}" /></td> <input type="hidden" name="phone" value=" {{ posted.phone|default:'' }}" /></td> <textarea type="hidden" name="productinfo" style="display:none;">{{ posted.productinfo|default: '' }} </textarea> </td> <input type="hidden" name="surl" value="{{ posted.surl }}" size="64" /></td> <input type="hidden" name="furl" value="{{ posted.furl }}" size="64" /></td> <input type="hidden" name="service_provider" value=" {{posted.service_provider}}" size="64" /> <input type="hidden" name="lastname" id="lastname" value="{{ posted.lastname }}" /></td> <input type="hidden" name="address1" value=" {{ posted.address1 }}" /></td> <input type="hidden" name="address2" value=" {{ posted.address2 }}" /></td> <input type="hidden" name="city" value=" {{ posted.city }}" /></td> <input type="hidden" name="state" value=" {{ posted.state }}" /></td> <input type="hidden" name="country" value=" {{ posted.country }}" /></td> <input type="hidden" name="zipcode" value=" {{ posted.zipcode }}" /></td> <input type="hidden" name="udf1" value=" {{ posted.udf1 }}" /></td> <input type="hidden" name="udf2" value=" {{ posted.udf2 }}" /></td> <input type="hidden" name="udf3" value=" {{ posted.udf3 }}" /></td> <input type="hidden" name="udf4" value=" {{ posted.udf4 }}" /></td> <input type="hidden" name="udf5" value=" {{ posted.udf5 }}" /></td> </form> </body> </html>
The team at Mindbowser was highly professional, patient, and collaborative throughout our engagement. They struck the right balance between offering guidance and taking direction, which made the development process smooth. Although our project wasn’t related to healthcare, we clearly benefited...
Founder, Texas Ranch Security
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork