1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the cfdi-xml project. |
5
|
|
|
* |
6
|
|
|
* (c) Kinedu |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Kinedu\CfdiXML; |
13
|
|
|
|
14
|
|
|
use Kinedu\CfdiXML\Node\Comprobante; |
15
|
|
|
use Kinedu\CfdiXML\Common\Node; |
16
|
|
|
use XSLTProcessor; |
17
|
|
|
use DOMDocument; |
18
|
|
|
|
19
|
|
|
class CFDI |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SAT XSL endpoint. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const XSL_ENDPOINT = 'http://www.sat.gob.mx/sitio_internet/cfd/3/cadenaoriginal_3_3/cadenaoriginal_3_3.xslt'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* CFDI version. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $version = '3.3'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* CSD key. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $key; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* CSD cer. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $cer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Comprobante instance. |
51
|
|
|
* |
52
|
|
|
* @var \Kinedu\CfdiXML\Node\Comprobante |
53
|
|
|
*/ |
54
|
|
|
protected $comprobante; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a new cfdi instance. |
58
|
|
|
* |
59
|
|
|
* @param array $data |
60
|
|
|
* @param string $key |
61
|
|
|
* @param string $cer |
62
|
|
|
*/ |
63
|
|
|
public function __construct(array $data, string $key, string $cer) |
64
|
|
|
{ |
65
|
|
|
$this->comprobante = new Comprobante($data, $this->version); |
66
|
|
|
|
67
|
|
|
$this->key = file_get_contents($key); |
68
|
|
|
$this->cer = file_get_contents($cer); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Add new node to comprobante instance. |
73
|
|
|
* |
74
|
|
|
* @param \Kinedu\CfdiXML\Common\Node $node |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public function add(Node $node) |
79
|
|
|
{ |
80
|
|
|
$this->comprobante->add($node); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the original string. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getCadenaOriginal() : string |
89
|
|
|
{ |
90
|
|
|
$xsl = new DOMDocument(); |
91
|
|
|
$xsl->load($this->getXSL()); |
92
|
|
|
|
93
|
|
|
$xslt = new XSLTProcessor(); |
94
|
|
|
@$xslt->importStyleSheet($xsl); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$xml = new DOMDocument(); |
97
|
|
|
$xml->loadXML($this->comprobante->getDocument()->saveXML()); |
98
|
|
|
|
99
|
|
|
return (string) $xslt->transformToXml($xml); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get sello. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getSello() : string |
108
|
|
|
{ |
109
|
|
|
$pkey = openssl_get_privatekey($this->key); |
110
|
|
|
openssl_sign(@$this->getCadenaOriginal(), $signature, $pkey, OPENSSL_ALGO_SHA256); |
111
|
|
|
openssl_free_key($pkey); |
112
|
|
|
return base64_encode($signature); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Put sello. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function putSello() |
121
|
|
|
{ |
122
|
|
|
$this->comprobante->setAttr( |
123
|
|
|
$this->comprobante->getElement(), |
124
|
|
|
[ |
125
|
|
|
'Sello' => $this->getSello(), |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get certificado. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getCertificado() : string |
136
|
|
|
{ |
137
|
|
|
$cer = preg_replace('/(-+[^-]+-+)/', '', $this->cer); |
138
|
|
|
$cer = preg_replace('/\s+/', '', $cer); |
139
|
|
|
return $cer; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Put certificado. |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
protected function putCertificado() |
148
|
|
|
{ |
149
|
|
|
$this->comprobante->setAttr( |
150
|
|
|
$this->comprobante->getElement(), |
151
|
|
|
[ |
152
|
|
|
'Certificado' => $this->getCertificado(), |
153
|
|
|
] |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the xml with the stamp and certificate attributes. |
159
|
|
|
* |
160
|
|
|
* @return DOMDocument |
161
|
|
|
*/ |
162
|
|
|
protected function xml() : DOMDocument |
163
|
|
|
{ |
164
|
|
|
$this->putSello(); |
165
|
|
|
$this->putCertificado(); |
166
|
|
|
return $this->comprobante->getDocument(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the xml. |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getXML() : string |
175
|
|
|
{ |
176
|
|
|
return $this->xml()->saveXML(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $filename |
181
|
|
|
*/ |
182
|
|
|
public function save(string $filename) |
183
|
|
|
{ |
184
|
|
|
return $this->xml()->save($filename); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function getXSL() |
191
|
|
|
{ |
192
|
|
|
$file = './xslt/cadenaoriginal_3_3.xslt'; |
193
|
|
|
|
194
|
|
|
if (file_exists($file)) { |
195
|
|
|
return $file; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return static::XSL_ENDPOINT; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: