<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lokesh </title>
    <description>The latest articles on Forem by Lokesh  (@lokisri).</description>
    <link>https://forem.com/lokisri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1164860%2F4fde23e5-b07c-4a5c-b18f-ef3823e9ef6f.png</url>
      <title>Forem: Lokesh </title>
      <link>https://forem.com/lokisri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lokisri"/>
    <language>en</language>
    <item>
      <title>Firefox Navigation Bug with Razorpay Payment Gateway - Here's the Fix</title>
      <dc:creator>Lokesh </dc:creator>
      <pubDate>Mon, 13 Oct 2025 12:47:49 +0000</pubDate>
      <link>https://forem.com/lokisri/firefox-navigation-bug-with-razorpay-payment-gateway-heres-the-fix-3a2</link>
      <guid>https://forem.com/lokisri/firefox-navigation-bug-with-razorpay-payment-gateway-heres-the-fix-3a2</guid>
      <description>&lt;p&gt;I recently encountered a frustrating bug while integrating Razorpay payment gateway in a Next.js application. After completing a payment, Firefox would automatically navigate back to the previous page (flight results in my case), while Chrome, Safari, and Edge worked perfectly fine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Symptoms:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Payment completes successfully&lt;/li&gt;
&lt;li&gt;Verification loading spinner appears briefly&lt;/li&gt;
&lt;li&gt;Firefox suddenly navigates back to the previous page&lt;/li&gt;
&lt;li&gt;Success message never displays&lt;/li&gt;
&lt;li&gt;Issue is &lt;strong&gt;Firefox-specific only&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Console Warnings (Red Herrings)
&lt;/h2&gt;

&lt;p&gt;Firefox threw several warnings that initially seemed related:&lt;br&gt;
However, these were &lt;strong&gt;not the root cause&lt;/strong&gt; - the real issue was Firefox's handling of &lt;code&gt;window.history&lt;/code&gt; when the Razorpay modal closes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Root Cause 🔍
&lt;/h2&gt;

&lt;p&gt;Firefox treats the Razorpay modal closure differently than other browsers. When the payment modal closes, Firefox interprets it as a navigation event and triggers a &lt;code&gt;popstate&lt;/code&gt; event, causing the browser to go back in history.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution ✅
&lt;/h2&gt;

&lt;p&gt;The fix involves preventing Firefox from navigating back by managing the browser history state:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
typescript
const onPayClick = async (e: React.MouseEvent&amp;lt;HTMLButtonElement&amp;gt;) =&amp;gt; {
  e.preventDefault();

  try {
    // Your existing order creation and Razorpay script loading...
    const order = await createOrder(fixedAmount, id);
    const loaded = await loadRazorpayScript();
    if (!loaded) throw new Error('Razorpay SDK failed to load');

    // 🔥 FIX: Prevent Firefox back navigation
    const preventBackNav = (e: PopStateEvent) =&amp;gt; {
      window.history.pushState(null, '', window.location.href);
    };

    // Push initial state
    window.history.pushState(null, '', window.location.href);
    window.addEventListener('popstate', preventBackNav);

    const options = {
      key: process.env.NEXT_PUBLIC_RAZORPAY_KEY_ID,
      amount: order.amount,
      currency: order.currency,
      name: 'Your Company',
      order_id: order.id,

      handler: async (response: any) =&amp;gt; {
        // ✅ Clean up listener on success
        window.removeEventListener('popstate', preventBackNav);

        setIsVerifying(true);

        try {
          // Your payment verification logic...
          const data = await verifyPayment(response, id);

          if (data.success) {
            setBookingDetails(data);
            // Show success message
          }
        } catch (err) {
          console.error('Verification error:', err);
        } finally {
          setIsVerifying(false);
        }
      },

      modal: {
        ondismiss: () =&amp;gt; {
          // ✅ Clean up listener on modal dismiss
          window.removeEventListener('popstate', preventBackNav);
          console.log('Checkout closed by user');
        },
      },
    };

    const rzp = new window.Razorpay(options);

    rzp.on('payment.failed', (resp: any) =&amp;gt; {
      // ✅ Clean up listener on payment failure
      window.removeEventListener('popstate', preventBackNav);
      console.error('Payment failed:', resp.error);
    });

    rzp.open();
  } catch (err) {
    console.error('Payment flow error:', err);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>firefox</category>
      <category>bugfix</category>
      <category>razorpay</category>
      <category>react</category>
    </item>
  </channel>
</rss>
