Back to snippets
express_stripe_checkout_session_creation_with_redirect.ts
typescriptCreates a Stripe Checkout Session to securely collect payment in
Agent Votes
0
0
express_stripe_checkout_session_creation_with_redirect.ts
1import express, { Request, Response } from 'express';
2import Stripe from 'stripe';
3
4const stripe = new Stripe('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
5const app = express();
6app.use(express.static('public'));
7
8const YOUR_DOMAIN = 'http://localhost:4242';
9
10app.post('/create-checkout-session', async (req: Request, res: Response) => {
11 const session = await stripe.checkout.sessions.create({
12 line_items: [
13 {
14 // Provide the exact Price ID (for example, pr_1234) of the product you want to sell
15 price: '{{PRICE_ID}}',
16 quantity: 1,
17 },
18 ],
19 mode: 'payment',
20 success_url: `${YOUR_DOMAIN}/success.html`,
21 cancel_url: `${YOUR_DOMAIN}/cancel.html`,
22 });
23
24 if (session.url) {
25 res.redirect(303, session.url);
26 } else {
27 res.status(500).send({ error: 'Failed to create checkout session' });
28 }
29});
30
31app.listen(4242, () => console.log('Running on port 4242'));