fix: use new stripe api to fetch receipt from latest charge (#97)

This commit is contained in:
Tianzhou (天舟)
2023-05-18 22:50:33 +08:00
committed by GitHub
parent b4cfc6defc
commit 8d5fbc3b64

View File

@ -49,6 +49,10 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
// Cast event data to Stripe object. // Cast event data to Stripe object.
if (event.type === "payment_intent.succeeded") { if (event.type === "payment_intent.succeeded") {
const paymentIntent = event.data.object as Stripe.PaymentIntent; 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; const customerId = paymentIntent.customer as string;
if (customerId) { if (customerId) {
// Save the stripe customer id so that we can relate this customer to future payments. // 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, description: paymentIntent.metadata.description,
amount: paymentIntent.amount, amount: paymentIntent.amount,
currency: paymentIntent.currency, currency: paymentIntent.currency,
receipt: paymentIntent.charges.data[0].receipt_url, receipt: charge.receipt_url as string,
}; };
await prisma.subscription.create({ data: subscription }); await prisma.subscription.create({ data: subscription });
} else if (event.type === "payment_intent.payment_failed") { } else if (event.type === "payment_intent.payment_failed") {