forked from w2c/sepa-sdd-xml-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.SEPADirectDebitTransaction.php
390 lines (335 loc) · 10.4 KB
/
class.SEPADirectDebitTransaction.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
<?php
/**
* This class represents a single transaction.
*
* @author Johannes Feichtner <johannes@web-wack.at>
* @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 sales@web-wack.at
*/
class SEPADirectDebitTransaction {
/**
* Code allocated to a currency, by a maintenance agency, under an international identification
* scheme as described in the latest edition of the international standard ISO 4217.
*/
const CURRENCY_CODE = 'EUR';
/**
* Specifies which party/parties will bear the charges associated with the
* processing of the payment transaction. (Tag: <ChrgBr>)
*
* @var string
*/
const CHARGE_BEARER = 'SLEV';
/**
* The instruction identification is a point to point reference that can be used between
* the instructing party and the instructed party to refer to the individual instruction.
* It can be included in several messages related to the instruction. (Tag: <DrctDbtTxInf->PmtId->InstrId>)
*
* @var string
*/
private $instructionIdentification = '';
/**
* The end-to-end identification can be used for reconciliation or to link tasks relating to the transaction.
* It can be included in several messages related to the transaction. (Tag: <DrctDbtTxInf->PmtId->EndToEndId>)
*
* @var string
*/
private $endToEndIdentification = '';
/**
* Amount of money to be moved between the debtor and creditor, before deduction of charges,
* expressed in the currency as ordered by the initiating party. (Tag: <DrctDbtTxInf->InstdAmt>)
*
* @var float
*/
private $instructedAmount = 0.00;
/**
* Unique identification, as assigned by the creditor,
* to unambiguously identify the mandate. (Tag: <DrctDbtTxInf->DrctDbtTx->MndtRltdInf->MndtId>)
*
* @var string
*/
private $mandateIdentification = '';
/**
* Date on which the direct debit mandate has been signed
* by the debtor. (Tag: <DrctDbtTxInf->DrctDbtTx->MndtRltdInf->DtOfSgntr>)
*
* @var string
*/
private $dateOfSignature = '';
/**
* Indicator notifying whether the underlying mandate is amended or not.
* (Tag: <DrctDbtTxInf->DrctDbtTx->MndtRltdInf->AmdmntInd>)
*
* @var string
*/
private $amendmentIndicator = 'false';
/**
* Financial institution servicing an account for the debtor. (Tag: <DbtrAgt->FinInstnId->BIC>)
*
* @var string
*/
private $debtorAgentBIC = '';
/**
* Party that owes an amount of money to the (ultimate) creditor. (Tag: <Dbtr->Nm>)
*
* @var string
*/
private $debtorName = '';
/**
* Unambiguous identification of the account of the debtor to which a debit entry will
* be made as a result of the transaction. (Tag: <DbtrAcct->Id->IBAN>)
*
* @var string
*/
private $debtorIBAN = '';
/**
* Information supplied to enable the matching/reconciliation of an entry with the items that the
* payment is intended to settle, such as commercial invoices in an accounts' receivable system.
* (Tag: <RmtInf->Ustrd>)
*
* @var string
*/
private $remittanceInformation = '';
/**
* Getter for Instruction Identification (DrctDbtTxInf->PmtId->InstrId)
*
* @return string
*/
public function getInstructionIdentification()
{
if (empty($this->instructionIdentification))
return $this->getEndToEndIdentification();
return $this->instructionIdentification;
}
/**
* Setter for Instruction Identification (DrctDbtTxInf->PmtId->InstrId)
*
* @param string $instrId
* @throws SEPAException
*/
public function setInstructionIdentification($instrId)
{
$instrId = URLify::downcode($instrId, "de");
if (!preg_match("/^([\-A-Za-z0-9\+\/\?:\(\)\., ]){0,35}\z/", $instrId))
throw new SEPAException("Invalid InstructionIdentification (max. 35).");
$this->instructionIdentification = $instrId;
}
/**
* Getter for EndToEnd Identification (DrctDbtTxInf->PmtId->EndToEndId)
*
* @return string
*/
public function getEndToEndIdentification()
{
return $this->endToEndIdentification;
}
/**
* Setter for EndToEnd Identification (DrctDbtTxInf->PmtId->EndToEndId)
*
* @param string $endToEndId
* @throws SEPAException
*/
public function setEndToEndIdentification($endToEndId)
{
$endToEndId = URLify::downcode($endToEndId, "de");
if (!preg_match("/^([\-A-Za-z0-9\+\/\?:\(\)\., ]){1,35}\z/", $endToEndId))
throw new SEPAException("Invalid EndToEndIdentification (max. 35).");
$this->endToEndIdentification = $endToEndId;
}
/**
* Getter for Instructed Amount (DrctDbtTxInf->InstdAmt)
*
* @return float
*/
public function getInstructedAmount()
{
return $this->instructedAmount;
}
/**
* Setter for Instructed Amount (DrctDbtTxInf->InstdAmt)
*
* @param float $instdAmt
*/
public function setInstructedAmount($instdAmt)
{
$this->instructedAmount = floatval($instdAmt);
}
/**
* Getter for Mandate Identification (DrctDbtTxInf->DrctDbtTx->MndtRltdInf->MndtId)
*
* @return string
*/
public function getMandateIdentification()
{
return $this->mandateIdentification;
}
/**
* Setter for Mandate Identification (DrctDbtTxInf->DrctDbtTx->MndtRltdInf->MndtId)
*
* @param string $mndtId
* @throws SEPAException
*/
public function setMandateIdentification($mndtId)
{
$mndtId = URLify::downcode($mndtId, "de");
if (!preg_match("/^([A-Za-z0-9]|[\+|\?|\/|\-|:|\(|\)|\.|,|']){1,35}\z/", $mndtId))
throw new SEPAException("MndtId empty, contains invalid characters or too long (max. 35).");
$this->mandateIdentification = $mndtId;
}
/**
* Getter for Mandate Identification (DrctDbtTxInf->DrctDbtTx->MndtRltdInf->DtOfSgntr)
*
* @return string
*/
public function getDateOfSignature()
{
return $this->dateOfSignature;
}
/**
* Setter for Mandate Identification (DrctDbtTxInf->DrctDbtTx->MndtRltdInf->DtOfSgntr)
*
* @param string $dtOfSgntr
*/
public function setDateOfSignature($dtOfSgntr)
{
$this->dateOfSignature = $dtOfSgntr;
}
/**
* Getter for Amendment Indicator (DrctDbtTxInf->DrctDbtTx->MndtRltdInf->AmdmntInd)
*
* @return string
*/
public function getAmendmentIndicator()
{
return $this->amendmentIndicator;
}
/**
* Setter for Amendment Indicator (DrctDbtTxInf->DrctDbtTx->MndtRltdInf->AmdmntInd)
*
* @param string $amdmntInd
*/
public function setAmendmentIndicator($amdmntInd)
{
if ($amdmntInd === true || $amdmntInd == 'true')
$this->amendmentIndicator = 'true';
else
$this->amendmentIndicator = 'false';
}
/**
* Getter for Debtor Agent BIC (DbtrAgt->FinInstnId->BIC)
*
* @return string
*/
public function getDebtorAgentBIC()
{
return $this->debtorAgentBIC;
}
/**
* Setter for Debtor Agent BIC (DbtrAgt->FinInstnId->BIC)
*
* @param string $bic
* @throws SEPAException
*/
public function setDebtorAgentBIC($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 debtor BIC.");
$this->debtorAgentBIC = $bic;
}
/**
* Getter for Debtor Name (Dbtr->Nm)
*
* @return string
*/
public function getDebtorName()
{
return $this->debtorName;
}
/**
* Setter for Debtor Name (Dbtr->Nm)
*
* @param string $dbtr
* @throws SEPAException
*/
public function setDebtorName($dbtr)
{
$dbtr = URLify::downcode($dbtr, "de");
if (strlen($dbtr) == 0 || strlen($dbtr) > 70)
throw new SEPAException("Invalid debtor name (max. 70 characters).");
$this->debtorName = $dbtr;
}
/**
* Getter for Debtor IBAN (DbtrAcct->Id->IBAN)
*
* @return string
*/
public function getDebtorIBAN()
{
return $this->debtorIBAN;
}
/**
* Setter for Debtor IBAN (DbtrAcct->Id->IBAN)
*
* @param string $iban
* @throws SEPAException
*/
public function setDebtorIBAN($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 debtor IBAN.");
$this->debtorIBAN = $iban;
}
/**
* Getter for Remittance Information (RmtInf->Ustrd)
*
* @return string
*/
public function getRemittanceInformation()
{
return $this->remittanceInformation;
}
/**
* Getter for Remittance Information (RmtInf->Ustrd)
*
* @param string $ustrd
* @throws SEPAException
*/
public function setRemittanceInformation($ustrd)
{
$ustrd = URLify::downcode($ustrd, "de");
if (!preg_match("/^([A-Za-z0-9]|[\+|\?|\/|\-|:|\(|\)|\.|,|'| ]){0,140}\z/", $ustrd))
throw new SEPAException("RmtInf contains invalid chars or is too long (max. 140).");
$this->remittanceInformation = $ustrd;
}
/**
* Returns a SimpleXMLElement for the SEPADirectDebitTransaction object.
*
* @return SimpleXMLElement object
*/
public function getXmlDirectDebitTransaction()
{
$xml = new SimpleXMLElement("<DrctDbtTxInf></DrctDbtTxInf>");
$xml->addChild('PmtId')->addChild('InstrId', $this->getInstructionIdentification());
$endToEndId = $this->getEndToEndIdentification();
if (empty($endToEndId))
$endToEndId = 'NOTPROVIDED';
$xml->PmtId->addChild('EndToEndId', $endToEndId);
$xml->addChild('InstdAmt', $this->getInstructedAmount())->addAttribute('Ccy', self::CURRENCY_CODE);
$xml->addChild('ChrgBr', self::CHARGE_BEARER);
$xml->addChild('DrctDbtTx')->addChild('MndtRltdInf')->addChild('MndtId', $this->getMandateIdentification());
$xml->DrctDbtTx->MndtRltdInf->addChild('DtOfSgntr', $this->getDateOfSignature());
$xml->DrctDbtTx->MndtRltdInf->addChild('AmdmntInd', $this->getAmendmentIndicator());
// The BIC is optional for national payments (IBAN only)
$bic = $this->getDebtorAgentBIC();
if (!empty($bic))
$xml->addChild('DbtrAgt')->addChild('FinInstnId')->addChild('BIC', $bic);
else
$xml->addChild('DbtrAgt')->addChild('FinInstnId')->addChild('Othr')->addChild('Id', 'NOTPROVIDED');
$xml->addChild('Dbtr')->addChild('Nm', $this->getDebtorName());
$xml->addChild('DbtrAcct')->addChild('Id')->addChild('IBAN', $this->getDebtorIBAN());
$xml->addChild('RmtInf')->addChild('Ustrd', $this->getRemittanceInformation());
return $xml;
}
}