From b4eb863dc9b8f69434217bddf802cc9c31ebd0c3 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Thu, 25 Feb 2021 11:29:40 +0100 Subject: [PATCH] accounts/** --- .../pages/api/stripe/createCheckoutSession.js | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js index 8e7524d1..a3dc0698 100644 --- a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js +++ b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js @@ -4,13 +4,11 @@ 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 getStripeCustomer = (stripeCustomerId = null) => { + if (stripeCustomerId) { + return stripe.customers.retrieve(user.stripeCustomerId); + } + return stripe.customers.create(); }; export default async (req, res) => { @@ -33,24 +31,20 @@ export default async (req, res) => { try { const authorization = req.headers.authorization; // authorization header from request const user = await got("http://accounts:3000/user", { headers: { authorization } }); + const customer = await getStripeCustomer(user.stripeCustomerId); if (!user.stripeCustomerId) { - const customer = await stripe.customers.create(); - - console.log(customer); - const user = await got.put(`http://accounts:3000/user`, { + await got.put(`http://accounts:3000/user`, { headers: { authorization }, - json: { stripeCustomerId }, + json: { stripeCustomerId: customer.id }, }); - console.log(user); } - const stripeCustomerId = "cus_J09ECKPgFEPXoq"; const session = await stripe.checkout.sessions.create({ mode: "subscription", payment_method_types: ["card"], line_items: [{ price, quantity: 1 }], - customer: stripeCustomerId, + customer: customer.id, // ?session_id={CHECKOUT_SESSION_ID} means the redirect will have the session ID set as a query param success_url: `${process.env.SKYNET_DASHBOARD_URL}/payments?session_id={CHECKOUT_SESSION_ID}`, cancel_url: `${process.env.SKYNET_DASHBOARD_URL}/payments`,