Giter VIP home page Giter VIP logo

sat-cfdi's Introduction

SAT CFDI

A pure PHP library to simplify the handling and processing of CFDIs. Creates new CFDIs and parses and validates existing CFDI files.

A CFDI is a "Comprobante Fiscal Digital por Internet" of Mexico's SAT "Servicio de Administración Tributaria" (Tax Administration Service).

Features

  • Parse XML representation of CFDIs
  • Write XML files from a CFDI
  • Validate cryptographic signatures
  • Validate CFDI status online

Installation

Requires PHP 7.2+ and Composer.

composer require anglemx/sat-cfdi

Usage

<?

use Angle\CFDI\CFDI33;
use Angle\CFDI\XmlLoader;

// ...
// ...
// ...

$xmlFile = 'invoice.xml';

$loader = new XmlLoader();

$cfdi = $loader->fileToCFDI($xmlFile);

if (!$cfdi) {
    // Parsing failed
    // All the helper classes and utilities keep a log of errors that are meant for internal debugging
    echo "Errors:" . PHP_EOL;
    echo print_r($loader->getErrors(), true) . PHP_EOL;

    // And the utilities also keep a log of validations, which are user-friendly and meant for public display
    echo "Validations:" . PHP_EOL;
    echo print_r($loader->getValidations(), true) . PHP_EOL;
}

// Parsing success!
print_r($cfdi);

// The validations log is also available upon success
echo "Validations:" . PHP_EOL;
echo print_r($loader->getValidations(), true) . PHP_EOL;

For more implementation examples, check the Test files.

Dependencies

In order to make this library as robust as possible, we're using many libraries that are included by default on most environments.

  • Implements XSD schema files to validate XML. (ext-dom, ext-libxml)
  • Implements XSLT stylesheets to generate the Original Chain Sequence for signature validations. (ext-xsl)
  • Implements OpenSSL to operate and verify X.509 Certificates. ( ext-openssl)

Translations

This library is written in english to maintain code consistency. However, some keywords are very specific to this domain.

Español English
Comprobante Invoice
Relacionados Related
Emisor Issuer
Concepto Item
Impuesto Tax
Impuestos Trasladados Transferred Tax
Impuestos Retenidos Retained Tax
Información Aduanera Customs Information
Predial Property Tax
pending..

Resources

This library bundles some resources to simplify the installation process on production servers, and to allow for offline processing. All of these files are published by SAT and are made available completely free through SAT's official website.

Document Date URL
Standard (pdf) 2017-07-28 http://omawww.sat.gob.mx/tramitesyservicios/Paginas/documentos/cfdv33.pdf
Schema (xsd) 2017-07-28 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd
Original Chain Sequence (xslt) 2021-12-01 http://www.sat.gob.mx/sitio_internet/cfd/3/cadenaoriginal_3_3/cadenaoriginal_3_3.xslt
Data Catalog (xsd) 2020-12-29 http://www.sat.gob.mx/sitio_internet/cfd/catalogos/catCFDI.xsd
Data Types & Patterns (xsd) 2017-12-14 http://www.sat.gob.mx/sitio_internet/cfd/tipoDatos/tdCFDI/tdCFDI.xsd
Fiscal Digital Signature, Schema v1.1 (xsd) 2017-04-12 http://www.sat.gob.mx/sitio_internet/cfd/timbrefiscaldigital/TimbreFiscalDigitalv11.xsd
Fiscal Digital Signature, Original Chain Sequence (xslt) 2017-05-29 http://www.sat.gob.mx/sitio_internet/cfd/timbrefiscaldigital/cadenaoriginal_TFD_1_1.xslt
Error Catalog (xls) 2017-04-12 http://omawww.sat.gob.mx/tramitesyservicios/Paginas/documentos/MatrizDeErrores_CFDI_v33.xls
This list is not complete, but any reference to the complementary XSD and XSLT resources are included in the root definition schemas.

Certificates

We've bundled the production root X.509 Certificates (CA) in a convenient PEM file. If you'd prefer, you can download them from the official website and install those files locally: http://omawww.sat.gob.mx/tramitesyservicios/Paginas/certificado_sello_digital.htm

SAT publishes a list of all the certificates for every single registered and active taxpayer. You can consult any specific certificate with SAT's official tool "Recuperación de Certificados".

You can use that system to look up all of SAT's certificates by searching their RFC: SAT970701NN3

References

The official publication regarding CFDI is called "Anexo 20" and we are currently on CFDI version 3.3. View online.

Web Services

This employs some public web services to validate the status of CFDI.

pending..

Validations

See tests/ValidatorTest.php for a sample implementation of the Validation Steps

  • STEP 1: Parse XML into an Invoice
  • STEP 2: Validate properties
  • STEP 3: Validate CFDI signature
  • STEP 4: Validate Fiscal Stamp (TFD)
  • STEP 5: Validate CFDI's UUID against SAT

Testing

To run tests with PHPUnit, simply install the dev dependencies

composer install

Create a /test-data/ directory in this library's root path, and place all the XML files inside that you would like to test.

Finally, run the tests:

php vendor/bin/phpunit tests

TO DO

  • Validate properties when parsing
  • Validate basic business rules
  • Implement cache for X.509 Certificates
  • Check for Certificate Revocation status before validating
  • Implement cache for XSLT 2.0 transpilation
  • Documentation and more samples

TO FIX

  • Duplicated Namespace declarations on CFDI::toXML() when we have any child (even if not on the root node) with a different namespace than the default for the Document. We should clean this up when pretty printing our XMLs.
  • Replace all instances of Node::NODE_NS_NAME with Node::NODE_NS_URI_NAME inside setChildrenFromDOMNodes(). See CFDI40\Complements for reference.

Other notes

echo 'Node class: ' . get_class($node) . PHP_EOL;
echo 'Node Name: ' . $node->nodeName . PHP_EOL;
echo 'Node Prefix ' . $node->prefix . PHP_EOL;
echo 'Node Local Name: ' . $node->localName . PHP_EOL;
echo 'Node Base URI: ' . $node->baseURI . PHP_EOL;
echo 'Node Namespace URI: ' . $node->namespaceURI . PHP_EOL;
echo PHP_EOL;
Node class: DOMElement
Node Name: tfd:TimbreFiscalDigital
Node Prefix tfd
Node Local Name: TimbreFiscalDigital
Node Base URI: /Users/mundofr/GitHub/Angle/sat-cfdi/test-data/4D75D60D-48CF-434C-96A6-26DABB3AC5AF.xml
Node Namespace URI: http://www.sat.gob.mx/TimbreFiscalDigital

Node class: DOMElement
Node Name: Pagos20:Pagos
Node Prefix Pagos20
Node Local Name: Pagos
Node Base URI: /Users/mundofr/GitHub/Angle/sat-cfdi/test-data/4D75D60D-48CF-434C-96A6-26DABB3AC5AF.xml
Node Namespace URI: http://www.sat.gob.mx/Pagos20

sat-cfdi's People

Contributors

edmundofuentes avatar alexhg11 avatar

Watchers

Edgar Durán avatar へすす  あるべると Baizabal avatar James Cloos avatar  avatar  avatar

sat-cfdi's Issues

CSD

Para los CSDs de PACs estos se obtienen a traves de la lista oficial publicada aqui y posteriormente descargando los certificados de uno en uno con la herramienta de recuperación? O existe algun feed donde se puedan obtener todos?

Unknown children node 'iedu:instEducativas' in cfdi:ComplementoConcepto

I came across your library and I am very thankful for the job you have done. Wonderful work!
This is not important but in case you are interested, I got this error in domToCFDI()with one of my older 3.3 CFDIs.

Unknown children node 'iedu:instEducativas' in cfdi:ComplementoConcepto

XSD & XSLT resources

Hi!, I've seen that you are storing the XML resources from SAT. They look outdated.

You can download a recent copy from SAT, or you might want to use resources-sat-xml repository, it is build every day and automatically updated if source files have changed.

You might find some of the phpcfdi libraries useful (also MIT).
We are working on similar projects like this and if you like we can help each other.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.