accounts/**

This commit is contained in:
Karol Wypchlo 2021-02-25 11:51:36 +01:00
parent 4dee4b5037
commit e2681eabb5
2 changed files with 13 additions and 5 deletions

View File

@ -6,7 +6,7 @@ const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
const getStripeCustomer = (stripeCustomerId = null) => { const getStripeCustomer = (stripeCustomerId = null) => {
if (stripeCustomerId) { if (stripeCustomerId) {
return stripe.customers.retrieve(user.stripeCustomerId); return stripe.customers.retrieve(stripeCustomerId);
} }
return stripe.customers.create(); 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 // For full details see https://stripe.com/docs/api/checkout/sessions/create
try { try {
const authorization = req.headers.authorization; // authorization header from request const authorization = req.headers.authorization; // authorization header from request
const user = await got("http://accounts:3000/user", { headers: { authorization } }).json(); const { stripeCustomerId } = await got("http://accounts:3000/user", { headers: { authorization } }).json();
const customer = await getStripeCustomer(user.stripeCustomerId); const customer = await getStripeCustomer(stripeCustomerId);
if (!user.stripeCustomerId) { if (!stripeCustomerId) {
await got.put(`http://accounts:3000/user`, { await got.put(`http://accounts:3000/user`, {
headers: { authorization }, headers: { authorization },
json: { stripeCustomerId: customer.id }, json: { stripeCustomerId: customer.id },

View File

@ -4,12 +4,20 @@ import { StatusCodes } from "http-status-codes";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY); 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) => { export default async (req, res) => {
try { try {
const authorization = req.headers.authorization; // authorization header from request const authorization = req.headers.authorization; // authorization header from request
const { stripeCustomerId } = await got("http://accounts:3000/user", { headers: { authorization } }).json(); const { stripeCustomerId } = await got("http://accounts:3000/user", { headers: { authorization } }).json();
const customer = await getStripeCustomer(stripeCustomerId);
const session = await stripe.billingPortal.sessions.create({ const session = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId, customer: customer.id,
return_url: `${process.env.SKYNET_DASHBOARD_URL}/payments`, return_url: `${process.env.SKYNET_DASHBOARD_URL}/payments`,
}); });