DEV Community

naval
naval

Posted on

Two Stripe subscription bugs I fixed in a live app: failed renewals and end dates that never move

I sent two fixes to a live marketplace backend this week. Both were Stripe subscription bugs, and both are easy to ship without noticing, because nothing errors. The local copy of the plan just slowly stops matching what Stripe says.

Here are the two patterns and the fix for each. The app is Node, Express and MongoDB, but the bugs are the same in any stack.

Bug 1: one failed card payment ends the plan

The handler did this on invoice.payment_failed:

if (invoice.billing_reason !== 'subscription_create') {
  localPlan.status = 'cancelled';
}
Enter fullscreen mode Exit fullscreen mode

So the first declined renewal hid the vendor's listings. Stripe doesn't cancel on the first failure. It moves the subscription to past_due and retries the charge over the next days. If a retry succeeds, the vendor paid and still lost their listings in between.

The same handler had the opposite bug too. customer.subscription.deleted only expired pending records, so when Stripe really did end a subscription (or the vendor cancelled in the billing portal), the local plan stayed active.

Fix: stop deciding the plan from the invoice event. Record the failed payment, and end the plan only when Stripe says the subscription ended:

// invoice.payment_failed: record it, don't end anything
localPlan.paymentStatus = 'FAILED';

// customer.subscription.updated / .deleted
const current = await stripe.subscriptions.retrieve(subscriptionId);
if (['canceled', 'unpaid', 'incomplete_expired'].includes(current.status)) {
  await Plan.updateOne({ stripeSubscriptionId: subscriptionId, status: 'active' },
                       { $set: { status: 'cancelled' } });
}
Enter fullscreen mode Exit fullscreen mode

The re-fetch matters. Events arrive out of order and get retried for up to 3 days, so an old "unpaid" event can show up after Stripe has already reactivated the subscription. Reading the current state from Stripe means the late event changes nothing.

past_due is left alone. How long a vendor keeps access while Stripe retries is a product decision, not a webhook one.

Bug 2: the end date never moves

The plan got an endDate once, at checkout: checkout time plus 365 days. Activation flipped the status to active and never touched the dates again. A renewal's invoice.paid matched status != 'active', so for an active plan it did nothing.

Listings were gated on endDate being in the future. So about a year after subscribing, every paying vendor's listings would disappear, while Stripe kept charging them and the dashboard kept saying the plan was fine.

Fix: take the dates from Stripe's billing period on every paid invoice, and only ever move the end date forward:

const item = sub.items?.data?.[0];
const end = item?.current_period_end ?? sub.current_period_end; // newer API vs older
await Plan.updateOne(
  { _id: plan._id, status: 'active', endDate: { $lt: new Date(end * 1000) } },
  { $set: { endDate: new Date(end * 1000) } },
);
Enter fullscreen mode Exit fullscreen mode

Two details:

  • On Stripe API version 2025-03-31 and later, the period moved off the subscription onto its items. Reading only subscription.current_period_end gives you undefined on newer versions, so read both.
  • The $lt filter keeps a late invoice for an older period from shortening the plan.

What both bugs have in common

Both handlers trusted one event to describe the whole state: a failed invoice meant "cancelled", a first activation meant "dated forever". Stripe's events are hints that something changed. The subscription itself is the state.

If you keep your own copy of subscriptions, two checks are worth a test each:

  1. A declined renewal followed by a successful retry. Does access survive?
  2. A renewal on an already active plan. Does the end date move?

Neither shows up in a happy-path test, because a new subscription works fine for its first year.

I put the re-fetch and revision-check approach in a small open source library while working on this: https://github.com/navalpro8-lgtm/stripe-subscription-sync. npm run demo replays the late-event case against a real Postgres in a few seconds, no Stripe account needed.

Top comments (2)

Collapse
 
hamichidev profile image
Hamichi •

The "first year works fine" part is what makes bug 2 so nasty, since no happy-path test ever reaches the renewal. One addition to the end-date fix: take the period from the plan's line on the paid invoice rather than from the subscription item, because the invoice records exactly which period that payment covered, so a retried invoice for an older period can never push the date to the wrong place. Paired with your forward-only filter, that makes the end date a pure function of invoices that actually got paid. How long do you keep access during past_due: tied to Stripe's retry schedule, or a fixed grace period of your own?

Collapse
 
navalpro8lgtm profile image
naval •

good call on the invoice line. it records exactly which period was paid so it's cleaner than the subscription item. rn the forward only filter does that job, worth testing the invoice version against it. on past_due: in that app access stays till stripe moves it to canceled or unpaid. thats the maintainers rule, i didnt set it. in my own lib its a policy function you can change