(req: NextApiRequest, res: NextApiResponse)
| 27 | }; |
| 28 | |
| 29 | const handler = async (req: NextApiRequest, res: NextApiResponse) => { |
| 30 | if (req.method !== "POST") { |
| 31 | res.setHeader("Allow", "POST"); |
| 32 | return res.status(405).end("Method Not Allowed"); |
| 33 | } |
| 34 | |
| 35 | const rawBody = await readRequestBody(req); |
| 36 | const sig = req.headers["stripe-signature"]; |
| 37 | |
| 38 | let event; |
| 39 | try { |
| 40 | event = stripe.webhooks.constructEvent(rawBody, sig ?? "", endpointSecret); |
| 41 | } catch { |
| 42 | return res.status(400).send(`Webhook Error`); |
| 43 | } |
| 44 | |
| 45 | let invoiceId; |
| 46 | |
| 47 | switch (event.type) { |
| 48 | case "charge.succeeded": |
| 49 | invoiceId = event.data.object.metadata.invoiceId; |
| 50 | |
| 51 | if (invoiceId) { |
| 52 | await prisma.invoice.update({ |
| 53 | where: { |
| 54 | id: invoiceId, |
| 55 | }, |
| 56 | data: { |
| 57 | status: "PAID", |
| 58 | paidAt: new Date(), |
| 59 | }, |
| 60 | }); |
| 61 | |
| 62 | await sendPaymentNotification(invoiceId, "SUCCESS"); |
| 63 | } |
| 64 | break; |
| 65 | |
| 66 | case "charge.failed": |
| 67 | invoiceId = event.data.object.metadata.invoiceId; |
| 68 | |
| 69 | if (invoiceId) { |
| 70 | await prisma.invoice.update({ |
| 71 | where: { |
| 72 | id: invoiceId, |
| 73 | }, |
| 74 | data: { |
| 75 | status: "UNPAID", |
| 76 | paidAt: null, |
| 77 | }, |
| 78 | }); |
| 79 | |
| 80 | await sendPaymentNotification(invoiceId, "FAILURE"); |
| 81 | } |
| 82 | break; |
| 83 | |
| 84 | case "charge.expired": |
| 85 | invoiceId = event.data.object.metadata.invoiceId; |
| 86 |
nothing calls this directly
no test coverage detected