Download the PHP package khaledhajsalem/laravel-zatca-phase2 without Composer
On this page you can find all versions of the php package khaledhajsalem/laravel-zatca-phase2. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download khaledhajsalem/laravel-zatca-phase2
More information about khaledhajsalem/laravel-zatca-phase2
Files in khaledhajsalem/laravel-zatca-phase2
Package laravel-zatca-phase2
Short Description Laravel package for implementing ZATCA (Saudi e-invoicing) Phase 2 compliance
License MIT
Informations about the package laravel-zatca-phase2
Laravel ZATCA Phase 2 Integration
A comprehensive Laravel package for integrating with ZATCA (Saudi Arabia Tax Authority) Phase 2 e-invoicing requirements. This package provides tools for generating, reporting, and clearing invoices and credit notes according to ZATCA Phase 2 specifications.
Table of Contents
- Features
- Requirements
- Installation
- Configuration
- Basic Configuration
- Model Integration
- Field Mapping
- Credit Note Configuration
- Sandbox Mode
- Usage
- Invoice Reporting and Clearance
- Credit Note Handling
- Webhooks
- QR Code Display
- Certificate Management
- Advanced Usage
- Batch Processing
- Custom XML Generation
- Event Handling
- Commands
- Testing
- Best Practices
- Common Issues and Solutions
- Upgrading
- Contributing
- Security
- Credits
- License
Features
- Model Flexibility: Compatible with existing Laravel invoice and credit note models
- Schema Adaptability: Configurable field mapping to adapt to your existing database schema
- Complete Document Support: Handles both invoices and credit notes according to ZATCA requirements
- UBL Compliance: XML generation in ZATCA UBL format with proper validation
- QR Code Generation: Generates QR codes according to ZATCA requirements
- PDF Generation: Creates professional PDF documents for invoices and credit notes
- Certificate Management: Handles certificate creation and management for ZATCA compliance
- API Integration: Complete integration with ZATCA API for reporting and clearance
- Queue Integration: Process documents asynchronously using Laravel's queue system
- Webhook Support: Built-in webhook handling for ZATCA callbacks
- Detailed Logging: Comprehensive logging for audit and troubleshooting
- Command-line Tools: Convenient commands for installation and certificate management
- Error Handling: Robust error handling and reporting
Requirements
- PHP 8.1 or higher
- Laravel 9.0 or higher
- GuzzleHTTP 7.0 or higher
- OpenSSL extension
- JSON extension
- Imagick extension
Installation
Composer Installation
You can install the package via composer:
Package Setup
After installing the package, run the installation command:
This will:
- Publish the necessary configuration files
- Publish migrations
- Set up logging channels
- Create certificate directories
Run Migrations
Run the migrations to add ZATCA fields to your database tables:
Configuration
Basic Configuration
After installation, configure the package in your .env
file:
Model Integration
The package works with your existing invoice and credit note models. Specify the models and table names in the configuration file:
Add ZATCA functionality to your models with the HasZatcaSupport
trait:
Field Mapping
The package uses field mapping to adapt to your existing database schema:
This configuration allows the package to work with your existing database structure without requiring schema changes beyond the ZATCA-specific fields added through migrations.
Credit Note Configuration
Credit notes require special handling for ZATCA compliance. Configure how your system handles credit notes:
This flexibility supports different implementation approaches:
- Separate Models: If your credit notes and invoices are separate models
- Type Field: If you use a single model with a type field to distinguish credit notes
- Separate Tables: If you store credit notes and invoices in different tables
Sandbox Mode
The package supports ZATCA's sandbox environment for testing purposes before moving to production.
Configuration
Add these settings to your .env
file:
Usage
Invoice Reporting and Clearance
To report and clear invoices with ZATCA:
Credit Note Handling
Handling credit notes follows a similar pattern:
Webhooks
The package sets up a route for handling ZATCA callbacks. Configure this URL in your ZATCA portal settings:
When ZATCA sends a callback to this URL, the package will update the document status automatically.
To customize the webhook handling, you can extend the ZatcaCallbackController
class and update the route registration in your RouteServiceProvider
.
QR Code Display
Display the QR code on your invoices:
The QR code contains the required ZATCA data in TLV (Tag-Length-Value) format, including:
- Seller name
- VAT registration number
- Timestamp
- Invoice total (with VAT)
- VAT amount
- Document hash (when available)
- Special handling for credit notes
Certificate Management
Generate a certificate for ZATCA compliance:
This creates a certificate request (CSR) that you submit to the ZATCA portal. After obtaining the signed certificate, save it in the configured certificates path.
Advanced Usage
PDF Generation
Generate professional PDF documents for invoices and credit notes:
The package includes default templates for both invoices and credit notes, but you can create your own custom templates:
Queue Integration
Process documents asynchronously using Laravel's queue system:
Batch Processing
For batch processing of invoices or credit notes with queue support:
Custom XML Generation
To customize the XML generation process:
Event Handling
The package dispatches events that you can listen for:
Commands
The package provides several useful Artisan commands:
The status checking command helps you monitor and update the status of documents submitted to ZATCA. It can be scheduled to run automatically to keep statuses up to date.
Testing
Run the package tests with:
The test suite includes:
- Unit tests for all services and components
- Feature tests for API integration
- Integration tests for database interactions
Best Practices
Regular Testing
Regularly test your integration using the zatca:test-connection
command to ensure your connectivity to the ZATCA API remains active.
Certificate Management
- Set up a process for certificate renewal before expiration
- Store certificates securely
- Implement automated alerts for certificate expiration dates
Logging and Monitoring
- Monitor the ZATCA logs for errors and issues
- Set up alerts for failed submissions
- Implement a dashboard for tracking ZATCA compliance
Fallback Mechanisms
- Implement retry logic for API failures
- Create queues for document submissions during API downtime
- Develop a process for manual submission when automation fails
Data Validation
- Validate invoice data before submission to avoid rejection
- Ensure all required fields are properly mapped
- Implement pre-validation checks based on ZATCA specifications
Common Issues and Solutions
Certificate Issues
Issue: Certificate errors during API calls
Solution:
- Ensure the certificate is properly installed and has not expired
- Verify the private key matches the certificate
- Check certificate permissions in the storage directory
API Connection Issues
Issue: Unable to connect to ZATCA API
Solution:
- Verify network connectivity and API URL configuration
- Check firewall and proxy settings
- Ensure SSL/TLS configurations are correct
- Use the test connection command to diagnose issues
Data Mapping Issues
Issue: Invalid data in ZATCA submissions
Solution:
- Review field mappings in configuration
- Ensure all required fields are properly mapped
- Add data validation before submission
- Check for format issues (dates, numbers, etc.)
Credit Note Issues
Issue: Credit notes rejected by ZATCA
Solution:
- Ensure credit notes reference valid original invoices
- Verify negative amounts are correctly formatted
- Check that billing references are properly included
- Ensure the credit note type code is correct (381)
Upgrading
From Version 1.x to 2.x
If you're upgrading from version 1.x:
- Update your dependencies:
composer require khaledhajsalem/laravel-zatca-phase2:^2.0
- Run the upgrade command:
php artisan zatca:upgrade
- Check the updated configuration file for new options
- Review the changes in field mapping structure
- Update any custom implementations to match the new API
Enhanced Features
I've implemented additional improvements to make the package more powerful and user-friendly:
Improved QR Code Generation
The package now includes enhanced QR code generation that follows ZATCA's TLV (Tag-Length-Value) format requirements precisely:
The QR code includes all required ZATCA fields:
- Seller name
- VAT registration number
- Timestamp
- Invoice total (with VAT)
- VAT amount
- Document hash (if available)
- Document type (for credit notes)
- Original invoice reference (for credit notes)
PDF Generation Support
Generate professional PDF documents for both invoices and credit notes:
Customization options:
- Use default templates or your own custom views
- Control paper size and orientation
- Add custom CSS
- Skip item loading for large invoices
Queue Integration
Process invoices asynchronously using Laravel's queue system:
Batch processing features:
- Process large numbers of invoices efficiently
- Configurable batch size
- Automatic retry logic for failed jobs
- Completion callbacks
- Queue specification
Document Routes
The package now comes with built-in routes for document downloads:
/api/zatca/documents/{id}/pdf
: Download document as PDF/api/zatca/documents/{id}/qr
: Download QR code as PNG/api/zatca/documents/{id}/xml
: Download document XML
Security
If you discover any security issues, please email [email protected] instead of using the issue tracker.
Credits
- Khaled Haj Salem
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-zatca-phase2 with dependencies
spatie/array-to-xml Version ^3.0
simplesoftwareio/simple-qrcode Version ^4.2
php Version ^8.1|^8.2
laravel/framework Version ^9.0|^10.0|^11.0
robrichards/xmlseclibs Version ^3.1