Skip to content

Commit c11b8db

Browse files
committed
improvement(billing): immediately charge for billing upgrades
1 parent a2c08e1 commit c11b8db

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

apps/sim/app/api/billing/switch-plan/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export async function POST(request: NextRequest) {
142142
quantity: currentQuantity,
143143
},
144144
],
145-
proration_behavior: 'create_prorations',
145+
proration_behavior: 'always_invoice',
146146
})
147147
}
148148

apps/sim/lib/billing/webhooks/invoices.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,15 @@ export async function handleInvoicePaymentSucceeded(event: Stripe.Event) {
469469
return
470470
}
471471

472+
// Skip proration invoices (e.g. from mid-cycle plan upgrades) — only process cycle renewals
473+
if (invoice.billing_reason && invoice.billing_reason !== 'subscription_cycle') {
474+
logger.info('Skipping non-cycle invoice payment succeeded', {
475+
invoiceId: invoice.id,
476+
billingReason: invoice.billing_reason,
477+
})
478+
return
479+
}
480+
472481
const records = await db
473482
.select()
474483
.from(subscriptionTable)
@@ -556,6 +565,20 @@ export async function handleInvoicePaymentFailed(event: Stripe.Event) {
556565
return
557566
}
558567

568+
// Skip proration invoices (e.g. from mid-cycle plan upgrades) — don't block users over proration failures.
569+
// Overage invoices (threshold + cycle-end) bypass this guard via !isOverageInvoice and still block.
570+
if (
571+
!isOverageInvoice &&
572+
invoice.billing_reason &&
573+
invoice.billing_reason !== 'subscription_cycle'
574+
) {
575+
logger.info('Skipping non-cycle invoice payment failure', {
576+
invoiceId: invoice.id,
577+
billingReason: invoice.billing_reason,
578+
})
579+
return
580+
}
581+
559582
// Extract and validate customer ID
560583
const customerId = invoice.customer
561584
if (!customerId || typeof customerId !== 'string') {

0 commit comments

Comments
 (0)