nfephp-org/sped-gtin
Valide GTINs (EAN-8/12/13/14) para NFe/NFCe layout 4.00 conforme NT 2021.003: verifica estrutura, prefixo 789/790, região e dígito verificador, ajudando a evitar rejeições da SEFAZ por código inválido.
ProductService, OrderService) or as a standalone validator in API request pipelines. The package’s stateless, rule-based design aligns with Laravel’s dependency injection and service container patterns.Product model saves/updates.StoreProductRequest or UpdateOrderRequest.Gtin class exposes properties (region, prefix, type) and exceptions, enabling custom logic (e.g., logging invalid GTINs to a compliance audit table).App\Exceptions\Handler).phpunit.xml).GtinValidatorService).| Risk | Mitigation Strategy | Severity |
|---|---|---|
| False Positives/Negatives | Unit test edge cases (e.g., "SEM GTIN", malformed inputs). Add integration tests with real GTINs. |
Medium |
| SEFAZ Rejections Despite Validation | Log invalid GTINs to a compliance_issues table; flag for manual CNP review. Plan for future CNP API integration. |
High (Business) |
| Performance Bottleneck | Validation is O(1); negligible impact. Monitor in production if used in bulk operations (e.g., batch imports). | Low |
| Prefix Table Staleness | Update the CSV prefix table manually or build a cron job to fetch updates from NFe site. | Medium |
| PHP Version Compatibility | Test on Laravel’s supported PHP versions (8.0+). Use composer require with ^1.0 for stability. |
Low |
Gtin class to a custom validator interface:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(GtinValidatorInterface::class, function ($app) {
return new \NFePHP\Gtin\Gtin();
});
}
FormRequest classes:
// app/Http/Requests/StoreProductRequest.php
public function rules()
{
return [
'gtin' => ['required', 'string', new GtinValidationRule],
];
}
Product model events:
// app/Observers/ProductObserver.php
public function saving(Product $product)
{
if ($product->gtin && !$product->gtin->isValid()) {
throw new \InvalidArgumentException("Invalid GTIN: {$product->gtin}");
}
}
// app/Http/Middleware/ValidateGtin.php
public function handle($request, Closure $next)
{
if ($request->has('gtin') && !$request->gtin->isValid()) {
return response()->json(['error' => 'Invalid GTIN'], 422);
}
return $next($request);
}
$validator = new \NFePHP\Gtin\Gtin($gtin);
if (!$validator->isValid()) {
// Handle error
}
nfephp-org/sped-gtin to composer.json.compliance_issues table for manual review.GtinValidatorService) for reusability."SEM GTIN", malformed inputs).region, type) to product models for reporting.| Step | Dependency | Effort | Owner |
|---|---|---|---|
Add package to composer.json |
None | 5 mins | Backend Engineer |
| Implement basic validation | Composer install | 2 hours | Backend Engineer |
Create compliance_issues table |
Database access | 1 hour | Backend Engineer |
| Integrate with Product model | Model layer | 3 hours | Backend Engineer |
| Add API middleware | API routes | 2 hours | Backend Engineer |
| Write unit tests | Validation logic | 4 hours | QA/Backend |
| Log invalid GTINs to admin panel | Frontend integration | 3 hours | Frontend Engineer |
| Monitor SEFAZ rejections | Business analytics | Ongoing | PM/Compliance Team |
config/gtin_prefixes.php).^1.0 in composer.json to avoid breaking changes.How can I help you explore Laravel packages today?