#arya_
1 messages ยท Page 1 of 1 (latest)
Hello! The answer to your overall question is really up to you, and depends on your specific business needs. When you receive an invoice.paid Event you can retrieve the subscription associated with it to determine the Price ID.
Thank you. How can the price_id be determined from the subscription? I do store the subscription in my db
Cant the payment still fail even after customer.subscription.created weebhook has been fired
The Subscription will have the Price ID in items.data.price.id: https://stripe.com/docs/api/subscriptions/object#subscription_object-items-data-price-id
It's possible for the payment to fail after the Subscription is created, yep. It depends on how you're using Subscriptions.
Inside the invoice.paid I console.log(data) which has a field called subscription which has the subscription id, but there is not field called items and there is no place where i see the price id
For context:
const sig = req.headers["stripe-signature"];
let event;
try {
event = stripe.webhooks.constructEvent(
req.rawBody,
sig,
process.env.STRIPE_WEBHOOK_SECRET
);
} catch (err) {
return res.sendStatus(400);
}
const data = event.data.object;
The subscription property on the Invoice is the Subscription's ID. You need to then fetch that Subscription using the API to get the full Subscription object.
Got it. Wouldn't it be nice if all this was an atomic operation? All actions fail or all actions succeed
Also, what's the difference between invoice.paid and invoice.payment_succeeded? How do I understand which one to use when
Hello ๐ Rubeus had to step out but I can help. invoice.paid is typically the one we reccommend listening to. They are pretty much the same events, the one difference is that invoice.paid will also trigger if you mark a payment as "paid out of bank" (aka paid outside of Stripe) so that event is a bit more versatile
Other than that they have the same exact data and will be triggered for the same things
THank you! That's all I had right now ๐
Wouldn't it be nice if all this was an atomic operation? All actions fail or all actions succeed
What actions specifically are you interested in here?
The invoice being paid and the subscription becoming active are tied together. I am not 100% sure if the operation is strictly atomic but even if there was a temporary error preventing the status from changing we would correct it
When I catch the event.data.object inside invoice.paid, I get access to the subscription id as event.data.object.subscription which is then used to make an API call stripe.subscriptions.retrieve(event.data.object.subscription); to retrieve the subscription object and grab the price id. Just curious if this api call could fail as well
The API call can fail but that would be because of something like a network connectivity issue
In that case we'd recommend making the retrieve call a few more times and maybe doing an exponential backoff to space out the retries
Got it, how can i implement this
Which of our client libraries are you using? Node? I think our libraries have a setting for retries and can look it up for your specific one
Yes im using node.js
We have this configuration for that https://github.com/stripe/stripe-node#network-retries
Thank you!
When cancelling a subscription, I want the user to have access till the end of the current billing cycle and then cancel. I know I have to call stripe.subscriptions.update withcancel_at_period_end: true, but Iโm not sure how to update this in my database. Is there a webhook that fires when the subscription actually cancels (at the end of the period). If so, how can I know? If not, how should I handle this case in my server and db?
Yes, customer.subscription.updated will fire when you initially update the subscription with cancel_at_period_end and customer.subscription.deleted will fire when it actually cancels
We have a very useful tool called test clocks for testing out this kind of thing https://stripe.com/docs/billing/testing/test-clocks
You can simulate time passing to that future cancel date without having to wait for it