Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(billing): scope price ID validation to current deployment environment

massmarketconsumer-arch4165938b64c7
diff --git a/apps/web/__tests__/unit/subscribe-checkout.test.ts b/apps/web/__tests__/unit/subscribe-checkout.test.ts
index 20892bf2c..c7ad64a44 100644
--- a/apps/web/__tests__/unit/subscribe-checkout.test.ts
+++ b/apps/web/__tests__/unit/subscribe-checkout.test.ts
@@ -1,5 +1,5 @@
-import { beforeEach, describe, expect, it, vi } from "vitest";
import { isValidStripePlanPriceId, STRIPE_PLAN_IDS } from "@cap/utils";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { POST as subscribe } from "@/app/api/settings/billing/subscribe/route";
const checkoutMocks = vi.hoisted(() => ({
@@ -80,6 +80,31 @@ describe("Stripe plan allowlist", () => {
expect(isValidStripePlanPriceId("price_free_tier")).toBe(false);
expect(isValidStripePlanPriceId("")).toBe(false);
});
+
+ it("validates price IDs against specific deployment environments", () => {
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.development.yearly,
+ "development",
+ ),
+ ).toBe(true);
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.production.yearly,
+ "development",
+ ),
+ ).toBe(false);
+
+ expect(
+ isValidStripePlanPriceId(STRIPE_PLAN_IDS.production.yearly, "production"),
+ ).toBe(true);
+ expect(
+ isValidStripePlanPriceId(
+ STRIPE_PLAN_IDS.development.yearly,
+ "production",
+ ),
+ ).toBe(false);
+ });
});
describe("POST /api/settings/billing/subscribe", () => {
diff --git a/apps/web/app/api/desktop/[...route]/root.ts b/apps/web/app/api/desktop/[...route]/root.ts
index 5af87f039..729f05a05 100644
--- a/apps/web/app/api/desktop/[...route]/root.ts
+++ b/apps/web/app/api/desktop/[...route]/root.ts
@@ -10,8 +10,8 @@ import {
} from "@cap/database/schema";
import { serverEnv } from "@cap/env";
import {
- isValidStripePlanPriceId,
isProSubscription,
+ isValidStripePlanPriceId,
STRIPE_AVAILABLE,
stripe,
userIsPro,
@@ -706,9 +706,20 @@ app.post(
zValidator(
"json",
z.object({
- priceId: z.string().refine((id) => isValidStripePlanPriceId(id), {
- message: "Invalid priceId",
- }),
+ priceId: z
+ .string()
+ .refine(
+ (id) =>
+ isValidStripePlanPriceId(
+ id,
+ serverEnv().VERCEL_ENV === "production"
+ ? "production"
+ : "development",
+ ),
+ {
+ message: "Invalid priceId",
+ },
+ ),
platform: z.literal("mobile").optional(),
}),
),
diff --git a/apps/web/app/api/settings/billing/guest-checkout/route.ts b/apps/web/app/api/settings/billing/guest-checkout/route.ts
index 0caaa2ccd..a437d2290 100644
--- a/apps/web/app/api/settings/billing/guest-checkout/route.ts
+++ b/apps/web/app/api/settings/billing/guest-checkout/route.ts
@@ -11,10 +11,13 @@ export async function POST(request: NextRequest) {
console.log("Received guest checkout request:", { priceId, quantity });
+ const environment =
+ serverEnv().VERCEL_ENV === "production" ? "production" : "development";
+
if (
!priceId ||
typeof priceId !== "string" ||
- !isValidStripePlanPriceId(priceId)
+ !isValidStripePlanPriceId(priceId, environment)
) {
console.error("Invalid or missing priceId");
return Response.json({ error: "Invalid priceId" }, { status: 400 });
diff --git a/apps/web/app/api/settings/billing/subscribe/route.ts b/apps/web/app/api/settings/billing/subscribe/route.ts
index 379b0e432..42dc62fe7 100644
--- a/apps/web/app/api/settings/billing/subscribe/route.ts
+++ b/apps/web/app/api/settings/billing/subscribe/route.ts
@@ -13,10 +13,13 @@ export async function POST(request: NextRequest) {
let customerId = user?.stripeCustomerId;
const { priceId, quantity, isOnBoarding } = await request.json();
+ const environment =
+ serverEnv().VERCEL_ENV === "production" ? "production" : "development";
+
if (
!priceId ||
typeof priceId !== "string" ||
- !isValidStripePlanPriceId(priceId)
+ !isValidStripePlanPriceId(priceId, environment)
) {
console.error("Invalid or missing priceId");
return Response.json(
diff --git a/packages/utils/src/constants/plans.ts b/packages/utils/src/constants/plans.ts
index 749702e5f..e6f1b44d7 100644
--- a/packages/utils/src/constants/plans.ts
+++ b/packages/utils/src/constants/plans.ts
@@ -27,10 +27,19 @@ export const isValidStripePlanPriceId = (
priceId: string,
environment?: "development" | "production",
): boolean => {
- if (environment) {
- const envPlans = STRIPE_PLAN_IDS[environment];
+ const env =
+ environment ??
+ (process.env.VERCEL_ENV === "production"
+ ? "production"
+ : process.env.VERCEL_ENV
+ ? "development"
+ : undefined);
+
+ if (env) {
+ const envPlans = STRIPE_PLAN_IDS[env];
return priceId === envPlans.yearly || priceId === envPlans.monthly;
}
+
return VALID_STRIPE_PLAN_PRICE_IDS.has(priceId);
};