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