Created
August 29, 2017 22:16
-
-
Save dbischof/9bf89fc052141ba8466d847dd62ccc39 to your computer and use it in GitHub Desktop.
Projects/forecasts upcoming Stripe subscription payments (grouped by payment day)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections, stripe | |
from datetime import datetime | |
stripe.api_key = '' | |
print "Stripe subscription forecast" | |
last_obj = None | |
paydays = {} | |
loop = 0 | |
while True: | |
loop += 1 | |
print "Starting loop %i" % loop | |
customers = stripe.Customer.list(limit=100, starting_after=last_obj) | |
customers_processed = 0; | |
for customer in customers: | |
last_obj = customer | |
customers_processed += 1; | |
if len(customer.subscriptions.data) > 0: | |
for subscription in customer.subscriptions.data: | |
payday = datetime.fromtimestamp(subscription.current_period_end).strftime('%Y-%m-%d') #Does not account for UTC time | |
#print payday | |
if 'data' in subscription['items'] and len(subscription['items'].data) > 0: | |
#TODO: This does not factor in invoice items, only plan amount | |
amount = subscription['items'].data[0].plan.amount | |
fees = amount * 0.029 + 30 # Stripe charges 2.9% + 30 cents per transaction | |
#print amount | |
if payday in paydays: | |
paydays[payday]['amount'] += amount | |
paydays[payday]['fees'] += fees | |
paydays[payday]['count'] += 1 | |
else: | |
paydays[payday] = {'amount': amount, 'fees': fees, 'count': 1} | |
print "Processed %i customers" % customers_processed | |
if 0 == customers_processed: | |
break | |
paydays = collections.OrderedDict(sorted(paydays.items())) | |
print 'payday, count, amount, fees, net' | |
for payday, totals in paydays.iteritems(): | |
#print payday, totals | |
print "%s, %i, %0.2f, %0.2f, %0.2f" % (payday, totals['count'], totals['amount']/100.0, totals['fees']/100.0, (totals['amount']-totals['fees'])/100.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment