import {
Elements,
PaymentElement,
useElements,
useStripe,
} from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import { type FormEvent, useState } from "react";
import { env } from "~/env";
import { formatCurrency } from "~/lib/utils";
import { useTranslation } from "~/providers/I18nProvider";
import LoadingButton from "./custom_ui/loading-button";
import { Card, CardContent, CardHeader, CardTitle } from "./ui/card";
const stripePromise = loadStripe(env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY);
export default function CheckoutForm({
clientSecret,
titolo,
prezzo,
descrizione,
}: {
clientSecret: string;
titolo: string;
prezzo: number;
descrizione: string;
}) {
return (
);
}
const PaymentForm = ({
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();
if (!stripe || !elements) {
return;
}
setIsLoading(true);
const { error } = await stripe.confirmPayment({
confirmParams: {
return_url: `${env.NEXT_PUBLIC_BASE_URL}/servizio/pagamento-status`,
},
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}
);
};