d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { user } from '@sim/db/schema'
|
|
import { createLogger } from '@sim/logger'
|
|
import { eq } from 'drizzle-orm'
|
|
import type Stripe from 'stripe'
|
|
import { getEmailSubject, renderAbandonedCheckoutEmail } from '@/components/emails'
|
|
import { isProPlan } from '@/lib/billing/core/subscription'
|
|
import { sendEmail } from '@/lib/messaging/email/mailer'
|
|
import { getHelpEmailAddress, getPersonalEmailFrom } from '@/lib/messaging/email/utils'
|
|
|
|
const logger = createLogger('CheckoutWebhooks')
|
|
|
|
/**
|
|
* Handles checkout.session.expired — fires when a user starts an upgrade but doesn't complete it.
|
|
* Sends a plain personal email to check in and offer help.
|
|
* Only fires for subscription-mode sessions to avoid misfires on credit purchase or setup sessions.
|
|
* Skips users who have already completed a subscription (session may expire after a successful upgrade).
|
|
*/
|
|
export async function handleAbandonedCheckout(event: Stripe.Event): Promise<void> {
|
|
const session = event.data.object as Stripe.Checkout.Session
|
|
|
|
if (session.mode !== 'subscription') return
|
|
|
|
const customerId = typeof session.customer === 'string' ? session.customer : session.customer?.id
|
|
if (!customerId) {
|
|
logger.warn('No customer ID on expired session', { sessionId: session.id })
|
|
return
|
|
}
|
|
|
|
const [userData] = await db
|
|
.select({ id: user.id, email: user.email, name: user.name })
|
|
.from(user)
|
|
.where(eq(user.stripeCustomerId, customerId))
|
|
.limit(1)
|
|
|
|
if (!userData?.email) {
|
|
logger.warn('No user found for Stripe customer', { customerId, sessionId: session.id })
|
|
return
|
|
}
|
|
|
|
// Skip if the user already has a paid plan (direct or via org) — covers session expiring after a successful upgrade
|
|
const alreadySubscribed = await isProPlan(userData.id)
|
|
if (alreadySubscribed) return
|
|
|
|
const { from } = getPersonalEmailFrom()
|
|
const replyTo = getHelpEmailAddress()
|
|
const html = await renderAbandonedCheckoutEmail(userData.name || undefined)
|
|
|
|
await sendEmail({
|
|
to: userData.email,
|
|
subject: getEmailSubject('abandoned-checkout'),
|
|
html,
|
|
from,
|
|
replyTo,
|
|
emailType: 'notifications',
|
|
})
|
|
|
|
logger.info('Sent abandoned checkout email', { userId: userData.id, sessionId: session.id })
|
|
}
|