1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment\Message; |
4
|
|
|
|
5
|
|
|
use Z38\SwissPayment\Money; |
6
|
|
|
use Z38\SwissPayment\PaymentInformation\PaymentInformation; |
7
|
|
|
use Z38\SwissPayment\Text; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* CustomerCreditTransfer represents a Customer Credit Transfer Initiation (pain.001) message |
11
|
|
|
*/ |
12
|
|
|
class CustomerCreditTransfer extends AbstractMessage |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $id; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $initiatingParty; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $payments; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \DateTime |
31
|
|
|
*/ |
32
|
|
|
protected $creationTime; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* @param string $id Identifier of the message (should usually be unique over a period of at least 90 days) |
38
|
|
|
* @param string $initiatingParty Name of the initiating party |
39
|
|
|
* |
40
|
|
|
* @throws \InvalidArgumentException When any of the inputs contain invalid characters or are too long. |
41
|
|
|
*/ |
42
|
3 |
|
public function __construct($id, $initiatingParty) |
43
|
|
|
{ |
44
|
3 |
|
$this->id = Text::assertIdentifier($id); |
45
|
3 |
|
$this->initiatingParty = Text::assert($initiatingParty, 70); |
46
|
3 |
|
$this->payments = []; |
47
|
3 |
|
$this->creationTime = new \DateTime(); |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Manually sets the creation time |
52
|
|
|
* |
53
|
|
|
* @param \DateTime $creationTime The desired creation time |
54
|
|
|
* |
55
|
|
|
* @return CustomerCreditTransfer This message |
56
|
|
|
*/ |
57
|
|
|
public function setCreationTime(\DateTime $creationTime) |
58
|
|
|
{ |
59
|
|
|
$this->creationTime = $creationTime; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds a payment instruction |
66
|
|
|
* |
67
|
|
|
* @param PaymentInformation $payment The payment to be added |
68
|
|
|
* |
69
|
|
|
* @return CustomerCreditTransfer This message |
70
|
|
|
*/ |
71
|
3 |
|
public function addPayment(PaymentInformation $payment) |
72
|
|
|
{ |
73
|
3 |
|
$this->payments[] = $payment; |
74
|
|
|
|
75
|
3 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Gets the number of payments |
80
|
|
|
* |
81
|
|
|
* @return int Number of payments |
82
|
|
|
*/ |
83
|
1 |
|
public function getPaymentCount() |
84
|
|
|
{ |
85
|
1 |
|
return count($this->payments); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
2 |
|
protected function getSchemaName() |
92
|
|
|
{ |
93
|
2 |
|
return 'http://www.six-interbank-clearing.com/de/pain.001.001.03.ch.02.xsd'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
2 |
|
protected function getSchemaLocation() |
100
|
|
|
{ |
101
|
2 |
|
return 'pain.001.001.03.ch.02.xsd'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
2 |
|
protected function buildDom(\DOMDocument $doc) |
108
|
|
|
{ |
109
|
2 |
|
$transactionCount = 0; |
110
|
2 |
|
$transactionSum = new Money\Mixed(0); |
111
|
2 |
|
foreach ($this->payments as $payment) { |
112
|
2 |
|
$transactionCount += $payment->getTransactionCount(); |
113
|
2 |
|
$transactionSum = $transactionSum->plus($payment->getTransactionSum()); |
114
|
2 |
|
} |
115
|
|
|
|
116
|
2 |
|
$root = $doc->createElement('CstmrCdtTrfInitn'); |
117
|
2 |
|
$header = $doc->createElement('GrpHdr'); |
118
|
2 |
|
$header->appendChild(Text::xml($doc, 'MsgId', $this->id)); |
119
|
2 |
|
$header->appendChild(Text::xml($doc, 'CreDtTm', $this->creationTime->format('Y-m-d\TH:i:sP'))); |
120
|
2 |
|
$header->appendChild(Text::xml($doc, 'NbOfTxs', $transactionCount)); |
121
|
2 |
|
$header->appendChild(Text::xml($doc, 'CtrlSum', $transactionSum->format())); |
122
|
2 |
|
$initgParty = $doc->createElement('InitgPty'); |
123
|
2 |
|
$initgParty->appendChild(Text::xml($doc, 'Nm', $this->initiatingParty)); |
124
|
2 |
|
$initgParty->appendChild($this->buildContactDetails($doc)); |
125
|
2 |
|
$header->appendChild($initgParty); |
126
|
2 |
|
$root->appendChild($header); |
127
|
|
|
|
128
|
2 |
|
foreach ($this->payments as $payment) { |
129
|
2 |
|
$root->appendChild($payment->asDom($doc)); |
130
|
2 |
|
} |
131
|
|
|
|
132
|
2 |
|
return $root; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|