use the correct key

This commit is contained in:
Jun-te Kim 2026-02-21 14:48:09 +00:00
parent 555a1ab828
commit 124bd68182
5 changed files with 27 additions and 8 deletions

View file

@ -2,12 +2,12 @@ import { NextRequest, NextResponse } from "next/server";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
import { getStripe } from "@/lib/stripe/service"; import { getStripeBilling } from "@/lib/stripe/service";
import { getUserFromSession } from "@/lib/auth/get-user"; import { getUserFromSession } from "@/lib/auth/get-user";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { subscriptions } from "@/lib/schema"; import { subscriptions } from "@/lib/schema";
const stripe = getStripe(); const stripe = getStripeBilling();
const SUBSCRIPTION_PRICE_ID = process.env.STRIPE_SUBSCRIPTION_PRICE_ID!; const SUBSCRIPTION_PRICE_ID = process.env.STRIPE_SUBSCRIPTION_PRICE_ID!;
const ORIGIN = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"; const ORIGIN = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000";

View file

@ -1,12 +1,12 @@
import { NextRequest, NextResponse } from "next/server"; import { NextRequest, NextResponse } from "next/server";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { getStripe } from "@/lib/stripe/service"; import { getStripeBilling } from "@/lib/stripe/service";
import { getUserFromSession } from "@/lib/auth/get-user"; import { getUserFromSession } from "@/lib/auth/get-user";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { subscriptions } from "@/lib/schema"; import { subscriptions } from "@/lib/schema";
const stripe = getStripe(); const stripe = getStripeBilling();
const ORIGIN = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000"; const ORIGIN = process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000";
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {

View file

@ -6,11 +6,11 @@ import { NextRequest, NextResponse } from "next/server";
import Stripe from "stripe"; import Stripe from "stripe";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
import { getStripe } from "@/lib/stripe/service"; import { getStripeBilling } from "@/lib/stripe/service";
import { db } from "@/lib/db"; import { db } from "@/lib/db";
import { subscriptions, payments } from "@/lib/schema"; import { subscriptions, payments } from "@/lib/schema";
const stripe = getStripe(); const stripe = getStripeBilling();
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
try { try {
@ -32,7 +32,7 @@ export async function POST(req: NextRequest) {
event = stripe.webhooks.constructEvent( event = stripe.webhooks.constructEvent(
body, body,
sig, sig,
process.env.STRIPE_WEBHOOK_SECRET! process.env.STRIPE_BILLING_WEBHOOK_SECRET!
); );
console.log("✅ [SUBSCRIPTION WEBHOOK] Signature verified", { eventType: event.type }); console.log("✅ [SUBSCRIPTION WEBHOOK] Signature verified", { eventType: event.type });
} catch (err: any) { } catch (err: any) {

View file

@ -31,6 +31,7 @@ PROD_SES_FROM_EMAIL=no-reply@juntekim.com
PROD_STRIPE_REDIRECT_URI=https://stripetoinvoice.com/api/stripe/callback PROD_STRIPE_REDIRECT_URI=https://stripetoinvoice.com/api/stripe/callback
PROD_STRIPE_WEBHOOK_SECRET=whsec_KU2qQlJGdpZHqcaUyV8ChqsGOFhLH0Bi PROD_STRIPE_WEBHOOK_SECRET=whsec_KU2qQlJGdpZHqcaUyV8ChqsGOFhLH0Bi
# PROD - Stripe Billing (Platform) # PROD - Stripe Billing (Platform)
PROD_STRIPE_BILLING_SECRET_KEY=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 PROD_STRIPE_BILLING_SECRET_KEY=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
PROD_STRIPE_BILLING_WEBHOOK_SECRET=whsec_n0JCnC9dLCD6itdSnjRtyylGsEmplsJM PROD_STRIPE_BILLING_WEBHOOK_SECRET=whsec_n0JCnC9dLCD6itdSnjRtyylGsEmplsJM

View file

@ -1,9 +1,10 @@
import Stripe from "stripe"; import Stripe from "stripe";
let stripe: Stripe | null = null; let stripe: Stripe | null = null;
let stripeBilling: Stripe | null = null;
/** /**
* Server-only Stripe client. * Server-only Stripe client for main Stripe Connect.
* Lazy-initialised to avoid build-time crashes. * Lazy-initialised to avoid build-time crashes.
*/ */
export function getStripe(): Stripe { export function getStripe(): Stripe {
@ -17,3 +18,20 @@ export function getStripe(): Stripe {
return stripe; return stripe;
} }
/**
* Server-only Stripe client for billing/subscriptions.
* Uses a separate Stripe account for billing.
* Lazy-initialised to avoid build-time crashes.
*/
export function getStripeBilling(): Stripe {
if (!process.env.STRIPE_BILLING_SECRET_KEY) {
throw new Error("STRIPE_BILLING_SECRET_KEY missing");
}
if (!stripeBilling) {
stripeBilling = new Stripe(process.env.STRIPE_BILLING_SECRET_KEY);
}
return stripeBilling;
}