feat: actualizar servicio de WhatsApp para incluir el nombre de la empresa y eliminar el soporte para XML en el envío de facturas

This commit is contained in:
Juan Felipe Zapata Moreno 2026-03-06 10:07:08 -06:00
parent b253e8a308
commit 232d9ccaa6
3 changed files with 12 additions and 58 deletions

View File

@ -60,14 +60,12 @@ public function sendInvoice(Request $request)
'phone_number' => ['required', 'string', 'regex:/^[0-9]{10,13}$/'], 'phone_number' => ['required', 'string', 'regex:/^[0-9]{10,13}$/'],
'invoice_number' => ['required', 'string'], 'invoice_number' => ['required', 'string'],
'pdf_url' => ['required', 'url'], 'pdf_url' => ['required', 'url'],
'xml_url' => ['nullable', 'url'],
'customer_name' => ['required', 'string', 'max:255'], 'customer_name' => ['required', 'string', 'max:255'],
]); ]);
$result = $this->whatsAppService->sendInvoice( $result = $this->whatsAppService->sendInvoice(
phoneNumber: $validated['phone_number'], phoneNumber: $validated['phone_number'],
pdfUrl: $validated['pdf_url'], pdfUrl: $validated['pdf_url'],
xmlUrl: $validated['xml_url'] ?? null,
invoiceNumber: $validated['invoice_number'], invoiceNumber: $validated['invoice_number'],
customerName: $validated['customer_name'] customerName: $validated['customer_name']
); );
@ -84,36 +82,4 @@ public function sendInvoice(Request $request)
'error' => $result 'error' => $result
]); ]);
} }
/**
* Enviar ticket de venta por WhatsApp
*/
public function sendSaleTicket(Request $request)
{
$validated = $request->validate([
'phone_number' => ['required', 'string', 'regex:/^[0-9]{10,13}$/'],
'sale_number' => ['required', 'string'],
'ticket_url' => ['required', 'url'],
'customer_name' => ['required', 'string', 'max:255'],
]);
$result = $this->whatsAppService->sendSaleTicket(
phoneNumber: $validated['phone_number'],
ticketUrl: $validated['ticket_url'],
saleNumber: $validated['sale_number'],
customerName: $validated['customer_name']
);
if ($result['success']) {
return ApiResponse::OK->response([
'message' => 'Ticket enviado correctamente por WhatsApp',
'data' => $result
]);
}
return ApiResponse::BAD_REQUEST->response([
'message' => $result['message'],
'error' => $result['error'] ?? null
]);
}
} }

View File

@ -13,6 +13,7 @@ class WhatsAppService
protected string $apiUrl; protected string $apiUrl;
protected int $orgId; protected int $orgId;
protected string $token; protected string $token;
protected string $companyName;
protected string $email = 'juan.zapata@golsystems.com.mx'; protected string $email = 'juan.zapata@golsystems.com.mx';
public function __construct() public function __construct()
@ -20,6 +21,7 @@ public function __construct()
$this->apiUrl = config('services.whatsapp.api_url', 'https://whatsapp.golsystems.mx/api/send-whatsapp'); $this->apiUrl = config('services.whatsapp.api_url', 'https://whatsapp.golsystems.mx/api/send-whatsapp');
$this->orgId = config('services.whatsapp.org_id', 1); $this->orgId = config('services.whatsapp.org_id', 1);
$this->token = config('services.whatsapp.token'); $this->token = config('services.whatsapp.token');
$this->companyName = config('services.whatsapp.company_name', 'PDV');
if (!$this->token) { if (!$this->token) {
throw new \Exception('El token de WhatsApp no está configurado. Agrega WHATSAPP_TOKEN en tu archivo .env'); throw new \Exception('El token de WhatsApp no está configurado. Agrega WHATSAPP_TOKEN en tu archivo .env');
@ -66,10 +68,17 @@ public function sendDocument(
'parameters' => [ 'parameters' => [
[ [
'type' => 'text', 'type' => 'text',
'parameter_name' => 'nombre_cliente',
'text' => $customerName, 'text' => $customerName,
], ],
[ [
'type' => 'text', 'type' => 'text',
'parameter_name' => 'nombre_empresa',
'text' => $this->companyName,
],
[
'type' => 'text',
'parameter_name' => 'referencia_factura',
'text' => $ticket, 'text' => $ticket,
], ],
], ],
@ -147,39 +156,17 @@ public function sendDocument(
public function sendInvoice( public function sendInvoice(
string $phoneNumber, string $phoneNumber,
string $pdfUrl, string $pdfUrl,
?string $xmlUrl,
string $invoiceNumber, string $invoiceNumber,
string $customerName string $customerName
): array { ): array {
// Enviar PDF return $this->sendDocument(
$pdfResult = $this->sendDocument(
phoneNumber: $phoneNumber, phoneNumber: $phoneNumber,
documentUrl: $pdfUrl, documentUrl: $pdfUrl,
filename: "Factura_{$invoiceNumber}.pdf", filename: "{$invoiceNumber}.pdf",
userEmail: $this->email, userEmail: $this->email,
ticket: $invoiceNumber, ticket: $invoiceNumber,
customerName: $customerName customerName: $customerName
); );
// Si hay XML y el PDF se envió correctamente, enviarlo también
if ($xmlUrl && $pdfResult['success']) {
$xmlResult = $this->sendDocument(
phoneNumber: $phoneNumber,
documentUrl: $xmlUrl,
filename: "Factura_{$invoiceNumber}.xml",
userEmail: $this->email,
ticket: "{$invoiceNumber}_XML",
customerName: $customerName
);
return [
'success' => $pdfResult['success'] && $xmlResult['success'],
'pdf' => $pdfResult,
'xml' => $xmlResult,
];
}
return $pdfResult;
} }
/** /**

View File

@ -39,6 +39,7 @@
'api_url' => env('WHATSAPP_API_URL', 'https://whatsapp.golsystems.mx/api/send-whatsapp'), 'api_url' => env('WHATSAPP_API_URL', 'https://whatsapp.golsystems.mx/api/send-whatsapp'),
'org_id' => env('WHATSAPP_ORG_ID', 1), 'org_id' => env('WHATSAPP_ORG_ID', 1),
'token' => env('WHATSAPP_TOKEN'), 'token' => env('WHATSAPP_TOKEN'),
'company_name' => env('APP_NAME', 'PDV'),
], ],
]; ];