134 lines
4.8 KiB
PHP
134 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
/**
|
|
* BancolombiaParser
|
|
*
|
|
* Extrae campos estructurados de los correos de notificación de Bancolombia.
|
|
* Soporta notificaciones de transferencias por llave de pago.
|
|
*
|
|
* Ejemplo de texto:
|
|
* "Bancolombia: LEDYS, recibiste una transferencia de HELMER DE JESUS RINCON ORTEGA
|
|
* por $1,000.00 en tu cuenta *0733 conectada a la llave @leon8909 el 21/04/26
|
|
* a las 19:51."
|
|
*/
|
|
class BancolombiaParser
|
|
{
|
|
/**
|
|
* Intenta parsear el cuerpo de texto del correo.
|
|
*
|
|
* @return array|null Null si el texto no parece una notificación de Bancolombia.
|
|
* Array con claves: destinatario, remitente, valor, cuenta, llave, fecha, hora, texto_original
|
|
*/
|
|
public static function parse(string $text): ?array
|
|
{
|
|
// Limpiar HTML si llega con etiquetas
|
|
$text = self::stripHtml($text);
|
|
|
|
// Limpiar referencias URL entre corchetes: "Logo Bancolombia [http://...]"
|
|
$text = preg_replace('/\[https?:\/\/[^\]]*\]/i', '', $text);
|
|
|
|
// Normalizar espacios y saltos de línea
|
|
$text = preg_replace('/\s+/', ' ', $text);
|
|
$text = trim($text);
|
|
|
|
// Detectar si es correo de Bancolombia
|
|
if (! self::esBancolombia($text)) {
|
|
return null;
|
|
}
|
|
|
|
$result = [
|
|
'banco' => 'Bancolombia',
|
|
'tipo' => self::detectarTipo($text),
|
|
'destinatario' => self::extraer($text, '/Bancolombia[:\s]+([A-ZÁÉÍÓÚÑÜ][A-Za-záéíóúñü\s]+?),\s+recibiste/u'),
|
|
'remitente' => self::extraer($text, '/transferencia\s+de\s+(.+?)\s+por\s+\$/iu'),
|
|
'valor' => self::extraerValor($text),
|
|
'cuenta' => self::extraer($text, '/cuenta\s+\*(\d+)/iu'),
|
|
'llave' => self::extraer($text, '/llave\s+([@\w.\-]+)/iu'),
|
|
'fecha' => self::extraer($text, '/el\s+(\d{1,2}\/\d{1,2}\/\d{2,4})/iu'),
|
|
'hora' => self::extraer($text, '/a\s+las\s+(\d{1,2}:\d{2})/iu'),
|
|
'texto_original' => $text,
|
|
];
|
|
|
|
return $result;
|
|
}
|
|
|
|
// ─────────────────────────────────────────────────────────
|
|
// Helpers privados
|
|
// ─────────────────────────────────────────────────────────
|
|
|
|
private static function esBancolombia(string $text): bool
|
|
{
|
|
return stripos($text, 'bancolombia') !== false
|
|
&& (
|
|
stripos($text, 'transferencia') !== false
|
|
|| stripos($text, 'pago') !== false
|
|
|| stripos($text, 'recibiste') !== false
|
|
|| stripos($text, 'enviaste') !== false
|
|
);
|
|
}
|
|
|
|
private static function detectarTipo(string $text): string
|
|
{
|
|
if (stripos($text, 'recibiste') !== false) {
|
|
return 'transferencia_recibida';
|
|
}
|
|
if (stripos($text, 'enviaste') !== false) {
|
|
return 'transferencia_enviada';
|
|
}
|
|
if (stripos($text, 'pago') !== false) {
|
|
return 'pago';
|
|
}
|
|
return 'notificacion';
|
|
}
|
|
|
|
/**
|
|
* Ejecuta un regex y devuelve el primer grupo capturado, trimmed.
|
|
*/
|
|
private static function extraer(string $text, string $pattern): string
|
|
{
|
|
if (preg_match($pattern, $text, $m)) {
|
|
return trim($m[1]);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Extrae el valor monetario como string limpio, ej: "1000.00"
|
|
* y también como float.
|
|
* Devuelve el string formateado con separadores originales.
|
|
*/
|
|
private static function extraerValor(string $text): string
|
|
{
|
|
// Captura números con puntos y comas: $1,000.00 | $1.000,00 | $500
|
|
if (preg_match('/por\s+\$([0-9][0-9.,]*)/iu', $text, $m)) {
|
|
return trim($m[1]);
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Elimina completamente bloques <style>, <script>, <head> y luego
|
|
* convierte etiquetas estructurales en saltos de línea antes de strip_tags.
|
|
*/
|
|
private static function stripHtml(string $text): string
|
|
{
|
|
// Eliminar bloques cuyo contenido no es texto visible
|
|
$text = preg_replace('/<style[^>]*>.*?<\/style>/si', '', $text);
|
|
$text = preg_replace('/<script[^>]*>.*?<\/script>/si', '', $text);
|
|
$text = preg_replace('/<head[^>]*>.*?<\/head>/si', '', $text);
|
|
|
|
// Convertir etiquetas estructurales a saltos de línea
|
|
$text = preg_replace('/<(br\s*\/?\s*|\/p|\/div|\/tr|\/li|\/td)>/i', "\n", $text);
|
|
|
|
// Quitar todas las etiquetas restantes
|
|
$text = strip_tags($text);
|
|
|
|
// Decodificar entidades HTML
|
|
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
|
|
return $text;
|
|
}
|
|
}
|