#Reformedot

1 messages · Page 1 of 1 (latest)

knotty runeBOT
dawn knot
#

👋 Hi there, Happy to help!

but I'm not able to distinguish if the webhook comes from the subscription charge or my manual charge via api
What event type are you listening for actually ?

whole yew
#

invoice.payment_failed

#

We have a filter if data["billing_reason"] != "subscription_cycle":

But is not enough, manual payments via dashboard have subscription_cycle billing reason

#

While testing, using Striple CLI:

                invoice_data = stripe.Invoice.pay(
                    data["id"],
                    payment_method=valid_payment,
                    off_session=True
                )
#

this invoice has "manual" as billing_reason, but probably because stripe cli creates a dummy invoice

#

So in prod, those stripe.Invoice.pay invoices will have "subscription_cycle" right?

#

And I end up in a infinite loop listening that webhook

dawn knot
whole yew
#

Okay, then, there's any other value that we can check to differentiate between a Manual/API charge and the one that Stripe does automatically?

dawn knot
#

Sorry, I'm not understanding your question then. For the event invoice.payment_failed in order to decide if the invoice is for a manuel/API charge then the billing_reason will be manual and if it was send by a subscription then the value will be one of these (subscription_cycle, subscription_create,subscription_update or subscription)

whole yew
#

Okay let me explain a bit better

dawn knot
#

Yes please

whole yew
#

In order to charge a subscription there are few methods:

  • Stripe = Automatically charges (each month) and automatically retries X times if the invoice paymeyment fails (for us, 5 retries every 2 days or something like that)
  • Dashboard = We can force a charge from a customer via dashboard, selecting the credit card and everything, this is useful to charge users that own different payment methods, cause Stripe only charges the default one, and can be invalid, which forces a user to pay the invoice via link)
  • Link = At the moment, when a customer subscription fails, they need to pay by link the invoice
  • Api = We want to use the API to charge like "Dashboard" but automatically

We want to:

  • The first time the payment fails (end of the month, the customer dont have funds on default payment method, for example) we loop all payments via API and we try to charge them
  • We want to skip all manual charges, only retry if the charge comes from the first Stripe fail (max one per month)

The problem:

  • When our loop fails (second cc is also empty) it raises a payment failed webhook again, and it retries, so it's an infinit loop
dawn knot
#

Now I see better, thank you very much for your detailed explanation 👌

#

let me check something for you

whole yew
#

Okay thanks ❤️

dawn knot
#

Sorry for keeping you waiting

whole yew
#

Don't worry

dawn knot
#

Unfortunatly, you can't differenciate that. It seems not a recommended way to handle failed payment if you check the documentation:
https://stripe.com/docs/billing/subscriptions/webhooks#payment-failures
You should instead do one of these options:

  • Notify the customer.
  • If you’re using PaymentIntents, collect new payment information and confirm the PaymentIntent.
  • Update the default payment method on the subscription.
  • Consider enabling Smart Retries.
#

If you want to keep this logic then you should store in your integration what PaymentIntent was recently used and not tryied. like you create a queue of PaymentMethod for each customer for an failed invoice and you try a remaining PaymentMethod after each failed webhook event

whole yew
#

Okay perfect

#

thanks a lot man!

#

I will try to implement that login checking the used payment methods

#

❤️