app/Customize/EventSubscriber/LpFlowGuardSubscriber.php line 100

Open in your IDE?
  1. <?php
  2. namespace Customize\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. class LpFlowGuardSubscriber implements EventSubscriberInterface
  7. {
  8.     private const KEY 'lp_flow';
  9.     /** @var string[] */
  10.     private array $lpCheckoutRoutes = [
  11.         'shopping',
  12.         'shopping_shipping',
  13.         'shopping_nonmember',
  14.         'shopping_shipping_edit',
  15.         'shopping_error',
  16.         'shopping_confirm',
  17.         'shopping_complete',
  18.         'shopping_redirect_to',
  19.         'amazon_pay_shopping',
  20.         'plugin_stripe_credit_card',
  21.         'plugin_stripe_konbini',
  22.         'plugin_stripe_payment_method_detach',
  23.         'plugin_stripe_payment_gateway_payment',
  24.         'plugin_stripe_pr_shipping',
  25.         'plugin_stripe_pr_pay',
  26.         'plugin_stripe_confirm_intent',
  27.         'plugin_stripe_payment_gateway_webhook',
  28.         'amazon_pay_shopping_confirm',
  29.         'shopping_checkout',
  30.         'amazon_checkout_review',
  31.         'paidy_confirm',
  32.         'paidy_complete',
  33.         'get_stripe_public_key',
  34.         'submit_stripe_shopping',
  35.         'payment_callback',
  36.         'payment_success',
  37.         'payment_error'
  38.     ];
  39.     public static function getSubscribedEvents(): array
  40.     {
  41.         // Run fairly early, but after routing
  42.         return [
  43.             KernelEvents::REQUEST => ['onKernelRequest'10],
  44.         ];
  45.     }
  46.     public function onKernelRequest(RequestEvent $event): void
  47.     {
  48.         if (!$event->isMainRequest()) {
  49.             return;
  50.         }
  51.         $req $event->getRequest();
  52.         if (!$req->hasSession()) {
  53.             return;
  54.         }
  55.         $session $req->getSession();
  56.         $path    = (string) $req->getPathInfo();
  57.         $route   = (string) $req->attributes->get('_route''');
  58.         
  59.         // --- Never touch lp_flow for admin or Symfony debug routes ---
  60.         if (
  61.             str_starts_with($path'/anerosadmin')
  62.             || str_starts_with($route'admin_')
  63.             || $route === '_wdt'
  64.             || $route === '_profiler'
  65.             || $route === '_error'
  66.             || $route === '_fragment'
  67.             || str_starts_with($path'/_wdt')
  68.             || str_starts_with($path'/_profiler')
  69.         ) {
  70.             return;
  71.         }
  72.         $isLpPage   = ($path === '/lp') || str_starts_with($path'/lp/');
  73.         $isLpParam  = ((string) $req->query->get('lp''')) === '1'// optional
  74.         $isLpEntry  $isLpPage || $isLpParam;
  75.         //$isCheckout = in_array($route, $this->lpCheckoutRoutes, true);
  76.         $isCheckout =
  77.             in_array($route$this->lpCheckoutRoutestrue)
  78.             || str_starts_with($route'amazon_checkout_')
  79.             || str_starts_with($route'amazon_pay_')
  80.             || str_starts_with($route'plugin_stripe_')
  81.             || str_starts_with($route'paidy_')
  82.             || str_starts_with($route'stripe_')
  83.             || str_starts_with($route'stripe_payment_')
  84.             || str_starts_with($route'payment_');
  85. // enable lpdebug for the session if the URL has ?lpdebug=1
  86. if ($req->query->get('lpdebug') === '1') {
  87.     $session->set('lpdebug''1');
  88. }
  89. $lpdebug = ($session->get('lpdebug') === '1');
  90. if ($lpdebug) {
  91.     $ref = (string) $req->headers->get('referer''');
  92.     $uri = (string) $req->getRequestUri();
  93.     error_log(
  94.         '[LPFLOW HIT] route='.$route.
  95.         ' path='.$path.
  96.         ' uri='.$uri.
  97.         ' method='.$req->getMethod().
  98.         ' sid='.$session->getId().
  99.         ' lp_flow='.$session->get(self::KEY'0').
  100.         ' isCheckout='.($isCheckout '1' '0').
  101.         ' ref='.$ref
  102.     );
  103. }
  104.         // 1) Coming in through LP => mark flow
  105.         if ($isLpEntry) {
  106.             $session->set(self::KEY'1');
  107.             return;
  108.         }
  109.         
  110.         // 2) If they have lp_flow but are now OUTSIDE the LP flow, clear it
  111. if ($session->get(self::KEY) === '1' && !$isCheckout) {
  112.     if ($lpdebug) {
  113.         $ref = (string) $req->headers->get('referer''');
  114.         $uri = (string) $req->getRequestUri();
  115.         error_log('[LPFLOW CLEAR] route='.$route.' path='.$path.' uri='.$uri.' method='.$req->getMethod().' sid='.$session->getId().' ref='.$ref);
  116.     }
  117.     $session->remove(self::KEY);
  118.             // Optional: clear any other LP-specific session keys you set
  119.             foreach ([
  120.                 'lp_starter_added_at',
  121.                 'lp_starter_seeded',
  122.                 'lp_starter_seeded_at',
  123.                 'lp_starter_pc_id',
  124.                 'lp_cleanup_ttl',
  125.                 'lp_starter_seed_reason',
  126.             ] as $k) {
  127.                 $session->remove($k);
  128.             }
  129.         }
  130.     }
  131. }