| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  | import type { NextApiRequest, NextApiResponse } from "next"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-09 22:56:05 +00:00
										 |  |  | import { getStripeCustomerIdFromUserId } from "@calcom/stripe/customer"; | 
					
						
							|  |  |  | import stripe from "@calcom/stripe/server"; | 
					
						
							| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { getSession } from "@lib/auth"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default async function handler(req: NextApiRequest, res: NextApiResponse) { | 
					
						
							|  |  |  |   if (req.method === "POST") { | 
					
						
							|  |  |  |     // Check that user is authenticated
 | 
					
						
							|  |  |  |     const session = await getSession({ req }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!session) { | 
					
						
							|  |  |  |       res.status(401).json({ message: "You must be logged in to do this" }); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-03 19:29:19 +00:00
										 |  |  |     const customerId = await getStripeCustomerIdFromUserId(session.user.id); | 
					
						
							| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 23:35:26 +00:00
										 |  |  |     if (!customerId) { | 
					
						
							|  |  |  |       res.status(500).json({ message: "Missing customer id" }); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 08:04:30 +00:00
										 |  |  |     const return_url = `${process.env.BASE_URL}/settings/billing`; | 
					
						
							| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  |     const stripeSession = await stripe.billingPortal.sessions.create({ | 
					
						
							| 
									
										
										
										
											2021-12-30 16:42:06 +00:00
										 |  |  |       customer: customerId, | 
					
						
							| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  |       return_url, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 16:21:45 +00:00
										 |  |  |     res.redirect(302, stripeSession.url); | 
					
						
							| 
									
										
										
										
											2021-09-27 22:57:23 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | } |