Complete implementation of fetch_stripe_bills
This commit is contained in:
parent
8dc00c9dd9
commit
6d42f88be1
3 changed files with 97 additions and 27 deletions
|
|
@ -1,9 +1,13 @@
|
|||
import logging
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from hosting.models import UserCardDetail
|
||||
from hosting.models import MonthlyHostingBill
|
||||
from membership.models import CustomUser
|
||||
from utils.stripe_utils import StripeUtils
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = '''Fetches invoices from Stripe and creates bills for a given
|
||||
|
|
@ -18,11 +22,32 @@ class Command(BaseCommand):
|
|||
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
|
||||
self.stdout.write(self.style.SUCCESS(
|
||||
'Found %s. Fetching bills for him.' % email))
|
||||
mhb = MonthlyHostingBill.objects.last(
|
||||
customer=user.stripecustomer
|
||||
)
|
||||
created_gt = {}
|
||||
if mhb is not None:
|
||||
# fetch only invoices which is created after
|
||||
# mhb.created, because we already have invoices till
|
||||
# this date
|
||||
created_gt = {'gt': mhb.created}
|
||||
|
||||
all_invoices_response = stripe_utils.get_all_invoices(
|
||||
user.stripecustomer.stripe_id,
|
||||
created=created_gt
|
||||
)
|
||||
all_invoices = all_invoices_response['response_object']
|
||||
logger.debug(
|
||||
"Obtained {} invoices".format(len(all_invoices))
|
||||
)
|
||||
for invoice in all_invoices:
|
||||
MonthlyHostingBill.create(
|
||||
invoice, stripe_customer=user.stripecustomer
|
||||
)
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS('Customer email %s does not have a stripe customer.' % email))
|
||||
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)))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue