duongtd's avatar

Development of an eWallet system for eCommerce application

So, I have a most basic handy crafted eCommerce application which was running smooth times ago. And now I would like to implement an eWallet feature for the site. As we are mostly saw it before, for example the billing in Digital Ocean: you deposit some funds as credit, and use it to create Droplets. So, the eWallet is what it has been named, it is an wallet which can provide features like these:

  • User can deposit funds into their own account
  • User can make purchases using the wallet balance
  • User can withdraw their funds
  • User can view all transaction history in their account
  • ...etc

However, there is not much information about development of this feature, and I was also unable to find any normalized business model (or maybe I was just not googled enough?). So I think i will have to implemented it by myself. And while googling for the development process I see a lot of people are also want to create this feature so I think I should create a discussion here, and we can together build the feature. So may we begin?

Model

I am consider between 2 options to provision the model. First is group all the deposit, withdraw... in to the Transaction, and secondly is separate them into some Model itself.

OPTION 1:
  • Wallet model:
    • user_id: foreign key to the users
    • balance: holding information of current wallet balance
  • Transaction model:
    • wallet_id: foreign key to the wallet
    • amount: the value of funds
    • type: array('deposit', 'withdrawal'...)
OPTION 2:
  • Wallet model:
    • user_id: foreign key to the users
    • balance: holding information of current wallet balance
  • Deposit model:
    • wallet_id: foreign key to the wallet
    • amount: the value of funds
  • Withdrawal model:
    • wallet_id: foreign key to the wallet
    • amount: the value of funds

Okay, it what i am thinking right now, and let me explain about the balance. I will have a balance field which holding information of current wallet balance which I can calculate it dynamically, on the fly. But imagine if you have about thousands of transactions, and whenever you want to access the balance it will need to calculate again, and again. And it will slow your application down to the hell.

Okay, this is enough for now, I will need your idea and suggestion of which model option should i use before continue.

Thank you for your time!

0 likes
5 replies
JoerJoers's avatar
Level 8

I have the same problem as well but i decided to go to the Option 1 because basically, what is the point of having two separate tables (Deposit and Withdrawal) if they have the same columns? So having the Transaction Model with the Transaction Type (Withdrawal, Deposit) is a perfect choice. Another good point is the process / performance when you try to retrieve all the transactions and sum it up. Instead of including the two tables in the query, you'll just need to sum the amounts and retrieve all transaction history in one table. It is also logically correct to make the amount of the Withdrawal to negative (-) and Deposit to positive (+).

And for the Balance field in the Wallet Mode, i totally agree in your approach. In fact this was also what i did. So every transaction that will happen, the balance in the wallet will be updated.

2 likes
duongtd's avatar

@JoerJoers yeah i was overthinking about separating the model, in fact I want to have some more features such as: transfer fund from an user to another. But i should ended up with the option 1.

JoerJoers's avatar

@duongtd Do you mind marking the answer as the correct one? For the benefit of others as well.

sanjay23's avatar

@JoerJoers - Is it possible to store some other references too, like for product purchase, service charges pay via only my wallet to another provider wallet. So would you mind if you post your thoughts on this too?

cyberorca's avatar

I assume you used Mysql, so then you can trigger update on the fly by creating mysql 'trigger' statement every time you insert data on your deposit or withdrawal table, ex :

CREATE TRIGGER trig_balance_deposit AFTER INSERT ON Deposit FOR EACH ROW UPDATE Wallet SET balance = balance + NEW.amount WHERE NEW.user_id = Wallet.user_id;

CREATE TRIGGER trig_balance_withdrawal AFTER INSERT ON Withdrawal FOR EACH ROW UPDATE Wallet SET balance = balance + NEW.amount WHERE NEW.user_id = Wallet.user_id;

Please or to participate in this conversation.