Skip to content

Instantly share code, notes, and snippets.

@nimid
Last active September 21, 2016 11:36
Show Gist options
  • Save nimid/96160ca5c37735918469a49f17d4d218 to your computer and use it in GitHub Desktop.
Save nimid/96160ca5c37735918469a49f17d4d218 to your computer and use it in GitHub Desktop.
<?php
// how to run:
// php download_refunds.php
// Works on Mac OS X
// It will generate (and overwrite) a csv file with name refunds.csv
// Update your valid Live Secret Key, this is only a test key:
$skey = 'skey_';
// Output file name
$filename = './refunds.csv';
// Dates to retrieve data
$from = '2016-01-01';
$to = '2016-12-31';
// Maximum limit for API is 100 records
$limit = '100';
$charges_response = shell_exec("curl -s https://api.omise.co/charges -X GET -u $skey: -d 'from=$from' -d 'to=$to' -d 'offset=0' -d 'limit=$limit'");
$charges = json_decode($charges_response, true);
if ($charges['object'] === 'error') {
echo $charges['code'] . ', ' . $charges['message'];
die();
}
if ($charges['total'] === 0) {
echo 'No data found';
}
$csv_header = array(
'id',
'amount',
'currency',
'voided',
'charge',
'transaction',
'created',
);
$file = fopen($filename, 'w');
fputcsv($file, $csv_header);
foreach ($charges['data'] as $charge) {
$charge_id = $charge['id'];
// if charge was refunded, pull data from API
if ($charge['refunded'] > 0) {
$response = shell_exec("curl -s https://api.omise.co/charges/$charge_id/refunds -X GET -u $skey: -d 'from=$from' -d 'to=$to' -d 'offset=0' -d 'limit=$limit'");
$refunds = json_decode($response, true);
foreach ($refunds['data'] as $refund) {
unset($refund['object']);
unset($refund['location']);
fputcsv($file, $refund);
}
}
}
fclose($file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment