File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Services/WhatsAppService.php
<?php
namespace App\Services;
use App\IntegrationProvider;
use Illuminate\Support\Facades\Http;
class WhatsAppService
{
protected $token;
protected $phoneId;
protected $url;
public function __construct()
{
$provider = IntegrationProvider::where('alias', 'whatsapp-api')
->where('active', 1)
->first();
if (!$provider) {
throw new \Exception("No hay configuración activa para WhatsApp (whatsapp-api).");
}
$this->token = $provider->token;
$this->phoneId = $provider->nit;
$this->url = $provider->endpoint;
}
public function sendMessageDocument($to, $link, $template = 'saludo_pedido', $language = 'es_MX')
{
$response = Http::withToken($this->token)->post(
"{$this->url}/{$this->phoneId}/messages",
[
"messaging_product" => "whatsapp",
"to" => $to,
"type" => "template",
"template" => [
"name" => $template,
"language" => ["code" => $language],
"components" => [
[
"type" => "header",
"parameters" => [
[
"type" => "document",
"document" => [
"link" => $link,
"filename" => "Boletas.pdf"
]
]
]
],
]
]
]
);
return $response->json();
}
}