From 8d5fbc3b647beae5dd986cf0bbcd6c695ffc520d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tianzhou=20=28=E5=A4=A9=E8=88=9F=29?= Date: Thu, 18 May 2023 22:50:33 +0800 Subject: [PATCH] fix: use new stripe api to fetch receipt from latest charge (#97) --- src/pages/api/webhook/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/pages/api/webhook/index.ts b/src/pages/api/webhook/index.ts index 8b40cf4..b12a38a 100644 --- a/src/pages/api/webhook/index.ts +++ b/src/pages/api/webhook/index.ts @@ -49,6 +49,10 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => { // Cast event data to Stripe object. if (event.type === "payment_intent.succeeded") { const paymentIntent = event.data.object as Stripe.PaymentIntent; + const charge = await stripe.charges.retrieve( + paymentIntent.latest_charge as string + ); + const customerId = paymentIntent.customer as string; if (customerId) { // Save the stripe customer id so that we can relate this customer to future payments. @@ -86,7 +90,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => { description: paymentIntent.metadata.description, amount: paymentIntent.amount, currency: paymentIntent.currency, - receipt: paymentIntent.charges.data[0].receipt_url, + receipt: charge.receipt_url as string, }; await prisma.subscription.create({ data: subscription }); } else if (event.type === "payment_intent.payment_failed") {