-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathclass.SEPAPaymentInfo.php
442 lines (382 loc) · 11.4 KB
/
class.SEPAPaymentInfo.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/**
* This class represents a PmtInf block within a SEPA message.
*
* @author Johannes Feichtner <[email protected]>
* @copyright http://www.web-wack.at web wack creations
* @license http://creativecommons.org/licenses/by-nc/3.0/ CC Attribution-NonCommercial 3.0 license
* For commercial use please contact [email protected]
*/
class SEPAPaymentInfo
{
/**
* Specifies the means of payment that will be used to move the amount of money. (Tag: <PmtMtd>)
*
* Collection of an amount of money from the debtor's bank account by the creditor.
* The amount of money and dates of collections may vary.
*
* @var string
*/
const PAYMENT_METHOD = 'DD';
/**
* Specifies a pre-agreed service or level of service between the parties,
* as published in an external service level code list. (Tag: <SvcLvl->Cd>)
*
* @var string
*/
const SERVICELEVEL_CODE = 'SEPA';
/**
* Specifies which party/parties will bear the charges associated with the
* processing of the payment transaction. (Tag: <ChrgBr>)
*
* @var string
*/
const CHARGE_BEARER = 'SLEV';
/**
* Name of the identification scheme, in a free text form. (Tag: <CdtrSchmeId->Id->PrvtId->Othr->SchmeNm->Prtry>)
*
* @var string
*/
const PROPRIETARY_NAME = 'SEPA';
/**
* Unique identification, as assigned by a sending party, to unambiguously identify the
* payment information group within the message. (Tag: <PmtInfId>)
*
* @var string
*/
private $paymentInformationIdentification = '';
/**
* Identifies whether a single entry per individual transaction or a batch entry for the
* sum of the amounts of all transactions within the group of a message is requested (Tag: <BtchBookg>)
*
* @var string
*/
private $batchBooking = 'false';
/**
* Number of individual transactions contained in the payment information group (Tag: <NbOfTxs>)
*
* @var int
*/
private $numberOfTransactions = 0;
/**
* Total of all individual amounts included in the group (Tag: <CtrlSum>)
*
* @var float
*/
private $controlSum = 0.00;
/**
* Specifies the local instrument, as published in an external local instrument code list.
* Only 'CORE', 'COR1', or 'B2B' are allowed. (Tag: <LclInstrm->Cd>)
*
* @var string
*/
private $localInstrumentCode = 'CORE';
/**
* Identifies the direct debit sequence, such as first, recurrent, final or one-off. (Tag: <SeqTp>)
*
* @var string
*/
private $sequenceType = 'FRST';
/**
* Date and time at which the creditor requests that the amount of money is to be
* collected from the debtor. (Tag: <ReqdColltnDt>)
*
* @var DateTime object
*/
private $requestedCollectionDate = null;
/**
* Party to which an amount of money is due. (Tag: <Cdtr->Nm>)
*
* @var string
*/
private $creditorName = '';
/**
* Unambiguous identification of the account of the creditor to which a credit entry will
* be posted as a result of the payment transaction. (Tag: <CdtrAcct->Id->IBAN>)
*
* @var string
*/
private $creditorAccountIBAN = '';
/**
* Financial institution servicing an account for the creditor. (Tag: <CdtrAgt->FinInstnId->BIC>)
*
* @var string
*/
private $creditorAgentBIC = '';
/**
* Credit party that signs the mandate. (Tag: <CdtrSchmeId->Id->PrvtId->Othr->Id>)
*
* @var string
*/
private $creditorSchemeIdentification = '';
/**
* A container for all transactions within this PaymentInfo.
*
* @var SEPADirectDebitTransaction[] array
*/
private $transactions = array();
/**
* Getter for PaymentInformationIdentification (PmtInfId)
*
* @return string
*/
public function getPaymentInformationIdentification()
{
return $this->paymentInformationIdentification;
}
/**
* Setter for PaymentInformationIdentification (PmtInfId)
*
* @param string $pmtInfId
* @throws SEPAException
*/
public function setPaymentInformationIdentification($pmtInfId)
{
$pmtInfId = URLify::downcode($pmtInfId, "de");
if (!preg_match("/^([A-Za-z0-9]|[\+|\?|\/|\-|:|\(|\)|\.|,|'| ]){1,35}\z/", $pmtInfId))
throw new SEPAException("PmtInfId empty, contains invalid characters or too long (max. 35).");
$this->paymentInformationIdentification = $pmtInfId;
}
/**
* Getter for BatchBooking (BtchBookg)
*
* @return string
*/
public function getBatchBooking()
{
return $this->batchBooking;
}
/**
* Setter for BatchBooking (BtchBookg)
*
* @param bool|string $btchBookg
*/
public function setBatchBooking($btchBookg)
{
if ($btchBookg === true || $btchBookg == 'true')
$this->batchBooking = 'true';
else
$this->batchBooking = 'false';
}
/**
* Getter for Number of Transactions (NbOfTxs)
*
* @return int
*/
public function getNumberOfTransactions()
{
return $this->numberOfTransactions;
}
/**
* Getter for ControlSum (CtrlSum)
*
* @return float
*/
public function getControlSum()
{
return round($this->controlSum, 2);
}
/**
* Getter for LocalInstrument->Code (LclInstrm->Cd)
*
* @return string
*/
public function getLocalInstrumentCode()
{
return $this->localInstrumentCode;
}
/**
* Setter for LocalInstrument->Code (LclInstrm->Cd)
*
* @param string $cd
* @throws SEPAException
*/
public function setLocalInstrumentCode($cd)
{
if (!preg_match("/^(B2B|COR1|CORE)\z/", $cd))
throw new SEPAException("Only 'CORE', 'COR1', or 'B2B' are allowed.");
$this->localInstrumentCode = $cd;
}
/**
* Getter for SequenceType (SeqTp)
*
* @return string
*/
public function getSequenceType()
{
return $this->sequenceType;
}
/**
* Setter for SequenceType (SeqTp)
*
* @param string $seqTp
* @throws SEPAException
*/
public function setSequenceType($seqTp)
{
if (!preg_match("/^(FNAL|FRST|OOFF|RCUR)\z/", $seqTp))
throw new SEPAException("Only 'FNAL', 'FRST', 'OOFF', or 'RCUR' are allowed.");
$this->sequenceType = $seqTp;
}
/**
* Getter for Requested Collection Date (ReqdColltnDt)
*
* @return DateTime object
*/
public function getRequestedCollectionDate()
{
$reqdColltnDt = new DateTime();
if (!$this->requestedCollectionDate)
$this->requestedCollectionDate = $reqdColltnDt->format('Y-m-d');
return $this->requestedCollectionDate;
}
/**
* Setter for Requested Collection Date (ReqdColltnDt)
*
* @param string $reqdColltnDt
*/
public function setRequestedCollectionDate($reqdColltnDt)
{
$this->requestedCollectionDate = $reqdColltnDt;
}
/**
* Getter for Creditor Name (Cdtr->Nm)
*
* @return string
*/
public function getCreditorName()
{
return $this->creditorName;
}
/**
* Setter for Creditor Name (Cdtr->Nm)
*
* @param string $cdtr
* @throws SEPAException
*/
public function setCreditorName($cdtr)
{
$cdtr = URLify::downcode($cdtr, "de");
if (strlen($cdtr) == 0 || strlen($cdtr) > 70)
throw new SEPAException("Invalid creditor name (max. 70 characters).");
$this->creditorName = $cdtr;
}
/**
* Getter for Creditor Account IBAN (CdtrAcct->Id->IBAN)
*
* @return string
*/
public function getCreditorAccountIBAN()
{
return $this->creditorAccountIBAN;
}
/**
* Setter for Creditor Account IBAN (CdtrAcct->Id->IBAN)
*
* @param string $iban
* @throws SEPAException
*/
public function setCreditorAccountIBAN($iban)
{
$iban = str_replace(' ', '', trim($iban));
if (!preg_match("/^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}\z/i", $iban))
throw new SEPAException("Invalid creditor IBAN.");
$this->creditorAccountIBAN = $iban;
}
/**
* Getter for Creditor Agent BIC (CdtrAgt->FinInstnId->BIC)
*
* @return string
*/
public function getCreditorAgentBIC()
{
return $this->creditorAgentBIC;
}
/**
* Setter for Creditor Agent BIC (CdtrAgt->FinInstnId->BIC)
*
* @param string $bic
* @throws SEPAException
*/
public function setCreditorAgentBIC($bic)
{
$bic = str_replace(' ', '', trim($bic));
if (!preg_match("/^[0-9a-z]{4}[a-z]{2}[0-9a-z]{2}([0-9a-z]{3})?\z/i", $bic))
throw new SEPAException("Invalid creditor BIC.");
$this->creditorAgentBIC = $bic;
}
/**
* Getter for Creditor Scheme Identification (CdtrSchmeId->Id->PrvtId->Othr->Id)
*
* @return string
*/
public function getCreditorSchemeIdentification()
{
return $this->creditorSchemeIdentification;
}
/**
* Setter for Creditor Scheme Identification (CdtrSchmeId->Id->PrvtId->Othr->Id)
*
* @param string $cdtrSchmeId
* @throws SEPAException
*/
public function setCreditorSchemeIdentification($cdtrSchmeId)
{
if (empty($cdtrSchmeId) || is_null($cdtrSchmeId))
throw new SEPAException("Invalid CreditorSchemeIdentification.");
$this->creditorSchemeIdentification = $cdtrSchmeId;
}
/**
* Adds a transaction object to the list of transactions.
*
* @param SEPADirectDebitTransaction $transaction
*/
public function addTransaction(SEPADirectDebitTransaction $transaction)
{
$this->transactions[] = $transaction;
$this->numberOfTransactions++;
$this->controlSum += $transaction->getInstructedAmount();
}
/**
* Returns a SimpleXMLElement for the SEPAPaymentInfo object.
*
* @return SimpleXMLElement object
*/
public function getXmlPaymentInfo()
{
$xml = new SimpleXMLElement("<PmtInf></PmtInf>");
$xml->addChild('PmtInfId', $this->getPaymentInformationIdentification());
$xml->addChild('PmtMtd', self::PAYMENT_METHOD);
$xml->addChild('BtchBookg',$this->getBatchBooking());
$xml->addChild('NbOfTxs', $this->getNumberOfTransactions());
$xml->addChild('CtrlSum', $this->getControlSum());
$xml->addChild('PmtTpInf')->addChild('SvcLvl')->addChild('Cd', self::SERVICELEVEL_CODE);
$xml->PmtTpInf->addChild('LclInstrm')->addChild('Cd', $this->getLocalInstrumentCode());
$xml->PmtTpInf->addChild('SeqTp', $this->getSequenceType());
$xml->addChild('ReqdColltnDt', $this->getRequestedCollectionDate());
$xml->addChild('Cdtr')->addChild('Nm', $this->getCreditorName());
$xml->addChild('CdtrAcct')->addChild('Id')->addChild('IBAN', $this->getCreditorAccountIBAN());
// The BIC is optional for national payments (IBAN only)
$bic = $this->getCreditorAgentBIC();
if (!empty($bic))
$xml->addChild('CdtrAgt')->addChild('FinInstnId')->addChild('BIC', $bic);
else
$xml->addChild('CdtrAgt')->addChild('FinInstnId')->addChild('Othr')->addChild('Id', 'NOTPROVIDED');
$xml->addChild('ChrgBr', self::CHARGE_BEARER);
$othr = $xml->addChild('CdtrSchmeId')->addChild('Id')->addChild('PrvtId')->addChild('Othr');
$cdtrSchmeId = $this->getCreditorSchemeIdentification();
if (!empty($cdtrSchmeId))
$othr->addChild('Id', $this->getCreditorSchemeIdentification());
$othr->addChild('SchmeNm')->addChild('Prtry', self::PROPRIETARY_NAME);
// Add all transactions to the current PaymentInfo block
for ($i = 0; $i < $this->getNumberOfTransactions(); $i++)
{
$domPaymentInfo = dom_import_simplexml($xml);
$domTransaction = dom_import_simplexml($this->transactions[$i]->getXmlDirectDebitTransaction());
$domPaymentInfo->appendChild($domPaymentInfo->ownerDocument->importNode($domTransaction, true));
}
$xml->NbOfTxs = $this->getNumberOfTransactions();
$xml->CtrlSum = $this->getControlSum();
return $xml;
}
}