Created
December 18, 2015 00:15
-
-
Save kevindavis/ab5ab28ca6a417272594 to your computer and use it in GitHub Desktop.
Geckoboard Stripe Integration
This file contains hidden or 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
#!/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