diff --git a/src/components/PlansSection.tsx b/src/components/PlansSection.tsx index cdf1278..7b46d20 100644 --- a/src/components/PlansSection.tsx +++ b/src/components/PlansSection.tsx @@ -1,12 +1,15 @@ import { useState, useMemo, useEffect } from "react"; -import { Loader2, CheckCircle2 } from "lucide-react"; +import { Loader2, CheckCircle2, CreditCard, FileText } from "lucide-react"; import { supabase } from "@/integrations/supabase/client"; import { useAuth } from "@/hooks/useAuth"; import { toast } from "sonner"; import { z } from "zod"; +import { StripeEmbeddedCheckout } from "@/components/StripeEmbeddedCheckout"; +import { getStripeEnvironment, hasPaymentsConfigured } from "@/lib/stripe"; const PRICE_PER_KG = 325; const KG_PER_EMPLOYEE = 0.3; +const WEEKS_PER_MONTH = 4; const isStockholmPostalCode = (postalCode: string) => { const cleaned = postalCode.replace(/\s/g, ""); @@ -29,13 +32,17 @@ const formSchema = z.object({ kommentarer: z.string().trim().max(1000).optional(), }); +type Method = "card" | "invoice"; + const PlansSection = () => { const { user } = useAuth(); const [employees, setEmployees] = useState(10); const [showForm, setShowForm] = useState(false); + const [method, setMethod] = useState("card"); const [submitting, setSubmitting] = useState(false); - const [submitted, setSubmitted] = useState(false); + const [submitted, setSubmitted] = useState(null); const [errors, setErrors] = useState>({}); + const [clientSecret, setClientSecret] = useState(null); const [form, setForm] = useState({ företag: "", epost: "", @@ -51,8 +58,8 @@ const PlansSection = () => { [employees] ); const weeklyPrice = recommendedKg * PRICE_PER_KG; + const monthlyPrice = weeklyPrice * WEEKS_PER_MONTH; - // Prefyll från profil om inloggad useEffect(() => { if (!user || !showForm) return; (async () => { @@ -93,31 +100,79 @@ const PlansSection = () => { return; } + if (!hasPaymentsConfigured()) { + toast.error("Betalningar är inte konfigurerade ännu."); + return; + } + setSubmitting(true); try { - const { data, error } = await supabase.functions.invoke("submit-order", { - body: { - ...result.data, - employees, - kg_per_vecka: recommendedKg, - pris_per_vecka: weeklyPrice, - }, - }); + const body = { + method, + employees, + kg_per_week: recommendedKg, + company: result.data.företag, + email: result.data.epost, + phone: result.data.telefon ?? "", + address: result.data.adress ?? "", + postal_code: result.data.postnummer, + city: result.data.stad ?? "", + comments: result.data.kommentarer ?? "", + return_url: `${window.location.origin}/?checkout=success&session_id={CHECKOUT_SESSION_ID}`, + environment: getStripeEnvironment(), + }; + + const { data, error } = await supabase.functions.invoke("create-checkout", { body }); if (error) throw error; if ((data as any)?.error) throw new Error((data as any).error); - setSubmitted(true); - } catch (err) { + + if (method === "card") { + if (!(data as any)?.clientSecret) throw new Error("Kunde inte skapa checkout-session"); + setClientSecret((data as any).clientSecret); + } else { + setSubmitted("invoice"); + } + } catch (err: any) { console.error(err); - toast.error("Kunde inte skicka beställningen. Försök igen."); + toast.error(err?.message || "Kunde inte starta abonnemang. Försök igen."); } finally { setSubmitting(false); } }; + // Show embedded checkout after we get clientSecret + if (clientSecret) { + return ( +
+
+
+
+

+ Slutför betalning +

+

+ {recommendedKg} kg/vecka · {monthlyPrice.toLocaleString("sv-SE")} kr/månad (exkl. moms) +

+
+
+ clientSecret} /> +
+
+ +
+
+
+ ); + } + return (
- {/* Header */}

@@ -128,25 +183,23 @@ const PlansSection = () => {

- {/* Calculator card */}
- {submitted ? ( + {submitted === "invoice" ? (

- Tack för din beställning! + Abonnemang startat!

- Vi har tagit emot din förfrågan för{" "} + Vi har skapat ditt abonnemang på{" "} {recommendedKg} kg kakor per vecka - {" "} - och kontaktar dig inom kort för att bekräfta leverans. + + . Första fakturan skickas till {form.epost} inom kort.

) : !showForm ? ( <> - {/* Employee count */}
{
- {/* Slider */}
{
- {/* Result */}
@@ -191,12 +242,14 @@ const PlansSection = () => {
- Pris per vecka + Pris per månad
- {weeklyPrice.toLocaleString("sv-SE")} kr + {monthlyPrice.toLocaleString("sv-SE")} kr +
+
+ exklusive moms · {weeklyPrice.toLocaleString("sv-SE")} kr/vecka
-
exklusive moms
+ +
+
+

- Vi kontaktar dig för att bekräfta leverans och betalning. + Alla priser exklusive moms. Moms (25%) läggs på vid betalning.

)}