// Run with: node trial-reconciliation.mjs // This example uses invented evidence. It does not read files, contact Stripe, // send analytics, or require credentials. import assert from 'node:assert/strict'; const notices = [ { invoice: 'in_renewal', notice: 'paid' }, { invoice: 'in_first', notice: 'payment_failed' }, { invoice: 'in_trial', notice: 'paid' }, { invoice: 'in_first', notice: 'paid' }, { invoice: 'in_first', notice: 'paid' }, { invoice: 'in_external', notice: 'paid' }, ]; // These rows represent invoice and payment evidence after all notices. // This is the complete history for one subscription. The amounts are in // cents. An external settlement can mark a positive invoice paid without // Stripe collecting the money. const invoices = [ { id: 'in_trial', paidAt: '2026-10-01', invoiceTotal: 0, stripeCollected: 0 }, { id: 'in_external', paidAt: '2026-10-05', invoiceTotal: 4900, stripeCollected: 0 }, { id: 'in_first', paidAt: '2026-10-08', invoiceTotal: 4900, stripeCollected: 4900 }, { id: 'in_renewal', paidAt: '2026-11-08', invoiceTotal: 4900, stripeCollected: 4900 }, ]; // Raw GA4 purchase events from a stream intended for first paid conversions. // Here transaction IDs are invoice IDs. GA4 reports may de-duplicate events. const purchaseIds = ['in_first', 'in_first', 'in_renewal', 'in_trial']; function reconcile(notices, invoices, purchaseIds) { const paidNotices = new Set(notices.filter(({ notice }) => notice === 'paid').map(({ invoice }) => invoice)); const collected = invoices.filter(({ stripeCollected }) => stripeCollected > 0) .sort((a, b) => a.paidAt.localeCompare(b.paidAt)); assert.ok(collected.length < 2 || collected[0].paidAt !== collected[1].paidAt, 'The two earliest charges need more precise timestamps to establish ordering'); const first = collected[0]; return { firstStripeCharge: first?.id ?? null, laterStripeCharges: collected.slice(1).map(({ id }) => id), positiveInvoiceIdsWithoutStripeCollection: invoices.filter(row => row.invoiceTotal > 0 && row.stripeCollected === 0) .map(({ id }) => id), missingPaidNotices: collected.filter(({ id }) => !paidNotices.has(id)).map(({ id }) => id), firstChargePurchaseEvents: purchaseIds.filter(id => id === first?.id).length, unexpectedPurchaseIdsForFirstCharge: [...new Set(purchaseIds.filter(id => id !== first?.id))], }; } const result = reconcile(notices, invoices, purchaseIds); assert.deepEqual(result, { firstStripeCharge: 'in_first', laterStripeCharges: ['in_renewal'], positiveInvoiceIdsWithoutStripeCollection: ['in_external'], missingPaidNotices: [], firstChargePurchaseEvents: 2, unexpectedPurchaseIdsForFirstCharge: ['in_renewal', 'in_trial'], }); assert.deepEqual(reconcile(notices.toReversed(), invoices.toReversed(), purchaseIds), result); assert.equal(reconcile(notices, invoices, []).firstChargePurchaseEvents, 0); assert.equal(reconcile(notices, invoices.filter(({ stripeCollected }) => stripeCollected === 0), []).firstStripeCharge, null); assert.deepEqual(reconcile(notices.filter(({ invoice }) => invoice !== 'in_first'), invoices, purchaseIds) .missingPaidNotices, ['in_first']); console.log(JSON.stringify(result, null, 2));