Reputation: 5177
So I have a Django web application and I need to add a payment module to it.
Basically a user will prepay for a certain amount of service and this will slowly reduce over as the user uses the service. I'm wondering what is the best practice to facilitate this? I can process payments using Satchmo, but then just storing the USD value in a database and having my code interacting with that value directly seems kind of risky. Sure I can do that but I am wondering if there is a well tested solution to this problem out there already?
Upvotes: 3
Views: 576
Reputation: 2620
My language agnostic recommendation would be to make sure that the database that communicates with the web app is read only; at least for the table(s) that deal with these account balances. So, you process payments, and manage the reduction of account balances in a database that is not accessible to anyone other than you (i.e. not connected to the internet, or this web app). You can periodically take snapshots of that database and update the one that interacts with the webapp, so your users have a read copy of their balance. This way, even if a user is able to modify the data to increase their balance by a million bucks, you know that you have their true balance in a separate location. Basically, you'd never have to trust the data on the webapp side - it would be purely informational for your users.
Upvotes: 1
Reputation: 14447
I don't know about a "well-tested solution" as you put it, but I would strongly caution against just storing a dollar value in the database and increasing or decreasing that dollar value. Instead, I would advise storing transactions that can be audited if anything goes wrong. Calculate the amount available from the credit and debit transactions in the user account rather than storing it directly.
For extra safety, you would want to ensure that your application cannot delete any transaction records. If you cannot deny write permissions on the relevant tables for some reason, try replicating the transactions to a second database (that the application does not touch) as they are created.
Upvotes: 6