diff --git a/surfsense_backend/app/routes/stripe_routes.py b/surfsense_backend/app/routes/stripe_routes.py index 2ae4ce79f..43fa8c4b7 100644 --- a/surfsense_backend/app/routes/stripe_routes.py +++ b/surfsense_backend/app/routes/stripe_routes.py @@ -26,6 +26,7 @@ from app.schemas.stripe import ( CreateCheckoutSessionResponse, CreateTokenCheckoutSessionRequest, CreateTokenCheckoutSessionResponse, + FinalizeCheckoutResponse, PagePurchaseHistoryResponse, StripeStatusResponse, StripeWebhookResponse, @@ -65,7 +66,15 @@ def _get_checkout_urls(search_space_id: int) -> tuple[str, str]: ) base_url = config.NEXT_FRONTEND_URL.rstrip("/") - success_url = f"{base_url}/dashboard/{search_space_id}/purchase-success" + # Stripe substitutes ``{CHECKOUT_SESSION_ID}`` with the actual session id + # at redirect time. The frontend uses it to call /stripe/finalize-checkout + # which fulfils synchronously without waiting for the webhook — fixing the + # webhook-vs-redirect race where users land on /purchase-success before + # checkout.session.completed has been delivered. + success_url = ( + f"{base_url}/dashboard/{search_space_id}/purchase-success" + f"?session_id={{CHECKOUT_SESSION_ID}}" + ) cancel_url = f"{base_url}/dashboard/{search_space_id}/purchase-cancel" return success_url, cancel_url @@ -106,6 +115,17 @@ def _get_metadata(checkout_session: Any) -> dict[str, str]: return {str(key): str(value) for key, value in items} +# Canonical purchase_type metadata values. ``premium_credit`` was emitted +# by an earlier release of ``create_token_checkout_session`` so it's still +# accepted on the read side for backward compat with in-flight sessions. +_PURCHASE_TYPE_TOKEN_VALUES = frozenset({"premium_tokens", "premium_credit"}) + + +def _is_token_purchase(metadata: dict[str, str]) -> bool: + """Return True for premium-credit (a.k.a. premium_token) purchases.""" + return metadata.get("purchase_type", "page_packs") in _PURCHASE_TYPE_TOKEN_VALUES + + async def _get_or_create_purchase_from_checkout_session( db_session: AsyncSession, checkout_session: Any, @@ -470,8 +490,7 @@ async def stripe_webhook( return StripeWebhookResponse() metadata = _get_metadata(checkout_session) - purchase_type = metadata.get("purchase_type", "page_packs") - if purchase_type == "premium_tokens": + if _is_token_purchase(metadata): return await _fulfill_completed_token_purchase( db_session, checkout_session ) @@ -483,8 +502,7 @@ async def stripe_webhook( }: checkout_session = event.data.object metadata = _get_metadata(checkout_session) - purchase_type = metadata.get("purchase_type", "page_packs") - if purchase_type == "premium_tokens": + if _is_token_purchase(metadata): return await _mark_token_purchase_failed( db_session, str(checkout_session.id) ) @@ -504,6 +522,124 @@ async def stripe_webhook( return StripeWebhookResponse() +@router.get("/finalize-checkout", response_model=FinalizeCheckoutResponse) +async def finalize_checkout( + session_id: str, + user: User = Depends(current_active_user), + db_session: AsyncSession = Depends(get_async_session), +) -> FinalizeCheckoutResponse: + """Synchronously fulfil a checkout session from the success page. + + Solves the webhook-vs-redirect race: the user lands on + ``/dashboard//purchase-success?session_id=cs_...`` typically a + few hundred ms after paying, but Stripe's + ``checkout.session.completed`` webhook can take 5-30s+ to arrive. + Calling this endpoint on success-page mount fulfils the purchase + immediately by retrieving the session from Stripe's API and + invoking the same idempotent helpers the webhook uses. + + Idempotency: if the webhook has already fulfilled this purchase + (status=COMPLETED), the helpers short-circuit and we just return + the latest balance. Concurrent webhook + finalize calls are safe + because both acquire ``SELECT ... FOR UPDATE`` on the purchase row. + + Authorization: the session's ``client_reference_id`` must match the + authenticated user's id. This prevents a user from finalising + someone else's checkout session if they happen to know the id. + """ + stripe_client = get_stripe_client() + + try: + checkout_session = stripe_client.v1.checkout.sessions.retrieve(session_id) + except StripeError as exc: + logger.warning( + "finalize_checkout: stripe lookup failed for session=%s user=%s: %s", + session_id, + user.id, + exc, + ) + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Checkout session not found.", + ) from exc + + # Authorization check: the user finalising must be the user who + # initiated the checkout. ``client_reference_id`` is set in + # ``create_checkout_session`` / ``create_token_checkout_session``. + client_reference_id = getattr(checkout_session, "client_reference_id", None) + if client_reference_id != str(user.id): + logger.warning( + "finalize_checkout: ownership mismatch session=%s client_ref=%s user=%s", + session_id, + client_reference_id, + user.id, + ) + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="This checkout session does not belong to you.", + ) + + metadata = _get_metadata(checkout_session) + is_token = _is_token_purchase(metadata) + payment_status = getattr(checkout_session, "payment_status", None) + session_status = getattr(checkout_session, "status", None) + + is_paid = payment_status in {"paid", "no_payment_required"} + is_expired = session_status == "expired" + + if is_paid: + if is_token: + await _fulfill_completed_token_purchase(db_session, checkout_session) + else: + await _fulfill_completed_purchase(db_session, checkout_session) + elif is_expired: + if is_token: + await _mark_token_purchase_failed(db_session, str(checkout_session.id)) + else: + await _mark_purchase_failed(db_session, str(checkout_session.id)) + # Otherwise (e.g. payment_status="unpaid", session_status="open"), + # leave the purchase row alone — frontend will keep polling and the + # webhook will eventually win the race. + + # Refresh the user row so the response reflects any update applied + # by the fulfilment helpers in this same session. + await db_session.refresh(user) + + if is_token: + purchase = ( + await db_session.execute( + select(PremiumTokenPurchase).where( + PremiumTokenPurchase.stripe_checkout_session_id + == str(checkout_session.id) + ) + ) + ).scalar_one_or_none() + return FinalizeCheckoutResponse( + purchase_type="premium_tokens", + status=purchase.status.value if purchase else "pending", + premium_credit_micros_limit=user.premium_credit_micros_limit, + premium_credit_micros_used=user.premium_credit_micros_used, + premium_credit_micros_granted=( + purchase.credit_micros_granted if purchase else None + ), + ) + + purchase = ( + await db_session.execute( + select(PagePurchase).where( + PagePurchase.stripe_checkout_session_id == str(checkout_session.id) + ) + ) + ).scalar_one_or_none() + return FinalizeCheckoutResponse( + purchase_type="page_packs", + status=purchase.status.value if purchase else "pending", + pages_limit=user.pages_limit, + pages_used=user.pages_used, + pages_granted=purchase.pages_granted if purchase else None, + ) + + @router.get("/purchases", response_model=PagePurchaseHistoryResponse) async def get_page_purchases( user: User = Depends(current_active_user), @@ -550,7 +686,11 @@ def _get_token_checkout_urls(search_space_id: int) -> tuple[str, str]: detail="NEXT_FRONTEND_URL is not configured.", ) base_url = config.NEXT_FRONTEND_URL.rstrip("/") - success_url = f"{base_url}/dashboard/{search_space_id}/purchase-success" + # See ``_get_checkout_urls`` for why session_id is appended. + success_url = ( + f"{base_url}/dashboard/{search_space_id}/purchase-success" + f"?session_id={{CHECKOUT_SESSION_ID}}" + ) cancel_url = f"{base_url}/dashboard/{search_space_id}/purchase-cancel" return success_url, cancel_url @@ -601,7 +741,11 @@ async def create_token_checkout_session( "user_id": str(user.id), "quantity": str(body.quantity), "credit_micros_per_unit": str(config.STRIPE_CREDIT_MICROS_PER_UNIT), - "purchase_type": "premium_credit", + # Canonical value matched by ``_is_token_purchase``. + # The legacy ``"premium_credit"`` is still accepted on + # the read side for any in-flight sessions started + # before this rename. + "purchase_type": "premium_tokens", }, } ) diff --git a/surfsense_backend/app/schemas/stripe.py b/surfsense_backend/app/schemas/stripe.py index 57265ec8e..ad13ddf04 100644 --- a/surfsense_backend/app/schemas/stripe.py +++ b/surfsense_backend/app/schemas/stripe.py @@ -56,6 +56,26 @@ class StripeWebhookResponse(BaseModel): received: bool = True +class FinalizeCheckoutResponse(BaseModel): + """Response from /stripe/finalize-checkout. + + Returned by the success page so the UI can show the post-purchase + balance immediately, even when the Stripe webhook hasn't been + delivered yet. ``status`` mirrors the underlying purchase row + (``pending`` / ``completed`` / ``failed``); the FE polls this + endpoint until it sees ``completed`` or a final ``failed``. + """ + + purchase_type: str # "page_packs" | "premium_tokens" + status: str # PagePurchaseStatus / PremiumTokenPurchaseStatus value + pages_limit: int | None = None + pages_used: int | None = None + pages_granted: int | None = None + premium_credit_micros_limit: int | None = None + premium_credit_micros_used: int | None = None + premium_credit_micros_granted: int | None = None + + class CreateTokenCheckoutSessionRequest(BaseModel): """Request body for creating a premium token purchase checkout session.""" diff --git a/surfsense_web/app/dashboard/[search_space_id]/purchase-success/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/purchase-success/page.tsx index 85bc4aaa6..b3d504ed5 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/purchase-success/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/purchase-success/page.tsx @@ -1,8 +1,9 @@ "use client"; -import { CheckCircle2 } from "lucide-react"; +import { AlertCircle, CheckCircle2, Loader2 } from "lucide-react"; import Link from "next/link"; -import { useParams } from "next/navigation"; +import { useParams, useSearchParams } from "next/navigation"; +import { useEffect, useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { Card, @@ -12,23 +13,133 @@ import { CardHeader, CardTitle, } from "@/components/ui/card"; +import type { FinalizeCheckoutResponse } from "@/contracts/types/stripe.types"; +import { stripeApiService } from "@/lib/apis/stripe-api.service"; + +type FinalizeState = + | { kind: "loading" } + | { kind: "completed"; data: FinalizeCheckoutResponse } + | { kind: "pending"; data: FinalizeCheckoutResponse } + | { kind: "still_pending"; data: FinalizeCheckoutResponse } + | { kind: "failed"; data: FinalizeCheckoutResponse } + | { kind: "error"; message: string } + | { kind: "no_session" }; + +const POLL_INTERVAL_MS = 2000; +const MAX_POLL_ATTEMPTS = 15; // ~30s total before falling back to the still_pending state export default function PurchaseSuccessPage() { const params = useParams(); + const searchParams = useSearchParams(); const searchSpaceId = String(params.search_space_id ?? ""); + const sessionId = searchParams.get("session_id"); + + const [state, setState] = useState( + sessionId ? { kind: "loading" } : { kind: "no_session" } + ); + // Tracks active polling so component unmount cancels it + const cancelledRef = useRef(false); + + useEffect(() => { + if (!sessionId) return; + + cancelledRef.current = false; + + const poll = async (attempt: number): Promise => { + if (cancelledRef.current) return; + try { + const data = await stripeApiService.finalizeCheckout(sessionId); + if (cancelledRef.current) return; + + if (data.status === "completed") { + setState({ kind: "completed", data }); + return; + } + if (data.status === "failed") { + setState({ kind: "failed", data }); + return; + } + + // Status is "pending" - either the user paid via async + // payment method (Klarna, ACH) or webhook + finalize both + // raced and lost. Keep polling up to MAX_POLL_ATTEMPTS, + // then fall back to a friendlier message that explains + // fulfilment may complete asynchronously. + if (attempt < MAX_POLL_ATTEMPTS) { + setState({ kind: "pending", data }); + setTimeout(() => poll(attempt + 1), POLL_INTERVAL_MS); + } else { + setState({ kind: "still_pending", data }); + } + } catch (err) { + if (cancelledRef.current) return; + const message = err instanceof Error ? err.message : "Unable to finalize checkout."; + setState({ kind: "error", message }); + } + }; + + void poll(1); + + return () => { + cancelledRef.current = true; + }; + }, [sessionId]); return (
- - Purchase complete - Your purchase is being applied to your account now. + {state.kind === "loading" || state.kind === "pending" ? ( + + ) : state.kind === "completed" ? ( + + ) : ( + + )} + + {state.kind === "loading" && "Confirming payment…"} + {state.kind === "pending" && "Processing your payment…"} + {state.kind === "still_pending" && "Payment still processing"} + {state.kind === "completed" && "Purchase complete"} + {state.kind === "failed" && "Purchase failed"} + {state.kind === "error" && "Couldn't confirm payment"} + {state.kind === "no_session" && "Purchase complete"} + + + {state.kind === "loading" && "We're verifying your payment with Stripe."} + {state.kind === "pending" && + "Your bank is taking a moment to confirm. This usually takes 5–30 seconds."} + {state.kind === "still_pending" && + "Your payment is still being processed by your bank. We'll apply your purchase as soon as it clears — usually within a few minutes. You can safely close this page."} + {state.kind === "completed" && + (state.data.purchase_type === "page_packs" + ? `Added ${formatNumber(state.data.pages_granted ?? 0)} pages to your account.` + : `Added ${formatCredit(state.data.premium_credit_micros_granted ?? 0)} of premium credit to your account.`)} + {state.kind === "failed" && + "Stripe reported the checkout as failed or expired. Your card was not charged."} + {state.kind === "error" && + "Don't worry — if your card was charged, your purchase will still apply within a minute or two."} + {state.kind === "no_session" && + "Your purchase is being applied to your account."} + -

- Your usage meters should refresh automatically in a moment. -

+ {state.kind === "completed" && state.data.purchase_type === "page_packs" && ( +

+ New balance: {formatNumber(state.data.pages_limit ?? 0)} total pages + {typeof state.data.pages_used === "number" + ? ` (${formatNumber((state.data.pages_limit ?? 0) - state.data.pages_used)} remaining)` + : ""} +

+ )} + {state.kind === "completed" && state.data.purchase_type === "premium_tokens" && ( +

+ New premium credit balance: {formatCredit(state.data.premium_credit_micros_limit ?? 0)} +

+ )} + {state.kind === "error" && ( +

{state.message}

+ )}
); } + +function formatNumber(n: number): string { + return new Intl.NumberFormat("en-US").format(n); +} + +function formatCredit(micros: number): string { + const dollars = micros / 1_000_000; + return new Intl.NumberFormat("en-US", { + style: "currency", + currency: "USD", + maximumFractionDigits: 2, + }).format(dollars); +} diff --git a/surfsense_web/contracts/types/stripe.types.ts b/surfsense_web/contracts/types/stripe.types.ts index 251f7a176..35ec0cb17 100644 --- a/surfsense_web/contracts/types/stripe.types.ts +++ b/surfsense_web/contracts/types/stripe.types.ts @@ -73,6 +73,19 @@ export const getTokenPurchasesResponse = z.object({ purchases: z.array(tokenPurchase), }); +// Response from /stripe/finalize-checkout. Either page or token fields +// are populated depending on purchase_type. +export const finalizeCheckoutResponse = z.object({ + purchase_type: z.enum(["page_packs", "premium_tokens"]), + status: pagePurchaseStatusEnum, + pages_limit: z.number().nullable().optional(), + pages_used: z.number().nullable().optional(), + pages_granted: z.number().nullable().optional(), + premium_credit_micros_limit: z.number().nullable().optional(), + premium_credit_micros_used: z.number().nullable().optional(), + premium_credit_micros_granted: z.number().nullable().optional(), +}); + export type PagePurchaseStatus = z.infer; export type CreateCheckoutSessionRequest = z.infer; export type CreateCheckoutSessionResponse = z.infer; @@ -85,3 +98,4 @@ export type TokenStripeStatusResponse = z.infer; export type TokenPurchase = z.infer; export type GetTokenPurchasesResponse = z.infer; +export type FinalizeCheckoutResponse = z.infer; diff --git a/surfsense_web/lib/apis/stripe-api.service.ts b/surfsense_web/lib/apis/stripe-api.service.ts index 6e74d7edc..f119fbf6a 100644 --- a/surfsense_web/lib/apis/stripe-api.service.ts +++ b/surfsense_web/lib/apis/stripe-api.service.ts @@ -5,6 +5,8 @@ import { type CreateTokenCheckoutSessionResponse, createCheckoutSessionResponse, createTokenCheckoutSessionResponse, + type FinalizeCheckoutResponse, + finalizeCheckoutResponse, type GetPagePurchasesResponse, type GetTokenPurchasesResponse, getPagePurchasesResponse, @@ -54,6 +56,20 @@ class StripeApiService { getTokenPurchases = async (): Promise => { return baseApiService.get("/api/v1/stripe/token-purchases", getTokenPurchasesResponse); }; + + /** + * Synchronously fulfil a checkout session from the success page. + * + * Solves the race where the user lands on /purchase-success before + * Stripe's checkout.session.completed webhook arrives. Idempotent — + * safe to call concurrently with the webhook. + */ + finalizeCheckout = async (sessionId: string): Promise => { + return baseApiService.get( + `/api/v1/stripe/finalize-checkout?session_id=${encodeURIComponent(sessionId)}`, + finalizeCheckoutResponse + ); + }; } export const stripeApiService = new StripeApiService();