Skip to content

feat(core): track Stripe renewal invoice refunds - #147

Draft
alexasomba wants to merge 1 commit into
emdashCommerce:mainfrom
alexasomba:codex/stripe-renewal-invoice-refunds
Draft

alexasomba wants to merge 1 commit into
emdashCommerce:mainfrom
alexasomba:codex/stripe-renewal-invoice-refunds

Conversation

@alexasomba

@alexasomba alexasomba commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Review order

Please review Stripe subscription checkout and customer linking #138 and Correct Stripe renewal amounts #139 on their own timeline. They were opened first and establish the Stripe subscription checkout and renewal behavior this refund draft will build on. The current branches merge cleanly in a local Git preview. Once the Stripe changes settle, I'll recheck the invoice linkage and keep this PR focused on recording refunds against paid renewals.

What changes

Match signed Stripe refund events to paid renewal invoices by the saved PaymentIntent. Preserve the invoice and subscription state, and expose refund status and the confirmed total through a private admin route. Immutable observations let duplicate or out-of-order callbacks converge without counting a refund twice.

Draft scope

This first slice handles invoices with an exact persisted PaymentIntent link. Existing invoice rows and Stripe API versions that deliver payment links through the Invoice Payments API need an explicit backfill or lookup path. Paystack and Flutterwave renewal refunds can extend the invoice history after their provider subscription seams land. No live Stripe refunds were initiated.

Validation

Core typecheck and all 365 Core tests passed. Signed webhook fixtures cover paid renewal linkage, a partial refund, replay, a late pending event, and an unrelated payment.

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

## Bug

From Stripe API version 2025-03-31, an invoice no longer has a top-level
`subscription`: it names its subscription under
`parent.subscription_details.subscription`. `upsertInvoiceFromStripe`
starts with `if (!inv.subscription) return null`, so on current API
versions **every subscription invoice is ignored**:

- `invoice.paid` stores no invoice row, so the renewal email never goes
out.
- `invoice.payment_failed` (dunning) never marks the subscription past
due from the invoice.

Found while testing a shop end to end: after several paid Stripe
subscriptions, the shop had no subscription invoices at all.

There's a second problem on the same path. When an invoice was recorded,
the subscription period was taken from the invoice's own
`period_start`/`period_end`. Those aren't the subscription period: on
the first invoice they're both the creation time.

## Fix

- `invoiceSubscriptionId(inv)` reads `inv.subscription`, or
`inv.parent.subscription_details.subscription`.
- `invoiceSubscriptionPeriod(inv)` takes the period from the invoice's
subscription lines (`lines.data[].period`). It falls back to the
invoice's own dates when there are no line periods and they span
something.
- The `StripeInvoice` type gains the optional `parent` and `lines`
fields.

That's all of it: `upsertInvoiceFromStripe` uses the two helpers, and
`invoice.payment_failed` goes through it too. No change to `webhook.ts`
or `checkout.ts`.

## Tests

New `packages/core/test/stripe-invoice-parent.test.ts`, 4 tests:
- **Both shapes:** the subscription is found under `parent`, or at the
top level.
- **The period:** it comes from the subscription line, not the invoice's
own dates.
- **A current-shape renewal invoice** is recorded, and it moves the
subscription's period.
- **An older-shape invoice** is still recorded.

The recording test fails without the change. Core 364 pass; the 4 local
`routing-response-adapter` failures pass on CI. Typecheck and Biome
clean.

Also tested by hand on a running shop (EmDash 1.6, Stripe test mode):
paid subscription checkouts now record their invoices, with the period
running one interval from the payment.

## Review points

- **#147** reads `inv.payment_intent`, which current API versions don't
send either. #138 adds an Invoice Payments lookup that could serve it.
This PR doesn't touch `payment_intent`, and it merges cleanly with #147,
#138 and #107.
- Older API versions behave as before, apart from the period now coming
from the lines when they carry one.
@alexasomba
alexasomba force-pushed the codex/stripe-renewal-invoice-refunds branch from 27dc20e to b489d74 Compare December 4, 2026 07:03
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.

1 participant