Implement backend for blackjack game with Discord authentication, database integration, and WebSocket support
This commit is contained in:
38
src/routes/wallet.js
Normal file
38
src/routes/wallet.js
Normal file
@@ -0,0 +1,38 @@
|
||||
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'
|
||||
});
|
||||
|
||||
router.post('/api/wallet/deposit-intent', authMiddleware, async (req, res) => {
|
||||
try {
|
||||
const amount = Number(req.body.amount);
|
||||
if (!Number.isFinite(amount) || amount < 50 || amount > 100) {
|
||||
return res.status(400).json({ error: 'A feltoltes 50 es 100 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 });
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: 'Nem sikerult letrehozni a fizetest.' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user