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