Skip to content

Commit 8bc9e19

Browse files
committed
Merge branch 'refs/heads/main' into fix-cerificate-json
2 parents 5ff9662 + f92b7b6 commit 8bc9e19

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ $certificate = $json_data['certificate'];
321321
$secret = $json_data['secret'];
322322

323323
// Load the private key
324-
$privateKey = (new Storage)->put(__DIR__ . '/output/private.pem');
324+
$privateKey = (new Storage)->get(__DIR__ . '/output/private.pem');
325325
$cleanPrivateKey = trim(str_replace(["-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"], "", $privateKey));
326326

327327
// Create a Certificate instance
@@ -332,10 +332,9 @@ $certificate = new Certificate(
332332
);
333333

334334
// Sign the invoice
335-
$signedInvoice = InvoiceSigner::signInvoice($xmlInvoice, $certificate);
336-
335+
$signedInvoice = InvoiceSigner::signInvoice($xmlInvoice, $certificate)->getXML();
337336
// Save the signed invoice
338-
InvoiceSigner::signInvoice($xmlInvoice, $certificate)->saveXMLFile('/output/signed_invoice.xml');
337+
(new Storage)->put(__DIR__.'/output/signed_invoice.xml', $signedInvoice);
339338
```
340339

341340
### 📤 **5. Submitting the Signed Invoice to ZATCA**

examples/SigningInvoices.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// get private key
2323
$privateKey = file_get_contents(__DIR__ .'/output/private.pem');
2424

25-
$claenPrivateKey = trim(str_replace(["-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"], "", $privateKey));
25+
$cleanPrivateKey = trim(str_replace(["-----BEGIN PRIVATE KEY-----", "-----END PRIVATE KEY-----"], "", $privateKey));
2626

2727
$certificate = (new Certificate(
2828
$certificate,
29-
$claenPrivateKey,
29+
$cleanPrivateKey,
3030
$secret
3131
));
3232

src/CertificateBuilder.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
*
99
* Builds a CSR and private key using OpenSSL.
1010
* The CSR is saved as a file (default "certificate.csr") and the key as a file (default "private.pem").
11+
* @see https://zatca.gov.sa/en/E-Invoicing/Introduction/Guidelines/Documents/Fatoora_Portal_User_Manual_English.pdf page 31 ..
1112
*/
1213
class CertificateBuilder {
1314
private const OID_PROD = 'ZATCA-Code-Signing';
14-
private const OID_TEST = 'TSTZATCA-Code-Signing';
15+
private const OID_TEST = 'PREZATCA-Code-Signing';
1516
private const CONFIG_TEMPLATE = <<<EOL
1617
[req]
1718
prompt = no
@@ -21,7 +22,7 @@ class CertificateBuilder {
2122
[req_dn]
2223
2324
[v3_req]
24-
1.3.6.1.4.1.311.20.2 = ASN1:UTF8String:%s
25+
1.3.6.1.4.1.311.20.2 = ASN1:PRINTABLESTRING:%s
2526
subjectAltName = dirName:dir_sect
2627
2728
[dir_sect]

src/Storage.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,21 @@ protected function ensureDirectoryExists(string $path): void
147147
}
148148

149149
// If directory exists but is not writable, throw exception
150-
if (is_dir($path) && !is_writable($path)) {
151-
throw new ZatcaStorageException('Directory exists but is not writable.', ['path' => $path]);
150+
if (is_dir($path)) {
151+
if (!is_writable($path)) {
152+
throw new ZatcaStorageException('Directory exists but is not writable.', ['path' => $path]);
153+
}
154+
return;
152155
}
153156

154-
// If parent directory is not writable, fail before mkdir()
157+
// Ensure the parent directory exists before creating the target directory
155158
$parentDir = dirname($path);
156-
if (!is_writable($parentDir)) {
157-
throw new ZatcaStorageException('Parent directory is not writable.', ['path' => $parentDir]);
159+
if (!is_dir($parentDir)) {
160+
$this->ensureDirectoryExists($parentDir); // Recursively create parent directories
158161
}
159162

160163
// If directory does not exist, attempt to create it
161-
if (!is_dir($path) && !mkdir($path, 0777, true) && !is_dir($path)) {
164+
if (!is_dir($path) && !mkdir($path, 0755, true) && !is_dir($path)) {
162165
throw new ZatcaStorageException('Failed to create directory.', ['path' => $path]);
163166
}
164167
}

src/ZatcaAPI.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ private function parseResponse(ResponseInterface $response): array
313313
private function formatCertificate(string $base64Certificate): string
314314
{
315315
$decoded = base64_decode($base64Certificate);
316-
return "-----BEGIN CERTIFICATE-----\n{$decoded}\n-----END CERTIFICATE-----";
316+
return $decoded;
317317
}
318318

319319
/**

0 commit comments

Comments
 (0)