-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe-reports.php
84 lines (64 loc) · 2.47 KB
/
stripe-reports.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
// Include the Stripe PHP library
require_once './stripe-php/init.php';
// Load configuration (api key)
$ini_array = parse_ini_file('stripe-reports.ini');
// Set the API key
$stripe = new \Stripe\StripeClient($ini_array['stripe_api_key']);
// Create a report run
date_default_timezone_set('UTC'); // set timezone to UTC
$report_request = $stripe->reporting->reportRuns->create([
'report_type' => 'balance_change_from_activity.itemized.2',
'parameters' => [
'interval_start' => strtotime('-30 days 00:00:00'),
'interval_end' => strtotime('today 00:00:00'),
'columns' => ['balance_transaction_id', 'created_utc', 'automatic_payout_effective_at', 'reporting_category', 'gross', 'fee', 'net', 'currency', 'description', 'customer_id', 'customer_email', 'customer_name', 'customer_description'],
],
]);
// Store the report run ID
$report_run_id = $report_request['id'];
// Output the report run ID
echo "Report run ID: " . $report_run_id . "\n";
// Wait for the report run to complete
sleep(10);
// Allow up to 20 hours for the report to complete
for ($i = 1; $i <= 3600; $i++) {
$report_run = $stripe->reporting->reportRuns->retrieve($report_run_id);
echo "Report run status: " . $report_run['status'] . "\n";
if ($report_run['status'] != 'succeeded') {
sleep(20);
} else {
break;
}
}
if ($report_run['status'] != 'succeeded') {
echo "Report run did not complete successfully.\n";
exit;
}
// make output directory if not exists
if (!is_dir('output')) {
mkdir('output', 0777, true);
}
//make stripe directory if not exists
if (!is_dir('output/stripe')) {
mkdir('output/stripe', 0777, true);
}
// Download the report with curl like curl {report_url} -u {stripe_api_key}:
echo "Downloading report...\n";
$report_url = $report_run['result']['url'];
$file_path = 'output/stripe/' . date('Y') . '_report.csv'; // specify the file name and path where you want to save the file
// Check if the file already exists and delete it
if (file_exists($file_path)) {
echo "Deleting existing file...\n";
unlink($file_path);
}
// Open the file for writing
$file = fopen($file_path, 'w');
echo "Report URL: " . $report_url . "\n";
// Use curl to download the report
$ch = curl_init($report_url);
curl_setopt($ch, CURLOPT_USERPWD, $ini_array['stripe_api_key'] . ':'); // use stripe api key with basic auth
curl_setopt($ch, CURLOPT_FILE, $file); // write curl response to file
curl_exec($ch);
print curl_error($ch);
curl_close($ch);