#radiant1106
1 messages · Page 1 of 1 (latest)
Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- radiant1106, 20 minutes ago, 65 messages
hello! how can I help?
this is my buy.js
const settings = require("../settings.json");
const stripe = require('stripe')(settings.stripe.key);
module.exports.load = async function(app, db) {
app.get('/create-checkout-session', async (req, res) => {
const unit_amount = req.query.amt; // Calculate the unit amount based on the number of coins to purchase
// console.log("Stripe amount per coin:", settings.stripe.amount);
// console.log("Amount received from form:", amount);
// console.log("Coins calculated:", coins);
// console.log("Unit amount calculated:", unit_amount);
if(!req.session.pterodactyl) return res.redirect("/?error="+encodeURIComponent((new Buffer("You are not logged in.")).toString('base64')));
const session = await stripe.checkout.sessions.create({
line_items: [
{
price_data: {
currency: 'usd',
product_data: {
name: 'Coins',
},
unit_amount: unit_amount,
},
quantity: 1,
},
],
mode: 'payment',
success_url: 'http://client.sentinelhost.xyz/success.html',
cancel_url: 'http://client.sentinelhost.xyz/cancel.html',
});
res.redirect(303, session.url);
});
};
I want to make an event in which the successful payment gets updated in the database and the coins get added to the user as follows:
let beforeCoins = await db.get(`coins-${req.session.userinfo.id}`);
let coinsToPurchase = parseInt(req.query.amt);
let afterCoins = parseInt(beforeCoins) + coinsToPurchase;
await db.set(`coins-${req.session.userinfo.id}`, afterCoins);
do this works for the succesful event?
app.post('/stripe-webhook', async (req, res) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, settings.stripe.webhookSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
// Handle the event
if (event.type === 'checkout.session.completed') {
const session = event.data.object;
// Get relevant data from the session object
const userId = session.client_reference_id;
const coinsPurchased = /* Calculate coins purchased based on session data */;
// Update the database
let beforeCoins = await db.get(`coins-${userId}`);
let afterCoins = parseInt(beforeCoins) + coinsPurchased;
await db.set(`coins-${userId}`, afterCoins);
console.log(`Database updated for user ${userId}.`);
}
// Return a response to acknowledge receipt of the event
res.json({ received: true });
});
@sweet spoke
have you tried running it?
nope
why not?
Yes, you should do it in test mode. Also, you can trigger a test webhook event using the Stripe CLI : https://stripe.com/docs/payments/checkout/fulfill-orders