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