Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

more fixes

Steven Teyc625c647978e
diff --git a/apps/web/app/api/callback/stripe/route.ts b/apps/web/app/api/callback/stripe/route.ts
index a07727a2aa..224cd41cf4 100644
--- a/apps/web/app/api/callback/stripe/route.ts
+++ b/apps/web/app/api/callback/stripe/route.ts
@@ -1,7 +1,7 @@
import { limiter } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { stripe } from "@/lib/stripe";
-import { getPlanFromPriceId, isNewCustomer } from "@/lib/stripe/utils";
+import { getPlanFromPriceId, isUpgrade } from "@/lib/stripe/utils";
import { redis } from "@/lib/upstash";
import { log } from "@dub/utils";
import { sendEmail } from "emails";
@@ -109,7 +109,7 @@ export const POST = async (req: Request) => {
if (event.type === "customer.subscription.updated") {
const subscriptionUpdated = event.data.object as Stripe.Subscription;
const priceId = subscriptionUpdated.items.data[0].price.id;
- const newCustomer = isNewCustomer(event.data.previous_attributes);
+ const upgraded = isUpgrade(event.data.previous_attributes);
const plan = getPlanFromPriceId(priceId);
const usageLimit = plan.quota;
@@ -129,7 +129,7 @@ export const POST = async (req: Request) => {
"`* not found in Stripe webhook `customer.subscription.updated` callback",
type: "cron",
});
- return;
+ return NextResponse.json({ received: true });
}
// If a project upgrades/downgrades their subscription, update their usage limit in the database.
@@ -155,7 +155,7 @@ export const POST = async (req: Request) => {
},
});
- if (newCustomer) {
+ if (upgraded) {
const users = updatedProject.users.map(({ user }) => ({
name: user.name,
email: user.email,
@@ -206,7 +206,7 @@ export const POST = async (req: Request) => {
"`* not found in Stripe webhook `customer.subscription.deleted` callback",
type: "cron",
});
- return;
+ return NextResponse.json({ received: true });
}
const projectDomains = project.domains.map((domain) => domain.slug);
diff --git a/apps/web/lib/stripe/utils.ts b/apps/web/lib/stripe/utils.ts
index f3ffb99f88..eeeaeefe6c 100644
--- a/apps/web/lib/stripe/utils.ts
+++ b/apps/web/lib/stripe/utils.ts
@@ -9,7 +9,7 @@ export function getPlanFromPriceId(priceId: string) {
}
// custom type coercion because Stripe's types are wrong
-export function isNewCustomer(
+export function isUpgrade(
previousAttributes:
| {
default_payment_method?: string;
@@ -23,26 +23,12 @@ export function isNewCustomer(
}
| undefined,
) {
- let isNewCustomer = false;
- try {
- if (
- // if the project is upgrading from free to pro
- previousAttributes?.default_payment_method === null
- ) {
- isNewCustomer = true;
- } else {
- // if the project is upgrading from pro to enterprise
- const oldPriceId =
- previousAttributes?.items?.data &&
- previousAttributes?.items?.data[0].price.id;
- if (oldPriceId && getPlanFromPriceId(oldPriceId).slug === "pro") {
- isNewCustomer = true;
- }
- }
- } catch (error) {
- console.error("An error occurred:", error);
- }
- return isNewCustomer;
+ const oldPriceId =
+ previousAttributes?.items?.data &&
+ previousAttributes?.items?.data[0].price.id;
+
+ //
+ return oldPriceId && getPlanFromPriceId(oldPriceId).slug === "pro";
}
export const PLANS = [