diff --git a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js index b2819fdd..eb7b7052 100644 --- a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js +++ b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js @@ -6,7 +6,7 @@ const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); const getStripeCustomer = (stripeCustomerId = null) => { if (stripeCustomerId) { - return stripe.customers.retrieve(user.stripeCustomerId); + return stripe.customers.retrieve(stripeCustomerId); } return stripe.customers.create(); }; @@ -30,10 +30,10 @@ export default async (req, res) => { // For full details see https://stripe.com/docs/api/checkout/sessions/create try { const authorization = req.headers.authorization; // authorization header from request - const user = await got("http://accounts:3000/user", { headers: { authorization } }).json(); - const customer = await getStripeCustomer(user.stripeCustomerId); + const { stripeCustomerId } = await got("http://accounts:3000/user", { headers: { authorization } }).json(); + const customer = await getStripeCustomer(stripeCustomerId); - if (!user.stripeCustomerId) { + if (!stripeCustomerId) { await got.put(`http://accounts:3000/user`, { headers: { authorization }, json: { stripeCustomerId: customer.id }, diff --git a/packages/dashboard/src/pages/api/stripe/customerPortal.js b/packages/dashboard/src/pages/api/stripe/customerPortal.js index 0d7c0516..3283bfaa 100644 --- a/packages/dashboard/src/pages/api/stripe/customerPortal.js +++ b/packages/dashboard/src/pages/api/stripe/customerPortal.js @@ -4,12 +4,20 @@ import { StatusCodes } from "http-status-codes"; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); +const getStripeCustomer = (stripeCustomerId = null) => { + if (stripeCustomerId) { + return stripe.customers.retrieve(stripeCustomerId); + } + return stripe.customers.create(); +}; + export default async (req, res) => { try { const authorization = req.headers.authorization; // authorization header from request const { stripeCustomerId } = await got("http://accounts:3000/user", { headers: { authorization } }).json(); + const customer = await getStripeCustomer(stripeCustomerId); const session = await stripe.billingPortal.sessions.create({ - customer: stripeCustomerId, + customer: customer.id, return_url: `${process.env.SKYNET_DASHBOARD_URL}/payments`, });