E
E
energimass2020-08-27 17:33:44
MySQL
energimass, 2020-08-27 17:33:44

How to implement an hourly write-off of the user's balance?

The site has a users table with fields: username, password, balance, and so on.
users has an associated services table containing service_name, tarif, status, updated_at, and more.
If the user publishes the service, the status changes to show and the updated_at time changes accordingly. How, with such a structure, to organize an hourly write-off of funds from the user's balance according to the tariff in the services table? It seems clear, but let's say the script runs every hour at hh-hours:00-minutes, and one of the users added the service 2 minutes ago, he will be charged as an hour.
How do they do such things, maybe something needs to be changed radically?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Barmunk, 2020-08-27
@energimass

You don't need to be tied to absolute time. Each user can have their own payment time and how many minutes they can see the content (eat 2 columns, payment time, service expiration time). Then, once every n minutes, you check all users whose tariff has expired and close the service. If the script cannot overpower the passage of all clients once a minute, increase the time and write the error in the tariff.
I would also advise not to use one "balance" field, sooner or later you will run into deadlocks, plus erroneous charges are very likely. It is best to use the debit-credit system, where the entire history of accrual or debit is written, and you calculate the total amount in the wallet dynamically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question