How to differentiate EVENT_BEFORE_UPDATE_SUBSCRIPTION events in Commerce 2/3 I'm listening for various subscription events to send emails to members. Right now, I'm dealing with subscriptions where a renewal payment fails. I'm specifically listening for EVENT_BEFORE_UPDATE_SUBSCRIPTION.
The problem is it's called twice per attempt (successful or not). Once for the Stripe invoice creation and once for the Stripe transaction attempt. Here's what is might look like:
Day 1: EVENT_BEFORE_UPDATE_SUBSCRIPTION called as the renewal invoice is created. The subscription status is active.
Day 1 (1 hr later): EVENT_BEFORE_UPDATE_SUBSCRIPTION called as the transaction fails. The subscription status is suspended.
Day 2: EVENT_BEFORE_UPDATE_SUBSCRIPTION called as a second renewal invoice is created. The subscription status is suspended.
Day 2 (1 hr later): EVENT_BEFORE_UPDATE_SUBSCRIPTION called as the transaction succeeds. The subscription status is active.
You can see from the above that just listening to the EVENT_BEFORE_UPDATE SUBSCRIPTION will result in two emails being sent each day. Even if I listen for the status being suspended, an unnecessary email will be sent the following day.
What I really want to do is differentiate between the event being called by Stripe creating an invoice and Stripe attempting a transaction.
Has anyone done this or know a variable I can test against to identify whether it's an invoice creation or transaction that has triggered the event?
Could you use one of the following events?
\craft\commerce\stripe\services\Invoices::EVENT_BEFORE_SAVE_INVOICE
\craft\commerce\stripe\services\Invoices::EVENT_SAVE_INVOICE

Maybe looking in the invoice model invoiceData attribute (that is passed to the above event).
If that won't suffice, we should expose a way for you to listen to any Stripe Webhook event that comes into commerce, and react.
Please comment on the ticket for that here if so: https://github.com/craftcms/commerce/issues/1799