-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
67 lines (57 loc) · 2.03 KB
/
Copy pathserver.js
File metadata and controls
67 lines (57 loc) · 2.03 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'dotenv/config';
import express from 'express';
import cors from 'cors';
import Stripe from 'stripe';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
const app = express();
app.use(cors());
app.use(express.json());
app.post('/api/checkout', async (req, res) => {
const { amount } = req.body;
// 校验金额:3.5 ~ 100,单位人民币
if (!amount || amount < 3.5 || amount > 100) {
return res.status(400).json({ error: 'Invalid amount' });
}
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'],
line_items: [
{
price_data: {
currency: 'cny',
product_data: {
name: 'Give Me Money',
},
unit_amount: Math.round(amount * 100), // 转为分
},
quantity: 1,
},
],
mode: 'payment',
success_url: `${req.headers.origin || 'http://localhost:5173'}?status=success&amount=${amount}`,
cancel_url: `${req.headers.origin || 'http://localhost:5173'}?status=cancel`,
});
res.json({ url: session.url });
} catch (err) {
console.error('Stripe error:', err.message);
res.status(500).json({ error: err.message });
}
});
// Webhook 验证支付结果(可选,生产环境推荐)
app.post('/api/webhook', express.raw({ type: 'application/json' }), (req, res) => {
const sig = req.headers['stripe-signature'];
const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
if (!endpointSecret) return res.status(400).send('Webhook secret not configured');
try {
const event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret);
if (event.type === 'checkout.session.completed') {
console.log('Payment confirmed:', event.data.object.id);
}
res.json({ received: true });
} catch (err) {
console.error('Webhook error:', err.message);
res.status(400).send(`Webhook Error: ${err.message}`);
}
});
const PORT = process.env.PORT || 3001;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));