From 1e770227d0426f933ab6523d5b01edf328cbc3fc Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Wed, 24 Feb 2021 17:20:46 +0100 Subject: [PATCH] accounts/** --- packages/dashboard/package.json | 1 + .../pages/api/stripe/createCheckoutSession.js | 27 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/dashboard/package.json b/packages/dashboard/package.json index 8a911f07..dbefafcc 100644 --- a/packages/dashboard/package.json +++ b/packages/dashboard/package.json @@ -18,6 +18,7 @@ "dayjs": "^1.10.4", "express-jwt": "^6.0.0", "formik": "^2.2.6", + "got": "^11.8.1", "http-status-codes": "^2.1.4", "jwks-rsa": "^1.12.2", "ky": "0.25.1", diff --git a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js index 249b8e15..a145a015 100644 --- a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js +++ b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js @@ -1,8 +1,32 @@ +import got from "got"; import Stripe from "stripe"; import { StatusCodes } from "http-status-codes"; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); +const getAuthenticatedUser = async () => { + const { body } = await got.get("http://user", { + json: { + hello: "world", + }, + responseType: "json", + }); +}; + +const getStripeCustomerId = async (req) => { + console.log(req.header); + console.log(req.headers); + // const user = got(`http://accounts:3000/user/${userId}`).json(); + + // if (user.stripeCustomerId) { + // return user.stripeCustomerId; + // } + + // const customer = await stripe.customers.create(); + + return "cus_J09ECKPgFEPXoq"; +}; + export default async (req, res) => { if (req.method !== "POST") { return res.status(StatusCodes.NOT_FOUND).end(); @@ -21,7 +45,8 @@ export default async (req, res) => { // [customer_email] - lets you prefill the email input in the form // For full details see https://stripe.com/docs/api/checkout/sessions/create try { - const stripeCustomerId = "cus_J09ECKPgFEPXoq"; + const userId = req.headers["x-user"]; + const stripeCustomerId = await getStripeCustomerId(userId); const session = await stripe.checkout.sessions.create({ mode: "subscription", payment_method_types: ["card"],