Stripe webhook sends the cancellation email on every subscription update
Split out of #688.
Problem
handleCustomerSubscriptionUpdated in src/payment/stripe/webhook.ts sends the "We hate to see you go" email whenever subscription.cancel_at_period_end is true:
if (subscription.cancel_at_period_end && user.email) {
await emailSender.send({ ... });
}Stripe fires customer.subscription.updated for many reasons, for example renewals, payment method changes, or metadata updates. Every one of those events that arrives while the subscription is scheduled to cancel sends the email again, so a user who cancels can get the same email several times before the period ends.
Only the Stripe handler sends this email. The Polar and Lemon Squeezy handlers don't, so this is Stripe-specific.
Suggested fix
Send the email only on the transition into the cancelled state. The handler already knows the user's previous subscriptionStatus from the database before it updates it, so compare the old status with the new one and send the email only when the status changes to cancel_at_period_end. Stripe's previous_attributes on the event is another option, but it only lists changed attributes and can be absent on retries.
Source: wasp-lang/open-saas