Last active
December 16, 2015 12:49
-
-
Save supercleanse/5437706 to your computer and use it in GitHub Desktop.
Sinatra script to output Stripe income and expenses as Outright CSV files...
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
# Requires the HTTParty, Stripe & Sinatra gems | |
require 'httparty' | |
require 'stripe' | |
require 'time' | |
require 'csv' | |
require 'sinatra' | |
# NOTE: This script also requires tht you set a ENV['LIVE_STRIPE_SECRET'] | |
# environment variable with your Live Secret Key from your Stripe Dashboard | |
# To get an income CSV file: | |
# http://example.com/stripe.csv?sd=2013-01-01&ed=2013-03-31 | |
# | |
# sd specifies the 'start date' for the range you want to output | |
# ed specifies the 'end date' for the range you want to output | |
# | |
# To get an expense CSV file: | |
# http://example.com?sd=2013-01-01&ed=2013-03-01&t=expenses | |
# | |
# t specifies the type of the file ... can either be 'income' or 'expenses' ... defaults to 'income' | |
# | |
# NOTE: You should know that due to a limitation of the Stripe API ... a maximum of 100 records can | |
# be returned in any one CSV file ... so try to make your date range small enough that it returns fewer | |
# than 100 records | |
# | |
get :stripe, :provides => :csv do | |
start_ts = Time.parse( params[:sd] ).to_i | |
end_ts = Time.parse( params[:ed] ).to_i | |
csvtype = params[:t] || 'income' | |
Stripe.api_key = ENV['LIVE_STRIPE_SECRET'] | |
charges = Stripe::Charge.all( :created => { :gte => start_ts, :lte => end_ts }, :count => 100 ) | |
csvstr = CSV.generate do |csv| | |
csv << ['Payee', 'Date', 'Amount', 'Category', 'Description'] | |
charges[:data].each do |ch| | |
next unless ch[:paid] | |
sub = '' | |
cusname = '' | |
unless ch[:customer].nil? | |
cus = Stripe::Customer.retrieve(ch[:customer]) | |
cusname = cus[:description] | |
unless cus[:subscription].nil? or cus[:subscription][:plan].nil? | |
sub = cus[:subscription][:plan][:name] | |
end | |
end | |
if ch[:card][:name].empty? or ch[:card][:name]==' ' | |
chname = cusname | |
else | |
chname = ch[:card][:name] | |
end | |
chname = "#{chname} [StripeOutright CSV]" | |
chdesc = "#{sub} #{ch[:id].to_s} [StripeOutright CSV]" | |
if csvtype=='income' | |
csvrow = [] | |
csvrow << chname | |
csvrow << Time.at(ch[:created]).strftime("%m/%d/%y") | |
csvrow << '%.2f' % ( ch[:amount].to_i / 100.00 ) | |
csvrow << 'Sales' | |
csvrow << chdesc | |
csv << csvrow | |
if ch[:refunded] | |
csvrow = [] | |
csvrow << chname | |
csvrow << Time.at(ch[:created]).strftime("%m/%d/%y") | |
csvrow << '%.2f' % ( ch[:amount].to_i / -100.00 ) | |
csvrow << 'Returns' | |
csvrow << chdesc | |
csv << csvrow | |
end | |
elsif( ch[:fee].to_i > 0 ) | |
csvrow = [] | |
csvrow << 'Stripe Fee' | |
csvrow << Time.at(ch[:created]).strftime("%m/%d/%y") | |
csvrow << '%.2f' % ( ch[:fee].to_i / 100.00 ) | |
csvrow << 'Stripe Fees' | |
csvrow << chdesc | |
csv << csvrow | |
end | |
end | |
end | |
csvstr | |
end | |
# To get an income CSV file for Application fees: | |
# http://example.com/fees.csv?sd=2013-01-01&ed=2013-03-31 | |
# | |
# sd specifies the 'start date' for the range you want to output | |
# ed specifies the 'end date' for the range you want to output | |
# | |
get :fees, :provides => :csv do | |
start_ts = Time.parse( params[:sd] ).to_i | |
end_ts = Time.parse( params[:ed] ).to_i | |
uri = "https://api.stripe.com/v1/balance/history?count=100&created[gte]=#{start_ts}&created[lte]=#{end_ts}&type=application_fee" | |
#options = { :count => 100, :created => { :gte => start_ts, :lte => end_ts }, :type => 'application_fee' } | |
options = { :basic_auth => { :username => ENV['LIVE_STRIPE_SECRET'] } } | |
response = HTTParty.get( uri, options ) | |
fees = JSON.parse(response.body,:symbolize_names => true) | |
csvstr = CSV.generate do |csv| | |
csv << ['Payee', 'Date', 'Amount', 'Category', 'Description'] | |
fees[:data].each do |ch| | |
next unless ch[:currency].downcase=='usd' | |
chname = "Stripe Application Fee" | |
chdesc = "#{ch[:description]} #{ch[:source]} [StripeOutright CSV]" | |
csvrow = [] | |
csvrow << chname | |
csvrow << Time.at(ch[:created]).strftime("%m/%d/%y") | |
csvrow << '%.2f' % ( ch[:amount].to_i / 100.00 ) | |
csvrow << 'Sales' | |
csvrow << chdesc | |
csv << csvrow | |
end | |
end | |
csvstr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment