"use client"; import { Elements, PaymentElement, useElements, useStripe, } from "@stripe/react-stripe-js"; import type { Stripe } from "@stripe/stripe-js"; import { ArrowRight, ExternalLink } from "lucide-react"; import Link from "next/link"; import { type FormEvent, useState } from "react"; import LoadingButton from "~/components/custom_ui/loading-button"; import { LoadingPage } from "~/components/loading"; import { Status500 } from "~/components/status-page"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card"; import { env } from "~/env"; import { formatCurrency } from "~/lib/utils"; import { useTranslation } from "~/providers/I18nProvider"; import type { PaymentsId } from "~/schemas/public/Payments"; import type { PrezziarioIdprezziario } from "~/schemas/public/Prezziario"; import { api } from "~/utils/api"; type AcquistoProcessingProps = { paymentId: PaymentsId; packId: PrezziarioIdprezziario; stripePromise: Promise; }; export const AcquistoProcessing = ({ paymentId, packId, stripePromise, }: AcquistoProcessingProps) => { const [stripeIntent, setStripeIntent] = useState<{ clientSecret: string | null; titolo: string; prezzo: number; descrizione: string; } | null>(null); const { t } = useTranslation(); const [generatedIntent, setGeneratedIntent] = useState(false); const { mutate, isPending } = api.stripe.createIntent.useMutation({ onSuccess: (data) => { setStripeIntent(data); }, }); if (generatedIntent && !isPending && !stripeIntent) { return ; } return (

{t.acquisto.titolo}

{(() => { if (!generatedIntent) { return ( { setGeneratedIntent(true); mutate({ paymentId, }); }} packId={packId} /> ); } if (isPending) { return ; } if (stripeIntent?.clientSecret) { return ( <> ); } })()}
); }; const PricePreparation = ({ packId, onProcedi, }: { packId: PrezziarioIdprezziario; onProcedi: () => void; }) => { const { data: prezziario, isLoading } = api.prezziario.getPrezzoPerServizio.useQuery({ idprezziario: packId, }); const { t } = useTranslation(); return (
{isLoading || !prezziario ? ( <>

{t.acquisto.preparazione}

) : (

{prezziario.nome_it}

Cod: “{prezziario.idprezziario}”

{formatCurrency(prezziario.prezzo_cent / 100)}

{prezziario.desc_it}

{prezziario.testo_condizioni && ( )}
)}
); }; const CheckoutForm = ({ titolo, prezzo, descrizione, }: { titolo: string; prezzo: number; descrizione: string; }) => { const stripe = useStripe(); const elements = useElements(); const [message, setMessage] = useState(null); const [isLoading, setIsLoading] = useState(false); const { t } = useTranslation(); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); console.log("submitting payment"); if (!stripe || !elements) { return; } setIsLoading(true); const { error } = await stripe.confirmPayment({ confirmParams: { return_url: `${env.NEXT_PUBLIC_BASE_URL}/servizio/acquisto-processing`, }, elements, }); if (error.type === "card_error" || error.type === "validation_error") { setMessage(error.message || "An unexpected error occurred."); } else { setMessage("An unexpected error occurred."); } setIsLoading(false); }; return (

{titolo}

{descrizione}

{formatCurrency(prezzo / 100)} {" "} {t.pricing_cmp.risultati_iva}
{t.procedi} {message &&
{message}
}
); };