Khalti Payment Gateway Integration With Django

Khalti Payment Gateway Integration With Django

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

Are you Alive?

Are you Alive?

The topic is creepy and that’s why you are here. Perhaps you could answer it or can you? It’s the most reasonable existential question that should bother you, instead of facing an existential crisis just by looking at those messages your crush didn’t even bother to open up. The question itself is doing a great job by striking right in your self-esteem, perhaps even cursing me for being such a jerk who now questions your existence.

‘O boy, that’s an obvious thing right there, isn’t it? Come on now, what are you telling me, that I ain’t alive? that I breathe and my heart beats, and I fulfill every point that a grade five science textbook describes,” What living things do” and there you are, questioning that very thing. That’s completely a nonsensical question.’

I recall the TV ad for soap Cinthol. And guess what, the tagline was cool, ”Alive is Awesome”. Oh, all frustrated and depressed guys could sue Cinthol. Not everyone’s life is joyful. Voila! You should try that soap. Perhaps it might make you awesome. Nonetheless, there is no such lie in soap that could neutralize the boring and pitiful lives most of us are having. ‘Hold on! I am not having a pitiful life. How dare you say that?’ Well, ask yourself. We all are organic machines programmed with intelligence. We are routined with contemporary civilization. You and I follow the very thing like most of our colleagues in this earthly office, let’s forget about those in ISS. Not that ISS, International Space Station, you fool. They abandoned us to live in these noises, so let’s not include them.

The routine is obvious. You are conceived and born, even if you didn’t ask for it. That’s the way it is with gods, I guess. You eat, you survive and then you die. That hasn’t changed for millions of years. Modern Humans have a lot to worry about between these events. The worries for food, predators, and shelter are replaced by enormous psychological titanosaurs. You crave dollars, power, fame, career, sex, knowledge, and rush every day to achieve something more. You don’t work for your satisfaction, instead, you work for money, power, and career. You don’t study for knowledge, but for the sake of accomplishments. I am no different, either.

There is always someone whom you idealize and wish your life was as good as his. ‘That’s right. I want a life like Richie Rich, Sexiest Sexy, highly Famous, Trumpet Trump, or Happiest Happy.’ Let’s not hop into fantasies right now. Let’s talk about someone closer not whom you want to be but envy his super chilling chills. What about the lazy neighbor’s son who according to you is the most chill man alive. He doesn’t study, doesn’t do any work. He eats, sleeps, and spends his days like, there is nothing to worry about. He is Lord Shiva with heavenly psychedelics. He is Jesus with Mark Manson’s Books instead of the Holy Bible. He is Buddha with no care for enlightenment. He is Allah with pervert Mohammad instead of prophet Mohammad. In nutshell, he lives.

But does he? His mind is a battlefield worse than Pearl Harbour. His life is joyous to others but a nightmare to him. In between the false relaxation, the questions ‘What am I doing with my life? Who am I?’ haunts his subconscious mind. In one way or another, we all have asked ourselves such questions with piles of self-doubt and reason for unhappiness. Even if you are following everything Manson said-“I ain’t giving no fucks. Well, What is it? Is it, you not being the ideal lover or an ideal child or an ideal parent or ideal whatever?” Is it the will or pressure to achieve polished stature in society than your friend over there? Is it the will or pressure to be an outstanding and lustrous man in the crowd? Everyone knows there is competition everywhere. Whether it’s with the animals or between us humans for adaptation. You survive to impress others.

Amygdala is responsible for multiple emotional responses, like love, fear, anger, and sexual desire. The evolution of the amygdala in humans is one of the reasons we are called social animals: by ourselves. Society is cool. Biological parents are emotionally attached to their offspring. You love your crush and you hate that ill-minded moron. Thanks to Amygdala. But it is due to such parts of our brain that we need therapists to listen to us while we break down our brain in their cozy couch. Americans are ahead of civilization so the therapeutic business is booming there. An underdeveloped country like us is so far in the chain that most of us don’t even know that the worries in our head and our sleepless nights could be cured in those magical couches. There is a great prospect in therapeutic business as long as people realize something is wrong in the amygdala. God, I should have approached Psychology instead of Engineering in school.

I think the Stone age was better than today. Just worry about fire, food, and the claws and sharp teeth of predators and enjoy the metabolism. But then, who would question life?. The question is not some personalizing self-enriching question as productivity writers would creatively misplace them in their fancy books. What counts as being alive? Being happy? Well, that could be a point but dopamine and serotonin injected in an organic robot wouldn’t make it more alive than us. What is it that makes us humans human? If being alive is just to perform biological acts, then we are no better than a louse connected to a powerful microprocessor. Do you really think you are alive? Do you think that you were alive the moment your soulmate kissed you? Did you feel alive when you became the reason for tears of joy in the eyes of the people who matter the most to you? Did you feel breathing when you reached the top after hours of hiking? What about the moment when you excreted after holding it for too long? And there are certainly many such instances when you really feel the breath, the heart’s pumps, and the simple existence in the vast voids of nothingness. Those instances were very momentary and played a crucial role in reminding our processors that this very instance of our time is not as alive as those instances. Alright then, is it those beautiful memories that we cherish, and such moments of true joy make us alive? Is it only the good things that trigger your life? Does your existence solely depend upon those sparks of lifeless electrons that make you feel good? Is being alive nothing but ‘not being alive’? What is alive? I will leave you with the very question that dragged you in this pitiful article.

Why ache your head when you can binge-watch resting on the cozy couch and who cares about the existence? Why should I give my opinion or anything at all? If these questions are popping up in your head then congrats buddy, the doomsday clock is ticking and the existential crisis that you easily left undealt in the corner of your brain, right beneath the comfort zone, needs to be excavated. You could let those hormones fool you and rely totally on gods, or you could dig up the grave while there is still time and pave your way to the afterlife and beyond. There are many things to question and time is relative, you may say.

Let’s not put this aside again and think about it cause it’s your life at stake. Question yourself: Are You Alive? Where do you stand in this vast universe? Is death even real if you’re not alive in the first place? Why are you ‘YOU’? Let us be our own Buddha.

Sagar Paudel,
CE II/I,
Kathmandu University

Thought-provoking questions about the Future of Technology raised by Movies

Thought-provoking questions about the Future of Technology raised by Movies

In the distant future, the interactions between human and computer may be vastly different than it is now. With the emergence of technology such as Artificial Intelligence (AI) and cloud computing, the world will not be the same again. In this article, the term AI is an artistic representation of AI, which is to say they are conscious and ever learning as humans. The following are some movies that portray the future and trigger the brain to raise questions about future technology.

Movies such as Her, Blade Runner, and the episode ‘San Junipero’ of the TV series Black Mirror have tried to imagine the future artistically and show a representation of how the story of humans and tech may turn out to be. They each have their own version of the future, ranging from optimistic and joyous to dark and gritty. These are all fictitious stories, and the future may not be the one portrayed in these stories.

In the movie Her, the protagonist falls in love with an AI and vice versa. They do not have a conventional relationship and thus face many problems. This idea of interconnection between AI and human is of noble emergence in itself and raises many questions. Is AI even capable of growing human emotions? Will they ever do in the future? Can we actually program emotions into lines of code?

The movie Blade Runner has many thought-provoking ideas as well. In the movie, the protagonist and his significant other are not human. They also share the feeling that their relationship is not the same as a normal human relationship. In the future, we may have to consider AI rights and to what level do these rights come to. Are AI rights equal to human rights? Do they have the right to love or reproduce? Or do they have the fundamental right to exist like humans?

‘San Junipero’ deals with a technology that allows the human mind to be uploaded to a cloud where people can choose to be in. The people in the cloud do not have a body due to various reasons such as diseases or disabilities, and the only physical space they occupy is a storage device. Now, with this technology, the difference between an AI and a human is blurred even further. Both of them do not have physical bodies and exist only electronically. Are they human anymore? Do they retain the rights they had as humans?

I do not know the answers to these questions, only time will tell if these works of fiction become a reality or not. Regardless, these are important questions people have to ask while developing technology if they are to be anywhere close to these works of fiction.

Ayush Bajracharya,
CS II/I,
Kathmandu University