Skip to content

fix(core): read the Stripe subscription period from its items - #136

Merged
cavewebs merged 1 commit into
emdashCommerce:mainfrom
FIKANOVA:fix/stripe-subscription-period
Dec 3, 2026
Merged

cavewebs merged 1 commit into
emdashCommerce:mainfrom
FIKANOVA:fix/stripe-subscription-period

Conversation

@moset15

@moset15 moset15 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

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.

Stripe API versions from 2025-03-31 moved `current_period_start` and
`current_period_end` from the subscription onto each subscription item.
`upsertFromStripe` only read the subscription's own fields, so on
current versions both fell back to "now": the admin showed the period
ending the day it started, and the cancel dialog said access continued
until today.

The period now comes from the item (the one that ends first, if there
are several), then the subscription's own fields for older API
versions, then the stored period, and only then "now".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cavewebs
cavewebs merged commit 82a33cf into emdashCommerce:main Dec 3, 2026
11 checks passed
@moset15
moset15 deleted the fix/stripe-subscription-period branch December 6, 2026 01:36
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