uncloud-mravi/uncloud_pay/management/commands/charge-negative-balance.py
amal b7aa1c6971 - Added PricingPlan Model
- Implement a complete cycle for buying a Matrix Chat Host
- Refactor the Payement cycle and stripe related methods
2021-07-19 16:36:10 +02:00

28 lines
1.1 KiB
Python

from django.core.management.base import BaseCommand
from uncloud_auth.models import User
from uncloud_pay.models import Order, Bill, get_balance_for_user
import uncloud_pay.stripe as uncloud_stripe
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:
balance = get_balance_for_user(user)
if balance < 0:
print("User {} has negative balance ({}), charging.".format(user.username, balance))
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)
)
print("=> Done.")