#ingeniousambivert-webhook

1 messages · Page 1 of 1 (latest)

prime hawk
#

Have you tested that code with a test mode endpoint yet? Are you seeing any errors with it currently?

fleet ridge
#

Hi, I have been testing with cli and it seems to be working fine, no errors yet, I just want to confirm the logic. I have a been little confused, reading docs and several other articles with different approaches baffled me a little.

prime hawk
#

What parts are you confused on?

fleet ridge
#

I am confused on payment failures

prime hawk
#

I'm happy to address questions but reviewing and signing off on a whole chunk of code is unfortunately out of the scope of what I can do.

fleet ridge
#

No problem, if you can just help me get some clarity that would be great

#

Okay . The lifecyle - cus.sub.created -> invoice.payment.sucess -> initial
But then during renewal -> the cus.sub.updated is triggered first and then the invoice.payment.sucess, what happens if the invoice.payment fails ?

So in my webhook setup I update the activatedAt and expiryAt from event data - current_period_start and current_period_end. And check if current_period_end/expiryAt > now the subscription is valid.
So far this works great. But if a payment fails according to the lifecycle the cus.sub.updated activatedAt and expiryAt will be updated first and then invoice.payment.sucess will be triggered which when failed will trigger invoice.payment_failed but the periods are already updated.

#

To fix this I update my database again. on invoice.payment_failed with expiryAt as now.

#

Is this the correct approach ??

#

@prime hawk Heyy !

prime hawk
#

Thanks for the further questions, will look at them in a moment. I have not forgotten about you, the server is just a bit busy at the moment.

fleet ridge
#

Oh okay, no problem. Thank You

gusty garnet
#

@fleet ridge Give me a minute to catch up and I can help!

fleet ridge
#

Sure

gusty garnet
#

Having invoice payment succeed/fail is separate from the actual subscription updating - even if the Invoice payment failed we'd still want to cycle through to the next billing cycle

fleet ridge
#

Hi, but if the payment fails, and the subscription becomes past due, I wouldnt want the user to still be able to access the app

#

And if the card is expired then it would fail in the next cycle as well

gusty garnet
#

Then it would be up to you to build that logic into your integration - you can listen for the subscription update events to see when the status of the subscription changes, and manage access through that. Typically, Subscriptions don't transition to past_due until ALL retries for a payment have failed. If you want them to transition to past_due when the first payment fails you can configure that in your settings https://dashboard.stripe.com/settings/billing/automatic

fleet ridge
#

Okay, actually thats what I did, I have a custom logic implemented. And in my retries settings - it transitions to past due after 3-4 retries, but during these retries will the subscription be updated everytime ?

#

So for instance a payment doesnt succeed on first and second try but it does on third, will I receive 3 different customer.subscription.updated events ? or just the first one from the first try ?? because my logic is something like this -

      updatedSubscription.activatedAt = eventData.current_period_start * 1000;
      updatedSubscription.expiryAt = eventData.current_period_end * 1000;
   } else {
      updatedSubscription.expiryAt = new Date();
   }```
#

What I mean to ask is, I will receive a webhook trigger when the subscription is transitioned into past_due, correct ??

gusty garnet
#

So I believe you'd get an initial customer.subscription.updated event when the billing cycle changes and the latest invoice is generated, and then you'd get an additional one when the status changes to past_due

fleet ridge
#

Okay so the above logic will be executed when customer.subscription.updated is triggered which is - once for the initial renewal and the second time when the subscription becomes past_due, correct ?

gusty garnet
#

Yup

fleet ridge
#

Okay, thank you so much. This was a huge help. I looked into way too many guides and articles and got really confused. Thanks a ton.