Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save adamjstevenson/5674899b7b0a587211bd2d0de3056066 to your computer and use it in GitHub Desktop.
Save adamjstevenson/5674899b7b0a587211bd2d0de3056066 to your computer and use it in GitHub Desktop.
List created future transfers and balance available by date on a connected account
require 'stripe'
# Your platform's secret key
Stripe.api_key = "YOUR-SECRET-KEY"
connected_account = "CONNECTED-ACCOUNT-ID"
now = Time.now.to_i
# List `transfer.created` events on the connected account
events = Stripe::Event.list({type: "transfer.created", limit: 100},{stripe_account: connected_account})
# Iterate through each event
events.each do | event |
# Inspect each transfer, looking for created transfers with expected pay dates in the future
transfer = event.data.object
if transfer.date >= now
# Format the date nicely
formatted_date = Time.at(transfer.date).getutc.strftime("%m/%d/%Y")
# Output the transfer id, amount, and expected deposit date
puts "Transfer Available #{formatted_date}"
puts "#{transfer.id},#{transfer.amount}"
end
end
# Retrieve transactions with an available_on date in the future
transactions = Stripe::BalanceTransaction.all({limit: 100, available_on: {gte: now}},{stripe_account: connected_account})
balances = Hash.new
# Iterate through transactions and sum values for each available_on date
transactions.auto_paging_each do |txn|
if balances.key?(txn.available_on)
balances[txn.available_on] += txn.net
else
balances[txn.available_on] = txn.net
end
end
# Sort the results
balances = balances.sort_by {|date,net| date}
# Output the available dates and net amounts
balances.each do |date,net|
# Format the date nicely
formatted_date = Time.at(date).getutc.strftime("%m/%d/%Y")
# Only output non-zero balances
unless net.eql?(0)
puts "Balance Available on #{formatted_date}"
puts net
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment