Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/sim/app/api/billing/switch-plan/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export async function POST(request: NextRequest) {
quantity: currentQuantity,
},
],
proration_behavior: 'create_prorations',
proration_behavior: 'always_invoice',
Comment thread
icecrasher321 marked this conversation as resolved.
})
}

Expand Down
2 changes: 1 addition & 1 deletion apps/sim/app/api/organizations/[id]/seats/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export async function PUT(request: NextRequest, { params }: { params: Promise<{
quantity: newSeatCount,
},
],
proration_behavior: 'create_prorations', // Stripe's default - charge/credit immediately
proration_behavior: 'always_invoice',
Comment thread
icecrasher321 marked this conversation as resolved.
}
)

Expand Down
21 changes: 12 additions & 9 deletions apps/sim/lib/billing/webhooks/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ export async function handleInvoicePaymentSucceeded(event: Stripe.Event) {
)
}

if (wasBlocked) {
// Only reset usage for cycle renewals — proration invoices (subscription_update) should
Comment thread
icecrasher321 marked this conversation as resolved.
Outdated
// unblock the user but not wipe their accumulated usage mid-cycle.
if (wasBlocked && invoice.billing_reason !== 'subscription_update') {
await resetUsageForSubscription({ plan: sub.plan, referenceId: sub.referenceId })
}
} catch (error) {
Expand Down Expand Up @@ -584,14 +586,6 @@ export async function handleInvoicePaymentFailed(event: Stripe.Event) {

// Block users after first payment failure
if (attemptCount >= 1) {
logger.error('Payment failure - blocking users', {
invoiceId: invoice.id,
customerId,
attemptCount,
isOverageInvoice,
stripeSubscriptionId,
})

const records = await db
.select()
.from(subscriptionTable)
Expand All @@ -600,6 +594,15 @@ export async function handleInvoicePaymentFailed(event: Stripe.Event) {

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', {
Expand Down
Loading