<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class DetectRegion
{
/**
* Mapa de dominios para configuracoes regionais
* CARD AO SEGUNDO CONTROL - Multi-Dominio
*/
protected array $regions = [
'card.aosegundo.pt' => [
'currency' => 'EUR',
'currency_symbol' => '',
'locale' => 'pt_PT',
'country' => 'PT',
'payment_gateway' => 'ifthen',
'stripe_key' => 'STRIPE_KEY_PT',
'stripe_secret' => 'STRIPE_SECRET_PT',
'flag' => 'PT',
'name' => 'Portugal',
],
'card.aosegundo.com.br' => [
'currency' => 'BRL',
'currency_symbol' => 'R$',
'locale' => 'pt_BR',
'country' => 'BR',
'payment_gateway' => 'asaas',
'stripe_key' => 'STRIPE_KEY_BR',
'stripe_secret' => 'STRIPE_SECRET_BR',
'flag' => 'BR',
'name' => 'Brasil',
],
'cardfyz.com' => [
'currency' => 'EUR',
'currency_symbol' => '',
'locale' => 'pt_BR',
'country' => 'BR',
'payment_gateway' => 'stripe_br',
'stripe_key' => 'STRIPE_KEY',
'stripe_secret' => 'STRIPE_SECRET',
'flag' => 'BR',
'name' => 'Card Ao Segundo (legado)',
],
];
public function handle(Request $request, Closure $next): Response
{
$host = $request->getHost();
$region = $this->regions[$host] ?? $this->regions['cardfyz.com'];
// Determinar gateway activo baseado no .env (dinamico - seguro dentro do handle)
if ($host === 'card.aosegundo.pt') {
$activeGateway = env('GATEWAY_PT', $region['payment_gateway']);
} elseif ($host === 'card.aosegundo.com.br') {
$activeGateway = env('GATEWAY_BR', $region['payment_gateway']);
} else {
$activeGateway = env('GATEWAY_BR', $region['payment_gateway']);
}
// Injectar configuracoes regionais em runtime
config([
'app.currency' => $region['currency'],
'app.currency_symbol' => $region['currency_symbol'],
'app.locale' => $region['locale'],
'app.country' => $region['country'],
'app.payment_gateway' => $activeGateway,
'app.region_flag' => $region['flag'],
'app.region_name' => $region['name'],
'app.url' => 'https://' . $host,
]);
// Partilhar com todas as views Blade
view()->share('appRegion', $region);
view()->share('appCurrency', $region['currency']);
view()->share('appCountry', $region['country']);
view()->share('currentDomain', $host);
view()->share('activeGateway', $activeGateway);
return $next($request);
}
}
|