Consolidate audit-fixes branch: webhooks, integrations, and deploy hardening
Batch commit of the pending working tree on security/audit-fixes-2026-07. Major areas: - Outbound webhooks / Zapier: schema + signed delivery with retries, public v1 API (REST-hook subscribe/unsubscribe), settings UI, cron drain. - Deploy hardening: email via SMTP2GO (Resend fully removed), verified DB TLS (DATABASE_SSL=require + DATABASE_CA), storage fails loud in production when Spaces is unconfigured instead of silently using ephemeral disk. - Integrations & features (concurrent work): accounting (QuickBooks/Xero), e-signature (DocuSign/Dropbox Sign), PayPal, geocoding/maps, onboarding, expanded legal pages. - DB migrations 0006–0009. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
969d5d4c8a
commit
c9968531e4
@@ -5,16 +5,26 @@ import { profiles } from "@/lib/db/schema"
|
||||
import { getSessionUser } from "@/lib/session"
|
||||
import { stripe } from "@/lib/stripe/client"
|
||||
import { PLAN_PRICES } from "@/lib/stripe/plans"
|
||||
import { resolvePriceId } from "@/lib/stripe/prices"
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const user = await getSessionUser()
|
||||
if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 })
|
||||
|
||||
const { plan } = await request.json() as { plan: string }
|
||||
const { plan, interval } = await request.json() as {
|
||||
plan: string
|
||||
interval?: "month" | "year"
|
||||
}
|
||||
|
||||
const planConfig = PLAN_PRICES[plan]
|
||||
if (!planConfig) return NextResponse.json({ error: "Invalid plan" }, { status: 400 })
|
||||
|
||||
const billingInterval = interval === "year" ? "year" : "month"
|
||||
const priceId = await resolvePriceId(planConfig.plan, billingInterval)
|
||||
if (!priceId) {
|
||||
return NextResponse.json({ error: "Could not resolve the plan price. Check STRIPE_SECRET_KEY." }, { status: 400 })
|
||||
}
|
||||
|
||||
const profile = await db.query.profiles.findFirst({
|
||||
where: eq(profiles.id, user.id),
|
||||
columns: { stripe_customer_id: true, email: true, full_name: true },
|
||||
@@ -27,7 +37,7 @@ export async function POST(request: Request) {
|
||||
const customer = await stripe.customers.create({
|
||||
email: profile?.email ?? user.email,
|
||||
name: profile?.full_name ?? undefined,
|
||||
metadata: { supabase_user_id: user.id },
|
||||
metadata: { user_id: user.id },
|
||||
})
|
||||
customerId = customer.id
|
||||
|
||||
@@ -43,16 +53,16 @@ export async function POST(request: Request) {
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
customer: customerId,
|
||||
mode: isLifetime ? "payment" : "subscription",
|
||||
line_items: [{ price: planConfig.priceId, quantity: 1 }],
|
||||
line_items: [{ price: priceId, quantity: 1 }],
|
||||
success_url: `${appUrl}/settings/billing?success=true`,
|
||||
cancel_url: `${appUrl}/settings/billing?canceled=true`,
|
||||
metadata: {
|
||||
supabase_user_id: user.id,
|
||||
user_id: user.id,
|
||||
plan: planConfig.plan,
|
||||
},
|
||||
...(isLifetime ? {} : {
|
||||
subscription_data: {
|
||||
metadata: { supabase_user_id: user.id, plan: planConfig.plan },
|
||||
metadata: { user_id: user.id, plan: planConfig.plan },
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -19,11 +19,13 @@ export async function POST(request: Request) {
|
||||
}
|
||||
|
||||
// Webhooks are not user-scoped: they identify the target row by the id /
|
||||
// customer id stored in Stripe metadata. There is no RLS to bypass anymore.
|
||||
// customer id stored in Stripe metadata. `user_id` is the current key;
|
||||
// `supabase_user_id` is read as a fallback so subscriptions/checkouts created
|
||||
// before the rename keep resolving. (Both hold the same app user id.)
|
||||
switch (event.type) {
|
||||
case "checkout.session.completed": {
|
||||
const session = event.data.object as Stripe.Checkout.Session
|
||||
const userId = session.metadata?.supabase_user_id
|
||||
const userId = session.metadata?.user_id ?? session.metadata?.supabase_user_id
|
||||
const plan = session.metadata?.plan
|
||||
|
||||
if (!userId || !plan) break
|
||||
@@ -41,7 +43,7 @@ export async function POST(request: Request) {
|
||||
case "customer.subscription.created":
|
||||
case "customer.subscription.updated": {
|
||||
const subscription = event.data.object as Stripe.Subscription
|
||||
const userId = subscription.metadata?.supabase_user_id
|
||||
const userId = subscription.metadata?.user_id ?? subscription.metadata?.supabase_user_id
|
||||
const plan = subscription.metadata?.plan
|
||||
|
||||
if (!userId) break
|
||||
@@ -62,7 +64,7 @@ export async function POST(request: Request) {
|
||||
|
||||
case "customer.subscription.deleted": {
|
||||
const subscription = event.data.object as Stripe.Subscription
|
||||
const userId = subscription.metadata?.supabase_user_id
|
||||
const userId = subscription.metadata?.user_id ?? subscription.metadata?.supabase_user_id
|
||||
|
||||
if (!userId) break
|
||||
|
||||
|
||||
Reference in New Issue
Block a user