infoalloggi-monorepo/apps/infoalloggi/src/server/controllers/stripe.controller.ts

420 lines
10 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { TRPCError } from "@trpc/server";
import { add, format } from "date-fns";
import { env } from "~/env";
import stripe from "~/lib/stripe";
import { orderTypeEnum } from "~/schemas/public/OrderTypeEnum";
import type { Ordini, OrdiniOrdineId } from "~/schemas/public/Ordini";
import { paymentStatusEnum } from "~/schemas/public/PaymentStatusEnum";
import type { TestiEStringheStingaId } from "~/schemas/public/TestiEStringhe";
import { tipologiaPosizioneEnum } from "~/schemas/public/TipologiaPosizioneEnum";
2025-08-28 18:27:07 +02:00
import { db } from "~/server/db";
2025-08-04 17:45:44 +02:00
import { NewMail } from "~/server/services/mailer";
import { CONDIZIONI_DEFAULT } from "~/server/services/prezziario.service";
2025-08-04 17:45:44 +02:00
import { getUser } from "~/server/services/user.service";
import { addEventToQueue } from "./event_queue.controller";
2025-08-04 17:45:44 +02:00
export const stripeHealthCheck = async () => {
try {
const endpoints = await stripe.webhookEndpoints.list();
return {
success: true,
endpoints,
secretKey: env.STRIPE_SECRET_KEY,
publicKey: env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY,
};
} catch (e) {
console.error("Stripe health check failed:", e);
return {
success: false,
endpoints: null,
secretKey: env.STRIPE_SECRET_KEY,
publicKey: env.NEXT_PUBLIC_STRIPE_PUBLIC_KEY,
};
}
};
2025-08-04 17:45:44 +02:00
export const whIntentCreatedHandler = async ({
2025-08-28 18:27:07 +02:00
pIntentId,
ordineId,
2025-08-04 17:45:44 +02:00
}: {
pIntentId: Ordini["intent_id"];
ordineId: OrdiniOrdineId;
2025-08-04 17:45:44 +02:00
}) => {
try {
return await db
.updateTable("ordini")
.set({
intent_id: pIntentId,
paymentstatus: paymentStatusEnum.enum.processing,
})
.where("ordine_id", "=", ordineId)
.returning("ordine_id")
.execute();
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error whIntentCreatedHandler: ${(error as Error).message}`,
});
}
2025-08-04 17:45:44 +02:00
};
export const whIntentSucceededHandler = async ({
2025-08-28 18:27:07 +02:00
pIntentId,
ordineId,
2025-08-28 18:27:07 +02:00
pm_id,
2025-08-04 17:45:44 +02:00
}: {
pIntentId: Ordini["intent_id"];
ordineId: OrdiniOrdineId;
2025-08-28 18:27:07 +02:00
pm_id: string;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
try {
const paymentMethod = await stripe.paymentMethods.retrieve(pm_id);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
await db.transaction().execute(async (trx) => {
const ordine = await trx
.updateTable("ordini")
2025-08-28 18:27:07 +02:00
.set({
paid_at: new Date(),
paymentmethod: paymentMethod.type,
paymentstatus: paymentStatusEnum.enum.success,
isActive: true,
2025-08-28 18:27:07 +02:00
})
.where("intent_id", "=", pIntentId)
.where("ordine_id", "=", ordineId)
.returningAll()
.executeTakeFirstOrThrow(() => {
throw new Error(
`Payment not found - intent_id: ${pIntentId}, ordineId: ${ordineId}`,
);
});
const condizioniFilename = `condizioni_contrattuali_${(ordine.paid_at || ordine.created_at).toISOString()}.pdf`;
const ricevutaFilename = `ricevuta_cortesia_${(ordine.paid_at || ordine.created_at).toISOString()}.pdf`;
const condizioni = await trx
.selectFrom("prezziario")
.select("testo_condizioni")
.where("idprezziario", "=", ordine.packid)
.executeTakeFirst();
2025-08-28 18:27:07 +02:00
const utente = await getUser({
db: trx,
userId: ordine.userid,
});
const lock_expires = add(new Date(), { seconds: 30 }); // Lock for 30 sec
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
switch (ordine.type) {
case orderTypeEnum.enum.Acconto: {
if (!ordine.servizio_id) {
throw new Error(
`No servizio_id for ordine_id: ${ordine.ordine_id}`,
);
}
const servizio = await trx
2025-08-28 18:27:07 +02:00
.updateTable("servizio")
.where("servizio_id", "=", ordine.servizio_id)
2025-08-28 18:27:07 +02:00
.set({
decorrenza: new Date(),
isOkAcconto: true,
})
.returning(["interruzioneDays"])
.executeTakeFirstOrThrow(
() =>
new Error(
`Failed to update servizio - servizioId: ${ordine.servizio_id}`,
),
);
if (utente.comms_enabled) {
await addEventToQueue({
data: {
tipo: "email",
data: {
template: {
mailType: "servizioAttivato",
},
mail: {
subject: "Pagamento Confermato - Acconto Infoalloggi.it",
to: utente.email,
attachments: [
{
filename: condizioniFilename,
generate: {
type: "condizioni",
stringId: (condizioni?.testo_condizioni ||
CONDIZIONI_DEFAULT) as TestiEStringheStingaId,
},
encoding: "base64",
contentType: "application/pdf",
2026-03-04 20:45:21 +01:00
},
{
filename: ricevutaFilename,
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64",
contentType: "application/pdf",
2026-03-04 20:45:21 +01:00
},
],
},
userId: utente.id,
},
},
lock_expires,
});
await addEventToQueue({
data: {
tipo: "email",
data: {
template: {
mailType: "avvisoInterruzione",
},
mail: {
subject: "Promemoria durata servizio - Infoalloggi.it",
to: utente.email,
},
userId: utente.id,
},
},
lock_expires: add(new Date(), {
days: servizio.interruzioneDays - 5,
}),
});
}
//SERVIZIO ATTIVATO
2025-08-28 18:27:07 +02:00
break;
}
case orderTypeEnum.enum.Saldo: {
if (!ordine.servizio_id) {
throw new Error(
`No servizio_id for ordine_id: ${ordine.ordine_id}`,
);
}
const servizio = await trx
.selectFrom("servizio")
.select(["servizio.tipologia"])
.where("servizio_id", "=", ordine.servizio_id)
.executeTakeFirstOrThrow(() => {
throw new Error(
`Servizio not found - servizio_id: ${ordine.servizio_id}`,
);
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
await trx
.updateTable("servizio")
.where("servizio_id", "=", ordine.servizio_id)
2025-08-28 18:27:07 +02:00
.set({
isOkConsulenza:
servizio.tipologia === tipologiaPosizioneEnum.enum.Transitorio,
2025-08-29 16:18:32 +02:00
isOkSaldo: true,
2025-08-28 18:27:07 +02:00
})
.execute();
if (utente.comms_enabled) {
await addEventToQueue({
data: {
tipo: "email",
data: {
template: {
mailType: "pagamentoConferma",
},
mail: {
subject: "Pagamento Confermato - Saldo Infoalloggi.it",
to: utente.email,
attachments: [
{
filename: ricevutaFilename,
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64",
contentType: "application/pdf",
2026-03-04 20:45:21 +01:00
},
],
},
userId: utente.id,
},
},
lock_expires,
});
}
2025-08-28 18:27:07 +02:00
break;
}
case orderTypeEnum.enum.Rinnovo: {
// nessuna azione aggiuntiva richiesta
if (utente.comms_enabled) {
await addEventToQueue({
data: {
tipo: "email",
data: {
template: {
mailType: "pagamentoConferma",
},
mail: {
subject: "Pagamento Confermato - Rinnovo Infoalloggi.it",
to: utente.email,
attachments: [
{
filename: ricevutaFilename,
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64",
contentType: "application/pdf",
2026-03-04 20:45:21 +01:00
},
],
},
userId: utente.id,
},
},
lock_expires,
});
}
2025-08-28 18:27:07 +02:00
break;
}
case orderTypeEnum.enum.Consulenza: {
if (!ordine.servizio_id) {
throw new Error(
`No servizio_id for ordine_id: ${ordine.ordine_id}`,
);
}
await trx
.updateTable("servizio")
.where("servizio_id", "=", ordine.servizio_id)
.set({
isOkConsulenza: true,
})
.execute();
if (utente.comms_enabled) {
await addEventToQueue({
data: {
tipo: "email",
data: {
template: {
mailType: "pagamentoConferma",
},
mail: {
subject: "Pagamento Confermato - Consulenza Infoalloggi.it",
to: utente.email,
attachments: [
{
filename: ricevutaFilename,
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64",
contentType: "application/pdf",
2026-03-04 20:45:21 +01:00
},
],
},
userId: utente.id,
},
},
lock_expires,
});
}
2025-08-28 18:27:07 +02:00
break;
}
case orderTypeEnum.enum.Altro:
break;
2025-08-28 18:27:07 +02:00
}
await addEventToQueue({
data: {
tipo: "email",
data: {
template: {
mailType: "generic",
props: {
title: "Nuovo pagamento ricevuto",
testo: `È stato ricevuto un nuovo pagamento.`,
},
},
mail: {
subject: "Ric pagamento",
to: "web@infoalloggi.it",
attachments: [
{
filename: `ricevuta_cortesia_${utente.username.replace(/\s+/g, "-")}-${format(ordine.created_at, "dd-MM-yyyy")}.pdf`,
generate: {
type: "ricevuta_cortesia",
ordineId: ordine.ordine_id,
},
encoding: "base64",
contentType: "application/pdf",
},
{
filename: condizioniFilename,
generate: {
type: "condizioni",
stringId: (condizioni?.testo_condizioni ||
CONDIZIONI_DEFAULT) as TestiEStringheStingaId,
},
encoding: "base64",
contentType: "application/pdf",
},
],
},
},
},
lock_expires,
});
2025-08-28 18:27:07 +02:00
});
return true;
} catch (error) {
console.error("whIntentSucceededHandler error:", {
pIntentId,
ordineId,
pm_id,
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
2025-08-28 18:27:07 +02:00
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Error updating payment intent: ${(error as Error).message}`,
cause: error,
2025-08-28 18:27:07 +02:00
});
}
2025-08-04 17:45:44 +02:00
};
export const whIntentFailedHandler = async ({
2025-08-28 18:27:07 +02:00
pIntentId,
2025-08-04 17:45:44 +02:00
}: {
pIntentId: Ordini["intent_id"];
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
const userId = await db
.updateTable("ordini")
2025-08-28 18:27:07 +02:00
.set({
paymentstatus: paymentStatusEnum.enum.failed,
2025-08-28 18:27:07 +02:00
})
.returning("userid")
.where("paymentstatus", "=", paymentStatusEnum.enum.processing)
2025-08-28 18:27:07 +02:00
.where("intent_id", "=", pIntentId)
.execute();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (userId[0]) {
const utente = await getUser({
db,
userId: userId[0].userid,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
await NewMail({
template: {
mailType: "pagamentoErrore",
},
mail: {
subject: "Pagamento Fallito - Infoalloggi.it",
to: utente.email,
},
2025-08-29 16:18:32 +02:00
userId: utente.id,
2025-08-28 18:27:07 +02:00
});
}
return true;
2025-08-04 17:45:44 +02:00
};