From e2681eabb57a8f3c2304b305e1ad12978515c5c7 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Thu, 25 Feb 2021 11:51:36 +0100 Subject: [PATCH] accounts/** --- .../src/pages/api/stripe/createCheckoutSession.js | 8 ++++---- .../dashboard/src/pages/api/stripe/customerPortal.js | 10 +++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) 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`, });