Last active
August 29, 2015 14:20
-
-
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)
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 | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just be aware of timezone overlapping between months.