Remove Stripe integration and related routes; update wallet routes for payment intent confirmation
This commit is contained in:
@@ -15,7 +15,6 @@ DISCORD_REDIRECT_URI=http://localhost:4000/auth/discord/callback
|
|||||||
DEFAULT_APP_REDIRECT=blackjack://auth
|
DEFAULT_APP_REDIRECT=blackjack://auth
|
||||||
|
|
||||||
STRIPE_SECRET_KEY=sk_test_...
|
STRIPE_SECRET_KEY=sk_test_...
|
||||||
STRIPE_WEBHOOK_SECRET=whsec_...
|
|
||||||
|
|
||||||
MIN_BET=10
|
MIN_BET=10
|
||||||
MAX_BET=500
|
MAX_BET=500
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import { WebSocketServer } from 'ws';
|
|||||||
import authRoutes from './routes/auth.js';
|
import authRoutes from './routes/auth.js';
|
||||||
import lobbyRoutes from './routes/lobby.js';
|
import lobbyRoutes from './routes/lobby.js';
|
||||||
import walletRoutes from './routes/wallet.js';
|
import walletRoutes from './routes/wallet.js';
|
||||||
import stripeRoutes from './routes/stripe.js';
|
|
||||||
import { setupWebSocket } from './ws.js';
|
import { setupWebSocket } from './ws.js';
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
@@ -19,7 +18,6 @@ app.use(helmet());
|
|||||||
app.use(cors({ origin: process.env.CORS_ORIGIN || '*', credentials: true }));
|
app.use(cors({ origin: process.env.CORS_ORIGIN || '*', credentials: true }));
|
||||||
app.use(morgan('dev'));
|
app.use(morgan('dev'));
|
||||||
|
|
||||||
app.use(stripeRoutes);
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
app.get('/health', (req, res) => res.json({ ok: true }));
|
app.get('/health', (req, res) => res.json({ ok: true }));
|
||||||
|
|||||||
@@ -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;
|
|
||||||
@@ -29,10 +29,47 @@ router.post('/api/wallet/deposit-intent', authMiddleware, async (req, res) => {
|
|||||||
[req.userId, amount, paymentIntent.id, 'created']
|
[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) {
|
} catch (err) {
|
||||||
return res.status(500).json({ error: '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 {
|
||||||
|
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;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user