-
Notifications
You must be signed in to change notification settings - Fork 1
/
sepa.php
339 lines (303 loc) · 15.2 KB
/
sepa.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Incasso direct debit module
*
* SEPA download file.
*
* @author Sensson <[email protected]>
* @copyright 2004-2016 Sensson
* @license This software is furnished under a license and may be used and copied
* only in accordance with the terms of such license and with the
* inclusion of the above copyright notice. This software or any other
* copies thereof may not be provided or otherwise made available to any
* other person. No title to and ownership of the software is hereby
* transferred.
*
*/
// Set up
ini_set("mysql.connect_timeout", "1200");
set_time_limit(0);
error_reporting(0);
use Illuminate\Database\Capsule\Manager as Capsule;
// Include our classes
include("../../../configuration.php");
include("../../../init.php");
include("class/sepa.php");
include("class/isocodes/IsoCodeInterface.php");
include("class/isocodes/Iban.php");
include("class/isocodes/SwiftBic.php");
// Get all settings
$settings = Capsule::table('mod_incasso')->select('configname', 'configvalue')->get();
foreach($settings as $key => $setting) {
eval("\$" . $setting->configname . " = \$setting->configvalue;");
}
// Security check
if($_GET['hash'] != $securehash) {
die("Unauthorized access. No direct access allowed.");
}
// Check for proforma
$proforma = Capsule::table('tblconfiguration')->select('value')->where('setting', '=', 'SequentialInvoiceNumbering')->get()[0]->value;
$normalize_characters = [
'Á'=>'A', 'À'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Å'=>'A', 'Ä'=>'A', 'Æ'=>'AE', 'Ç'=>'C',
'É'=>'E', 'È'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Í'=>'I', 'Ì'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ð'=>'Eth',
'Ñ'=>'N', 'Ó'=>'O', 'Ò'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O',
'Ú'=>'U', 'Ù'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y',
'á'=>'a', 'à'=>'a', 'â'=>'a', 'ã'=>'a', 'å'=>'a', 'ä'=>'a', 'æ'=>'ae', 'ç'=>'c',
'é'=>'e', 'è'=>'e', 'ê'=>'e', 'ë'=>'e', 'í'=>'i', 'ì'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'eth',
'ē' => 'e', 'ñ'=>'n', 'ó'=>'o', 'ò'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o',
'ú'=>'u', 'ù'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y',
'ß'=>'ss', 'þ'=>'thorn', 'ÿ'=>'y',
'&'=>' ', '@'=>'at', '#'=>'h', '$'=>'s', '%'=>'perc', '^'=>'-','*'=>'-'
];
// A simple function to replace all characters
function normalize_str($string, $character_set) {
if(is_array($character_set)) {
foreach($character_set as $original_chr => $new_chr) {
$string = str_replace($original_chr, $new_chr, $string);
}
return $string;
} else {
return false;
}
}
try {
// Setup our SEPA class
$sepa = new clsGenerateSepaXML();
if($_GET['batch']) {
$batch_id = $_GET['batch'];
$invoices = Capsule::table('mod_incasso_batch')
->join('tblinvoices', 'tblinvoices.id', '=', 'mod_incasso_batch.invoice_id')
->join('tblclients', 'tblinvoices.userid', '=', 'tblclients.id')
->select(
'mod_incasso_batch.uid',
'mod_incasso_batch.invoice_id',
'mod_incasso_batch.client_id',
'mod_incasso_batch.invoice_total',
'mod_incasso_batch.batch_type',
'mod_incasso_batch.date_frst',
'mod_incasso_batch.date_rcur',
'tblinvoices.id',
'tblinvoices.invoicenum',
'tblinvoices.total',
'tblinvoices.userid',
'tblinvoices.date',
'tblinvoices.duedate',
'tblinvoices.status',
'tblclients.city',
'tblclients.firstname',
'tblclients.lastname',
'tblclients.companyname'
)
->where('mod_incasso_batch.batch_id', '=', $batch)
->orderBy('tblinvoices.id', 'asc');
// Add a where clause for unpaid invoices
if($_GET['unpaid']) {
$invoices->where('tblinvoices.status', '=', 'Unpaid');
}
if($invoices->count() > 0) {
// Loop through all invoices and build a SEPA XML file
foreach($invoices->get() as $invoice) {
// Set some variables
$total = $invoice->total * 100;
$invoice_total = $invoice->invoice_total * 100;
$userid = $invoice->userid;
// Backwards compatible with old batches
if($original_invoice_value == 1) {
if($invoice_total == 0) {
$invoice_total = $total;
}
} else {
$invoice_total = $total;
}
// Set invoicenum and check if it's not empty
$invoice_number = $invoice->invoicenum;
if(strlen($invoice_number) == 0) {
$invoice_number = $invoice->id;
}
// Fill up our customer details
$customer_options = Capsule::table('tblcustomfieldsvalues')->where('relid', '=', $userid)->select('fieldid', 'value')->get();
foreach($customer_options as $option) {
if($option->fieldid == $bankno) {
$clientbank = $option->value;
}
if($option->fieldid == $bankholder) {
$clientholder = normalize_str($option->value, $normalize_characters);
}
if($option->fieldid == $bankcity && $bankcity != 9999) {
$clientcity = normalize_str($option->value, $normalize_characters);
}
if($option->fieldid == $customer_manref && $customer_manref != 9999) {
$customermanref = $option->value;
}
if($option->fieldid == $customer_mandate) {
$customermandate = $option->value;
}
if($option->fieldid == $bicno) {
$clientbic = $option->value;
}
}
// Set a default reference when required
if($customer_manref == 9999 OR strlen($customermanref) == 0) {
$customermanref = "CID-{$userid}";
}
// Set the right city
if($bankcity == 9999) {
$clientcity = $invoice->city;
}
// Set the paydate + 1
$paydate = date('Y-m-d', strtotime('+1 days'));
// Verify your own settings
if(!IsoCodes\Iban::validate($mybankaccount)) {
$error = true;
$errors[] = "You have enabled SEPA but your bank account ({$mybankaccount}) is not a valid IBAN number.";
}
if(!IsoCodes\SwiftBic::validate($mybic)) {
$error = true;
$errors[] = "You have enabled SEPA but your BIC number ({$mybic}) is invalid.";
}
// Customer checks
if(strlen($clientholder) == 0) {
$error = true;
$errors[] = "Bank account holder is incorrect. It can't be empty (userid: {$userid}).";
}
if(!IsoCodes\Iban::validate($clientbank)) {
$error = true;
$errors[] = "You have enabled SEPA but {$clientholder}'s (userid: {$userid}) bank account ({$clientbank}) is not a valid IBAN number.";
}
if(!IsoCodes\SwiftBic::validate($clientbic)) {
$error = true;
$errors[] = "You have enabled SEPA but {$clientholder}'s (userid: {$userid}) BIC number ({$clientbic}) is invalid.";
}
if(!preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/', $customermandate)) {
$error = true;
$errors[] = "You have enabled SEPA but the mandate's signing date ({$customermandate}) is incorrect for {$clientholder} (userid: {$userid}), it doesn't match YYYY-MM-DD.";
}
// Display errors
if($error == true) {
echo "<p>Some errors occurred:</p>\n<ul>\n";
foreach($errors as $errorid => $error) {
echo "<li>{$error}</li>\n";
}
echo "</ul>\n";
die();
}
// Set up FRST or RCUR.
$previous_payments = Capsule::table('tblaccounts')->where('gateway', '=', 'directdebit')->where('userid', '=', $invoice->userid)->count();
if($_GET['batchType'] != 1 AND $_GET['batchType'] != 2) {
if($invoice->batch_type == 0) {
// This is our default setting: automatic
if($previous_payments >= 1) {
$batch_type = "RCUR";
} else {
$batch_type = "FRST";
}
} else {
// Override: per invoice specified when creating a batch
if($invoice->batch_type == 1) {
$batch_type = "FRST";
}
if($invoice->batch_type == 2) {
$batch_type = "RCUR";
}
}
} else {
// Override: when downloading a batch
if($_GET['batchType'] == 1) {
$batch_type = "FRST";
}
if($_GET['batchType'] == 2) {
$batch_type = "RCUR";
}
}
// Set up the paydate
if($batch_type == "FRST") {
$paydate = date("Y-m-d", $invoice->date_frst);
}
if($batch_type == "RCUR") {
$paydate = date("Y-m-d", $invoice->date_rcur);
}
// Make sure $paydate always works
if($paydate == '1970-01-01') {
$paydate = date('Y-m-d', strtotime('+3 days'));
}
// Construct info field
$infounstruct = trim(Lang::trans('invoicenumber')) . ': ' . $invoice_number;
if(strlen($payment_description_prefix) != 0) {
$infounstruct = "{$payment_description_prefix} - $infounstruct";
}
// Set up the SEPA XML file
$payment = [
// Creditor details
'PayAmount' => $invoice_total / 100, // The total amount for this invoice in decimals
'IbanCr' => $mybankaccount, // Your own IBAN bank account (the creditor)
'BicCr' => $mybic, // Your own BIC number (the creditor)
'CrName' => $mybankholder, // The name of the account holder (the creditor)
'CreditorName' => $mybankholder, // The name of the account holder (the creditor)
'CreditorID' => $mycreditorid, // The creditor ID to identify the creditor. This ID is supplied by your bank
// Debitor details
'IbanDb' => $clientbank, // The IBAN bank account of the customer
'BicDb' => $clientbic, // The BIC number of the customer
'UniqueID' => $batch_id, // This is a unique identifier for this payment. We use the batch ID for this
'PayDate' => $paydate, // The date the invoice has to be paid
'UniqueIdentifier' => $invoice->uid, // This identifier is used to identify the transaction
'InfoUnstruct' => $infounstruct, // Information detailing the transaction such as an invoice number
'DbtrName' => $clientholder, // The name of the customer
'MandateID' => $customermanref, // The unique mandate identifiaction
'MandateDateSig' => $customermandate, // The date the mandate was signed by the customer
];
// Add the payment to the batch
if($enable_sepa == 1) {
$sepa->addPayment($payment, $batch_type);
}
if($enable_sepa == 2) {
$payment['SeqTp'] = $batch_type;
$sepa_csv[] = $payment;
}
unset($customermanref);
unset($customermandate);
}
// Download as SEPA XML
if($enable_sepa == 1) {
// This payment request needs to be initiated
$initiated_by = [
'Nm' => $mybankholder, // Identification of the initiating company
'PstlAdr' => '', // Postal address (optional)
'Id' => '', // Identification (optional)
'CtryOfRes' => '', // Country of residence (optional)
'CtctDtls' => '', // Contact details (optional)
];
// Set the message ID and create an XML file
$message_id = "{$myidentifier}-{$batch_id}-" . time();
$output = $sepa->generateXML($initiated_by, $message_id);
// Set our headers
header("Content-Type: text/xml");
header("Content-Disposition: attachment; filename=\"{$myidentifier}-{$batch_id}.xml\"");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
echo $output;
}
// Download as CSV
if($enable_sepa == 2) {
// Set our headers
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=\"{$myidentifier}-{$batch_id}.csv\"");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
// Direct output instead of saving it to a file
$csv_output = fopen('php://output', 'w');
// Loop through all entries and add them to the CSV file
foreach($sepa_csv as $sepa_csv_line) {
fputcsv($csv_output, $sepa_csv_line);
}
// Close the pointer to php://output
fclose($csv_output);
}
}
} else {
// If they managed to get passed the security checks, throw up another simple barrier just to make one bit more difficult
die("Unauthorized access. No direct access allowed.");
}
} catch(Exception $e) {
echo $e->getMessage();
}
?>