102 lines
3.3 KiB
JavaScript
102 lines
3.3 KiB
JavaScript
import { Router } from 'express';
|
|
import Stripe from 'stripe';
|
|
import { authMiddleware } from '../auth.js';
|
|
import { query } from '../db.js';
|
|
|
|
const router = Router();
|
|
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || '', {
|
|
apiVersion: '2024-06-20'
|
|
});
|
|
const MIN_DEPOSIT = Number(process.env.MIN_DEPOSIT || 175);
|
|
const MAX_DEPOSIT = Number(process.env.MAX_DEPOSIT || 1000);
|
|
|
|
function ensureStripeConfigured(res) {
|
|
if (!process.env.STRIPE_SECRET_KEY) {
|
|
res.status(500).json({ error: 'Stripe nincs beallitva.' });
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
router.post('/api/wallet/deposit-intent', authMiddleware, async (req, res) => {
|
|
try {
|
|
if (!ensureStripeConfigured(res)) {
|
|
return;
|
|
}
|
|
|
|
const amount = Number(req.body.amount);
|
|
if (!Number.isFinite(amount) || amount < MIN_DEPOSIT || amount > MAX_DEPOSIT) {
|
|
return res.status(400).json({ error: `A feltoltes ${MIN_DEPOSIT} es ${MAX_DEPOSIT} Ft kozott lehet.` });
|
|
}
|
|
|
|
const paymentIntent = await stripe.paymentIntents.create({
|
|
amount,
|
|
currency: 'huf',
|
|
automatic_payment_methods: { enabled: true },
|
|
metadata: {
|
|
userId: String(req.userId)
|
|
}
|
|
});
|
|
|
|
await query(
|
|
'INSERT INTO deposits (user_id, amount, stripe_payment_intent_id, status) VALUES (?, ?, ?, ?)',
|
|
[req.userId, amount, paymentIntent.id, 'created']
|
|
);
|
|
|
|
return res.json({ clientSecret: paymentIntent.client_secret, paymentIntentId: paymentIntent.id });
|
|
} catch (err) {
|
|
console.error('Stripe fizetes hiba:', err);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
return res.status(500).json({ error: err.message || 'Nem sikerult letrehozni a fizetest.' });
|
|
}
|
|
return res.status(500).json({ error: 'Nem sikerult letrehozni a fizetest.' });
|
|
}
|
|
});
|
|
|
|
router.post('/api/wallet/confirm', authMiddleware, async (req, res) => {
|
|
try {
|
|
if (!ensureStripeConfigured(res)) {
|
|
return;
|
|
}
|
|
|
|
const paymentIntentId = req.body.paymentIntentId?.toString();
|
|
if (!paymentIntentId) {
|
|
return res.status(400).json({ error: 'Hianyzo paymentIntentId.' });
|
|
}
|
|
|
|
const rows = await query(
|
|
'SELECT id, status, amount FROM deposits WHERE user_id = ? AND stripe_payment_intent_id = ?',
|
|
[req.userId, paymentIntentId]
|
|
);
|
|
const deposit = rows[0];
|
|
if (!deposit) {
|
|
return res.status(404).json({ error: 'Ismeretlen befizetes.' });
|
|
}
|
|
|
|
const intent = await stripe.paymentIntents.retrieve(paymentIntentId);
|
|
if (intent.status !== 'succeeded') {
|
|
return res.json({ status: intent.status });
|
|
}
|
|
|
|
if (deposit.status !== 'succeeded') {
|
|
await query(
|
|
'UPDATE deposits SET status = ? WHERE id = ?',
|
|
['succeeded', deposit.id]
|
|
);
|
|
await query('UPDATE users SET balance = balance + ? WHERE id = ?', [deposit.amount, req.userId]);
|
|
}
|
|
|
|
const balanceRows = await query('SELECT balance FROM users WHERE id = ?', [req.userId]);
|
|
const balance = balanceRows[0]?.balance ?? 0;
|
|
return res.json({ status: 'succeeded', balance });
|
|
} catch (err) {
|
|
console.error('Stripe confirm hiba:', err);
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
return res.status(500).json({ error: err.message || 'Nem sikerult a befizetes ellenorzese.' });
|
|
}
|
|
return res.status(500).json({ error: 'Nem sikerult a befizetes ellenorzese.' });
|
|
}
|
|
});
|
|
|
|
export default router;
|