-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe-xero.php
179 lines (129 loc) · 4.32 KB
/
stripe-xero.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
require_once('vendor/autoload.php');
use League\Csv\Writer;
/**
* Config
*/
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
\Stripe\Stripe::setApiKey(getenv('STRIPE_API_KEY'));
$timezone = getenv('STRIPE_TIMEZONE');
/**
* Preparation
*/
//timezone
date_default_timezone_set($timezone);
//macOS CSV line ending fix
if (! ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
//csv
$header = [ 'Date', 'Amount', 'Payee', 'Description', 'Reference' ];
$csv = Writer::createFromString('');
$csv->insertOne($header);
/**
* STRIPE CUSTOMER <--> EMAIL MAPPING
*/
echo "Fetching all Stripe customers... \n";
$customer_email = array();
$customers = \Stripe\Customer::all(array( "limit" => 100 ));
foreach ($customers->autoPagingIterator() as $customer) {
$customer_email [ $customer->id ] = $customer->email;
}
/**
* STRIPE CHARGE <--> EMAIL MAPPING
*/
echo "Fetching all Stripe charges... \n";
$charge_email = array();
$charges = \Stripe\Charge::all(array( "limit" => 100 ));
foreach ($charges->autoPagingIterator() as $charge) {
$charge_email[ $charge->id ] = $customer_email[ $charge->customer ];
}
/**
* STRIPE REFUND <--> EMAIL MAPPING
*/
echo "Fetching all Stripe refunds... \n";
$refund_email = array();
$refunds = \Stripe\Refund::all(array( "limit" => 100 ));
foreach ($refunds->autoPagingIterator() as $refund) {
$refund_email [ $refund->id ] = $charge_email[ $refund->charge ];
}
//date object
$txn_date = new DateTime();
echo "Fetching all Stripe balance affecting transactions... \n";
//Get Stripe balance affecting transactions
$transactions = \Stripe\BalanceTransaction::all(array( "limit" => 100 ));
foreach ($transactions->autoPagingIterator() as $transaction) {
$row = array();
//csv-date
$tx_date = ( new DateTime('@' . $transaction->created) )->setTimezone(new DateTimeZone($timezone))->format('d/m/Y');
$row[] = $tx_date;
//csv-amount
$row[] = $transaction->amount / 100;
//csv-Payee
switch ($transaction->type) {
case 'charge':
$row[] = $charge_email[ $transaction->source ];
break;
case 'refund':
$row[] = $refund_email[ $transaction->source ];
break;
case 'payout':
$row[] = 'Stripe';
break;
case 'adjustment':
$row[] = 'Customer';
break;
default:
echo "TODO :: unknown transaction of type $transaction->type encountered \n";
var_dump($transaction->__toArray(true));
}
//csv-Description
$row[] = $transaction->description;
//csv-reference
$row[] = $transaction->source;
//insert $row into CSV
$csv->insertOne($row);
//Handle non-zero fees
if ($transaction->fee) {
//only if it's stripe fee
if ('stripe_fee' === $transaction->fee_details[0]['type']) {
$row = array();
//csv-date
$row[] = $tx_date;
//csv-amount
$row[] = -1 * $transaction->fee / 100;
//csv-Payee
$row[] = 'Stripe';
//csv-Description
$row[] = $transaction->fee_details[0]['description'];
//csv-reference
$row[] = $transaction->source;
//insert $row into CSV
$csv->insertOne($row);
} else {
// non stripe fee
echo "TODO :: unknown fees of type" . $transaction->fee_details[0]['type'] . "encountered \n";
var_dump($transaction->__toArray(true));
}
}
}
echo $csv; //returns the CSV document as a string
$csv_filename = "stripe-xero-" . ( new DateTime() )->format('d-M-Y-h-i-a') . ".csv";
if (getenv('CREATE_FILE')) {
echo "\nStoring CSV file as " . $csv_filename ;
file_put_contents($csv_filename, $csv);
}
if (getenv('EMAIL_FROM') && getenv('EMAIL_TO') && getenv('AWS_ACCESS_KEY') && getenv('AWS_SECRET_KEY')) {
echo "\nEmailing CSV file";
$m = new SimpleEmailServiceMessage();
$m->addTo(getenv('EMAIL_TO'));
$m->setFrom(getenv('EMAIL_FROM'));
$m->setSubject($csv_filename);
$m->setMessageFromString('PFA Stripe Xero CSV');
$m->addAttachmentFromData($csv_filename, $csv, 'text/csv');
$ses = new SimpleEmailService(getenv('AWS_ACCESS_KEY'), getenv('AWS_SECRET_KEY'));
print_r($ses->sendEmail($m));
}
echo "<finish>";
exit(0);