#rossano
1 messages ยท Page 1 of 1 (latest)
Hi there ๐ what type of Events are you currently listening for, are they ones that include the Invoice object that is being paid?
if (req.method === "POST") {
try {
// Create Checkout Sessions from body params.
const params: Stripe.Checkout.SessionCreateParams = {
payment_method_types: ["card"],
client_reference_id: userLogged.id,
customer: stripeId,
line_items: [
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
price: "price_1Mj7vPLCFShODpNN72pH7810",
quantity: 1,
},
],
mode: "subscription",
success_url: `${req.headers.origin}/pricing?success=true`,
cancel_url: `${req.headers.origin}/pricing/?canceled=true`,
};
const checkoutSession: Stripe.Checkout.Session =
await stripe.checkout.sessions.create(params);
res.status(200).json(checkoutSession);
the webhook:
const dataObject = event.data.object as Stripe.Checkout.Session;
const usersCollection = adminDb.collection("customers");
const querySnapshot = await usersCollection
.where("stripeId", "==", dataObject.customer)
.get();
const uid = querySnapshot.docs[0].id;
console.log(uid);
console.log('event',event);
So you're listening to Checkout Session events?
Are you planning to use webhook events to trigger the credit update each month, or are you only listening for an initial event to set up the credit refill process on your end?
I want to listen the inital event to set up tthe credits. Then use the webhook to refil it every month
That is what I was planning but if there's something better I can change
That makes sense, but in that case I wouldn't listen for Checkout Session events. Instead I'd suggest listening for invoice.paid. That type of Event will be generated whenever an Invoice is successfully paid for, which will happen each billing period as our Subscription objects use Invoices to process their recurring payments.
Those Events will also contain the related Invoice in their data.object field, so you should be able to reference the Invoice's Line Items to see what Products/Prices are being paid for.
Nice. The event type should be?
const dataObject = event.data.object as Stripe.Invoice.
I believe so, but let me know if that gives you troubles.
It also looks like your webhook event handling code is interacting with your database, and I want to warn that if it is doing so before sending a response to us then that could lead to problems in the future.
If the database interactions are asynchronous from your code providing us with a response to indicate you successfully received the Event, then that is fine and won't lead to risks of your code not sending us a response before the 20-second timeout window expires.
Thank you for the warning.
The console.log of the invoice status returns: succeeded, complete, paid, active and open
The one that I should care now is 'paid' or 'succeeded'?
The expected values for the status of an Invoice are a bit more narrow than that, though I believe they may fluctuate depending on the API version that you're using.
https://stripe.com/docs/api/invoices/object#invoice_object-status
If you listen for the invoice.paid event specifically, you should only receive Invoices that are in a paid state.
https://stripe.com/docs/api/events/types#event_types-invoice.paid
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Thanks. About your warning, what you recommend when the webhook has to interact with external database based on the event.status?
That you first handle signature verification of the Event if you're doing that as part of your flow, then respond to our request sending you the event to indicate that your endpoint successfully received the Event. Then you should handle any heavy processing such as interacting with your database asynchronously.
The response that you send, when we send an Event to your endpoint, is only intended to be an indication of whether your endpoint successfully received the event. It should not be dependent on whether your code was able to successfully process the Event's contents.
If we don't receive a response with a status in the 2XX range within 20 seconds, we consider the Event to be undelivered and queue it to be resent.
https://stripe.com/docs/webhooks#acknowledge-events-immediately
https://stripe.com/docs/webhooks#built-in-retries
thanks. that's all. have a good day
Any time, hope you have a great day as well.