Complete implementation of fetch_stripe_bills

This commit is contained in:
PCoder 2019-04-03 06:12:48 +02:00
commit 6d42f88be1
3 changed files with 97 additions and 27 deletions

View file

@ -123,23 +123,42 @@ class StripeUtils(object):
return card_details
@handleStripeError
def get_all_invoices(self, customer_id):
invoices = stripe.Invoice.list(limit=100, customer=customer_id)
def get_all_invoices(self, customer_id, created):
return_list = []
for invoice in invoices:
invoice_details = {
'created': invoice.created,
'receipt_number': invoice.receipt_number,
'invoice_number': invoice.number,
'date_paid': invoice.date_paid,
'period_start': invoice.period_start,
'period_end': invoice.period_end,
'paid': invoice.paid,
'billing_reason': invoice.billing_reason,
'discount': invoice.discount.coupon.amount_off if invoice.discount else 0,
'total': invoice.total
}
return_list.append(invoice_details)
has_more_invoices = True
starting_after = False
while has_more_invoices:
if starting_after:
invoices = stripe.Invoice.list(
limit=10, customer=customer_id, created=created
)
else:
invoices = stripe.Invoice.list(
limit=10, customer=customer_id, created=created,
starting_after=starting_after
)
has_more_invoices = invoices.has_more
for invoice in invoices.data:
invoice_details = {
'created': invoice.created,
'receipt_number': invoice.receipt_number,
'invoice_number': invoice.number,
'paid_at': invoice.status_transitions.paid_at if invoice.paid else 0,
'period_start': invoice.period_start,
'period_end': invoice.period_end,
'billing_reason': invoice.billing_reason,
'discount': invoice.discount.coupon.amount_off if invoice.discount else 0,
'total': invoice.total,
# to see how many line items we have in this invoice and
# then later check if we have more than 1
'lines_data_count': len(invoice.lines.data),
'invoice_id': invoice.id,
'lines_meta_data_csv': ','.join(
[line.metadata.VM_ID for line in invoice.lines.data]
)
}
starting_after = invoice.id
return_list.append(invoice_details)
return return_list
@handleStripeError