diff --git a/src/App.tsx b/src/App.tsx index 7c5f4f5..c4382c5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -19,6 +19,7 @@ import { AuthModal } from './components/auth/AuthModal'; import { ProfileDrawer } from './components/auth/ProfileDrawer'; import { PaymentSuccessModal } from './components/checkout/PaymentSuccessModal'; import { PasswordUpdatedModal } from './components/auth/PasswordUpdatedModal'; +import { openMobileApp, DEFAULT_RETURN_ROUTE } from './utils/navigation'; const getInitialTab = (): 'storefront' | 'subscriptions' => { if (typeof window !== 'undefined') { @@ -43,14 +44,6 @@ export default function App() { } }; - useEffect(() => { - const handlePopState = () => { - setActiveTab(getInitialTab()); - }; - window.addEventListener('popstate', handlePopState); - return () => window.removeEventListener('popstate', handlePopState); - }, []); - const [currency, setCurrency] = useState('INR'); // Auth state @@ -106,6 +99,29 @@ export default function App() { // Active payment state const [verificationResult, setVerificationResult] = useState(null); const [razorpayKeyId, setRazorpayKeyId] = useState(''); + const [returnRoute, setReturnRoute] = useState(DEFAULT_RETURN_ROUTE); + const [appScheme, setAppScheme] = useState(undefined); + + // Handle browser back button navigation + useEffect(() => { + const handlePopState = () => { + // If purchase was completed and user presses the Back button, + // redirect them to the mobile app's All Modules / Library instead of Subscription page + if (verificationResult && verificationResult.status === 'success') { + const isMobile = /Android|iPhone|iPad|iPod/i.test(navigator.userAgent); + if (isMobile) { + openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined }); + } else { + switchTab('subscriptions'); + setVerificationResult(null); + } + return; + } + setActiveTab(getInitialTab()); + }; + window.addEventListener('popstate', handlePopState); + return () => window.removeEventListener('popstate', handlePopState); + }, [verificationResult, returnRoute, appScheme]); // Profile Update & Drawer State const [showProfileDrawer, setShowProfileDrawer] = useState(false); @@ -157,6 +173,15 @@ export default function App() { const params = new URLSearchParams(window.location.search); const urlToken = params.get('token'); const urlPlanCode = params.get('planCode'); + const urlReturnRoute = params.get('returnRoute') || params.get('redirectPath') || params.get('redirect'); + const urlScheme = params.get('scheme'); + + if (urlReturnRoute) { + setReturnRoute(urlReturnRoute); + } + if (urlScheme) { + setAppScheme(urlScheme); + } let activeToken = userToken; @@ -742,7 +767,13 @@ export default function App() { message: verifyRes.data.message || 'Payment verified successfully and module entitlements unlocked!' }); - // If on mobile browser, automatically attempt to redirect to mobile app after successful payment + + // Push a history state so that pressing browser/device Back button will be intercepted + // by popstate and redirected to All Modules / Library instead of returning to the Subscription page + if (typeof window !== 'undefined') { + window.history.pushState({ paymentCompleted: true }, document.title, window.location.href); + } + if (/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) { setTimeout(() => { if (/Android/i.test(navigator.userAgent)) { @@ -957,6 +988,8 @@ export default function App() { loggedInUser={loggedInUser} onDownloadInvoice={handleDownloadCheckoutInvoice} downloadingInvoice={downloadingModalInvoice} + returnRoute={returnRoute} + appScheme={appScheme} /> )} diff --git a/src/components/checkout/PaymentSuccessModal.tsx b/src/components/checkout/PaymentSuccessModal.tsx index 234bbfe..75ce43f 100644 --- a/src/components/checkout/PaymentSuccessModal.tsx +++ b/src/components/checkout/PaymentSuccessModal.tsx @@ -1,6 +1,7 @@ import React, { useState } from 'react'; import { XCircle, CheckCircle, Check, Download, RefreshCw, Smartphone } from 'lucide-react'; import type { VerificationResult, UserProfile } from '../../types'; +import { openMobileApp, getAppDeepLinkUrls } from '../../utils/navigation'; interface PaymentSuccessModalProps { verificationResult: VerificationResult; @@ -8,6 +9,8 @@ interface PaymentSuccessModalProps { loggedInUser: UserProfile | null; onDownloadInvoice: (paymentId: string, invoiceUrl?: string) => void; downloadingInvoice: boolean; + returnRoute?: string; + appScheme?: string; } export const PaymentSuccessModal: React.FC = ({ @@ -16,6 +19,8 @@ export const PaymentSuccessModal: React.FC = ({ loggedInUser, onDownloadInvoice, downloadingInvoice, + returnRoute, + appScheme, }) => { const [desktopNotice, setDesktopNotice] = useState(false); @@ -212,15 +217,18 @@ export const PaymentSuccessModal: React.FC = ({ )} {/* Open in App Button */} - - - Open in App - + {/* Open in App Button */} + { + e.preventDefault(); + openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined }); + }} + className="w-full h-9 bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-xs rounded-lg transition-all duration-300 flex items-center justify-center gap-2 shadow-md shadow-emerald-600/10 no-underline decoration-0 cursor-pointer" + > + + Open in App / Library + {desktopNotice && (
diff --git a/src/utils/navigation.ts b/src/utils/navigation.ts new file mode 100644 index 0000000..09cb581 --- /dev/null +++ b/src/utils/navigation.ts @@ -0,0 +1,46 @@ +/** + * Generic Navigation & Deep-Linking Utility for Acupedia Storefront + */ + +export const DEFAULT_APP_SCHEMES = ['acupedia', 'myapp']; +export const DEFAULT_RETURN_ROUTE = 'modules'; + +export interface MobileAppLaunchOptions { + route?: string; + schemes?: string[]; + delayBetweenFallbacks?: number; +} + +/** + * Cleanly format route for custom URI scheme (e.g. "/modules" -> "modules") + */ +export function formatDeepLinkRoute(route?: string): string { + if (!route) return DEFAULT_RETURN_ROUTE; + return route.replace(/^\/+/, '').trim() || DEFAULT_RETURN_ROUTE; +} + +/** + * Generate full deep-link URLs for supported schemes + */ +export function getAppDeepLinkUrls(options: MobileAppLaunchOptions = {}): string[] { + const route = formatDeepLinkRoute(options.route); + const schemes = options.schemes && options.schemes.length > 0 ? options.schemes : DEFAULT_APP_SCHEMES; + return schemes.map((scheme) => `${scheme}://${route}`); +} + +/** + * Generic function to launch the mobile app with progressive fallback across supported schemes + */ +export function openMobileApp(options: MobileAppLaunchOptions = {}): void { + const urls = getAppDeepLinkUrls(options); + if (urls.length === 0) return; + + const [primaryUrl, ...fallbackUrls] = urls; + window.location.href = primaryUrl; + + fallbackUrls.forEach((url, idx) => { + setTimeout(() => { + window.location.href = url; + }, (idx + 1) * (options.delayBetweenFallbacks || 500)); + }); +}