From 932a7f10a8943b5d6851f64470f03d1a97319ad1 Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Wed, 3 Mar 2021 16:26:03 +0100 Subject: [PATCH] payments --- .../src/pages/api/stripe/createCheckoutSession.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js index bc6191bd..104d7ca6 100644 --- a/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js +++ b/packages/dashboard/src/pages/api/stripe/createCheckoutSession.js @@ -4,11 +4,11 @@ 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); +const getStripeCustomer = (user) => { + if (user.stripeCustomerId) { + return stripe.customers.retrieve(user.stripeCustomerId); } - return stripe.customers.create(); + return stripe.customers.create({ email: user.email }); }; export default async (req, res) => { @@ -31,7 +31,7 @@ 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 } }).json(); - const customer = await getStripeCustomer(user.stripeCustomerId); + const customer = await getStripeCustomer(user); if (!user.stripeCustomerId) { await got.put(`http://accounts:3000/user`, { @@ -45,7 +45,7 @@ export default async (req, res) => { payment_method_types: ["card"], line_items: [{ price, quantity: 1 }], customer: customer.id, - customer_email: user.email, + client_reference_id: user.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`,