#neha_webhooks
1 messages ยท Page 1 of 1 (latest)
๐ 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/1215416824422727742
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
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.
- neha_unexpected, 1 hour ago, 73 messages
- neha_best-practices, 16 hours ago, 47 messages
@sturdy holly
is the difference that created is just done once, at the "start" (maybe after "updated" though)
๐ the created event means this is a brand-new Subscription, while the updated event means that something about the existing subscription has changed
and update is done on a recurring basis for subscriptions?
but when the user FIRST signs up
both will be called
correct?
so i should use "create" for like, one-time set up
and "update" for the meat of the stuff related to keeping the subscription running (or not, if it's past due)
Right, you may see both when the user first signs up if, for example, the subscription's status changes, the billing period changes, etc.
may or definitely will?
I recommend testing this out so you see it in action. You can test creating a subscription and going through renewal cycles by using Test Clocks: https://docs.stripe.com/billing/testing/test-clocks
before it didn't matter if i repeated work in the create and update handlers
but now i have a credit system
so i don' want to allocate the user 2x the credits
should i only allocate in "update" rather than "create" ?
You will need to see what update was made by inspecting the previous_attributes hash on the updated event, then determine whether that kind of update warrants a credit allocation
can you link me to that object declaration
and in general are you agreeing that, if my goal is to give the users 100 credits a month, for example, this should happen (encapsulated by certain conditons) in the "update" not the "create"
or are you saying it should happen in both
Previous attributes: https://docs.stripe.com/api/events/object#event_object-data-previous_attributes
You likely want to allocate credits on a subset of update events
But that will really be up to you
what, in the previous attributes field, would indicate that i don't want to allocate?
i basically only want to allocate when payment has been remitted and the subscription has officially begun its active period
Any change to a subscription would trigger a customer.subscription.updated event. So, for example, if you change the description, add metadata, or make any other change that doesn't impact billing/payment, you won't want to allocate credits then
so how do i know it's a renewal per se
like the clock ran out on, the invoice was finalized, they paid, it's time to top up
'interval_count"?
You could listen for invoice.paid instead and inspect the payload for its subscription. For example, your event handler logic can then pull up details about that specific subscription (if you maintain some internal database that maps subs to customers, plan details, etc.) then allocate credits accordingly
Sure thing!
sorry one more
so for the invoice paid and invoice payment failed webhooks
do i still have to check the latest status of the subscription? bc the webhooks can be called out of order?
You can check the subscription if you want but, assuming you have a subscription that bills monthly, you won't get invoice.paid and invoice.payment_failed for the same subscription on the same day as part of a normal renewal
and how do i put my client_reference_id as metadata in the invoice object? is that possibel?
If you do make a subsequent call, you can retrieve the Invoice that's mentioned in the event and expand the subscription This will return the invoice and subscription objects in one call: https://docs.stripe.com/expand
ok so if i double-check the status
then i can get the subscription=>metadata as part of that
but in general it's probably ok to not double check the sub status for invoice.paid and invoice.failed
Right, yep
will the payment_failed be called on every retry?
Also note that metadata on a subscription and metadata on an invoice are different things
If an automatic/smart retry payment attempt on an invoice fails, the invoice.payment_failed event will be emitted for every retry, yes
ok so i can look at attempt_count or attempted i guess
Yep, you can look at that too
what's the difference between invoice.paid and invoice.payment_succeeded
invoice.paid is the one you should listen for as it will be emitted anytime an invoice is transitioned into the paid status, including when you manually mark an invoice as paid
invoice.payment_succeeded is not sent if you manually mark an invoice as paid
so for payment_succeeded
i should fetch the full object associated with the subscription in the invoice object?
in order to get the line items
(let me look, don't close the thread please!)
I don't quite follow. If you listen for invoice.paid, you can make a subsequent request to retrieve the Invoice, and expand the subscription if you want to inspect the Subscription object in a single call
ok got it
but the invoice object has a "subscription" key
so can't i just get the subscription w/ that?
"subscription_details": {
"metadata": {
}
},
Can you be more specific? I don't follow. The invoice.paid event will be an Invoice object, so you'll see subscription: sub_123 in the payload but nothing else about the Subscription. If you want more information about the Subscription at that point, you can either make a call to retrieve the Subscription directly or make a call to retrieve the Invoice and expand subscription
ok got it
ok so here's what i did
checkout.session.completed: used for one-off purchases (topping up credits)
customer.subscription.created: used for one-time set up (storing stripe customer ID in my User table)
customer.subscription.updated: used to perform side-effects related to downgrading
customer.subscription.deleted: used to cancel
invoice.paid: used to create/update subscription and provision credits
invoice.payment_failed: used to perform side-effects related to failed payment
This looks like a good plan but I still recommend mocking this out and testing it with test clocks