PHP code example of marzsv / sv-dte-signer

1. Go to this page and download the library: Download marzsv/sv-dte-signer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marzsv / sv-dte-signer example snippets




arzsv\DteSigner\DteSigner;

// Initialize the signer
$signer = new DteSigner();

// Prepare your DTE signing request
$request = [
    'nit' => '12345678901234',
    'privateKeyPassword' => 'your_private_key_password',
    'dteJson' => [
        'identificacion' => [
            'version' => 1,
            'ambiente' => '00',
            'tipoDte' => '01',
            'numeroControl' => 'DTE-01-00000001-000000000000001',
            'codigoGeneracion' => 'A1B2C3D4-E5F6-7890-1234-567890ABCDEF',
            'fecEmi' => '2025-07-20',
            'horEmi' => '10:30:00'
        ],
        'emisor' => [
            'nit' => '12345678901234',
            'nombre' => 'EMPRESA EJEMPLO S.A. DE C.V.'
        ],
        'receptor' => [
            'nit' => '98765432109876',
            'nombre' => 'CLIENTE EJEMPLO S.A. DE C.V.'
        ],
        'resumen' => [
            'totalPagar' => 113.00
        ]
    ]
];

// Sign the DTE
$response = $signer->sign($request);

if ($response['success']) {
    echo "DTE signed successfully!\\n";
    echo "Signed JWS: " . $response['data'] . "\\n";
} else {
    echo "Error: " . $response['message'] . "\\n";
    echo "Code: " . $response['errorCode'] . "\\n";
}



use Marzsv\DteSigner\DteSigner;

$signer = new DteSigner();

// Sign from a JSON file
$response = $signer->sign('/path/to/dte_request.json');

if ($response['success']) {
    echo "DTE signed successfully from file!\\n";
}



use Marzsv\DteSigner\DteSigner;

// Use a custom certificate directory
$signer = new DteSigner('/path/to/certificates');
$response = $signer->sign($request);



use Marzsv\DteSigner\DteVerifier;

// Initialize the verifier
$verifier = new DteVerifier();

// Verify signature and extract original DTE content
$response = $verifier->verify($jwsToken, '12345678901234');

if ($response['success']) {
    echo "DTE signature verified successfully!\n";
    echo "Original DTE data:\n";
    print_r($response['data']);
} else {
    echo "Verification failed: " . $response['message'] . "\n";
}



use Marzsv\DteSigner\DteVerifier;

$verifier = new DteVerifier();

// Extract payload without signature verification (NOT RECOMMENDED for production)
$response = $verifier->extractPayload($jwsToken);

if ($response['success']) {
    echo "Payload extracted (signature not verified):\n";
    print_r($response['data']);
}



use Marzsv\DteSigner\DteVerifier;

// Use custom certificate directory for verification
$verifier = new DteVerifier('/path/to/certificates');
$response = $verifier->verify($jwsToken, $nit);

public function __construct(string $certificateDirectory = 'certificates')

public function sign(array|string $input): array

public function __construct(string $certificateDirectory = 'certificates')

public function verify(string $jwsToken, string $nit): array

public function extractPayload(string $jwsToken): array

[
    'nit' => 'string(14)',                    // Required: 14-digit NIT
    'privateKeyPassword' => 'string(8-100)', // Required: Private key password
    'dteJson' => array,                       // Required: DTE document data
    'nombreDocumento' => 'string'             // Optional: Document name
]
bash
   php examples/mock_certificate_generator.php
   
bash
   php examples/basic_usage.php
   
bash
   php examples/verification_usage.php
   
json
{
    "success": true,
    "message": "DTE signature verified successfully",
    "data": {
        "identificacion": {
            "version": 1,
            "ambiente": "00",
            "tipoDte": "01",
            "numeroControl": "DTE-01-00000001-000000000000001",
            "codigoGeneracion": "A1B2C3D4-E5F6-7890-1234-567890ABCDEF",
            "fecEmi": "2025-07-20",
            "horEmi": "10:30:00"
        },
        "emisor": {
            "nit": "12345678901234",
            "nombre": "EMPRESA DE EJEMPLO S.A. DE C.V."
        },
        "receptor": {
            "nit": "98765432109876",
            "nombre": "CLIENTE EJEMPLO S.A. DE C.V."
        },
        "resumen": {
            "totalPagar": 113.00
        }
    }
}
bash
php examples/mock_certificate_generator.php
bash
# Basic DTE signing
php examples/basic_usage.php

# Sign from JSON file
php examples/file_usage.php

# Verify DTE signatures and extract content
php examples/verification_usage.php

# Error handling demonstration
php examples/error_handling.php
bash
composer analyse