forked from Eseperio/verifactu-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerifactu.php
More file actions
147 lines (127 loc) · 5.21 KB
/
Verifactu.php
File metadata and controls
147 lines (127 loc) · 5.21 KB
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
<?php
declare(strict_types=1);
// Main entry point of the Verifactu library
namespace eseperio\verifactu;
use eseperio\verifactu\models\BatchInvoiceResponse;
use eseperio\verifactu\models\InvoiceCancellation;
use eseperio\verifactu\models\InvoiceQuery;
use eseperio\verifactu\models\InvoiceRecord;
use eseperio\verifactu\models\InvoiceResponse;
use eseperio\verifactu\models\InvoiceSubmission;
use eseperio\verifactu\models\QueryResponse;
use eseperio\verifactu\services\VerifactuService;
class Verifactu
{
public const ENVIRONMENT_PRODUCTION = 'production';
public const ENVIRONMENT_SANDBOX = 'sandbox';
/**
* Production environment URL.
*/
public const URL_PRODUCTION = 'https://www1.agenciatributaria.gob.es/wlpl/TIKE-CONT/ws/SistemaFacturacion/VerifactuSOAP';
/**
* Production environment URL (seal certificate).
*/
public const URL_PRODUCTION_SEAL = 'https://www10.agenciatributaria.gob.es/wlpl/TIKE-CONT/ws/SistemaFacturacion/VerifactuSOAP';
/**
* Test (homologation) environment URL.
*/
public const URL_TEST = 'https://prewww1.aeat.es/wlpl/TIKE-CONT/ws/SistemaFacturacion/VerifactuSOAP';
/**
* Test (seal certificate) environment URL.
*/
public const URL_TEST_SEAL = 'https://prewww10.aeat.es/wlpl/TIKE-CONT/ws/SistemaFacturacion/VerifactuSOAP';
/**
* QR verification URL (production).
*/
public const QR_VERIFICATION_URL_PRODUCTION = 'https://www2.agenciatributaria.gob.es/wlpl/TIKE-CONT/ValidarQR';
/**
* QR verification URL (testing/homologation).
*/
public const QR_VERIFICATION_URL_TEST = 'https://prewww2.aeat.es/wlpl/TIKE-CONT/ValidarQR';
public const TYPE_CERTIFICATE = 'certificate';
public const TYPE_SEAL = 'seal';
/**
* @param $certPath string Path to the certificate file.
* @param $certPassword string Password for the certificate.
* @param $certType string Type of certificate, either 'certificate' or 'seal'.
* @param $environment string Environment to use, either 'production' or 'sandbox'.
*/
public static function config($certPath, $certPassword, $certType, $environment = self::ENVIRONMENT_PRODUCTION): void
{
$endpoint = match ($environment) {
self::ENVIRONMENT_PRODUCTION => $certType === self::TYPE_SEAL ? self::URL_PRODUCTION_SEAL : self::URL_PRODUCTION,
self::ENVIRONMENT_SANDBOX => $certType === self::TYPE_SEAL ? self::URL_TEST_SEAL : self::URL_TEST,
default => throw new \InvalidArgumentException("Invalid environment: $environment")
};
$qrValidationUrl = match ($environment) {
self::ENVIRONMENT_PRODUCTION => self::QR_VERIFICATION_URL_PRODUCTION,
self::ENVIRONMENT_SANDBOX => self::QR_VERIFICATION_URL_TEST,
default => throw new \InvalidArgumentException("Invalid environment: $environment")
};
VerifactuService::config([
VerifactuService::CERT_PATH_KEY => $certPath,
VerifactuService::CERT_PASSWORD_KEY => $certPassword,
VerifactuService::SOAP_ENDPOINT => $endpoint,
VerifactuService::QR_VERIFICATION_URL => $qrValidationUrl,
]);
}
/**
* Registers a new invoice (Alta) with AEAT via VERI*FACTU.
*
* @throws \DOMException
* @throws \SoapFault
*/
public static function registerInvoice(InvoiceSubmission $invoice): InvoiceResponse
{
return VerifactuService::registerInvoice($invoice);
}
/**
* Cancels an invoice (Anulación) with AEAT via VERI*FACTU.
*/
public static function cancelInvoice(InvoiceCancellation $cancellation): InvoiceResponse
{
return VerifactuService::cancelInvoice($cancellation);
}
/**
* Registers multiple invoices in a batch with AEAT via VERI*FACTU.
*
* @param InvoiceSubmission[] $invoices
* @param int|null $maxBatchSize Maximum records per batch (defaults to 1000)
* @return BatchInvoiceResponse
* @throws \InvalidArgumentException
* @throws \SoapFault
*/
public static function registerInvoices(array $invoices, ?int $maxBatchSize = null): BatchInvoiceResponse
{
return VerifactuService::registerInvoices($invoices, $maxBatchSize);
}
/**
* Cancels multiple invoices in a batch with AEAT via VERI*FACTU.
*
* @param InvoiceCancellation[] $cancellations
* @param int|null $maxBatchSize Maximum records per batch (defaults to 1000)
* @return BatchInvoiceResponse
* @throws \InvalidArgumentException
* @throws \SoapFault
*/
public static function cancelInvoices(array $cancellations, ?int $maxBatchSize = null): BatchInvoiceResponse
{
return VerifactuService::cancelInvoices($cancellations, $maxBatchSize);
}
/**
* Queries submitted invoices from AEAT via VERI*FACTU.
*/
public static function queryInvoices(InvoiceQuery $query): QueryResponse
{
return VerifactuService::queryInvoices($query);
}
/**
* Generates a base64 QR code for the provided invoice.
*
* @return string base64-encoded PNG QR code
*/
public static function generateInvoiceQr(InvoiceRecord $record): string
{
return VerifactuService::generateInvoiceQr($record);
}
}