Skip to content

fix(core): index customers by their Stripe customer id - #127

Open
moset15 wants to merge 1 commit into
emdashCommerce:mainfrom
FIKANOVA:fix/index-refund-request-id
Open

moset15 wants to merge 1 commit into
emdashCommerce:mainfrom
FIKANOVA:fix/index-refund-request-id

Conversation

@moset15

@moset15 moset15 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

EmDash refuses a where on a field the collection doesn't index. customer.subscription.created and customer.subscription.updated find the local customer by stripeCustomerId, and customers doesn't index that field. So on a real shop every paid Stripe subscription gets a 500 from the webhook: "Cannot query on non-indexed field 'stripeCustomerId'." Stripe keeps retrying, and the subscription is never recorded.

The route test harness didn't enforce the rule, so the tests passed. refunds.refundRequestId had the same gap; #107 indexed it, so this PR no longer touches refunds.

I checked every storage query, count and orderBy in core, including dynamic where objects. stripeCustomerId is the only field still missing an index.

Changes

  • customers indexes now include stripeCustomerId. This is a plain index, not a unique one, so existing rows can't break it.
  • The route test harness now refuses where, orderBy and count on a field the collection doesn't index, as EmDash does.
  • New test: a Stripe subscription checkout through the real routes, then signed checkout.session.completed, customer.subscription.created and customer.subscription.updated webhooks.
  • Changeset: patch.

Checks

This should go in before the pending release (#120), which would otherwise ship with Stripe subscriptions unrecorded.

#138 builds on this index and fixes two more Stripe subscription bugs from the same test.

@cavewebs

cavewebs commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

This looks like the right fix for refunds and Stripe subscription webhooks failing on a real shop. #107 already adds the refundRequestId index, so after that lands this mainly needs the stripeCustomerId index. CI failed on the EmDash 1.1.0 starter typecheck, and this branch does not touch the starter, so I think that is a flake. Could you rerun that job?

cavewebs added a commit that referenced this pull request Dec 3, 2026
## Bug

From API version 2025-03-31, Stripe sends `current_period_start` and
`current_period_end` on each subscription item instead of on the
subscription. `upsertFromStripe` reads them from the subscription only,
so on current API versions both are missing and fall back to "now".
Found while testing a shop end to end: a subscription paid on 1 December
showed "1 Dec → 1 Dec" in the admin, and the cancel-at-period-end dialog
said "Access continues until" that same day, while Stripe would end it a
month later.

## Fix

In `upsertFromStripe` the period now comes from, in order:

1. the subscription item (with several items, the one whose period ends
first, which is what the customer has paid up to);
2. the subscription's own fields, for older API versions;
3. the period already stored, if the event carries none;
4. "now", as before.

The `StripeSubscription` type gains the item-level fields, and the
top-level ones become optional.

## Tests

New `packages/core/test/stripe-subscription-period.test.ts`, 4 tests,
each through the real webhook route with a signed
`customer.subscription.updated`:

- the period on the item is stored;
- the older API shape (period on the subscription) is still read;
- with two items, the earlier end wins;
- an event with no period keeps the stored one.

3 fail without the fix; the older-API test passes both ways, as
intended. Full suite: core 364 pass (plus the 4 local
`routing-response-adapter` failures: the EmDash patch isn't applied in a
fresh worktree; they pass on CI), paystack 105, lite 144. Core typecheck
and Biome lint clean.

## Compatibility

No change for accounts on API versions before 2025-03-31. Nothing else
reads the top-level fields.

## Review points

- `upsertInvoiceFromStripe` also moves the period on `invoice.paid`,
from `period_start` / `period_end`; that is untouched.
- Not in this PR, but worth a look: on the same API versions Stripe also
moved `invoice.subscription` under
`invoice.parent.subscription_details.subscription`. I haven't checked
how that affects `invoice.paid` handling here.

**Works on a real store once #127 merges:** until
`customers.stripeCustomerId` is indexed, `customer.subscription.*`
webhooks fail before they reach this code. The code doesn't depend on
#127 and merges on its own.
cavewebs added a commit that referenced this pull request Dec 3, 2026
#137)

## Bug

The admin's Pause sets `pause_collection` at Stripe, but Stripe keeps
the subscription's status `active`. `upsertFromStripe` stores Stripe's
status as it is, so the row stays Active after the
`customer.subscription.updated` webhook, and Resume, which the admin
offers only for `paused` rows, never appears. Found while testing a shop
end to end: once paused, a subscription couldn't be resumed from the
admin.

## Fix

An `active` or `trialing` subscription with `pause_collection` set is
stored as `paused`. When collection resumes, Stripe clears
`pause_collection` and the row goes back to Stripe's status. Other
statuses (`past_due`, `unpaid`, `canceled`, …) are kept as Stripe sends
them, so a payment problem isn't hidden behind "paused".

## Tests

New `packages/core/test/stripe-subscription-paused.test.ts`, 4 tests,
each through the real webhook route with a signed
`customer.subscription.updated`:

- active with `pause_collection` is stored as `paused`;
- the same during a trial;
- after a pause, an event with `pause_collection: null` sets it back to
`active`;
- `past_due` with `pause_collection` stays `past_due`.

3 fail without the fix; the `past_due` test passes both ways, as
intended. Full suite: core 364 pass (plus the 4 local
`routing-response-adapter` failures noted in the other PRs), paystack
105, lite 144. Core typecheck and Biome lint clean.

## Compatibility

Only rows for paused Stripe subscriptions change. `paused` is already a
`SubscriptionStatus` and the admin already offers Resume for it.

## Review points

- Stripe's own `paused` status (a trial that ended with no payment
method) is unchanged and still comes through as `paused`.
- The admin list updates when the webhook arrives, not the instant Pause
is clicked. Making it instant means storing Stripe's reply in
`lifecycle.ts`, which #107 is changing, so it's left for later.

**Works on a real store once #127 merges:** until
`customers.stripeCustomerId` is indexed, `customer.subscription.*`
webhooks fail before they reach this code. The code doesn't depend on
#127 and merges on its own.
EmDash refuses a where on a field the collection doesn't index.
customer.subscription.* webhooks find the local customer by
stripeCustomerId, which wasn't indexed, so every paid Stripe subscription
answered 500 and was never recorded. (refunds.refundRequestId had the
same gap; emdashCommerce#107 indexed it.)

An audit of every storage query, count and orderBy in core found no other
gaps. The route harness now refuses non-indexed where, orderBy and count
fields like EmDash does, so the refund tests and a new Stripe
subscription webhook test catch these.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@moset15
moset15 force-pushed the fix/index-refund-request-id branch from cd991de to 394ffb2 Compare December 5, 2026 22:53
@moset15 moset15 changed the title fix(core): index the fields core queries by fix(core): index customers by their Stripe customer id Dec 5, 2026
@moset15

moset15 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main. #107 already indexes refundRequestId, so this now adds only the customers.stripeCustomerId index, and I've updated the title and description to match. The rebase started a fresh CI run. Since #136 and #137 merged, their Stripe subscription tests go through the same lookup: with the stricter harness, 10 tests fail without the index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants