| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  | import { Prisma } from "@prisma/client"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | import type { NextApiRequest, NextApiResponse } from "next"; | 
					
						
							| 
									
										
										
										
											2021-09-28 21:27:06 +00:00
										 |  |  | import { stringify } from "querystring"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-09 22:56:05 +00:00
										 |  |  | import stripe, { StripeData } from "@calcom/stripe/server"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  | import { getSession } from "@lib/auth"; | 
					
						
							|  |  |  | import prisma from "@lib/prisma"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default async function handler(req: NextApiRequest, res: NextApiResponse) { | 
					
						
							| 
									
										
										
										
											2021-09-28 21:27:06 +00:00
										 |  |  |   const { code, error, error_description } = req.query; | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  |   // Check that user is authenticated
 | 
					
						
							|  |  |  |   const session = await getSession({ req: req }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!session?.user) { | 
					
						
							|  |  |  |     res.status(401).json({ message: "You must be logged in to do this" }); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-28 21:27:06 +00:00
										 |  |  |   if (error) { | 
					
						
							|  |  |  |     const query = stringify({ error, error_description }); | 
					
						
							|  |  |  |     res.redirect("/integrations?" + query); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 18:36:13 +00:00
										 |  |  |   const response = await stripe.oauth.token({ | 
					
						
							|  |  |  |     grant_type: "authorization_code", | 
					
						
							|  |  |  |     code: code.toString(), | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const data: StripeData = { ...response, default_currency: "" }; | 
					
						
							|  |  |  |   if (response["stripe_user_id"]) { | 
					
						
							|  |  |  |     const account = await stripe.accounts.retrieve(response["stripe_user_id"]); | 
					
						
							|  |  |  |     data["default_currency"] = account.default_currency; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   await prisma.credential.create({ | 
					
						
							|  |  |  |     data: { | 
					
						
							|  |  |  |       type: "stripe_payment", | 
					
						
							|  |  |  |       key: data as unknown as Prisma.InputJsonObject, | 
					
						
							|  |  |  |       userId: session.user.id, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   res.redirect("/integrations"); | 
					
						
							|  |  |  | } |