import { Elements, useStripe } from "@stripe/react-stripe-js"; import { loadStripe } from "@stripe/stripe-js"; import { ArrowLeft } from "lucide-react"; import type { GetServerSideProps } from "next"; import Link from "next/link"; import { useRouter } from "next/router"; import { useEffect, useState } from "react"; import PaymentStatus from "~/components/payment_status"; import { ProgressRedirect } from "~/components/progress_redirect"; import { Button, buttonVariants } from "~/components/ui/button"; import { env } from "~/env.mjs"; import { cn } from "~/lib/utils"; import { useTranslation } from "~/providers/I18nProvider"; import PaymentStatusEnum from "~/schemas/public/PaymentStatusEnum"; import { api } from "~/utils/api"; type AcquistoProcessingProps = { client_secret: string; }; export const getServerSideProps = (async (context) => { if ( !context.query.payment_intent_client_secret || typeof context.query.payment_intent_client_secret !== "string" ) { return { redirect: { destination: "/500", permanent: false, statusCode: 500, }, }; } return { props: { client_secret: context.query.payment_intent_client_secret, }, }; }) satisfies GetServerSideProps; export default function AcquistoProcessing({ client_secret, }: AcquistoProcessingProps) { const { t } = useTranslation(); const stripePromise = loadStripe(env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY); return (

{t.acquisto_elaborazione.titolo}

); } const CompletePage = ({ secret }: { secret: string }) => { const stripe = useStripe(); const utils = api.useUtils(); const router = useRouter(); const { t } = useTranslation(); const [status, setStatus] = useState< "success" | "processing" | "failed" | "internal_error" | "not_found" >("processing"); useEffect(() => { const fetchPaymentIntent = async () => { if (!stripe) { return; } try { const { paymentIntent } = await stripe.retrievePaymentIntent(secret); if (!paymentIntent) { setStatus("internal_error"); return; } const payment = await utils.pagamenti.getPaymentFromIntent.fetch({ pIntentId: paymentIntent.id, }); if (!payment) { setStatus("not_found"); return; } if (paymentIntent.status === "succeeded") { if (payment.paymentstatus !== PaymentStatusEnum.success) { setStatus("internal_error"); } setStatus("success"); } else if (paymentIntent.status === "processing") { if (payment.paymentstatus !== PaymentStatusEnum.processing) { setStatus("internal_error"); } setStatus("processing"); } else { setStatus("failed"); } } catch (error) { console.error("Error retrieving payment intent:", error); } }; void fetchPaymentIntent(); }, [stripe, secret]); if (!status) { return null; } return (
{status === "success" && ( <> )} {status === "processing" && ( <> {t.acquisto_elaborazione.to_dashboard} )} {status === "failed" && ( <> {t.acquisto_elaborazione.to_chat} )} {status === "internal_error" && ( <> {t.acquisto_elaborazione.to_chat} )} {status === "not_found" && ( <> {t.acquisto_elaborazione.to_chat} )}
); };