#pocoyo_webhooks

1 messages ยท Page 1 of 1 (latest)

plain waspBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1353760018440126679

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

umbral whale
#

hello

#

one second getting the code

#
const express = require('express');
const app = express();


app.post('/stripe-webhook', express.raw({type: 'application/json'}), async (req, res) => {
  const sig = req.headers['stripe-signature'];
  
  let event;
  
  try {
    event = stripe.webhooks.constructEvent(req.body, sig, config.stripeWebhookSecret);
  } catch (err) {
    console.error(`Webhook Error: ${err.message}`);
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }
  
  
  if (event.type === 'checkout.session.completed') {
    const session = event.data.object;
    
    const { ticketId, channelId, guildId } = session.metadata;
    const ticket = activeTickets.get(channelId);
    
    if (ticket && ticket.paymentSession && ticket.paymentSession.id === session.id) {
      
      ticket.paymentSession.status = 'completed';
      ticket.paymentSession.completedAt = new Date().toISOString();
      saveTickets();
      
      
      const guild = client.guilds.cache.get(guildId);
      if (!guild) return res.status(200).end();
      
      const channel = guild.channels.cache.get(channelId);
      if (!channel) return res.status(200).end();
      
      
      const completionEmbed = new EmbedBuilder()
        .setTitle('โœ… Payment Completed')
        .setDescription(`Payment for $${ticket.paymentSession.amount.toFixed(2)} USD has been completed successfully.`)
        .addFields(
          { name: 'Description', value: ticket.paymentSession.description, inline: true },
          { name: 'Amount', value: `$${ticket.paymentSession.amount.toFixed(2)} USD`, inline: true },
          { name: 'Payment ID', value: session.payment_intent, inline: true }
        )
        .setColor('#00FF00')
        .setTimestamp();
      
      channel.send({ 
        content: `<@${ticket.userId}> <@${ticket.paymentSession.createdBy}> Payment completed successfully!`, 
        embeds: [completionEmbed] 
      });
    }
  }
  
  
  if (event.type === 'checkout.session.expired') {
    const session = event.data.object;
    
    const { channelId, guildId } = session.metadata;
    const ticket = activeTickets.get(channelId);
    
    if (ticket && ticket.paymentSession && ticket.paymentSession.id === session.id) {
      
      ticket.paymentSession.status = 'cancelled';
      ticket.paymentSession.cancelledAt = new Date().toISOString();
      saveTickets();
      
      
      const guild = client.guilds.cache.get(guildId);
      if (!guild) return res.status(200).end();
      
      const channel = guild.channels.cache.get(channelId);
      if (!channel) return res.status(200).end();
      
      
      const cancellationEmbed = new EmbedBuilder()
        .setTitle('โŒ Payment Cancelled')
        .setDescription(`Payment for $${ticket.paymentSession.amount.toFixed(2)} USD has been cancelled or expired.`)
        .addFields(
          { name: 'Description', value: ticket.paymentSession.description, inline: true },
          { name: 'Amount', value: `$${ticket.paymentSession.amount.toFixed(2)} USD`, inline: true }
        )
        .setColor('#FF0000')
        .setTimestamp();
      
      channel.send({ 
        content: `<@${ticket.userId}> <@${ticket.paymentSession.createdBy}> Payment was cancelled or expired.`, 
        embeds: [cancellationEmbed] 
      });
    }
  }
  
  res.status(200).end();
});```
mint sorrel
#

Hello. I cannot review your code for you. But I can help you with specific issues you are encountering. Can you describe what behavior you are seeing and how that is different from what you expect?

umbral whale
#

well, the problem is really just its not sending after the purchase is completed

#

it may be a problem with this, but I have no idea. this is my first time making a bot

mint sorrel
#

What do you mean when you say "it's not sending"?

umbral whale
#

one second

#

nevermind

#

fixed it

#

it was an endpoint problem