2020-02-28 07:59:32 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from uncloud_auth.models import User
|
2021-07-19 14:36:10 +00:00
|
|
|
from uncloud_pay.models import Order, Bill, get_balance_for_user
|
|
|
|
import uncloud_pay.stripe as uncloud_stripe
|
2020-02-28 07:59:32 +00:00
|
|
|
|
|
|
|
from datetime import timedelta
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Generate bills and charge customers if necessary.'
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
users = User.objects.all()
|
|
|
|
print("Processing {} users.".format(users.count()))
|
|
|
|
for user in users:
|
2020-03-10 08:14:52 +00:00
|
|
|
balance = get_balance_for_user(user)
|
2020-02-28 07:59:32 +00:00
|
|
|
if balance < 0:
|
|
|
|
print("User {} has negative balance ({}), charging.".format(user.username, balance))
|
2021-07-19 14:36:10 +00:00
|
|
|
amount_to_be_charged = abs(balance)
|
|
|
|
result = uncloud_stripe.charge_customer(user, amount_to_be_charged)
|
|
|
|
if result.status != 'succeeded':
|
|
|
|
print("ERR: charging {} with method {} failed"
|
|
|
|
.format(user.username, result)
|
|
|
|
)
|
2020-02-28 07:59:32 +00:00
|
|
|
print("=> Done.")
|