Skip to content

Instantly share code, notes, and snippets.

@kevindavis
Created December 18, 2015 00:15
Show Gist options
  • Save kevindavis/ab5ab28ca6a417272594 to your computer and use it in GitHub Desktop.
Save kevindavis/ab5ab28ca6a417272594 to your computer and use it in GitHub Desktop.
Geckoboard Stripe Integration
#!/usr/bin/env ruby
require 'stripe'
require 'byebug'
require 'json'
task :import_stripe_sales do
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
latest = @db.exec("SELECT details->>'id' as id FROM stripe_charges ORDER BY occurred_at DESC LIMIT 1").first
get_charges(latest['id'])
end
def get_charges(first_charge=nil)
charges = Stripe::Charge.all(limit: 100, ending_before: first_charge)
insert_charges(charges)
get_charges(charges.data.first) if charges.has_more
end
def backfill_charges(last_charge=nil)
charges = Stripe::Charge.all(limit: 100, starting_after: last_charge)
insert_charges(charges)
backfill_charges(charges.data.last) if charges.has_more
end
def insert_charges(charges)
charges.each do |charge|
values = [DateTime.strptime(charge.created.to_s,'%s'), charge.amount, charge.to_json]
@db.exec("INSERT INTO stripe_charges ( \
occurred_at, \
amount, \
details \
) VALUES ($1, $2, $3)",
values)
end
puts "inserted 100 charges"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment