Skip to content
All writing

A plan switch should not become two bills

A member switched from a weekly plan to a monthly one. My software read that as a reason to bill them for both. The second charge did not come from the screen where they made the switch. It came from a nightly job I had written months earlier to be helpful.

PA Gym OS is the Next.js platform at app.persistenceathletics.com I built to replace Wodify. Stripe is only a card vault. A Sunday cron is the only thing that ever charges a card, and the membership table is the source of truth for who owes what. When a member changes plans, the old plan is supposed to end. In this case it did not end. It paused. And a different nightly job, the one whose whole purpose is to resume paused memberships, found it and switched it back on.

The shape

The rule is one sentence: a member may hold one billable recurring membership per category, and no more. Categories are real and separate. A gym membership, a remote-coaching plan, personal training, a class pack: a member is allowed to hold one of each at once. What they can never hold is two of the same kind, because two recurring rows in the same category is two charges for the same thing, plus two renewal emails crossing in the mail. I now enforce that at 3 chokepoints, all in one membership-lifecycle file, verified against 15 SQL scenarios and a 4-lens review, and I wrote it down as the eighth rule in my payment-paths playbook so it cannot quietly come back.

old: switching plans left the old one paused but alive, and a nightly job flipped it back on and lined up a second charge in the same category.
new: assigning a paid plan supersedes every other billable plan in its category, inside one transaction, and no cron can bring the old one back.

The bug was a helpful cron

The switch itself looked clean. The new monthly plan went active, the old weekly plan went to paused. The trouble is that paused is not gone. Every night a job resumes paused memberships, because a pause is meant to be temporary and members forget to come back. It found the abandoned weekly plan, flipped it to active, sent a weekly renewal email, and lined up a charge for a plan the member had already left behind. The original guard only blocked assigning a duplicate of the exact same plan. Switching to a different plan in the same category walked straight past it.

Enforce it everywhere a row can be written

A uniqueness rule is only real if every path that writes the table honors it, so the fix lives in three places. Assigning a paid recurring plan now supersedes any other active or paused billable plan in the same category, and the cancel and the insert run in one transaction, so if the cancel throws after the card has been charged, the new row rolls back with it and nobody is left double-membershipped. The resume path refuses, with a 409, any unpause that would recreate the conflict. And the nightly auto-resume job, the one that caused all of this, now skips a plan that was superseded. Every guard keys off effective price, not a flag: a complimentary plan, a prepaid plan, or a plan that nets to $0 is exempt, so a free membership a member legitimately holds next to a paid one is never cancelled out from under them.

The member said active, the membership said gone

Closing the first hole surfaced a quieter one. An audit found 16 members marked active with no active membership behind them. Their membership rows had all been flipped to terminal states by the nightly cleanup, but each member's own status flag never caught up, because nothing owned the job of reconciling the two. Pause-to-stop drains and leftover comp memberships from the old system had stranded them as active with nothing underneath. The new pass reconciles it: a member with no live membership, whose last one has been dead for more than 14 days, flips to inactive. It is silent, because they already got their end-of-membership email when the row terminated. The 14-day grace keeps a freshly lapsed member from being demoted mid-review, and the moment they resubscribe the signup path flips them straight back to active. It leaves staff and anyone who never held a membership alone. The dry run on production returns 0 rows today, because the drifted records are finally reconciled.

What I learned

A uniqueness rule is only as strong as the weakest path that writes the row. I had a guard on the front door, the plan-switch screen, and it worked. The breach came through a background job I had written months earlier to do something kind, resume paused memberships, and it cheerfully recreated the exact state the front door forbade. Enforce the invariant at every write, and pay the most attention to the automated ones nobody is watching.

A cancel and an insert that must both happen belong in one transaction. The supersede cancels the old plan and inserts the new one. If the cancel fails after the card is charged, you have taken the money and handed the member two memberships. Wrapping both in a single transaction means a failure rolls the whole thing back, so the worst case is a clean retry, never a double charge.

A parent flag and its child rows drift apart unless something is paid to reconcile them. The membership rows were all correct, each one flipped to its terminal state. The member records had simply never been told, so 16 of them sat marked active with nothing active beneath them. Any system with a status derived from rows below it needs a job whose only purpose is to make the two agree, plus a grace window so it heals the data without punishing whoever lapsed yesterday.

If your business charges on a schedule and you are not certain every path that touches a subscription agrees on the rules, that is the kind of plumbing Autosterea builds.

First published on ravikantdewangan.com.

Tell us what eats your week.

A free 20-minute call. We'll show you what we would take off your plate.

Book a call