fix(webhooks): read invoice.subscription in both API shapes (acacia + dahlia)

The live Stripe webhook endpoint has NO pinned api_version, so it follows the
account default (currently 2024-12-18.acacia), which delivers the subscription
link as the top-level invoice.subscription. The code only read the new
2026-03-25.dahlia shape (invoice.parent.subscription_details.subscription), so
recurring renewal/payment-failed events would have returned a null subscription
id and silently failed to fulfill once the events were enabled.

invoiceSubscriptionId() now reads the modern shape first, then falls back to the
legacy top-level field. All other invoice fields used by the handlers
(amount_due, attempt_count, hosted_invoice_url, id) are stable across both
versions. +5 tests (legacy string/object, modern-preferred-over-legacy).
This commit is contained in:
justin 2026-06-18 08:42:29 -05:00
parent cf021e2f91
commit 8af2685d07
2 changed files with 28 additions and 10 deletions

View file

@ -80,14 +80,22 @@ check("recurring line item has recurring.interval=month", built[0].price_data.re
check("recurring line item keeps $79 unit_amount", built[0].price_data.unit_amount === 7900);
check("recurring line item defaults quantity=1", built[0].quantity === 1);
// ── 7. invoiceSubscriptionId extraction (mirrors webhooks.ts; API 2026-03-25)
// ── 7. invoiceSubscriptionId extraction (mirrors webhooks.ts; version-tolerant)
function invoiceSubscriptionId(inv: any): string | null {
const sub = inv?.parent?.subscription_details?.subscription;
if (!sub) return null;
return typeof sub === "string" ? sub : sub.id;
// New API (>= 2026-03-25.dahlia)
const modern = inv?.parent?.subscription_details?.subscription;
if (modern) return typeof modern === "string" ? modern : modern.id;
// Legacy API (<= 2025, incl. account default 2024-12-18.acacia on the
// unpinned live endpoint): top-level invoice.subscription
const legacy = inv?.subscription;
if (legacy) return typeof legacy === "string" ? legacy : legacy.id;
return null;
}
check("subId from string field", invoiceSubscriptionId({ parent: { subscription_details: { subscription: "sub_123" } } }) === "sub_123");
check("subId from object field", invoiceSubscriptionId({ parent: { subscription_details: { subscription: { id: "sub_456" } } } }) === "sub_456");
check("subId from legacy top-level string (acacia)", invoiceSubscriptionId({ subscription: "sub_legacy" }) === "sub_legacy");
check("subId from legacy top-level object", invoiceSubscriptionId({ subscription: { id: "sub_legacy2" } }) === "sub_legacy2");
check("subId prefers modern over legacy", invoiceSubscriptionId({ subscription: "sub_old", parent: { subscription_details: { subscription: "sub_new" } } }) === "sub_new");
check("subId null when no parent", invoiceSubscriptionId({ id: "in_1" }) === null);
check("subId null for one-time invoice", invoiceSubscriptionId({ parent: { type: "quote_details" } }) === null);