File: /var/www/matriculas_api_dev/app/Http/Controllers/Toku/WebhookController.php
<?php
namespace App\Http\Controllers\Toku;
use App\Http\Controllers\Controller;
use App\Services\ApiTokuService;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class WebhookController extends Controller
{
private $apiTokuService;
public function __construct(ApiTokuService $apiTokuService)
{
$this->apiTokuService = $apiTokuService;
}
/**
* @OA\Get(
* path="/toku/webhook-endpoints",
* summary="Obtener todos los Webhook Endpoints",
* tags={"Toku/Webhooks"},
* @OA\Response(response=200, description="Listado de Webhooks"),
* @OA\Response(response=400, description="Error en petición")
* )
*/
public function index()
{
try {
$data = $this->apiTokuService->getWebhooks();
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
/**
* @OA\Post(
* path="/toku/webhook-endpoints",
* summary="Crear Webhook Endpoint",
* tags={"Toku/Webhooks"},
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="enabled_events", type="array", @OA\Items(type="string"), example={"customer.created","invoice.created"}),
* @OA\Property(property="url", type="string", example="https://miapp.com/webhook/toku"),
* @OA\Property(property="status", type="string", example="enabled"),
* @OA\Property(property="custom_headers", type="object", example={"custom-header-1":"value1"})
* )
* ),
* @OA\Response(response=200, description="Webhook creado"),
* @OA\Response(response=422, description="Error de validación")
* )
*/
public function store(Request $request)
{
try {
$data = $this->apiTokuService->createWebhook($request->all());
return genericResponse(['data' => $data, 'message' => 'Webhook creado', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
/**
* @OA\Put(
* path="/toku/webhook-endpoints/{id}",
* summary="Editar Webhook Endpoint",
* tags={"Toku/Webhooks"},
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="string"), example="whe_hXPK3YBs4EUfw2KFZlB6R44MiFa07vFU"),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="enabled_events", type="array", @OA\Items(type="string"), example={"customer.updated"}),
* @OA\Property(property="url", type="string", example="https://miapp.com/webhook/update"),
* @OA\Property(property="status", type="string", example="disabled")
* )
* ),
* @OA\Response(response=200, description="Webhook actualizado"),
* @OA\Response(response=404, description="Webhook no encontrado")
* )
*/
public function updateWebhook($id, Request $request)
{
try {
$data = $this->apiTokuService->updateWebhook($id, $request->all());
return genericResponse(['data' => $data, 'message' => 'Webhook actualizado', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
/**
* @OA\Get(
* path="/toku/webhook-endpoints/{id}",
* summary="Obtener un Webhook Endpoint",
* tags={"Toku/Webhooks"},
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="string"), example="whe_l8ojrji3k8SC7iLbVBxxgwVeOLRRbkEc"),
* @OA\Response(response=200, description="Webhook obtenido"),
* @OA\Response(response=404, description="Webhook no encontrado")
* )
*/
public function show($id)
{
try {
$data = $this->apiTokuService->getWebhook($id);
return genericResponse(['data' => $data, 'message' => 'ok', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
/**
* @OA\Post(
* path="/toku/dummy-webhook",
* summary="Probar un Webhook con evento dummy",
* tags={"Toku/Webhooks"},
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="event_type", type="string", example="invoice.created"),
* @OA\Property(property="url", type="string", example="https://miapp.com/webhook/test")
* )
* ),
* @OA\Response(response=200, description="Webhook dummy enviado"),
* @OA\Response(response=400, description="Error en prueba de webhook")
* )
*/
public function testDummy(Request $request)
{
try {
$data = $this->apiTokuService->testWebhook($request->all());
return genericResponse(['data' => $data, 'message' => 'Webhook dummy enviado', 'code' => 200], 200);
} catch (Exception $e) {
$errorCode = $e->getCode();
return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
}
}
}