Table of Contents
Get all invoices from Stripe
import stripe
import requests
### Global Variables
USER = os.getenv("USER")
stripe.api_key = os.getenv("STRIPE_API_KEY")
invoices_folder = '/path/where/invoices/are/saved'
existing_invoices = os.listdir(invoices_folder)
existing_invoices_ids = []
print(f"\nExisting invoices:")
for inv in existing_invoices:
print(inv)
if inv.endswith(".pdf"):
parts = inv.split('_')
id = parts[1]
existing_invoices_ids.append(id)
all_invoices = stripe.Invoice.list()
for invoice in all_invoices:
number = invoice.number
print(number)
if number not in existing_invoices_ids:
count += 1
amount = round(float(invoice.amount_due/100),2)
amount_int = int(amount)
currency = invoice.currency.upper()
pdf = invoice.invoice_pdf
invoice_file = requests.get(pdf)
customer = invoice.customer_name
if 'xxx' in customer:
customer = 'xxx'
if 'yyy' in customer:
customer = 'yyy'
created = invoice.created
created_date = datetime.utcfromtimestamp(created).strftime('%Y-%m-%d')
filepath = f"{invoices_folder}/{created_date}_{number}_{customer}_{amount_int}_{currency}.pdf"
with open(filepath, 'wb') as f:
f.write(invoice_file.content)
print(amount)
print(amount_int)
print(currency)
print(customer)
print(created_date)
print(pdf)
print()