TransWikia.com

How to differentiate EVENT_BEFORE_UPDATE_SUBSCRIPTION events in Commerce 2/3

Craft CMS Asked on October 1, 2021

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?

One Answer

Could you use one of the following events?

craftcommercestripeservicesInvoices::EVENT_BEFORE_SAVE_INVOICE
craftcommercestripeservicesInvoices::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

Answered by Luke Holder on October 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP