I was currently asked to implement a back-end feature which was payment verification of the Khalti payment gateway in an API. I did not find enough resources with clear information except for Khalti Docs which helped me understand the basic and logical concepts of integrating Khalti into Web apps. So, I learned and created a reusable Django app named django-khalti to make it very easy for all of us to integrate Khalti into our Django project.
In this post, I will show you how easy it is to implement Khalti Payment Gateway with Django. I presume the reader knows python, basic Django and API. I will be using the Django Rest Framework and django-khalti to demonstrate the implementation. Integrating Khalti Payment to your Django project requires following steps:
- First, create a merchant account at Khalti Payment Gateway and get your test secret API keys.
- Then create payment at the front-end of your application through SDKs provided by Khalti Docs.
SDKs available are:
–Web SDK
–Android SDK
–iOS SDK - These SDKs provide this type of response on success:
{ “idx”: “8xmeJnNXfoVjCvGcZiiGe7”,
“amount”: 1000,
“mobile”: “98XXXXX969”,
“product_identity”: “1234567890”,
“product_name”: “Dragon”,
“product_url”: “http://gameofthrones.wikia.com/wiki/Dragons“, “token”:“QUao9cqFzxPgvWJNi9aKac”
} - Now, pass the amount and token data responded by Khalti SDK on step 3 to our API, which we will create using Django REST framework and django-khalti in this tutorial, in order to verify the payment.
So, let’s get started with the tutorial.
django-khalti is a Django app based on Django REST framework to conduct server-based payment verification of Khalti payment gateway. For each payment, it can verify and return status of payment through transaction token and paid amount.
- Install the django-khalti and Django REST framework like this:
pip install djangorestframework pip install django-khalti
- Add “django-khalti” and “djangorestframework” to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ … ‘rest_framework’, ‘khalti’, ]
- Include the django-khalti URLconf in your project urls.py like this:
path(‘khalti/’, include(‘khalti.urls’)),
- Add Khalti Merchant API key and Verify URL in your settings.py:
KHALTI_SECRET_KEY = “<your api key>” KHALTI_VERIFY_URL = “https://khalti.com/api/v2/payment/verify/"
- Test your endpoints like this:
POST: /khalti/verifypayment/ body:{ 'token':<transaction token>, 'amount':<transaction amount> }
- You will get responses like this:
On Success: { 'status':True, 'details':{ "idx": "8xmeJnNXfoVjCvGcZiiGe7", "type": { "idx": "e476BL6jt9kgagEmsakyTL", "name": "Wallet payment" }, "state": { "idx": "DhvMj9hdRufLqkP8ZY4d8g", "name": "Completed", "template": "is complete" }, "amount": 1000, "fee_amount": 30, "refunded": false, "created_on": "2018-20T14:48:08.867125+05:45", "ebanker": null, "user": { "idx": "cCaPkRPQGn5D8StkiqqMJg", "name": "Test User", "mobile": "98XXXXXXX9" }, "merchant": { "idx": "UM75Gm2gWmZvA4TPwkwZye", "name": "Test Merchant", "mobile": "testmerchant@khalti.com" } } } On Error: { 'status':False, 'details':{'token': ['Invalid token.']} }
That’s all. It’s this easy to integrate Khalti payment gateway into your Django application.
Aashish Dhakal,
CE III/I,
Kathmandu University