Untitled
unknown
javascript
a year ago
1.3 kB
3
Indexable
Never
router.post('/webhook', express.raw({type: 'application/json'}), async (req, res) => { const sig = req.headers['stripe-signature']; let event; try { //console.log("Endpoint Secret:", endpointSecret); //console.log("Raw Body:", req.body); event = stripe.webhooks.constructEvent(req.body, sig, endpointSecret); } catch (err) { console.error("Stripe webhook error:", err); return res.status(400).send(`Webhook error: ${err.message}`); } if (event.type === 'checkout.session.completed') { const session = event.data.object; const customerId = session.client_reference_id; const paymentId = session.payment_intent; const totalPaid = session.amount_total / 100; try { const result = await pool.query( "INSERT INTO payments (customer_id, date, total_paid, stripe_charge_id) VALUES ($1, CURRENT_DATE, $2, $3) RETURNING id", [customerId, totalPaid, paymentId] ); console.log("Successfully inserted into payments table with ID:", result.rows[0].id); res.json({received: true}); } catch (err) { //console.error("Database error:", err.message); res.status(500).send("Server error"); } } else { return res.status(400).end(); } });