-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathinvoices.ts
More file actions
854 lines (761 loc) · 29 KB
/
invoices.ts
File metadata and controls
854 lines (761 loc) · 29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
import { render } from '@react-email/components'
import { db } from '@sim/db'
import {
member,
organization,
subscription as subscriptionTable,
user,
userStats,
} from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { and, eq, inArray, isNull, ne, or } from 'drizzle-orm'
import type Stripe from 'stripe'
import { getEmailSubject, PaymentFailedEmail, renderCreditPurchaseEmail } from '@/components/emails'
import { calculateSubscriptionOverage } from '@/lib/billing/core/billing'
import { addCredits, getCreditBalance, removeCredits } from '@/lib/billing/credits/balance'
import { setUsageLimitForCredits } from '@/lib/billing/credits/purchase'
import { blockOrgMembers, unblockOrgMembers } from '@/lib/billing/organizations/membership'
import { isEnterprise, isOrgPlan, isTeam } from '@/lib/billing/plan-helpers'
import { requireStripeClient } from '@/lib/billing/stripe-client'
import { getBaseUrl } from '@/lib/core/utils/urls'
import { sendEmail } from '@/lib/messaging/email/mailer'
import { getPersonalEmailFrom } from '@/lib/messaging/email/utils'
import { quickValidateEmail } from '@/lib/messaging/email/validation'
const logger = createLogger('StripeInvoiceWebhooks')
const OVERAGE_INVOICE_TYPES = new Set<string>([
'overage_billing',
'overage_threshold_billing',
'overage_threshold_billing_org',
])
function parseDecimal(value: string | number | null | undefined): number {
if (value === null || value === undefined) return 0
return Number.parseFloat(value.toString())
}
/**
* Create a billing portal URL for a Stripe customer
*/
async function createBillingPortalUrl(stripeCustomerId: string): Promise<string> {
try {
const stripe = requireStripeClient()
const baseUrl = getBaseUrl()
const portal = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId,
return_url: `${baseUrl}/workspace?billing=updated`,
})
return portal.url
} catch (error) {
logger.error('Failed to create billing portal URL', { error, stripeCustomerId })
// Fallback to generic billing page
return `${getBaseUrl()}/workspace?tab=subscription`
}
}
/**
* Get payment method details from Stripe invoice
*/
async function getPaymentMethodDetails(
invoice: Stripe.Invoice
): Promise<{ lastFourDigits?: string; failureReason?: string }> {
let lastFourDigits: string | undefined
let failureReason: string | undefined
// Try to get last 4 digits from payment method
try {
const stripe = requireStripeClient()
// Try to get from default payment method
if (invoice.default_payment_method && typeof invoice.default_payment_method === 'string') {
const paymentMethod = await stripe.paymentMethods.retrieve(invoice.default_payment_method)
if (paymentMethod.card?.last4) {
lastFourDigits = paymentMethod.card.last4
}
}
// If no default payment method, try getting from customer's default
if (!lastFourDigits && invoice.customer && typeof invoice.customer === 'string') {
const customer = await stripe.customers.retrieve(invoice.customer)
if (customer && !('deleted' in customer)) {
const defaultPm = customer.invoice_settings?.default_payment_method
if (defaultPm && typeof defaultPm === 'string') {
const paymentMethod = await stripe.paymentMethods.retrieve(defaultPm)
if (paymentMethod.card?.last4) {
lastFourDigits = paymentMethod.card.last4
}
}
}
}
} catch (error) {
logger.warn('Failed to retrieve payment method details', { error, invoiceId: invoice.id })
}
// Get failure message - check multiple sources
if (invoice.last_finalization_error?.message) {
failureReason = invoice.last_finalization_error.message
}
// If not found, check the payments array (requires expand: ['payments'])
if (!failureReason && invoice.payments?.data) {
const defaultPayment = invoice.payments.data.find((p) => p.is_default)
const payment = defaultPayment || invoice.payments.data[0]
if (payment?.payment) {
try {
const stripe = requireStripeClient()
if (payment.payment.type === 'payment_intent' && payment.payment.payment_intent) {
const piId =
typeof payment.payment.payment_intent === 'string'
? payment.payment.payment_intent
: payment.payment.payment_intent.id
const paymentIntent = await stripe.paymentIntents.retrieve(piId)
if (paymentIntent.last_payment_error?.message) {
failureReason = paymentIntent.last_payment_error.message
}
} else if (payment.payment.type === 'charge' && payment.payment.charge) {
const chargeId =
typeof payment.payment.charge === 'string'
? payment.payment.charge
: payment.payment.charge.id
const charge = await stripe.charges.retrieve(chargeId)
if (charge.failure_message) {
failureReason = charge.failure_message
}
}
} catch (error) {
logger.warn('Failed to retrieve payment details for failure reason', {
error,
invoiceId: invoice.id,
})
}
}
}
return { lastFourDigits, failureReason }
}
/**
* Send payment failure notification emails to affected users
* Note: This is only called when billing is enabled (Stripe plugin loaded)
*/
async function sendPaymentFailureEmails(
sub: { plan: string | null; referenceId: string },
invoice: Stripe.Invoice,
stripeCustomerId: string
): Promise<void> {
try {
const billingPortalUrl = await createBillingPortalUrl(stripeCustomerId)
const amountDue = invoice.amount_due / 100 // Convert cents to dollars
const { lastFourDigits, failureReason } = await getPaymentMethodDetails(invoice)
// Get users to notify
let usersToNotify: Array<{ email: string; name: string | null }> = []
if (isOrgPlan(sub.plan)) {
// For team/enterprise, notify all owners and admins
const members = await db
.select({
userId: member.userId,
role: member.role,
})
.from(member)
.where(eq(member.organizationId, sub.referenceId))
// Get owner/admin user details
const ownerAdminIds = members
.filter((m) => m.role === 'owner' || m.role === 'admin')
.map((m) => m.userId)
if (ownerAdminIds.length > 0) {
const users = await db
.select({ email: user.email, name: user.name })
.from(user)
.where(inArray(user.id, ownerAdminIds))
usersToNotify = users.filter((u) => u.email && quickValidateEmail(u.email).isValid)
}
} else {
// For individual plans, notify the user
const users = await db
.select({ email: user.email, name: user.name })
.from(user)
.where(eq(user.id, sub.referenceId))
.limit(1)
if (users.length > 0) {
usersToNotify = users.filter((u) => u.email && quickValidateEmail(u.email).isValid)
}
}
// Send emails to all affected users
for (const userToNotify of usersToNotify) {
try {
const emailHtml = await render(
PaymentFailedEmail({
userName: userToNotify.name || undefined,
amountDue,
lastFourDigits,
billingPortalUrl,
failureReason,
sentDate: new Date(),
})
)
const { from, replyTo } = getPersonalEmailFrom()
await sendEmail({
to: userToNotify.email,
subject: 'Payment Failed - Action Required',
html: emailHtml,
from,
replyTo,
emailType: 'transactional',
})
logger.info('Payment failure email sent', {
email: userToNotify.email,
invoiceId: invoice.id,
})
} catch (emailError) {
logger.error('Failed to send payment failure email', {
error: emailError,
email: userToNotify.email,
})
}
}
} catch (error) {
logger.error('Failed to send payment failure emails', { error })
}
}
/**
* Get total billed overage for a subscription, handling team vs individual plans
* For team plans: sums billedOverageThisPeriod across all members
* For other plans: gets billedOverageThisPeriod for the user
*/
export async function getBilledOverageForSubscription(sub: {
plan: string | null
referenceId: string
}): Promise<number> {
if (isTeam(sub.plan)) {
const ownerRows = await db
.select({ userId: member.userId })
.from(member)
.where(and(eq(member.organizationId, sub.referenceId), eq(member.role, 'owner')))
.limit(1)
const ownerId = ownerRows[0]?.userId
if (!ownerId) {
logger.warn('Organization has no owner when fetching billed overage', {
organizationId: sub.referenceId,
})
return 0
}
const ownerStats = await db
.select({ billedOverageThisPeriod: userStats.billedOverageThisPeriod })
.from(userStats)
.where(eq(userStats.userId, ownerId))
.limit(1)
return ownerStats.length > 0 ? parseDecimal(ownerStats[0].billedOverageThisPeriod) : 0
}
const userStatsRecords = await db
.select({ billedOverageThisPeriod: userStats.billedOverageThisPeriod })
.from(userStats)
.where(eq(userStats.userId, sub.referenceId))
.limit(1)
return userStatsRecords.length > 0 ? parseDecimal(userStatsRecords[0].billedOverageThisPeriod) : 0
}
export async function resetUsageForSubscription(sub: { plan: string | null; referenceId: string }) {
if (isOrgPlan(sub.plan)) {
const membersRows = await db
.select({ userId: member.userId })
.from(member)
.where(eq(member.organizationId, sub.referenceId))
for (const m of membersRows) {
const currentStats = await db
.select({
current: userStats.currentPeriodCost,
currentCopilot: userStats.currentPeriodCopilotCost,
})
.from(userStats)
.where(eq(userStats.userId, m.userId))
.limit(1)
if (currentStats.length > 0) {
const current = currentStats[0].current || '0'
const currentCopilot = currentStats[0].currentCopilot || '0'
await db
.update(userStats)
.set({
lastPeriodCost: current,
lastPeriodCopilotCost: currentCopilot,
currentPeriodCost: '0',
currentPeriodCopilotCost: '0',
billedOverageThisPeriod: '0',
})
.where(eq(userStats.userId, m.userId))
}
}
await db
.update(organization)
.set({ departedMemberUsage: '0' })
.where(eq(organization.id, sub.referenceId))
} else {
const currentStats = await db
.select({
current: userStats.currentPeriodCost,
snapshot: userStats.proPeriodCostSnapshot,
currentCopilot: userStats.currentPeriodCopilotCost,
})
.from(userStats)
.where(eq(userStats.userId, sub.referenceId))
.limit(1)
if (currentStats.length > 0) {
// For Pro plans, combine current + snapshot for lastPeriodCost, then clear both
const current = Number.parseFloat(currentStats[0].current?.toString() || '0')
const snapshot = Number.parseFloat(currentStats[0].snapshot?.toString() || '0')
const totalLastPeriod = (current + snapshot).toString()
const currentCopilot = currentStats[0].currentCopilot || '0'
await db
.update(userStats)
.set({
lastPeriodCost: totalLastPeriod,
lastPeriodCopilotCost: currentCopilot,
currentPeriodCost: '0',
currentPeriodCopilotCost: '0',
proPeriodCostSnapshot: '0', // Clear snapshot at period end
billedOverageThisPeriod: '0', // Clear threshold billing tracker at period end
})
.where(eq(userStats.userId, sub.referenceId))
}
}
}
/**
* Handle credit purchase invoice payment succeeded.
*/
async function handleCreditPurchaseSuccess(invoice: Stripe.Invoice): Promise<void> {
const { entityType, entityId, amountDollars, purchasedBy } = invoice.metadata || {}
if (!entityType || !entityId || !amountDollars) {
logger.error('Missing metadata in credit purchase invoice', {
invoiceId: invoice.id,
metadata: invoice.metadata,
})
return
}
if (entityType !== 'user' && entityType !== 'organization') {
logger.error('Invalid entityType in credit purchase', { invoiceId: invoice.id, entityType })
return
}
const amount = Number.parseFloat(amountDollars)
if (Number.isNaN(amount) || amount <= 0) {
logger.error('Invalid amount in credit purchase', { invoiceId: invoice.id, amountDollars })
return
}
await addCredits(entityType, entityId, amount)
const subscription = await db
.select()
.from(subscriptionTable)
.where(eq(subscriptionTable.referenceId, entityId))
.limit(1)
if (subscription.length > 0) {
const sub = subscription[0]
const { balance: newCreditBalance } = await getCreditBalance(entityId)
await setUsageLimitForCredits(entityType, entityId, sub.plan, sub.seats, newCreditBalance)
}
logger.info('Credit purchase completed via webhook', {
invoiceId: invoice.id,
entityType,
entityId,
amount,
purchasedBy,
})
// Send confirmation emails
try {
const { balance: newBalance } = await getCreditBalance(
entityType === 'organization' ? entityId : purchasedBy || entityId
)
let recipients: Array<{ email: string; name: string | null }> = []
if (entityType === 'organization') {
const members = await db
.select({ userId: member.userId, role: member.role })
.from(member)
.where(eq(member.organizationId, entityId))
const ownerAdminIds = members
.filter((m) => m.role === 'owner' || m.role === 'admin')
.map((m) => m.userId)
if (ownerAdminIds.length > 0) {
recipients = await db
.select({ email: user.email, name: user.name })
.from(user)
.where(inArray(user.id, ownerAdminIds))
}
} else if (purchasedBy) {
const users = await db
.select({ email: user.email, name: user.name })
.from(user)
.where(eq(user.id, purchasedBy))
.limit(1)
recipients = users
}
for (const recipient of recipients) {
if (!recipient.email) continue
const emailHtml = await renderCreditPurchaseEmail({
userName: recipient.name || undefined,
amount,
newBalance,
})
await sendEmail({
to: recipient.email,
subject: getEmailSubject('credit-purchase'),
html: emailHtml,
emailType: 'transactional',
})
logger.info('Sent credit purchase confirmation email', {
email: recipient.email,
invoiceId: invoice.id,
})
}
} catch (emailError) {
logger.error('Failed to send credit purchase emails', { emailError, invoiceId: invoice.id })
}
}
/**
* Handle invoice payment succeeded webhook.
* Handles both credit purchases and subscription payments.
*/
export async function handleInvoicePaymentSucceeded(event: Stripe.Event) {
try {
const invoice = event.data.object as Stripe.Invoice
// Handle credit purchase invoices
if (invoice.metadata?.type === 'credit_purchase') {
await handleCreditPurchaseSuccess(invoice)
return
}
// Handle subscription invoices
const subscription = invoice.parent?.subscription_details?.subscription
const stripeSubscriptionId = typeof subscription === 'string' ? subscription : subscription?.id
if (!stripeSubscriptionId) {
return
}
const records = await db
.select()
.from(subscriptionTable)
.where(eq(subscriptionTable.stripeSubscriptionId, stripeSubscriptionId))
.limit(1)
if (records.length === 0) return
const sub = records[0]
// Only reset usage here if the tenant was previously blocked; otherwise invoice.created already reset it
let wasBlocked = false
if (isOrgPlan(sub.plan)) {
const membersRows = await db
.select({ userId: member.userId })
.from(member)
.where(eq(member.organizationId, sub.referenceId))
const memberIds = membersRows.map((m) => m.userId)
if (memberIds.length > 0) {
const blockedRows = await db
.select({ blocked: userStats.billingBlocked })
.from(userStats)
.where(inArray(userStats.userId, memberIds))
wasBlocked = blockedRows.some((row) => !!row.blocked)
}
} else {
const row = await db
.select({ blocked: userStats.billingBlocked })
.from(userStats)
.where(eq(userStats.userId, sub.referenceId))
.limit(1)
wasBlocked = row.length > 0 ? !!row[0].blocked : false
}
// For proration invoices (mid-cycle upgrades/seat changes), only unblock if real money
// was collected. A $0 credit invoice from a downgrade should not unblock a user who
// was blocked for a different failed payment.
const isProrationInvoice = invoice.billing_reason === 'subscription_update'
const shouldUnblock = !isProrationInvoice || (invoice.amount_paid ?? 0) > 0
if (shouldUnblock) {
if (isOrgPlan(sub.plan)) {
await unblockOrgMembers(sub.referenceId, 'payment_failed')
} else {
await db
.update(userStats)
.set({ billingBlocked: false, billingBlockedReason: null })
.where(
and(
eq(userStats.userId, sub.referenceId),
eq(userStats.billingBlockedReason, 'payment_failed')
)
)
}
} else {
logger.info('Skipping unblock for zero-amount proration invoice', {
invoiceId: invoice.id,
billingReason: invoice.billing_reason,
amountPaid: invoice.amount_paid,
})
}
// Only reset usage for cycle renewals — proration invoices should not wipe
// accumulated usage mid-cycle.
if (wasBlocked && !isProrationInvoice) {
await resetUsageForSubscription({ plan: sub.plan, referenceId: sub.referenceId })
}
} catch (error) {
logger.error('Failed to handle invoice payment succeeded', { eventId: event.id, error })
throw error
}
}
/**
* Handle invoice payment failed webhook
* This is triggered when a user's payment fails for any invoice (subscription or overage)
*/
export async function handleInvoicePaymentFailed(event: Stripe.Event) {
try {
const invoice = event.data.object as Stripe.Invoice
const invoiceType = invoice.metadata?.type
const isOverageInvoice = !!(invoiceType && OVERAGE_INVOICE_TYPES.has(invoiceType))
let stripeSubscriptionId: string | undefined
if (isOverageInvoice) {
// Overage invoices store subscription ID in metadata
stripeSubscriptionId = invoice.metadata?.subscriptionId as string | undefined
} else {
// Regular subscription invoices have it in parent.subscription_details
const subscription = invoice.parent?.subscription_details?.subscription
stripeSubscriptionId = typeof subscription === 'string' ? subscription : subscription?.id
}
if (!stripeSubscriptionId) {
logger.info('No subscription found on invoice; skipping payment failed handler', {
invoiceId: invoice.id,
isOverageInvoice,
})
return
}
// Extract and validate customer ID
const customerId = invoice.customer
if (!customerId || typeof customerId !== 'string') {
logger.error('Invalid customer ID on invoice', {
invoiceId: invoice.id,
customer: invoice.customer,
})
return
}
const failedAmount = invoice.amount_due / 100 // Convert from cents to dollars
const billingPeriod = invoice.metadata?.billingPeriod || 'unknown'
const attemptCount = invoice.attempt_count ?? 1
logger.warn('Invoice payment failed', {
invoiceId: invoice.id,
customerId,
failedAmount,
billingPeriod,
attemptCount,
customerEmail: invoice.customer_email,
hostedInvoiceUrl: invoice.hosted_invoice_url,
isOverageInvoice,
invoiceType: isOverageInvoice ? 'overage' : 'subscription',
})
// Block users after first payment failure
if (attemptCount >= 1) {
const records = await db
.select()
.from(subscriptionTable)
.where(eq(subscriptionTable.stripeSubscriptionId, stripeSubscriptionId))
.limit(1)
if (records.length > 0) {
const sub = records[0]
logger.error('Payment failure - blocking users', {
invoiceId: invoice.id,
customerId,
attemptCount,
isOverageInvoice,
stripeSubscriptionId,
})
if (isOrgPlan(sub.plan)) {
const memberCount = await blockOrgMembers(sub.referenceId, 'payment_failed')
logger.info('Blocked team/enterprise members due to payment failure', {
organizationId: sub.referenceId,
memberCount,
isOverageInvoice,
})
} else {
// Don't overwrite dispute blocks (dispute > payment_failed priority)
await db
.update(userStats)
.set({ billingBlocked: true, billingBlockedReason: 'payment_failed' })
.where(
and(
eq(userStats.userId, sub.referenceId),
or(
ne(userStats.billingBlockedReason, 'dispute'),
isNull(userStats.billingBlockedReason)
)
)
)
logger.info('Blocked user due to payment failure', {
userId: sub.referenceId,
isOverageInvoice,
})
}
// Send payment failure notification emails
// Only send on FIRST failure (attempt_count === 1), not on Stripe's automatic retries
// This prevents spamming users with duplicate emails every 3-5-7 days
if (attemptCount === 1) {
await sendPaymentFailureEmails(sub, invoice, customerId)
logger.info('Payment failure email sent on first attempt', {
invoiceId: invoice.id,
customerId,
})
} else {
logger.info('Skipping payment failure email on retry attempt', {
invoiceId: invoice.id,
attemptCount,
customerId,
})
}
} else {
logger.warn('Subscription not found in database for failed payment', {
stripeSubscriptionId,
invoiceId: invoice.id,
})
}
}
} catch (error) {
logger.error('Failed to handle invoice payment failed', {
eventId: event.id,
error,
})
throw error // Re-throw to signal webhook failure
}
}
/**
* Handle base invoice finalized → create a separate overage-only invoice
* Note: Enterprise plans no longer have overages
*/
export async function handleInvoiceFinalized(event: Stripe.Event) {
try {
const invoice = event.data.object as Stripe.Invoice
// Only run for subscription renewal invoices (cycle boundary)
const subscription = invoice.parent?.subscription_details?.subscription
const stripeSubscriptionId = typeof subscription === 'string' ? subscription : subscription?.id
if (!stripeSubscriptionId) {
logger.info('No subscription found on invoice; skipping finalized handler', {
invoiceId: invoice.id,
})
return
}
if (invoice.billing_reason && invoice.billing_reason !== 'subscription_cycle') return
const records = await db
.select()
.from(subscriptionTable)
.where(eq(subscriptionTable.stripeSubscriptionId, stripeSubscriptionId))
.limit(1)
if (records.length === 0) return
const sub = records[0]
// Enterprise plans have no overages - reset usage and exit
if (isEnterprise(sub.plan)) {
await resetUsageForSubscription({ plan: sub.plan, referenceId: sub.referenceId })
return
}
const stripe = requireStripeClient()
const periodEnd =
invoice.lines?.data?.[0]?.period?.end || invoice.period_end || Math.floor(Date.now() / 1000)
const billingPeriod = new Date(periodEnd * 1000).toISOString().slice(0, 7)
// Compute overage (only for team and pro plans), before resetting usage
const totalOverage = await calculateSubscriptionOverage(sub)
// Get already-billed overage from threshold billing
const billedOverage = await getBilledOverageForSubscription(sub)
// Only bill the remaining unbilled overage
let remainingOverage = Math.max(0, totalOverage - billedOverage)
// Apply credits to reduce overage at end of cycle
let creditsApplied = 0
if (remainingOverage > 0) {
const entityType = isOrgPlan(sub.plan) ? 'organization' : 'user'
const entityId = sub.referenceId
const { balance: creditBalance } = await getCreditBalance(entityId)
if (creditBalance > 0) {
creditsApplied = Math.min(creditBalance, remainingOverage)
await removeCredits(entityType, entityId, creditsApplied)
remainingOverage = remainingOverage - creditsApplied
logger.info('Applied credits to reduce overage at cycle end', {
subscriptionId: sub.id,
creditBalance,
creditsApplied,
remainingOverageAfterCredits: remainingOverage,
})
}
}
logger.info('Invoice finalized overage calculation', {
subscriptionId: sub.id,
totalOverage,
billedOverage,
creditsApplied,
remainingOverage,
billingPeriod,
})
if (remainingOverage > 0) {
const customerId = String(invoice.customer)
const cents = Math.round(remainingOverage * 100)
const itemIdemKey = `overage-item:${customerId}:${stripeSubscriptionId}:${billingPeriod}`
const invoiceIdemKey = `overage-invoice:${customerId}:${stripeSubscriptionId}:${billingPeriod}`
// Inherit billing settings from the Stripe subscription/customer for autopay
const getPaymentMethodId = (
pm: string | Stripe.PaymentMethod | null | undefined
): string | undefined => (typeof pm === 'string' ? pm : pm?.id)
let collectionMethod: 'charge_automatically' | 'send_invoice' = 'charge_automatically'
let defaultPaymentMethod: string | undefined
try {
const stripeSub = await stripe.subscriptions.retrieve(stripeSubscriptionId)
if (stripeSub.collection_method === 'send_invoice') {
collectionMethod = 'send_invoice'
}
const subDpm = getPaymentMethodId(stripeSub.default_payment_method)
if (subDpm) {
defaultPaymentMethod = subDpm
} else if (collectionMethod === 'charge_automatically') {
const custObj = await stripe.customers.retrieve(customerId)
if (custObj && !('deleted' in custObj)) {
const cust = custObj as Stripe.Customer
const custDpm = getPaymentMethodId(cust.invoice_settings?.default_payment_method)
if (custDpm) defaultPaymentMethod = custDpm
}
}
} catch (e) {
logger.error('Failed to retrieve subscription or customer', { error: e })
}
// Create a draft invoice first so we can attach the item directly
const overageInvoice = await stripe.invoices.create(
{
customer: customerId,
collection_method: collectionMethod,
auto_advance: false,
...(defaultPaymentMethod ? { default_payment_method: defaultPaymentMethod } : {}),
metadata: {
type: 'overage_billing',
billingPeriod,
subscriptionId: stripeSubscriptionId,
},
},
{ idempotencyKey: invoiceIdemKey }
)
// Attach the item to this invoice
await stripe.invoiceItems.create(
{
customer: customerId,
invoice: overageInvoice.id,
amount: cents,
currency: 'usd',
description: `Usage Based Overage – ${billingPeriod}`,
metadata: {
type: 'overage_billing',
billingPeriod,
subscriptionId: stripeSubscriptionId,
},
},
{ idempotencyKey: itemIdemKey }
)
// Finalize to trigger autopay (if charge_automatically and a PM is present)
const draftId = overageInvoice.id
if (typeof draftId !== 'string' || draftId.length === 0) {
logger.error('Stripe created overage invoice without id; aborting finalize')
} else {
const finalized = await stripe.invoices.finalizeInvoice(draftId)
// Some manual invoices may remain open after finalize; ensure we pay immediately when possible
if (collectionMethod === 'charge_automatically' && finalized.status === 'open') {
try {
const payId = finalized.id
if (typeof payId !== 'string' || payId.length === 0) {
logger.error('Finalized invoice missing id')
throw new Error('Finalized invoice missing id')
}
await stripe.invoices.pay(payId, {
payment_method: defaultPaymentMethod,
})
} catch (payError) {
logger.error('Failed to auto-pay overage invoice', {
error: payError,
invoiceId: finalized.id,
})
}
}
}
}
// Finally, reset usage for this subscription after overage handling
await resetUsageForSubscription({ plan: sub.plan, referenceId: sub.referenceId })
} catch (error) {
logger.error('Failed to handle invoice finalized', { error })
throw error
}
}