Skip to content

Commit

Permalink
Ignore mismatched events without expiries too
Browse files Browse the repository at this point in the history
This happens for cancellation/termination events in PayPro, which
basically just apply to the current sub immediately. Any event without a
date that applies to a sub that isn't the current one doesn't matter I
think...
  • Loading branch information
pimterry committed Dec 24, 2024
1 parent 0b7e8a0 commit 5074e82
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions api/src/webhook-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export async function updateProUserData(email: string, subscriptionUpdate: Parti
// The possibilities here are quite complicated (e.g. new subs can start as 'cancelled' due to
// manual renewal configuration in PayPro) but "latest expiry" tends to be the right answer.

if (subscriptionUpdate.subscription_expiry! < appData.subscription_expiry) {
if (
!subscriptionUpdate.subscription_expiry || // Cancel event (applied to wrong sub)
subscriptionUpdate.subscription_expiry! < appData.subscription_expiry
) {
log.warn(`User ${email} received a outdated subscription event for an inactive subscription - ignoring`);
return; // Ignore the update entirely in this case
}
Expand All @@ -78,10 +81,12 @@ export async function updateProUserData(email: string, subscriptionUpdate: Parti
appData.subscription_status !== 'past_due' &&
moment(appData.subscription_expiry).subtract(5, 'days').valueOf() > Date.now()
) {
console.log('Existing data', JSON.stringify(appData));
console.log('Mismatched updated', JSON.stringify(subscriptionUpdate));

reportError(`Mismatched subscription event for Pro user ${email} with existing subscription`);
reportError(`Mismatched subscription event for Pro user ${email} with existing subscription`, {
extraMetadata: {
existing: appData,
updated: subscriptionUpdate
}
});
}
}

Expand Down

0 comments on commit 5074e82

Please sign in to comment.