accounts/**

This commit is contained in:
Karol Wypchlo 2021-02-25 11:29:40 +01:00
parent 661d1f3275
commit b4eb863dc9
1 changed files with 9 additions and 15 deletions

View File

@ -4,13 +4,11 @@ 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 getAuthenticatedUser = async () => { const getStripeCustomer = (stripeCustomerId = null) => {
const { body } = await got.get("http://user", { if (stripeCustomerId) {
json: { return stripe.customers.retrieve(user.stripeCustomerId);
hello: "world", }
}, return stripe.customers.create();
responseType: "json",
});
}; };
export default async (req, res) => { export default async (req, res) => {
@ -33,24 +31,20 @@ 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 user = await got("http://accounts:3000/user", { headers: { authorization } }); const user = await got("http://accounts:3000/user", { headers: { authorization } });
const customer = await getStripeCustomer(user.stripeCustomerId);
if (!user.stripeCustomerId) { if (!user.stripeCustomerId) {
const customer = await stripe.customers.create(); await got.put(`http://accounts:3000/user`, {
console.log(customer);
const user = await got.put(`http://accounts:3000/user`, {
headers: { authorization }, headers: { authorization },
json: { stripeCustomerId }, json: { stripeCustomerId: customer.id },
}); });
console.log(user);
} }
const stripeCustomerId = "cus_J09ECKPgFEPXoq";
const session = await stripe.checkout.sessions.create({ const session = await stripe.checkout.sessions.create({
mode: "subscription", mode: "subscription",
payment_method_types: ["card"], payment_method_types: ["card"],
line_items: [{ price, quantity: 1 }], 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 // ?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}`, success_url: `${process.env.SKYNET_DASHBOARD_URL}/payments?session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.SKYNET_DASHBOARD_URL}/payments`, cancel_url: `${process.env.SKYNET_DASHBOARD_URL}/payments`,