forked from grizwako/Fiskalizator_PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fiskalizator.php
115 lines (91 loc) · 2.96 KB
/
Fiskalizator.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
<?php
require_once('Certificate.php');
require_once('FiskalRequestXML.php');
require_once('FiskalResponseXML.php');
require_once('CIS_Service.php');
class Fiskalizator {
/** @var FiskalRequestXML */
private $request;
/** @var FiskalResponseXML */
private $response;
/** @var Certificate */
private $cert;
/** @var CIS_Service */
private $cis;
/**
* @param $privateCertPfx
* @param $privateCertPass
* @param bool $productionMode
* @throws Exception if there are problems with certificate loading
*/
public function __construct($privateCertPfx, $privateCertPass,$productionMode = FALSE){
$this->cert = new Certificate();
$this->cert->loadFile($privateCertPfx,$privateCertPass);
$this->cis = new CIS_Service();
if ($productionMode) {
$this->setProductionMode();
}
}
/**
* @return string Pure XML representation as string
*/
public function getRawRequest() {
return $this->request->saveXML();
}
/**
* @return string Pure XML representation as string
*/
public function getRawResponse() {
return $this->response->saveXML();
}
public function getRequest() {
return $this->request;
}
public function getResponse() {
return $this->response;
}
public function getZKI() {
return $this->request->getZKI();
}
public function getJIR() {
return $this->response->getJIR();
}
public function setProductionMode() {
$this->cis->setProductionMode();
}
/**
* @param DOMDocument $xmldoc
* @param int $attempts Maximum number of attempts in case of connection problems
* @param int|float $timeout timeout for each attempted request in seconds.
* Max precision is to milisecond (3 decimal places) like 4.217
* @return bool true on success
* @throws Exception On network problems, problems with signing, cis returning error information
* Use ->getMessage() on caught exception
*/
public function doRequest(DOMDocument $xmldoc, $attempts = 3, $timeout = 5) {
$this->request = new FiskalRequestXML($xmldoc, $this->cert);
if ($this->request->getType() == 'RacunZahtjev'){
$this->request->setupZKI();
}
$this->request->insertHeadInRequest();
$this->request->sign();
$this->request->wrapSoapEnvelope();
$xml = $this->getRawRequest();
try {
$responseXml = $this->cis->doRequest($xml,$attempts,$timeout);
} catch (Exception $e) {
throw $e;
}
$this->response = new FiskalResponseXML($responseXml);
if ($e = $this->response->getErrorMessage()) {
throw new Exception($e);
}
return true;
}
public function getRequestType(){
return $this->request->getType();
}
public function getResponseType(){
return $this->response->getType();
}
}