File size: 6,293 Bytes
9d4a3b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use client";

import { useEffect, useState } from "react";
import { Check, CreditCard, ExternalLink, ShieldCheck, Sparkles } from "lucide-react";
import { StripeCardForm } from "@/components/StripeCardForm";

type Subscription = {
  status: string;
  currentPeriodEnd: string | null;
  cancelAtPeriodEnd: boolean;
} | null;

export function PremiumView() {
  const [subscription, setSubscription] = useState<Subscription>(null);
  const [billingAvailable, setBillingAvailable] = useState(true);
  const [billingMode, setBillingMode] = useState("stripe-test");
  const [loading, setLoading] = useState(true);
  const [action, setAction] = useState<"checkout" | "portal" | null>(null);
  const [error, setError] = useState("");
  const [cardSetup, setCardSetup] = useState<{ setupIntentId: string } | null>(null);

  useEffect(() => {
    fetch("/api/billing/subscription")
      .then(async (response) => {
        if (response.status === 401) return;
        const result = await response.json() as { subscription?: Subscription; billingAvailable?: boolean; mode?: string; error?: string };
        if (!response.ok) throw new Error(result.error ?? "Unable to load billing.");
        setSubscription(result.subscription ?? null);
        setBillingAvailable(result.billingAvailable !== false);
        setBillingMode(result.mode ?? "stripe-test");
      })
      .catch((caught: unknown) => setError(caught instanceof Error ? caught.message : "Unable to load billing."))
      .finally(() => setLoading(false));
  }, []);

  async function openBilling(endpoint: "checkout" | "portal") {
    setAction(endpoint);
    setError("");
    try {
      const response = await fetch(`/api/billing/${endpoint}`, {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: "{}",
      });
      const result = await response.json() as { url?: string; setupIntentId?: string; error?: string };
      if (!response.ok) throw new Error(result.error ?? "Unable to open Stripe.");
      if (endpoint === "checkout") {
        if (!result.setupIntentId) throw new Error("Stripe did not prepare the test card simulation.");
        setCardSetup({ setupIntentId: result.setupIntentId });
        setAction(null);
      } else {
        if (!result.url) throw new Error("Unable to open Stripe.");
        window.location.assign(result.url);
      }
    } catch (caught) {
      setError(caught instanceof Error ? caught.message : "Unable to open Stripe.");
      setAction(null);
    }
  }

  const active = subscription && ["active", "trialing"].includes(subscription.status);

  return (
    <div className="premium-page">
      <header className="premium-header">
        <div>
          <span className="premium-kicker"><Sparkles size={17} /> LinkedIn Premium</span>
          <h1>More signal for your professional work</h1>
          <p>Advanced discovery and profile tools with secure Stripe Test Mode billing.</p>
        </div>
        <div className="premium-trust"><ShieldCheck size={21} /><span><b>Stripe Test Mode</b> Test values stay in your browser while Stripe binds its standard sandbox card to this account.</span></div>
      </header>

      <section className="premium-plan" aria-labelledby="premium-plan-title">
        <div className="premium-plan-copy">
          <span>Individual plan</span>
          <h2 id="premium-plan-title">Premium Career</h2>
          <p>One subscription for deeper job insights, stronger profile discovery, and focused learning.</p>
          <ul>
            <li><Check size={18} /> Applicant and company insights</li>
            <li><Check size={18} /> Advanced people and job search filters</li>
            <li><Check size={18} /> Full learning library access</li>
            <li><Check size={18} /> Profile discovery analytics</li>
          </ul>
        </div>
        <div className="premium-plan-action">
          <div><strong>$29.99</strong><span>USD / month</span></div>
          {loading ? (
            <button className="button button-primary button-full" disabled>Loading billing…</button>
          ) : !billingAvailable ? (
            <button className="button button-outline button-full" disabled>
              <CreditCard size={18} /> Billing temporarily unavailable
            </button>
          ) : active ? (
            <>
              <div className="subscription-status"><Check size={18} /> {subscription.status === "trialing" ? "Trial active" : "Subscription active"}</div>
              {billingMode === "stripe-test" ? (
                <div className="subscription-status"><ShieldCheck size={18} /> Stripe sandbox payment verified</div>
              ) : (
                <button className="button button-outline button-full" disabled={action === "portal"} onClick={() => openBilling("portal")}>
                  <CreditCard size={18} /> {action === "portal" ? "Opening…" : "Manage subscription"} <ExternalLink size={15} />
                </button>
              )}
            </>
          ) : cardSetup ? (
            <StripeCardForm
              setupIntentId={cardSetup.setupIntentId}
              onCancel={() => setCardSetup(null)}
              onComplete={(nextSubscription) => {
                setSubscription(nextSubscription);
                setCardSetup(null);
                setAction(null);
                setError("");
              }}
            />
          ) : (
            <button className="button button-primary button-full" disabled={action === "checkout"} onClick={() => openBilling("checkout")}>
              <CreditCard size={18} /> {action === "checkout" ? "Preparing secure card form…" : "Add a test card"}
            </button>
          )}
          {!active && billingAvailable && !loading && !cardSetup && <small>No card is attached to this account. Only name, card number, expiration date, and security code are collected.</small>}
          {subscription?.currentPeriodEnd && <small>Current period ends {new Date(subscription.currentPeriodEnd).toLocaleDateString("en-US")}.</small>}
          {subscription?.cancelAtPeriodEnd && <small>Your plan is set to cancel at the end of the current period.</small>}
          {error && <div className="form-error" role="alert">{error}</div>}
        </div>
      </section>
    </div>
  );
}