Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

1.1 KB
Raw Blame
import { useCallback, useState } from "react";
import type { Wallet } from "@processor/api/billing";
import { createPortalSession } from "@processor/billing/stripe";

/**
 * Opens the Stripe customer portal for the wallet's team. Card,
 * invoice, and cancellation changes all live in Stripe's hosted portal — both
 * the header "Manage Payment" action and the payment-method card's "Update"
 * button route through here.
 */
export function useStripePortal(wallet: Wallet | null) {
  const [opening, setOpening] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const open = useCallback(async () => {
    const teamId = wallet?.teamId;
    if (teamId == null) return;
    setOpening(true);
    setError(null);
    try {
      const url = await createPortalSession({
        teamId,
        returnUrl: window.location.href,
      });
      window.location.assign(url);
    } catch (e) {
      setError(e instanceof Error ? e.message : String(e));
    } finally {
      setOpening(false);
    }
  }, [wallet?.teamId]);

  return { open, opening, error };
}