Created
January 13, 2023 05:01
-
-
Save dipeshhkc/8bb96300ad09eaacc29c578e62a48378 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handleCheckoutSessionCompleted = async ( | |
session: Stripe.Event.Data.Object | |
) => { | |
const data = session as Stripe.Checkout.Session; | |
const user = await db.user.findFirst({ | |
where: { email: data.customer_email! }, | |
}); | |
const stripeSubscriptionId = data.subscription?.toString(); | |
const stripeSub = await stripe.subscriptions.retrieve(stripeSubscriptionId!); | |
const subItems = stripeSub.items.data; | |
const subItem = subItems[0]; | |
const subscription = await db.subscription.create({ | |
data: { | |
stripe_subscription_id: stripeSubscriptionId!, | |
stripe_customer_id: data.customer?.toString()!, | |
stripe_status: stripeSub.status, | |
userId: user!.id, | |
stripe_plan_id: subItem.price.id, | |
stripe_plan_name: data.metadata?.tier || subItem.plan.nickname, | |
}, | |
}); | |
await db.user.update({ | |
where: { id: user!.id }, | |
data: { | |
subscription_id: subscription.id, | |
}, | |
}); | |
console.log({ | |
message: `Session checkout completed`, | |
user, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment