user reference
This commit is contained in:
parent
88d9c5f2cd
commit
1dbbb8f095
|
@ -9,56 +9,6 @@ const stripe = new Stripe(env.stripeSecretKey, {
|
|||
|
||||
const paymentRouter = express.Router();
|
||||
|
||||
// Guard all api with authentication
|
||||
paymentRouter.use((req, res, next) => {
|
||||
if (req.isAuthenticated()) {
|
||||
return next();
|
||||
}
|
||||
const r: T.APIResponse = { ok: false, error: "401 Unauthorized" };
|
||||
return res.status(401).send(r);
|
||||
});
|
||||
|
||||
paymentRouter.post("/create-checkout-session", async (req, res, next) => {
|
||||
try {
|
||||
const prices = await stripe.prices.list({
|
||||
lookup_keys: [req.body.lookup_key],
|
||||
expand: ['data.product'],
|
||||
});
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
billing_address_collection: 'auto',
|
||||
line_items: [
|
||||
{
|
||||
price: prices.data[0].id,
|
||||
// For metered billing, do not pass quantity
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
mode: 'subscription',
|
||||
success_url: `https://account.lingdocs.com/user?upgrade=success`,
|
||||
cancel_url: `https://account.lingdocs.com/user`,
|
||||
});
|
||||
if (!session.url) {
|
||||
return next("error creating session url");
|
||||
}
|
||||
res.redirect(303, session.url);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return next("error connection to Stripe");
|
||||
}
|
||||
});
|
||||
|
||||
paymentRouter.post('/create-portal-session', async (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return next("error finding user");
|
||||
}
|
||||
const portalSession = await stripe.billingPortal.sessions.create({
|
||||
customer: req.user.userId,
|
||||
return_url: "/",
|
||||
});
|
||||
|
||||
res.redirect(303, portalSession.url);
|
||||
});
|
||||
|
||||
paymentRouter.post(
|
||||
'/webhook',
|
||||
express.raw({ type: 'application/json' }),
|
||||
|
@ -85,8 +35,8 @@ paymentRouter.post(
|
|||
return response.sendStatus(400);
|
||||
}
|
||||
}
|
||||
let subscription;
|
||||
let status;
|
||||
let subscription: Stripe.Subscription;
|
||||
let status: Stripe.Subscription.Status;
|
||||
// Handle the event
|
||||
switch (event.type) {
|
||||
case 'customer.subscription.deleted':
|
||||
|
@ -100,7 +50,8 @@ paymentRouter.post(
|
|||
subscription = event.data.object;
|
||||
status = subscription.status;
|
||||
console.log(`Subscription status is ${status}.`);
|
||||
console.log(`Will upgrade ${request.user?.name}`);
|
||||
console.log(subscription);
|
||||
console.log(subscription.metadata);
|
||||
// Then define and call a method to handle the subscription created.
|
||||
// handleSubscriptionCreated(subscription);
|
||||
break;
|
||||
|
@ -113,4 +64,59 @@ paymentRouter.post(
|
|||
}
|
||||
);
|
||||
|
||||
// Guard all api with authentication
|
||||
paymentRouter.use((req, res, next) => {
|
||||
if (req.isAuthenticated()) {
|
||||
return next();
|
||||
}
|
||||
const r: T.APIResponse = { ok: false, error: "401 Unauthorized" };
|
||||
return res.status(401).send(r);
|
||||
});
|
||||
|
||||
paymentRouter.post("/create-checkout-session", async (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return next("not logged in");
|
||||
}
|
||||
try {
|
||||
const prices = await stripe.prices.list({
|
||||
lookup_keys: [req.body.lookup_key],
|
||||
expand: ['data.product'],
|
||||
});
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
billing_address_collection: 'auto',
|
||||
line_items: [
|
||||
{
|
||||
price: prices.data[0].id,
|
||||
// For metered billing, do not pass quantity
|
||||
quantity: 1,
|
||||
},
|
||||
],
|
||||
client_reference_id: req.user.userId,
|
||||
mode: 'subscription',
|
||||
success_url: `https://account.lingdocs.com/user?upgrade=success`,
|
||||
cancel_url: `https://account.lingdocs.com/user`,
|
||||
});
|
||||
if (!session.url) {
|
||||
return next("error creating session url");
|
||||
}
|
||||
res.redirect(303, session.url);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return next("error connection to Stripe");
|
||||
}
|
||||
});
|
||||
|
||||
paymentRouter.post('/create-portal-session', async (req, res, next) => {
|
||||
if (!req.user) {
|
||||
return next("error finding user");
|
||||
}
|
||||
const portalSession = await stripe.billingPortal.sessions.create({
|
||||
customer: req.user.userId,
|
||||
return_url: "/",
|
||||
});
|
||||
|
||||
res.redirect(303, portalSession.url);
|
||||
});
|
||||
|
||||
|
||||
export default paymentRouter;
|
Loading…
Reference in New Issue