<?php
namespace Customize\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LpFlowGuardSubscriber implements EventSubscriberInterface
{
private const KEY = 'lp_flow';
/** @var string[] */
private array $lpCheckoutRoutes = [
'shopping',
'shopping_shipping',
'shopping_nonmember',
'shopping_shipping_edit',
'shopping_error',
'shopping_confirm',
'shopping_complete',
'shopping_redirect_to',
'amazon_pay_shopping',
'plugin_stripe_credit_card',
'plugin_stripe_konbini',
'plugin_stripe_payment_method_detach',
'plugin_stripe_payment_gateway_payment',
'plugin_stripe_pr_shipping',
'plugin_stripe_pr_pay',
'plugin_stripe_confirm_intent',
'plugin_stripe_payment_gateway_webhook',
'amazon_pay_shopping_confirm',
'shopping_checkout',
'amazon_checkout_review',
'paidy_confirm',
'paidy_complete',
'get_stripe_public_key',
'submit_stripe_shopping',
'payment_callback',
'payment_success',
'payment_error'
];
public static function getSubscribedEvents(): array
{
// Run fairly early, but after routing
return [
KernelEvents::REQUEST => ['onKernelRequest', 10],
];
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$req = $event->getRequest();
if (!$req->hasSession()) {
return;
}
$session = $req->getSession();
$path = (string) $req->getPathInfo();
$route = (string) $req->attributes->get('_route', '');
// --- Never touch lp_flow for admin or Symfony debug routes ---
if (
str_starts_with($path, '/anerosadmin')
|| str_starts_with($route, 'admin_')
|| $route === '_wdt'
|| $route === '_profiler'
|| $route === '_error'
|| $route === '_fragment'
|| str_starts_with($path, '/_wdt')
|| str_starts_with($path, '/_profiler')
) {
return;
}
$isLpPage = ($path === '/lp') || str_starts_with($path, '/lp/');
$isLpParam = ((string) $req->query->get('lp', '')) === '1'; // optional
$isLpEntry = $isLpPage || $isLpParam;
//$isCheckout = in_array($route, $this->lpCheckoutRoutes, true);
$isCheckout =
in_array($route, $this->lpCheckoutRoutes, true)
|| str_starts_with($route, 'amazon_checkout_')
|| str_starts_with($route, 'amazon_pay_')
|| str_starts_with($route, 'plugin_stripe_')
|| str_starts_with($route, 'paidy_')
|| str_starts_with($route, 'stripe_')
|| str_starts_with($route, 'stripe_payment_')
|| str_starts_with($route, 'payment_');
// enable lpdebug for the session if the URL has ?lpdebug=1
if ($req->query->get('lpdebug') === '1') {
$session->set('lpdebug', '1');
}
$lpdebug = ($session->get('lpdebug') === '1');
if ($lpdebug) {
$ref = (string) $req->headers->get('referer', '');
$uri = (string) $req->getRequestUri();
error_log(
'[LPFLOW HIT] route='.$route.
' path='.$path.
' uri='.$uri.
' method='.$req->getMethod().
' sid='.$session->getId().
' lp_flow='.$session->get(self::KEY, '0').
' isCheckout='.($isCheckout ? '1' : '0').
' ref='.$ref
);
}
// 1) Coming in through LP => mark flow
if ($isLpEntry) {
$session->set(self::KEY, '1');
return;
}
// 2) If they have lp_flow but are now OUTSIDE the LP flow, clear it
if ($session->get(self::KEY) === '1' && !$isCheckout) {
if ($lpdebug) {
$ref = (string) $req->headers->get('referer', '');
$uri = (string) $req->getRequestUri();
error_log('[LPFLOW CLEAR] route='.$route.' path='.$path.' uri='.$uri.' method='.$req->getMethod().' sid='.$session->getId().' ref='.$ref);
}
$session->remove(self::KEY);
// Optional: clear any other LP-specific session keys you set
foreach ([
'lp_starter_added_at',
'lp_starter_seeded',
'lp_starter_seeded_at',
'lp_starter_pc_id',
'lp_cleanup_ttl',
'lp_starter_seed_reason',
] as $k) {
$session->remove($k);
}
}
}
}