Skip to content

Instantly share code, notes, and snippets.

@cnicodeme
Created February 6, 2023 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnicodeme/982d5cd7391a89b3b8d847c0bb08d458 to your computer and use it in GitHub Desktop.
Save cnicodeme/982d5cd7391a89b3b8d847c0bb08d458 to your computer and use it in GitHub Desktop.
Generate a list of incomes and Stripe fees, per months
import requests, datetime
STRIPE_PRIVATE_KEY='' # Indicate your Stripe private key here
params = {
'limit': 100,
'created[gte]': int(datetime.datetime(year=2022, month=1, day=1, hour=0, minute=0, second=0).timestamp()) # if you want to filter starting in the year 2022
}
operations = {}
while True:
response = requests.get('https://api.stripe.com/v1/balance_transactions', auth=(STRIPE_PRIVATE_KEY, ''), params=params)
body = response.json()
for entry in body.get('data'):
created = datetime.datetime.fromtimestamp(entry.get('created'))
created_str = created.strftime('%Y-%m')
if created_str not in operations:
operations[created_str] = {'amount': 0, 'fees': 0}
if entry.get('type') in ('charge', 'refund'):
operations[created_str]['amount'] += entry.get('amount') / entry.get('exchange_rate')
operations[created_str]['fees'] += entry.get('fee') / entry.get('exchange_rate')
elif entry.get('type') in ('payout', 'stripe_fee'):
continue
else:
print(entry.get('type'))
params['starting_after'] = entry.get('id')
if not body.get('has_more'):
break
for dt in operations:
print(dt)
print(' * Amount ${:.2f} USD'.format(operations[dt]['amount'] / 100))
print(' * Fees ${:.2f} USD'.format(operations[dt]['fees'] / 100))
print('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment