1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpCfdi\Finkok\Helpers; |
6
|
|
|
|
7
|
|
|
use DOMDocument; |
8
|
|
|
use PhpCfdi\Finkok\Definitions\RfcRole; |
9
|
|
|
use PhpCfdi\XmlCancelacion\Credentials; |
10
|
|
|
use PhpCfdi\XmlCancelacion\DOMSigner; |
11
|
|
|
|
12
|
|
|
class GetRelatedSigner |
13
|
|
|
{ |
14
|
|
|
public const DEFAULT_PACRFC = 'CVD110412TF6'; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $uuid; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $rfc; |
21
|
|
|
|
22
|
|
|
/** @var RfcRole */ |
23
|
|
|
private $role; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $pacRfc; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* GetRelatedSigner constructor. |
30
|
|
|
* |
31
|
|
|
* @param string $uuid |
32
|
|
|
* @param string $rfc |
33
|
|
|
* @param RfcRole|null $role If null (ommited) then uses emitter role |
34
|
|
|
* @param string $pacRfc If empty (ommited) then uses DEFAULT_PACRFC |
35
|
|
|
*/ |
36
|
1 |
|
public function __construct(string $uuid, string $rfc, RfcRole $role = null, string $pacRfc = self::DEFAULT_PACRFC) |
37
|
|
|
{ |
38
|
1 |
|
$this->uuid = $uuid; |
39
|
1 |
|
$this->rfc = $rfc; |
40
|
1 |
|
$this->role = $role ?? RfcRole::emitter(); |
41
|
1 |
|
$this->pacRfc = $pacRfc ?: static::DEFAULT_PACRFC; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
public function uuid(): string |
45
|
|
|
{ |
46
|
|
|
return $this->uuid; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function rfc(): string |
50
|
|
|
{ |
51
|
|
|
return $this->rfc; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function role(): RfcRole |
55
|
|
|
{ |
56
|
|
|
return $this->role; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function pacRfc(): string |
60
|
|
|
{ |
61
|
|
|
return $this->pacRfc; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function createDocumentToSign(): DOMDocument |
65
|
|
|
{ |
66
|
1 |
|
$document = new DOMDocument('1.0', 'UTF-8'); |
67
|
1 |
|
$root = $document->createElementNS('http://cancelacfd.sat.gob.mx', 'PeticionConsultaRelacionados'); |
68
|
1 |
|
$xmlns = 'http://www.w3.org/2000/xmlns/'; |
69
|
1 |
|
$root->setAttributeNS($xmlns, 'xmlns:xsd', 'http://www.w3.org/2001/XMLSchema'); |
70
|
1 |
|
$root->setAttributeNS($xmlns, 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); |
71
|
1 |
|
$root->setAttribute('RfcEmisor', ($this->role->isEmitter()) ? $this->rfc : ''); |
72
|
1 |
|
$root->setAttribute('RfcPacEnviaSolicitud', $this->pacRfc); |
73
|
1 |
|
$root->setAttribute('RfcReceptor', ($this->role->isRecipient()) ? $this->rfc : ''); |
74
|
1 |
|
$root->setAttribute('Uuid', $this->uuid); |
75
|
1 |
|
$document->appendChild($root); |
76
|
1 |
|
$document->normalizeDocument(); |
77
|
1 |
|
return $document; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function sign(string $certificateFile, string $privateKeyFile, string $passPhrase): string |
81
|
|
|
{ |
82
|
1 |
|
$credentials = new Credentials($certificateFile, $privateKeyFile, $passPhrase); |
83
|
1 |
|
return $this->signUsingCredentials($credentials); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function signUsingCredentials(Credentials $credentials): string |
87
|
|
|
{ |
88
|
1 |
|
$document = $this->createDocumentToSign(); |
89
|
1 |
|
$domSigner = new DOMSigner($document); |
90
|
1 |
|
$domSigner->sign($credentials); |
91
|
1 |
|
return $document->saveXML(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|