-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (37 loc) · 1.48 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require('dotenv').config(); // Load environment variables from .env file
const express = require('express');
const Stripe = require('stripe');
const app = express();
const stripe = Stripe(process.env.STRIPE_SECRET_KEY); // Replace with your actual Stripe secret key
app.use(express.json());
app.post('/payment-sheet', async (req, res) => {
try {
const { email, name, totalAmount } = req.body;
// Step 1: Create a Customer
const customer = await stripe.customers.create({
email: email,
name: name,
});
// Step 2: Create an Ephemeral Key for the Customer
const ephemeralKey = await stripe.ephemeralKeys.create(
{ customer: customer.id },
{ apiVersion: '2022-11-15' }
);
// Step 3: Create a PaymentIntent with the Customer ID
const paymentIntent = await stripe.paymentIntents.create({
amount: totalAmount,
currency: 'cad',
customer: customer.id,
automatic_payment_methods: { enabled: true },
});
res.send({
paymentIntent: paymentIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: customer.id,
publishableKey: process.env.STRIPE_PUBLISHABLE_KEY, // Replace with your actual publishable key
});
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(4242, () => console.log('Server running on port 4242'));