PHP code example of samuelaguilera / facturaonline
1. Go to this page and download the library: Download samuelaguilera/facturaonline 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/ */
samuelaguilera / facturaonline example snippets
use FacturaOnline\FacturaOnlineClient;
$client = new FacturaOnlineClient(
apiKey: $_ENV['FACTURAONLINE_API_KEY'],
secretKey: $_ENV['FACTURAONLINE_SECRET_KEY'],
baseUrl: $_ENV['FACTURAONLINE_BASE_URL'] ?? 'https://facturaonline.com.mx/api/',
httpConfig: [
'timeout' => 30,
'connect_timeout' => 10,
],
);
use FacturaOnline\DTO\CfdiData;
use FacturaOnline\DTO\Concepto;
$cfdi = CfdiData::make()
->receptorUid('receptor_uid')
->tipoDocumento('factura')
->usoCfdi('G03')
->moneda('MXN')
->addConcepto(
Concepto::make()
->claveProdServ('01010101')
->cantidad('1')
->claveUnidad('H87')
->descripcion('Servicio profesional')
->valorUnitario('1000.00')
);
$response = $client->cfdi()->create($cfdi);
$response = $client->cartaPorte()->createIngreso([
'Receptor' => ['UID' => '63ebd090d6015'],
'Conceptos' => [[
'ClaveProdServ' => '78101800',
'Cantidad' => '1',
'ClaveUnidad' => 'E48',
'Descripcion' => 'SERVICIO DE FLETE',
'ValorUnitario' => '2200.00',
'Importe' => '2200.00',
]],
'CartaPorte' => [
'Version' => '3.1',
'TranspInternac' => 'No',
'Ubicaciones' => [
'Ubicacion' => [[
'TipoUbicacion' => 'Origen',
'RFCRemitenteDestinatario' => 'XAXX010101000',
'FechaHoraSalidaLlegada' => '2026-03-19T12:00:00',
]],
],
'Mercancias' => [
'PesoBrutoTotal' => '100',
'UnidadPeso' => 'KGM',
'NumTotalMercancias' => '1',
'Mercancia' => [[
'BienesTransp' => '25174200',
'Descripcion' => 'Refacciones',
'Cantidad' => '1',
'ClaveUnidad' => 'H87',
'PesoEnKg' => '100',
]],
],
],
]);
$response = $client->payment()->create([
'TipoDocumento' => 'pago',
'Serie' => '1247',
'UsoCFDI' => 'CP01',
'Moneda' => 'XXX',
'Receptor' => ['UID' => '55c0fdc67593d'],
'Conceptos' => [[
'ClaveProdServ' => '84111506',
'Cantidad' => '1',
'ClaveUnidad' => 'ACT',
'Descripcion' => 'Pago',
'ValorUnitario' => '0',
'Importe' => '0',
]],
'Pagos' => [[
'FechaPago' => '2026-03-18T12:00:00',
'FormaDePagoP' => '01',
'MonedaP' => 'MXN',
'Monto' => '2500',
'DoctoRelacionado' => [[
'IdDocumento' => '3e27ae21-b190-4c4c-a8d1-d3b6ab993122',
'MonedaDR' => 'MXN',
'EquivalenciaDR' => '1',
'NumParcialidad' => '1',
'ImpSaldoAnt' => '1200',
'ImpPagado' => '1200',
'ImpSaldoInsoluto' => '0',
'Impuestos' => [
'Traslados' => [],
'Retenidos' => [],
],
]],
]],
]);
$response = $client->retention()->create([
'LugarExpedicion' => '26015',
'CveRetenc' => '03',
'Receptor' => ['UID' => '61af9fdc030e2'],
'Periodo' => [
'MesIni' => '01',
'MesFin' => '03',
'Ejercicio' => '2022',
],
'Totales' => [
'MontoTotOperacion' => 0,
'MontoTotGrav' => 0,
'MontoTotExent' => 0,
'MontoTotRet' => 123,
'ImpRetenidos' => [[
'BaseRet' => 12,
'ImpuestoRet' => '001',
'MontoRet' => 123,
'TipoPagoRet' => '03',
]],
],
]);
$usoCfdi = $client->catalogs()->usoCfdi();
$bancos = $client->catalogs()->bancos();
$cliente = $client->clientes()->crear([
'rfc' => 'WERX631016S30',
'razons' => 'RAZON SOCIAL DE PRUEBA',
'codpos' => '44680',
'email' => '[email protected] ',
'regimen' => '601',
'pais' => 'MEX',
]);
use FacturaOnline\FacturaOnlineClient;
$this->app->singleton(FacturaOnlineClient::class, function () {
return new FacturaOnlineClient(
apiKey: config('services.facturaonline.key'),
secretKey: config('services.facturaonline.secret'),
baseUrl: config('services.facturaonline.base_url', 'https://facturaonline.com.mx/api/'),
);
});