From 8dc00c9dd9040d1921bee56c3a67fd908def5ee3 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 2 Apr 2019 09:18:46 +0200 Subject: [PATCH] Add management command --- .../management/commands/fetch_stripe_bills.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 hosting/management/commands/fetch_stripe_bills.py diff --git a/hosting/management/commands/fetch_stripe_bills.py b/hosting/management/commands/fetch_stripe_bills.py new file mode 100644 index 00000000..3a06a1d9 --- /dev/null +++ b/hosting/management/commands/fetch_stripe_bills.py @@ -0,0 +1,28 @@ +from django.core.management.base import BaseCommand + +from hosting.models import UserCardDetail +from membership.models import CustomUser +from utils.stripe_utils import StripeUtils + + +class Command(BaseCommand): + help = '''Fetches invoices from Stripe and creates bills for a given + customer in the MonthlyHostingBill model''' + + def add_arguments(self, parser): + parser.add_argument('customer_email', nargs='+', type=str) + + def handle(self, *args, **options): + try: + for email in options['customer_email']: + stripe_utils = StripeUtils() + user = CustomUser.objects.get(email=email) + if hasattr(user, 'stripecustomer'): + self.stdout.write(self.style.SUCCESS('Found %s. Fetching bills for him.' % email)) + stripe_utils.get_all_invoices( + user.stripecustomer.stripe_id + ) + else: + self.stdout.write(self.style.SUCCESS('Customer email %s does not have a stripe customer.' % email)) + except Exception as e: + print(" *** Error occurred. Details {}".format(str(e)))