Skip to content

Instantly share code, notes, and snippets.

@fred
Last active August 29, 2015 14:20
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 fred/8462c8df9dcce1fe6430 to your computer and use it in GitHub Desktop.
Save fred/8462c8df9dcce1fe6430 to your computer and use it in GitHub Desktop.
exporting charges and refunds to a CSV format files (charges.csv, refunds.csv)
#!/usr/bin/env ruby
# how to run:
# ruby ./omise_export.rb
require 'date'
require 'json'
# CHANGE the next 4 variables
# just be aware of timezone overlapping between months, Omise will parse dates as UTC time
skey = 'your secret key'
from = '2015-04-01'
to = '2015-04-30'
limit = 100
# No change required below
response = `curl -s https://api.omise.co/charges -X GET -u #{skey}: -d 'from=#{from}' -d 'to=#{to}' -d 'offset=0' -d 'limit=#{limit}'`;nil
charges = JSON.parse(response);nil
file = File.new('./charges.csv', "w+")
file.write "charge_id,amount,date,description\n"
charges['data'].each do |charge|
date = DateTime.parse(charge['created'])
date = date.to_time
file.write "#{charge['id']},#{charge['amount']},#{date},#{charge['description']}\n"
end;nil
file.close
file = File.new('./refunds.csv', "w+")
file.write "refund_id,amount,date,description,charge_id\n"
charges['data'].each do |charge|
if charge["refunds"]["data"].any?
charge["refunds"]["data"].each do |refund|
date = DateTime.parse(refund['created'])
date = date.to_time
file.write "#{refund['id']},#{refund['amount']},#{date},#{charge['description']},#{charge['id']}\n"
end
end
end
file.close
@fred
Copy link
Author

fred commented May 5, 2015

just be aware of timezone overlapping between months.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment