7b0a61803c
Adds withExceptions render callback for ValidationException that forces JSON 422 response when request matches api/webhook/supplier/* — regardless of Accept header. Default Laravel behavior is 302 redirect for non-JSON clients, which strips POST body. Observed on prod 2026-05-25: 76 of 234 supplier webhook hits got 302 (Location: /), mostly for non-B-prefix projects (client.carmoney.ru, cabinet.caranga.ru, cashmotor.ru). Supplier doesn't follow 302 redirects on POST, so the lead body is lost. This fix ensures supplier always sees a meaningful 422 with errors[] instead of a redirect. Other routes unaffected (render returns null for non-webhook URLs).
65 lines
2.8 KiB
PHP
65 lines
2.8 KiB
PHP
<?php
|
|
|
|
use App\Http\Middleware\EnsureSaasAdmin;
|
|
use App\Http\Middleware\SetTenantContext;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
)
|
|
->withMiddleware(function (Middleware $middleware): void {
|
|
// /api/auth/* размещены в web.php (см. routes/web.php) и используют
|
|
// session-based Sanctum auth. Token-based mode (через api.php) пока
|
|
// не нужен — добавим если понадобится для интеграций.
|
|
|
|
$middleware->alias([
|
|
'tenant' => SetTenantContext::class,
|
|
'saas-admin' => EnsureSaasAdmin::class,
|
|
]);
|
|
|
|
// Webhook receive endpoint (POST /api/webhook/{token}) не должен требовать
|
|
// CSRF — запросы приходят от внешних CRM-систем без сессии браузера.
|
|
// Авторизация — через webhook_token в URL + (на prod) HMAC.
|
|
$middleware->validateCsrfTokens(except: [
|
|
'api/webhook/*',
|
|
]);
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions): void {
|
|
$exceptions->render(function (QueryException $e, Request $request) {
|
|
Log::error('db.query_exception', [
|
|
'message' => $e->getMessage(),
|
|
'sql' => $e->getSql(),
|
|
'path' => $request->path(),
|
|
]);
|
|
if ($request->expectsJson()) {
|
|
return response()->json([
|
|
'message' => 'Не удалось сохранить. Проверьте данные или попробуйте ещё раз.',
|
|
], 422);
|
|
}
|
|
|
|
return null; // default render for non-JSON
|
|
});
|
|
|
|
// Supplier webhook always returns JSON, even when client omits Accept header.
|
|
// Without this render, Laravel's default ValidationException handler returns
|
|
// 302 redirect to /, which strips POST body — losing supplier leads.
|
|
// Confirmed 2026-05-25: 76 of 234 webhook hits today got 302 instead of 422.
|
|
$exceptions->render(function (\Illuminate\Validation\ValidationException $e, Request $request) {
|
|
if ($request->is('api/webhook/supplier/*')) {
|
|
return response()->json([
|
|
'message' => 'Validation failed',
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
}
|
|
return null; // default render for other routes
|
|
});
|
|
})->create();
|