1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment\PaymentInformation; |
4
|
|
|
|
5
|
|
|
use Z38\SwissPayment\BC; |
6
|
|
|
use Z38\SwissPayment\BIC; |
7
|
|
|
use Z38\SwissPayment\FinancialInstitutionInterface; |
8
|
|
|
use Z38\SwissPayment\IBAN; |
9
|
|
|
use Z38\SwissPayment\Money; |
10
|
|
|
use Z38\SwissPayment\TransactionInformation\CreditTransfer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* PaymentInformation contains a group of transactions as well as details about the debtor |
14
|
|
|
*/ |
15
|
|
|
class PaymentInformation |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $id; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $transactions; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $batchBooking; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected $serviceLevel; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string|null |
39
|
|
|
*/ |
40
|
|
|
protected $localInstrument; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \DateTime |
44
|
|
|
*/ |
45
|
|
|
protected $executionDate; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $debtorName; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var FinancialInstitutionInterface |
54
|
|
|
*/ |
55
|
|
|
protected $debtorAgent; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var IBAN |
59
|
|
|
*/ |
60
|
|
|
protected $debtorIBAN; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor |
64
|
|
|
* |
65
|
|
|
* @param string $id Identifier of this group (should be unique within a message) |
66
|
|
|
* @param string $debtorName Name of the debtor |
67
|
|
|
* @param BC|BIC $debtorAgent BC or BIC of the debtor's financial institution |
68
|
|
|
* @param IBAN $debtorIBAN IBAN of the debtor's account |
69
|
|
|
*/ |
70
|
3 |
|
public function __construct($id, $debtorName, FinancialInstitutionInterface $debtorAgent, IBAN $debtorIBAN) |
71
|
|
|
{ |
72
|
3 |
|
if (!$debtorAgent instanceof BC && !$debtorAgent instanceof BIC) { |
73
|
1 |
|
throw new \InvalidArgumentException('The debtor agent must be an instance of BC or BIC.'); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$this->id = (string) $id; |
77
|
2 |
|
$this->transactions = array(); |
78
|
2 |
|
$this->batchBooking = true; |
79
|
2 |
|
$this->executionDate = new \DateTime(); |
80
|
2 |
|
$this->debtorName = (string) $debtorName; |
81
|
2 |
|
$this->debtorAgent = $debtorAgent; |
82
|
2 |
|
$this->debtorIBAN = $debtorIBAN; |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds a single transaction to this payment |
87
|
|
|
* |
88
|
|
|
* @param CreditTransfer $transaction The transaction to be added |
89
|
|
|
* |
90
|
|
|
* @return PaymentInformation This payment instruction |
91
|
|
|
*/ |
92
|
2 |
|
public function addTransaction(CreditTransfer $transaction) |
93
|
|
|
{ |
94
|
2 |
|
$this->transactions[] = $transaction; |
95
|
|
|
|
96
|
2 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the number of transactions |
101
|
|
|
* |
102
|
|
|
* @return int Number of transactions |
103
|
|
|
*/ |
104
|
2 |
|
public function getTransactionCount() |
105
|
|
|
{ |
106
|
2 |
|
return count($this->transactions); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Gets the sum of transactions |
111
|
|
|
* |
112
|
|
|
* @return Money\Mixed Sum of transactions |
113
|
|
|
*/ |
114
|
2 |
|
public function getTransactionSum() |
115
|
|
|
{ |
116
|
2 |
|
$sum = new Money\Mixed(0); |
117
|
|
|
|
118
|
2 |
|
foreach ($this->transactions as $transaction) { |
119
|
2 |
|
$sum = $sum->plus($transaction->getAmount()); |
120
|
2 |
|
} |
121
|
|
|
|
122
|
2 |
|
return $sum; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Sets the required execution date. |
127
|
|
|
* Where appropriate, the value data is automatically modified to the next possible banking/Post Office working day. |
128
|
|
|
* |
129
|
|
|
* @param \DateTime $executionDate |
130
|
|
|
* |
131
|
|
|
* @return PaymentInformation This payment instruction |
132
|
|
|
*/ |
133
|
|
|
public function setExecutionDate(\DateTime $executionDate) |
134
|
|
|
{ |
135
|
|
|
$this->executionDate = $executionDate; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets the batch booking option. |
142
|
|
|
* It is recommended that one payment instruction is created for each currency transferred. |
143
|
|
|
* |
144
|
|
|
* @param bool $batchBooking |
145
|
|
|
* |
146
|
|
|
* @return PaymentInformation This payment instruction |
147
|
|
|
*/ |
148
|
|
|
public function setBatchBooking($batchBooking) |
149
|
|
|
{ |
150
|
|
|
$this->batchBooking = boolval($batchBooking); |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Checks whether the payment type information is included on B- or C-level |
157
|
|
|
* |
158
|
|
|
* @return bool true if it is included on B-level |
159
|
|
|
*/ |
160
|
4 |
|
public function hasPaymentTypeInformation() |
161
|
|
|
{ |
162
|
4 |
|
return ($this->localInstrument !== null || $this->serviceLevel !== null); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Gets the local instrument |
167
|
|
|
* |
168
|
|
|
* @return string|null The local instrument |
169
|
|
|
*/ |
170
|
2 |
|
public function getLocalInstrument() |
171
|
|
|
{ |
172
|
2 |
|
return $this->localInstrument; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Gets the service level |
177
|
|
|
* |
178
|
|
|
* @return string|null The service level |
179
|
|
|
*/ |
180
|
2 |
|
public function getServiceLevel() |
181
|
|
|
{ |
182
|
2 |
|
return $this->serviceLevel; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Builds a DOM tree of this payment instruction |
187
|
|
|
* |
188
|
|
|
* @param \DOMDocument $doc |
189
|
|
|
* |
190
|
|
|
* @return \DOMElement The built DOM tree |
191
|
|
|
*/ |
192
|
4 |
|
public function asDom(\DOMDocument $doc) |
193
|
|
|
{ |
194
|
4 |
|
$root = $doc->createElement('PmtInf'); |
195
|
|
|
|
196
|
4 |
|
$root->appendChild($doc->createElement('PmtInfId', $this->id)); |
197
|
4 |
|
$root->appendChild($doc->createElement('PmtMtd', 'TRF')); |
198
|
4 |
|
$root->appendChild($doc->createElement('BtchBookg', ($this->batchBooking ? 'true' : 'false'))); |
199
|
|
|
|
200
|
4 |
|
if ($this->hasPaymentTypeInformation()) { |
201
|
4 |
|
$paymentType = $doc->createElement('PmtTpInf'); |
202
|
4 |
|
if ($this->localInstrument !== null) { |
203
|
|
|
$localInstrumentNode = $doc->createElement('LclInstrm'); |
204
|
|
|
$localInstrumentNode->appendChild($doc->createElement('Prtry', $this->localInstrument)); |
205
|
|
|
$paymentType->appendChild($localInstrumentNode); |
206
|
|
|
} |
207
|
4 |
|
if ($this->serviceLevel !== null) { |
208
|
4 |
|
$serviceLevelNode = $doc->createElement('SvcLvl'); |
209
|
4 |
|
$serviceLevelNode->appendChild($doc->createElement('Cd', $this->serviceLevel)); |
210
|
4 |
|
$paymentType->appendChild($serviceLevelNode); |
211
|
4 |
|
} |
212
|
4 |
|
$root->appendChild($paymentType); |
213
|
4 |
|
} |
214
|
|
|
|
215
|
4 |
|
$root->appendChild($doc->createElement('ReqdExctnDt', $this->executionDate->format('Y-m-d'))); |
216
|
|
|
|
217
|
4 |
|
$debtor = $doc->createElement('Dbtr'); |
218
|
4 |
|
$debtor->appendChild($doc->createElement('Nm', $this->debtorName)); |
219
|
4 |
|
$root->appendChild($debtor); |
220
|
|
|
|
221
|
4 |
|
$debtorAccount = $doc->createElement('DbtrAcct'); |
222
|
4 |
|
$debtorAccountId = $doc->createElement('Id'); |
223
|
4 |
|
$debtorAccountId->appendChild($doc->createElement('IBAN', $this->debtorIBAN->normalize())); |
224
|
4 |
|
$debtorAccount->appendChild($debtorAccountId); |
225
|
4 |
|
$root->appendChild($debtorAccount); |
226
|
|
|
|
227
|
4 |
|
$debtorAgent = $doc->createElement('DbtrAgt'); |
228
|
4 |
|
$debtorAgent->appendChild($this->debtorAgent->asDom($doc)); |
229
|
4 |
|
$root->appendChild($debtorAgent); |
230
|
|
|
|
231
|
4 |
|
foreach ($this->transactions as $transaction) { |
232
|
4 |
|
$root->appendChild($transaction->asDom($doc, $this)); |
233
|
3 |
|
} |
234
|
|
|
|
235
|
3 |
|
return $root; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|