-
Notifications
You must be signed in to change notification settings - Fork 10
/
FiskalRequestXML.php
184 lines (136 loc) · 6.5 KB
/
FiskalRequestXML.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
<?php
require_once('UUID.php');
class FiskalRequestXML
{
private $doc;
private $allowedTypes;
public function __construct(DOMDocument $doc, Certificate $cert) {
$this->doc = $doc;
$this->cert = $cert;
$this->allowedTypes = array('RacunZahtjev','PoslovniProstorZahtjev');
}
public function getZKI() {
if ($node = $this->doc->getElementsByTagName('ZastKod')->item(0)) {
return $node->nodeValue;
}
return false;
}
/**
* Adds "ZastKod" if it is not already set
*/
public function setupZKI() {
if ($this->doc->getElementsByTagName('ZastKod')->item(0)) {
return true;
}
$prefix = $this->doc->documentElement->prefix;
$zki = $this->calculateZKI();
$nakDost = $this->doc->getElementsByTagName('NakDost')->item(0);
$zkiNode = new DOMElement("{$prefix}:ZastKod", $zki, $this->doc->documentElement->namespaceURI);
$invoice = $this->doc->getElementsByTagName('Racun')->item(0);
$invoice->insertBefore($zkiNode, $nakDost);
return true;
}
/**
* @return string|bool ZKI or FALSE on failure
*/
private function calculateZKI() {
$nodes = array('Oib', 'DatVrijeme', 'BrOznRac', 'OznPosPr', 'OznNapUr', 'IznosUkupno');
$vals = array();
foreach ($nodes as $node) {
$vals[$node] = $this->doc->getElementsByTagName($node)->item(0)->nodeValue;
}
$vals['DatVrijeme'] = str_replace('T', ' ', $vals['DatVrijeme']);
$temp = implode('', $vals);
try {
$zkiTemp = $this->cert->calculateSignature($temp);
return md5($zkiTemp);
} catch (Exception $e) {
return false;
}
}
/**
* Wraps SOAP-ENV around current root element
*/
public function wrapSoapEnvelope() {
$soapNs = 'SOAP-ENV';
$soapNsUri = 'http://schemas.xmlsoap.org/soap/envelope/';
$this->doc->createAttributeNS($soapNsUri, $soapNs);
$envelope = new DOMElement("{$soapNs}:Envelope", null, $soapNsUri);
$body = new DOMElement("{$soapNs}:Body", null, $soapNsUri);
#take reference so we don't loose it
$rootNode = $this->doc->documentElement;
$this->doc->replaceChild($envelope, $rootNode);
$envelope->appendChild($body);
$body->appendChild($rootNode);
}
public function insertHeadInRequest() {
$prefix = $this->doc->documentElement->prefix;
$dateTime = date('d.m.Y') . 'T' . date('H:i:s');
$header = new DOMElement("{$prefix}:Zaglavlje", '', $this->doc->documentElement->namespaceURI);
$reqDateTime = new DOMElement("{$prefix}:DatumVrijeme", $dateTime, $this->doc->documentElement->namespaceURI);
$reqUUID = new DOMElement("{$prefix}:IdPoruke", UUID::v4(), $this->doc->documentElement->namespaceURI);
$refElem = $this->doc->documentElement->firstChild;
$this->doc->documentElement->insertBefore($header, $refElem);
$header->appendChild($reqUUID);
$header->appendChild($reqDateTime);
}
public function sign() {
$this->doc->documentElement->setAttribute('Id', 'signXmlId');
$canonical = $this->doc->C14N(true, false);
$signatureDigest = base64_encode(hash('sha1', $canonical, true));
$this->addSignatureNode($signatureDigest);
$signedInfoNode = $this->doc->getElementsByTagName('SignedInfo')->item(0);
$sigNodeXMLString = $signedInfoNode->C14N(true);
try {
$signature = $this->cert->calculateSignature($sigNodeXMLString);
} catch (Exception $e) {
return false;
}
$signatureValue = base64_encode($signature);
$sigNode = $this->doc->getElementsByTagName('Signature')->item(0);
$sigNode->appendChild(new DOMElement('SignatureValue', $signatureValue));
$this->addX509Node();
return true;
}
private function addSignatureNode($digest) {
$rootElem = $this->doc->documentElement;
$sigNode = $rootElem->appendChild(new DOMElement('Signature'));
$sigNode->setAttribute('xmlns', 'http://www.w3.org/2000/09/xmldsig#');
$signedInfoNode = $sigNode->appendChild(new DOMElement('SignedInfo'));
$signedInfoNode->setAttribute('xmlns', 'http://www.w3.org/2000/09/xmldsig#');
$canonMethodNode = $signedInfoNode->appendChild(new DOMElement('CanonicalizationMethod'));
$canonMethodNode->setAttribute('Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#');
$signatureMethodNode = $signedInfoNode->appendChild(new DOMElement('SignatureMethod'));
$signatureMethodNode->setAttribute('Algorithm', 'http://www.w3.org/2000/09/xmldsig#rsa-sha1');
$referenceNode = $signedInfoNode->appendChild(new DOMElement('Reference'));
$referenceNode->setAttribute('URI', '#signXmlId');
$transformsNode = $referenceNode->appendChild(new DOMElement('Transforms'));
$tr1Node = $transformsNode->appendChild(new DOMElement('Transform'));
$tr2Node = $transformsNode->appendChild(new DOMElement('Transform'));
$tr1Node->setAttribute('Algorithm', 'http://www.w3.org/2000/09/xmldsig#enveloped-signature');
$tr2Node->setAttribute('Algorithm', 'http://www.w3.org/2001/10/xml-exc-c14n#');
$digestMethodNode = $referenceNode->appendChild(new DOMElement('DigestMethod'));
$digestMethodNode->setAttribute('Algorithm', 'http://www.w3.org/2000/09/xmldsig#sha1');
$referenceNode->appendChild(new DOMElement('DigestValue', $digest));
}
private function addX509Node() {
$sigNode = $this->doc->getElementsByTagName('Signature')->item(0);
$keyInfoNode = $sigNode->appendChild(new DOMElement('KeyInfo'));
$x509DataNode = $keyInfoNode->appendChild(new DOMElement('X509Data'));
$x509DataNode->appendChild(new DOMElement('X509Certificate', $this->cert->getX509Cert()));
$x509IssuerSerialNode = $x509DataNode->appendChild(new DOMElement('X509IssuerSerial'));
$x509IssuerSerialNode->appendChild(new DOMElement('X509IssuerName', $this->cert->getIssuerAsString()));
$x509IssuerSerialNode->appendChild(new DOMElement('X509SerialNumber', $this->cert->getSerialNumber()));
}
public function saveXML() {
return $this->doc->saveXML();
}
public function getType() {
foreach ($this->allowedTypes as $type) {
if ($this->doc->getElementsByTagName($type)->item(0)) {
return $type;
}
}
return false;
}
}