top
Kickstart the New Year with exciting deals on all courses Use Coupon NY15 Click to Copy

Creating Your First Offline Payment Wallet With Django (Part-3)

In part-2 of this article, we have successfully created a login system for the users now let us work on providing the main functionality which is the wallet management and get things working.Let us look into the steps for Django offline wallet managementStep 1:  Display the balance of each personThe very first thing before starting on with the transaction is the need to display the balance of each person which we will be storing in a table, so let’s create it, in accounts/models.py we create a model called balance.from django.contrib.auth.models import User class balance(models.Model):         user = models.ForeignKey(User)         balance = models.IntegerField(default = 0) Notice that Username will be the ForeignKey as we are mapping balance with the given user. When we have successfully created the table now we migrate, on terminal we writepython manage.py makemigrations python manage.py migrate Step 2:  Display the balance of a particular userOur next step will be to display the balance of a particular user in the profile dashboard which we have created in the previous part, now let us write a query to fetch balance for the user and send it to the front-end for displaying it. Below is the updated code in accounts/views.py to achieve this.from models import balance def profile(request):      user = balance.objects.get(user=request.user      return render(request,'profile.html',{"balance":user.balance})Please note that we are fetching the balance of the user in from the database at the time of showing the profile but we have not saved it into database till now so this may break our code so to make this work we have to insert this record into the balance table at the time of profile creation of the user in the signup function so we add the following lines to the code inside the if block in the signup function which will set the default balance of the user to 0 initially in the balance model.instance = balance(user = request.user, balance = 0) instance.save()Step 3:  Display the balance to the userNow we have to display the balance to the user, so to do this we just have to display the variable balance which we have sent int the profile function in profile.html.Balance: {{balance}} Adding above line of code in the file should now display the balance of the user on logging in.Step 4:  Setting up offline paymentWe are able to display the balance to the user, Now we come to our last step of setting up offline payment with Django, wherein the user can transfer an amount from his wallet to wallet to some other user which will lead to deduction of the amount from the wallet of the user and addition of amount in the wallet of another user, so let us try to implement it.Lets start with the front-end where we make a form where the user can insert the username and amount for making the transfer. So we add following lines to profile.html.<form method="post"> <input type="text" name="username" placeholder="Enter Username"> <input type="text" name="amount" placeholder="Enter Amount"> <input type="submit" value="Submit"> </form> Now we again refresh the page to see the following pageLet’s write the back-end for handling this form now, as discussed earlier when a transfer request is made we have to deduct the amount from the user and put it in wallet of some other user, which is just a matter of few queries, lets see the code for thisdef profile(request):  msg=""  if request.method == "POST":    try:      username = request.POST["username"]      amount = request.POST["amount"]      senderUser= User.objects.get(username=request.user.username)      receiverUser=User.objects.get(username=username)      sender = balance.objects.get(user = senderUser)      receiver = balance.objects.get(user = receiverUser)                 sender.balance = sender.balance - int(amount)      receiver.balance = receiver.balance + int(amount)      sender.save()      receiver.save()      msg = "Transaction Success"    except Exception as e:      print(e)      msg = "Transaction Failure, Please check and try again"  user = balance.objects.get(user=request.user)  return render(request,'profile.html',{"balance":user.balance,"msg":msg})In the above code in the try block we are getting the form data from the user try to search for the credentials for the receiver and the sender and fetch their balance from the balance table and add the amount to be transferred from to the receiver’s wallet and deduct the same from the sender’s wallet and we return the transaction message to the profile.html which can be displayed in the same way as discussed earlier. That concludes this part of the article, the complete code for this can be found on this repository , please feel free to make any issue or pull request, Further steps for Implementing offline payment gateway in Django may include:Adding transaction history page for a particular user which can be done by saving the transactions history in a new table having fields sender, receiver and amount.Display more accurate error messages on transaction failure.Adding features like notification and messaging in the profile dashboard.Making the pages look good using CSS and Bootstrap.Any other enhancements you may think up.Feel free to fork and contribute to the repository and leave a comment here if help is needed. That is all for this article, I hope this article helped you to learn how to set up an offline payment with Django. Thanks for reading.
Rated 4.5/5 based on 12 customer reviews
Normal Mode Dark Mode

Creating Your First Offline Payment Wallet With Django (Part-3)

Pratik Singh Chauhan
Blog
06th Feb, 2019
Creating Your First Offline Payment Wallet With Django  (Part-3)

In part-2 of this article, we have successfully created a login system for the users now let us work on providing the main functionality which is the wallet management and get things working.

Let us look into the steps for Django offline wallet management

Step 1:  Display the balance of each person

The very first thing before starting on with the transaction is the need to display the balance of each person which we will be storing in a table, so let’s create it, in accounts/models.py we create a model called balance.

from django.contrib.auth.models import User
class balance(models.Model):
        user = models.ForeignKey(User)
        balance = models.IntegerField(default = 0)

Notice that Username will be the ForeignKey as we are mapping balance with the given user. When we have successfully created the table now we migrate, on terminal we write

python manage.py makemigrations
python manage.py migrate


Step 2:  Display the balance of a particular user

Our next step will be to display the balance of a particular user in the profile dashboard which we have created in the previous part, now let us write a query to fetch balance for the user and send it to the front-end for displaying it. Below is the updated code in accounts/views.py to achieve this.

from models import balance
def profile(request):
     user = balance.objects.get(user=request.user
     return render(request,'profile.html',{"balance":user.balance})


Please note that we are fetching the balance of the user in from the database at the time of showing the profile but we have not saved it into database till now so this may break our code so to make this work we have to insert this record into the balance table at the time of profile creation of the user in the signup function so we add the following lines to the code inside the if block in the signup function which will set the default balance of the user to 0 initially in the balance model.

instance = balance(user = request.user, balance = 0)
instance.save()


Step 3:  Display the balance to the user

Now we have to display the balance to the user, so to do this we just have to display the variable balance which we have sent int the profile function in profile.html.

Balance: {{balance}}

Adding above line of code in the file should now display the balance of the user on logging in.

Step 4:  Setting up offline payment

We are able to display the balance to the user, Now we come to our last step of setting up offline payment with Django, wherein the user can transfer an amount from his wallet to wallet to some other user which will lead to deduction of the amount from the wallet of the user and addition of amount in the wallet of another user, so let us try to implement it.

Lets start with the front-end where we make a form where the user can insert the username and amount for making the transfer. So we add following lines to profile.html.

<form method="post">
<input type="text" name="username" placeholder="Enter Username">
<input type="text" name="amount" placeholder="Enter Amount">
<input type="submit" value="Submit">
</form>

Now we again refresh the page to see the following page

Let’s write the back-end for handling this form now, as discussed earlier when a transfer request is made we have to deduct the amount from the user and put it in wallet of some other user, which is just a matter of few queries, lets see the code for this

def profile(request):
 msg=""
 if request.method == "POST":
   try:
     username = request.POST["username"]
     amount = request.POST["amount"]
     senderUser= User.objects.get(username=request.user.username)
     receiverUser=User.objects.get(username=username)
     sender = balance.objects.get(user = senderUser)
     receiver = balance.objects.get(user = receiverUser)           
     sender.balance = sender.balance - int(amount)
     receiver.balance = receiver.balance + int(amount)
     sender.save()
     receiver.save()
     msg = "Transaction Success"
   except Exception as e:
     print(e)
     msg = "Transaction Failure, Please check and try again"
 user = balance.objects.get(user=request.user)
 return render(request,'profile.html',{"balance":user.balance,"msg":msg})


In the above code in the try block we are getting the form data from the user try to search for the credentials for the receiver and the sender and fetch their balance from the balance table and add the amount to be transferred from to the receiver’s wallet and deduct the same from the sender’s wallet and we return the transaction message to the profile.html which can be displayed in the same way as discussed earlier. 

That concludes this part of the article, the complete code for this can be found on this repository , please feel free to make any issue or pull request, Further steps for Implementing offline payment gateway in Django may include:

  1. Adding transaction history page for a particular user which can be done by saving the transactions history in a new table having fields sender, receiver and amount.
  2. Display more accurate error messages on transaction failure.
  3. Adding features like notification and messaging in the profile dashboard.
  4. Making the pages look good using CSS and Bootstrap.
  5. Any other enhancements you may think up.

Feel free to fork and contribute to the repository and leave a comment here if help is needed. That is all for this article, I hope this article helped you to learn how to set up an offline payment with Django. Thanks for reading.

Pratik Singh

Pratik Singh Chauhan

Blog author

Pratik is a self taught programmer and loves to share his knowledge,from the Indian Institute of Technology, the best engineering colleges in India, is a Python and JavaScript developer and currently working on challenges in the field of machine learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE OUR BLOG

Follow Us On

Share on