infoalloggi-monorepo/apps/infoalloggi/src/pages/servizio/pagamento/[ordineId].tsx

119 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { loadStripe } from "@stripe/stripe-js";
2025-08-04 17:45:44 +02:00
import type { GetServerSideProps } from "next";
2025-08-28 18:27:07 +02:00
import { AcquistoProcessing } from "~/components/acquisto_processing";
2025-08-04 17:45:44 +02:00
import { AreaRiservataLayout } from "~/components/Layout";
import { LoadingPage } from "~/components/loading";
2025-08-28 18:27:07 +02:00
import { Status500 } from "~/components/status-page";
2025-08-04 17:45:44 +02:00
import { env } from "~/env.mjs";
2025-08-28 18:27:07 +02:00
import type { NextPageWithLayout } from "~/pages/_app";
import { useTranslation } from "~/providers/I18nProvider";
2025-08-28 18:27:07 +02:00
import type { OrdiniOrdineId } from "~/schemas/public/Ordini";
import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper";
import { zOrdineId } from "~/server/utils/zod_types";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
type ServizioPurchaseProps = {
2025-08-28 18:27:07 +02:00
ordineId: OrdiniOrdineId;
2025-08-04 17:45:44 +02:00
};
const ServicePurchasePage: NextPageWithLayout<ServizioPurchaseProps> = ({
2025-08-28 18:27:07 +02:00
ordineId,
2025-08-04 17:45:44 +02:00
}: ServizioPurchaseProps) => {
2025-08-28 18:27:07 +02:00
const { data, isLoading } = api.servizio.getServizioPaymentData.useQuery({
ordineId,
});
const { t } = useTranslation();
const stripePromise = loadStripe(env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (isLoading) {
return <LoadingPage />;
}
if (!data) {
return <Status500 />;
}
return (
<div className="mx-auto w-full max-w-6xl grow space-y-4 p-2 sm:px-8 sm:py-4">
<div className="flex justify-between">
<h3 className="text-primary text-3xl font-bold">
{t.acquisto.pagamento}
</h3>
</div>
<AcquistoProcessing
2025-08-29 16:18:32 +02:00
packId={data.ordine.packid}
2025-08-28 18:27:07 +02:00
paymentId={data.payment.id}
stripePromise={stripePromise}
/>
</div>
);
2025-08-04 17:45:44 +02:00
};
ServicePurchasePage.getLayout = function getLayout(page) {
2025-08-28 18:27:07 +02:00
return (
2025-08-29 16:18:32 +02:00
<AreaRiservataLayout noFooter noSidebar>
2025-08-28 18:27:07 +02:00
{page}
</AreaRiservataLayout>
);
2025-08-04 17:45:44 +02:00
};
export default ServicePurchasePage;
export const getServerSideProps = (async (context) => {
2025-08-28 18:27:07 +02:00
const id = context.params?.ordineId;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (typeof id !== "string") {
console.error("Error: ordineId is not a string");
return {
redirect: {
destination: "/500",
permanent: false,
},
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const parsed = zOrdineId.safeParse(id);
if (!parsed.success) {
console.error("Error parsing ordineId", parsed.error);
return {
redirect: {
destination: "/500",
permanent: false,
},
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const access_token = context.req.cookies.access_token;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const helper = await TrpcAuthedFetchingIstance({ access_token });
if (helper) {
const isAwaitingPayment =
await helper.trpc.servizio.isOrdineAwaitingPayment.fetch({
ordineId: parsed.data,
});
if (!isAwaitingPayment) {
return {
redirect: {
destination: "/area-riservata/dashboard",
permanent: false,
},
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
await helper.trpc.servizio.getServizioPaymentData.prefetch({
ordineId: parsed.data,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return {
props: {
ordineId: parsed.data,
2025-08-29 16:18:32 +02:00
trpcState: helper.trpc.dehydrate(),
2025-08-28 18:27:07 +02:00
},
};
}
return {
redirect: {
destination: "/500",
permanent: false,
},
};
2025-08-04 17:45:44 +02:00
}) satisfies GetServerSideProps<ServizioPurchaseProps>;