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 \DateTime |
34
|
|
|
*/ |
35
|
|
|
protected $executionDate; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $debtorName; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var FinancialInstitutionInterface |
44
|
|
|
*/ |
45
|
|
|
protected $debtorAgent; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var IBAN |
49
|
|
|
*/ |
50
|
|
|
protected $debtorIBAN; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor |
54
|
|
|
* |
55
|
|
|
* @param string $id Identifier of this group (should be unique within a message) |
56
|
|
|
* @param string $debtorName Name of the debtor |
57
|
|
|
* @param BC|BIC $debtorAgent BC or BIC of the debtor's financial institution |
58
|
|
|
* @param IBAN $debtorIBAN IBAN of the debtor's account |
59
|
|
|
*/ |
60
|
3 |
|
public function __construct($id, $debtorName, FinancialInstitutionInterface $debtorAgent, IBAN $debtorIBAN) |
61
|
|
|
{ |
62
|
3 |
|
if (!$debtorAgent instanceof BC && !$debtorAgent instanceof BIC) { |
63
|
1 |
|
throw new \InvalidArgumentException('The debtor agent must be an instance of BC or BIC.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$this->id = (string) $id; |
67
|
2 |
|
$this->transactions = array(); |
68
|
2 |
|
$this->batchBooking = true; |
69
|
2 |
|
$this->executionDate = new \DateTime(); |
70
|
2 |
|
$this->debtorName = (string) $debtorName; |
71
|
2 |
|
$this->debtorAgent = $debtorAgent; |
72
|
2 |
|
$this->debtorIBAN = $debtorIBAN; |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds a single transaction to this payment |
77
|
|
|
* |
78
|
|
|
* @param CreditTransfer $transaction The transaction to be added |
79
|
|
|
* |
80
|
|
|
* @return PaymentInformation This payment instruction |
81
|
|
|
*/ |
82
|
2 |
|
public function addTransaction(CreditTransfer $transaction) |
83
|
|
|
{ |
84
|
2 |
|
$this->transactions[] = $transaction; |
85
|
|
|
|
86
|
2 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets the number of transactions |
91
|
|
|
* |
92
|
|
|
* @return int Number of transactions |
93
|
|
|
*/ |
94
|
2 |
|
public function getTransactionCount() |
95
|
|
|
{ |
96
|
2 |
|
return count($this->transactions); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets the sum of transactions |
101
|
|
|
* |
102
|
|
|
* @return Money\Mixed Sum of transactions |
103
|
|
|
*/ |
104
|
2 |
|
public function getTransactionSum() |
105
|
|
|
{ |
106
|
2 |
|
$sum = new Money\Mixed(0); |
107
|
|
|
|
108
|
2 |
|
foreach ($this->transactions as $transaction) { |
109
|
2 |
|
$sum = $sum->plus($transaction->getAmount()); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
2 |
|
return $sum; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Sets the required execution date. |
117
|
|
|
* Where appropriate, the value data is automatically modified to the next possible banking/Post Office working day. |
118
|
|
|
* |
119
|
|
|
* @param \DateTime $executionDate |
120
|
|
|
* |
121
|
|
|
* @return PaymentInformation This payment instruction |
122
|
|
|
*/ |
123
|
|
|
public function setExecutionDate(\DateTime $executionDate) |
124
|
|
|
{ |
125
|
|
|
$this->executionDate = $executionDate; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Sets the batch booking option. |
132
|
|
|
* It is recommended that one payment instruction is created for each currency transferred. |
133
|
|
|
* |
134
|
|
|
* @param bool $batchBooking |
135
|
|
|
* |
136
|
|
|
* @return PaymentInformation This payment instruction |
137
|
|
|
*/ |
138
|
|
|
public function setBatchBooking($batchBooking) |
139
|
|
|
{ |
140
|
|
|
$this->batchBooking = boolval($batchBooking); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Builds a DOM tree of this payment instruction |
147
|
|
|
* |
148
|
|
|
* @param \DOMDocument $doc |
149
|
|
|
* |
150
|
|
|
* @return \DOMElement The built DOM tree |
151
|
|
|
*/ |
152
|
2 |
|
public function asDom(\DOMDocument $doc) |
153
|
|
|
{ |
154
|
2 |
|
$root = $doc->createElement('PmtInf'); |
155
|
|
|
|
156
|
2 |
|
$root->appendChild($doc->createElement('PmtInfId', $this->id)); |
157
|
2 |
|
$root->appendChild($doc->createElement('PmtMtd', 'TRF')); |
158
|
2 |
|
$root->appendChild($doc->createElement('BtchBookg', ($this->batchBooking ? 'true' : 'false'))); |
159
|
2 |
|
$root->appendChild($doc->createElement('ReqdExctnDt', $this->executionDate->format('Y-m-d'))); |
160
|
|
|
|
161
|
2 |
|
$debtor = $doc->createElement('Dbtr'); |
162
|
2 |
|
$debtor->appendChild($doc->createElement('Nm', $this->debtorName)); |
163
|
2 |
|
$root->appendChild($debtor); |
164
|
|
|
|
165
|
2 |
|
$debtorAccount = $doc->createElement('DbtrAcct'); |
166
|
2 |
|
$debtorAccountId = $doc->createElement('Id'); |
167
|
2 |
|
$debtorAccountId->appendChild($doc->createElement('IBAN', $this->debtorIBAN->normalize())); |
168
|
2 |
|
$debtorAccount->appendChild($debtorAccountId); |
169
|
2 |
|
$root->appendChild($debtorAccount); |
170
|
|
|
|
171
|
2 |
|
$debtorAgent = $doc->createElement('DbtrAgt'); |
172
|
2 |
|
$debtorAgent->appendChild($this->debtorAgent->asDom($doc)); |
173
|
2 |
|
$root->appendChild($debtorAgent); |
174
|
|
|
|
175
|
2 |
|
foreach ($this->transactions as $transaction) { |
176
|
2 |
|
$root->appendChild($transaction->asDom($doc)); |
177
|
2 |
|
} |
178
|
|
|
|
179
|
2 |
|
return $root; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|