diff --git a/.env.example b/.env.example index ec4e075..04765d1 100644 --- a/.env.example +++ b/.env.example @@ -15,7 +15,6 @@ DISCORD_REDIRECT_URI=http://localhost:4000/auth/discord/callback DEFAULT_APP_REDIRECT=blackjack://auth STRIPE_SECRET_KEY=sk_test_... -STRIPE_WEBHOOK_SECRET=whsec_... MIN_BET=10 MAX_BET=500 diff --git a/src/index.js b/src/index.js index b29c53c..2d0e170 100644 --- a/src/index.js +++ b/src/index.js @@ -8,7 +8,6 @@ import { WebSocketServer } from 'ws'; import authRoutes from './routes/auth.js'; import lobbyRoutes from './routes/lobby.js'; import walletRoutes from './routes/wallet.js'; -import stripeRoutes from './routes/stripe.js'; import { setupWebSocket } from './ws.js'; dotenv.config(); @@ -19,7 +18,6 @@ app.use(helmet()); app.use(cors({ origin: process.env.CORS_ORIGIN || '*', credentials: true })); app.use(morgan('dev')); -app.use(stripeRoutes); app.use(express.json()); app.get('/health', (req, res) => res.json({ ok: true })); diff --git a/src/routes/stripe.js b/src/routes/stripe.js deleted file mode 100644 index e212306..0000000 --- a/src/routes/stripe.js +++ /dev/null @@ -1,49 +0,0 @@ -import express, { Router } from 'express'; -import Stripe from 'stripe'; -import { query } from '../db.js'; - -const router = Router(); -const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || '', { - apiVersion: '2024-06-20' -}); - -router.post('/api/stripe/webhook', express.raw({ type: 'application/json' }), async (req, res) => { - const signature = req.headers['stripe-signature']; - let event; - - try { - event = stripe.webhooks.constructEvent( - req.body, - signature, - process.env.STRIPE_WEBHOOK_SECRET || '' - ); - } catch (err) { - return res.status(400).send('Webhook alairas hiba.'); - } - - if (event.type === 'payment_intent.succeeded') { - const intent = event.data.object; - const amount = Number(intent.amount || 0); - const userId = Number(intent.metadata?.userId || 0); - - if (userId && amount) { - const rows = await query( - 'SELECT status FROM deposits WHERE stripe_payment_intent_id = ?', - [intent.id] - ); - const status = rows[0]?.status; - - if (status !== 'succeeded') { - await query( - 'UPDATE deposits SET status = ? WHERE stripe_payment_intent_id = ?', - ['succeeded', intent.id] - ); - await query('UPDATE users SET balance = balance + ? WHERE id = ?', [amount, userId]); - } - } - } - - return res.json({ received: true }); -}); - -export default router; diff --git a/src/routes/wallet.js b/src/routes/wallet.js index 52299f2..5625fff 100644 --- a/src/routes/wallet.js +++ b/src/routes/wallet.js @@ -29,10 +29,47 @@ router.post('/api/wallet/deposit-intent', authMiddleware, async (req, res) => { [req.userId, amount, paymentIntent.id, 'created'] ); - return res.json({ clientSecret: paymentIntent.client_secret }); + return res.json({ clientSecret: paymentIntent.client_secret, paymentIntentId: paymentIntent.id }); } catch (err) { return res.status(500).json({ error: 'Nem sikerult letrehozni a fizetest.' }); } }); +router.post('/api/wallet/confirm', authMiddleware, async (req, res) => { + try { + 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) { + return res.status(500).json({ error: 'Nem sikerult a befizetes ellenorzese.' }); + } +}); + export default router;