#mulo_webhooks

1 messages ยท Page 1 of 1 (latest)

maiden elbowBOT
#

๐Ÿ‘‹ 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/1295332020268044331

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

late cloud
#

hi! well yes if you're using Checkout to start the subscription there's an event for that CheckoutSession completing.

forest geyser
#

how can I differenciate the two?

late cloud
#

check the line_items and the mode for instance

forest geyser
#

I'd like only to get when a subscirption is paid, and only when a one-off is paid

late cloud
#

makes sense but that's not really how it works. You'd just write some code that inspects the actual details of the CheckoutSession while handling checkout.session.completed, determines if it's telling you a Subscription has just been created or if it's a one-time payment, and take whatever appropriate action on your server

forest geyser
late cloud
#

if you're using Checkout to start the subscription (mode:"subscription")there's an event for that CheckoutSession completing. if you use Checkout for a one-time payment, there's an event for that CheckoutSession completing

forest geyser
#

something like:

if (event.type === "checkout.session.completed") {
    if (event.type === 'invoice.paid') {
      //this is monthly sub?
    } else {
      // one off?
    }
}
late cloud
#

no nothing like that

#

the event can only be one type at a time

forest geyser
#

oh ye right, meant logic-wise

late cloud
#

https://docs.stripe.com/checkout/fulfillment
that shows how you can get the checkoutSession object
and then as I mentioned earlier, you can check fields of that object (like mode or the details of what's in the line_items) to see what has been bought and react accordingly

forest geyser
#

so i can remove invoice.paid, and just use checkout.session.completed
which both one-off and monthly sub fall under?

late cloud
#

well it depends. You should still listen to invoice.paid so you can handle the recurring payments.

forest geyser
#

but how's that those get also logged in checkout.session.completed

#

I just move the checkout.session.completed below invoice.paid?

late cloud
#

no the order is not important at all. Sorry I don't think you're getting this

#

you create a CheckoutSession, in mode:subscription. The events you get are like :

  • checkout.session.completed โ€” when the customer fills in Checkout and starts their subscription
  • invoice.paid โ€” at the same time; it's the event for the first invoice of this subscription
  • invoice.paid โ€” a month later; it's the event for a recurring payment on the created subscription
  • invoice.paid โ€” a month later; it's the event for a recurring payment on the created subscription
  • ...
#

each of those is a distinct event that hits your endpoint and triggers your code separately

forest geyser
#

checkout.session.completed โ€” when the customer fills in Checkout and starts their subscription
This triggers for both one-off payments and recurring payments?

late cloud
#

if you're using Checkout to start the subscription (mode:"subscription")there's an event for that CheckoutSession completing. if you use Checkout for a one-time payment, there's an event for that CheckoutSession completing

forest geyser
#

this is to create one-off payments

 const session = await stripe.checkout.sessions.create({
        success_url: url,
        cancel_url: url,
        payment_method_types: ['card'],
        line_items: [{ price: selectedItem, quantity: 1 }],
        mode: 'payment', 
        metadata: {                   custom_id:`${discordUserId}@${guildId}@${amount}`,},
      });
  
      return session.url

this for monthly sub

       const session = await stripe.checkout.sessions.create({
                        success_url: url,
                        cancel_url: url,
                        payment_method_types: ['card'],
                        line_items: [{ price: officialSubId, quantity: 1, },],
                        mode: 'subscription',
                        subscription_data: { metadata: { custom: `${discordUserId}@${guildId}` } }
                    });
late cloud
#

yes so they both use Checkout, they both generate a checkout.session.completed event. Your code needs to inspect the CheckoutSession when handling that event, determine if it's telling you a Subscription has just been created or if it's a one-time payment, and take whatever appropriate action on your server

forest geyser
#

Ok, so i see the mode I can directly check in the event. Tho in my case for subs:

  • when a sub is first created I do get both checkout.session.completed and invoice.paid?
  • then onward I just get invoice.paid

for one-off payments I just get checkout.session.completed?

late cloud
#

yes

forest geyser
# late cloud yes

If so, is there another endpoint i can listen to for when one-off payements are are finalized instead of listening to checkout.session.completed?

maiden elbowBOT
late cloud
#

the recommended event is checkout.session.completed and the recommended integration is to check the details in your code when handling it, as described above

forest geyser
#

ye but as you said if I just listen to checkout.session.completed then I wouldnt get the recurring payments of monthly subs? or am I mistaken

frank bay
#

Hey! Taking over for my colleague.
Yes you need to fetch the correspnding details from that event using your code and Stripe APIs.

forest geyser
# frank bay Hey! Taking over for my colleague. Yes you need to fetch the correspnding detai...

ok so would this be a workaround:

if (event.type === "checkout.session.completed") {
    if (event.data.object.mode === 'subscription') {
      // skips event for subs, will get handled by 'invoice.paid' instead
      response.status(200).end();
      return
    }
  // processing one-off payments instead here
  return
}

if (event.type === 'invoice.paid') {
//processing monthly subs

// this is never triggered by on one-off payments, it's the first cycle of monthly sub and all subsequent ones
}
frank bay
#

Yes that should work I think...

forest geyser
frank bay
#

Yes, if you don't use Invoice for one-time payments

forest geyser
frank bay
#

Yeah, it's ok for checkout

#

The invoice.paid won't be triggered for that case.

maiden elbowBOT
forest geyser
#

thanks folks!